SQLAnywhereConnection.php 5.22 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\DBAL\Driver\SQLAnywhere;

use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
7
use Doctrine\DBAL\ParameterType;
8
use function assert;
9 10 11 12
use function func_get_args;
use function is_float;
use function is_int;
use function is_resource;
13
use function is_string;
14 15 16 17 18 19 20 21 22 23 24
use function sasql_affected_rows;
use function sasql_commit;
use function sasql_connect;
use function sasql_error;
use function sasql_errorcode;
use function sasql_escape_string;
use function sasql_insert_id;
use function sasql_pconnect;
use function sasql_real_query;
use function sasql_rollback;
use function sasql_set_option;
25 26 27 28

/**
 * SAP Sybase SQL Anywhere implementation of the Connection interface.
 */
29
class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
30
{
31
    /** @var resource The SQL Anywhere connection resource. */
Steve Müller's avatar
Steve Müller committed
32
    private $connection;
33 34 35 36

    /**
     * Connects to database with given connection string.
     *
37 38
     * @param string $dsn        The connection string.
     * @param bool   $persistent Whether or not to establish a persistent connection.
39 40 41 42 43
     *
     * @throws SQLAnywhereException
     */
    public function __construct($dsn, $persistent = false)
    {
Steve Müller's avatar
Steve Müller committed
44
        $this->connection = $persistent ? @sasql_pconnect($dsn) : @sasql_connect($dsn);
45

46
        if (! is_resource($this->connection)) {
47 48 49
            throw SQLAnywhereException::fromSQLAnywhereError();
        }

Steve Müller's avatar
Steve Müller committed
50
        // Disable PHP warnings on error.
51
        if (! sasql_set_option($this->connection, 'verbose_errors', false)) {
Steve Müller's avatar
Steve Müller committed
52
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
53 54
        }

Steve Müller's avatar
Steve Müller committed
55
        // Enable auto committing by default.
56
        if (! sasql_set_option($this->connection, 'auto_commit', 'on')) {
Steve Müller's avatar
Steve Müller committed
57
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
58 59
        }

Steve Müller's avatar
Steve Müller committed
60
        // Enable exact, non-approximated row count retrieval.
61
        if (! sasql_set_option($this->connection, 'row_counts', true)) {
Steve Müller's avatar
Steve Müller committed
62
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
63 64 65 66 67 68 69 70 71 72
        }
    }

    /**
     * {@inheritdoc}
     *
     * @throws SQLAnywhereException
     */
    public function beginTransaction()
    {
73
        if (! sasql_set_option($this->connection, 'auto_commit', 'off')) {
Steve Müller's avatar
Steve Müller committed
74
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
75 76 77 78 79 80 81 82 83 84 85 86
        }

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @throws SQLAnywhereException
     */
    public function commit()
    {
87
        if (! sasql_commit($this->connection)) {
Steve Müller's avatar
Steve Müller committed
88
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
89 90 91 92 93 94 95 96 97 98 99 100
        }

        $this->endTransaction();

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function errorCode()
    {
Steve Müller's avatar
Steve Müller committed
101
        return sasql_errorcode($this->connection);
102 103 104 105 106 107 108
    }

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

    /**
     * {@inheritdoc}
     */
    public function exec($statement)
    {
117
        if (sasql_real_query($this->connection, $statement) === false) {
118 119
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
        }
120

121
        return sasql_affected_rows($this->connection);
122 123
    }

124 125 126 127 128
    /**
     * {@inheritdoc}
     */
    public function getServerVersion()
    {
129 130 131 132 133
        $version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();

        assert(is_string($version));

        return $version;
134 135
    }

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

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

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

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

164 165 166 167 168 169 170 171
        $stmt->execute();

        return $stmt;
    }

    /**
     * {@inheritdoc}
     */
172
    public function quote($input, $type = ParameterType::STRING)
173 174 175 176 177
    {
        if (is_int($input) || is_float($input)) {
            return $input;
        }

Steve Müller's avatar
Steve Müller committed
178
        return "'" . sasql_escape_string($this->connection, $input) . "'";
179 180
    }

181 182 183 184 185 186 187 188
    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return true;
    }

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

        $this->endTransaction();

        return true;
    }

    /**
     * Ends transactional mode and enables auto commit again.
     *
208
     * @return bool Whether or not ending transactional mode succeeded.
209 210
     *
     * @throws SQLAnywhereException
211 212 213
     */
    private function endTransaction()
    {
214
        if (! sasql_set_option($this->connection, 'auto_commit', 'on')) {
Steve Müller's avatar
Steve Müller committed
215
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
216 217 218 219
        }

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