SQLAnywhereConnection.php 5.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Driver\SQLAnywhere;

use Doctrine\DBAL\Driver\Connection;

/**
 * SAP Sybase SQL Anywhere implementation of the Connection interface.
 *
 * @author Steve Müller <st.mueller@dzh-online.de>
 * @link   www.doctrine-project.org
 * @since  2.5
 */
class SQLAnywhereConnection implements Connection
{
    /**
     * @var resource The SQL Anywhere connection resource.
     */
Steve Müller's avatar
Steve Müller committed
36
    private $connection;
37 38 39 40 41 42 43 44 45 46 47 48 49

    /**
     * Constructor.
     *
     * Connects to database with given connection string.
     *
     * @param string  $dsn        The connection string.
     * @param boolean $persistent Whether or not to establish a persistent connection.
     *
     * @throws SQLAnywhereException
     */
    public function __construct($dsn, $persistent = false)
    {
Steve Müller's avatar
Steve Müller committed
50
        $this->connection = $persistent ? @sasql_pconnect($dsn) : @sasql_connect($dsn);
51

Steve Müller's avatar
Steve Müller committed
52
        if ( ! is_resource($this->connection) || get_resource_type($this->connection) !== 'SQLAnywhere connection') {
53 54 55
            throw SQLAnywhereException::fromSQLAnywhereError();
        }

Steve Müller's avatar
Steve Müller committed
56 57 58
        // Disable PHP warnings on error.
        if ( ! sasql_set_option($this->connection, 'verbose_errors', false)) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
59 60
        }

Steve Müller's avatar
Steve Müller committed
61 62 63
        // Enable auto committing by default.
        if ( ! sasql_set_option($this->connection, 'auto_commit', 'on')) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
64 65
        }

Steve Müller's avatar
Steve Müller committed
66 67 68
        // Enable exact, non-approximated row count retrieval.
        if ( ! sasql_set_option($this->connection, 'row_counts', true)) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
69 70 71 72 73 74 75 76 77 78
        }
    }

    /**
     * {@inheritdoc}
     *
     * @throws SQLAnywhereException
     */
    public function beginTransaction()
    {
Steve Müller's avatar
Steve Müller committed
79 80
        if ( ! sasql_set_option($this->connection, 'auto_commit', 'off')) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
81 82 83 84 85 86 87 88 89 90 91 92
        }

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @throws SQLAnywhereException
     */
    public function commit()
    {
Steve Müller's avatar
Steve Müller committed
93 94
        if ( ! sasql_commit($this->connection)) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
95 96 97 98 99 100 101 102 103 104 105 106
        }

        $this->endTransaction();

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function errorCode()
    {
Steve Müller's avatar
Steve Müller committed
107
        return sasql_errorcode($this->connection);
108 109 110 111 112 113 114
    }

    /**
     * {@inheritdoc}
     */
    public function errorInfo()
    {
Steve Müller's avatar
Steve Müller committed
115
        return sasql_error($this->connection);
116 117 118 119 120 121 122 123
    }

    /**
     * {@inheritdoc}
     */
    public function exec($statement)
    {
        $stmt = $this->prepare($statement);
Steve Müller's avatar
Steve Müller committed
124

125 126 127 128 129 130 131 132 133 134
        $stmt->execute();

        return $stmt->rowCount();
    }

    /**
     * {@inheritdoc}
     */
    public function lastInsertId($name = null)
    {
Steve Müller's avatar
Steve Müller committed
135 136
        if (null === $name) {
            return sasql_insert_id($this->connection);
137 138 139 140 141 142 143 144 145 146
        }

        return $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();
    }

    /**
     * {@inheritdoc}
     */
    public function prepare($prepareString)
    {
Steve Müller's avatar
Steve Müller committed
147
        return new SQLAnywhereStatement($this->connection, $prepareString);
148 149 150 151 152 153 154 155 156
    }

    /**
     * {@inheritdoc}
     */
    public function query()
    {
        $args = func_get_args();
        $stmt = $this->prepare($args[0]);
Steve Müller's avatar
Steve Müller committed
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171
        $stmt->execute();

        return $stmt;
    }

    /**
     * {@inheritdoc}
     */
    public function quote($input, $type = \PDO::PARAM_STR)
    {
        if (is_int($input) || is_float($input)) {
            return $input;
        }

Steve Müller's avatar
Steve Müller committed
172
        return "'" . sasql_escape_string($this->connection, $input) . "'";
173 174 175 176 177 178 179 180 181
    }

    /**
     * {@inheritdoc}
     *
     * @throws SQLAnywhereException
     */
    public function rollBack()
    {
Steve Müller's avatar
Steve Müller committed
182 183
        if ( ! sasql_rollback($this->connection)) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        }

        $this->endTransaction();

        return true;
    }

    /**
     * Ends transactional mode and enables auto commit again.
     *
     * @throws SQLAnywhereException
     *
     * @return boolean Whether or not ending transactional mode succeeded.
     */
    private function endTransaction()
    {
Steve Müller's avatar
Steve Müller committed
200 201
        if ( ! sasql_set_option($this->connection, 'auto_commit', 'on')) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
202 203 204 205
        }

        return true;
    }
Steve Müller's avatar
Steve Müller committed
206
}