SQLAnywhereConnection.php 5.77 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
<?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;
23
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
24 25 26 27 28 29 30 31

/**
 * 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
 */
32
class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
33 34 35 36
{
    /**
     * @var resource The SQL Anywhere connection resource.
     */
Steve Müller's avatar
Steve Müller committed
37
    private $connection;
38 39 40 41 42 43 44 45 46 47 48 49 50

    /**
     * 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
51
        $this->connection = $persistent ? @sasql_pconnect($dsn) : @sasql_connect($dsn);
52

53
        if ( ! is_resource($this->connection)) {
54 55 56
            throw SQLAnywhereException::fromSQLAnywhereError();
        }

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

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

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

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

        return true;
    }

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

        $this->endTransaction();

        return true;
    }

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

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

    /**
     * {@inheritdoc}
     */
    public function exec($statement)
    {
124 125 126
        if (false === sasql_real_query($this->connection, $statement)) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
        }
127

128
        return sasql_affected_rows($this->connection);
129 130
    }

131 132 133 134 135 136 137 138
    /**
     * {@inheritdoc}
     */
    public function getServerVersion()
    {
        return $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
    }

139 140 141 142 143
    /**
     * {@inheritdoc}
     */
    public function lastInsertId($name = null)
    {
Steve Müller's avatar
Steve Müller committed
144 145
        if (null === $name) {
            return sasql_insert_id($this->connection);
146 147 148 149 150 151 152 153 154 155
        }

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

    /**
     * {@inheritdoc}
     */
    public function prepare($prepareString)
    {
Steve Müller's avatar
Steve Müller committed
156
        return new SQLAnywhereStatement($this->connection, $prepareString);
157 158 159 160 161 162 163 164 165
    }

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

167 168 169 170 171 172 173 174 175 176 177 178 179 180
        $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
181
        return "'" . sasql_escape_string($this->connection, $input) . "'";
182 183
    }

184 185 186 187 188 189 190 191
    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return true;
    }

192 193 194 195 196 197 198
    /**
     * {@inheritdoc}
     *
     * @throws SQLAnywhereException
     */
    public function rollBack()
    {
Steve Müller's avatar
Steve Müller committed
199 200
        if ( ! sasql_rollback($this->connection)) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        }

        $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
217 218
        if ( ! sasql_set_option($this->connection, 'auto_commit', 'on')) {
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
219 220 221 222
        }

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