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

namespace Doctrine\DBAL\Driver\SQLAnywhere;

use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\Result;
7
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
8
use Doctrine\DBAL\ParameterType;
9

10
use function assert;
11 12 13 14
use function func_get_args;
use function is_float;
use function is_int;
use function is_resource;
15
use function is_string;
16 17 18 19 20 21 22 23 24 25 26
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;
27 28 29 30

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

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

50
        if (! is_resource($this->connection)) {
51 52 53
            throw SQLAnywhereException::fromSQLAnywhereError();
        }

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

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

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

        return true;
    }

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

        $this->endTransaction();

        return true;
    }

    /**
     * {@inheritdoc}
97 98
     *
     * @deprecated The error information is available via exceptions.
99 100 101
     */
    public function errorCode()
    {
Steve Müller's avatar
Steve Müller committed
102
        return sasql_errorcode($this->connection);
103 104 105 106
    }

    /**
     * {@inheritdoc}
107 108
     *
     * @deprecated The error information is available via exceptions.
109 110 111
     */
    public function errorInfo()
    {
Steve Müller's avatar
Steve Müller committed
112
        return sasql_error($this->connection);
113 114 115 116 117
    }

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

124
        return sasql_affected_rows($this->connection);
125 126
    }

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

134
        if ($stmt instanceof Result) {
135 136 137 138
            $version = $stmt->fetchOne();
        } else {
            $version = $stmt->fetchColumn();
        }
139 140 141 142

        assert(is_string($version));

        return $version;
143 144
    }

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

154 155
        $stmt = $this->query('SELECT ' . $name . '.CURRVAL');

156
        if ($stmt instanceof Result) {
157 158 159 160
            return $stmt->fetchOne();
        }

        return $stmt->fetchColumn();
161 162 163 164 165
    }

    /**
     * {@inheritdoc}
     */
166
    public function prepare($sql)
167
    {
168
        return new SQLAnywhereStatement($this->connection, $sql);
169 170 171 172 173 174 175 176 177
    }

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

179 180 181 182 183 184 185 186
        $stmt->execute();

        return $stmt;
    }

    /**
     * {@inheritdoc}
     */
187
    public function quote($input, $type = ParameterType::STRING)
188 189 190 191 192
    {
        if (is_int($input) || is_float($input)) {
            return $input;
        }

Steve Müller's avatar
Steve Müller committed
193
        return "'" . sasql_escape_string($this->connection, $input) . "'";
194 195
    }

196 197 198 199 200 201 202 203
    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return true;
    }

204 205 206 207 208 209 210
    /**
     * {@inheritdoc}
     *
     * @throws SQLAnywhereException
     */
    public function rollBack()
    {
211
        if (! sasql_rollback($this->connection)) {
Steve Müller's avatar
Steve Müller committed
212
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
213 214 215 216 217 218 219 220 221 222
        }

        $this->endTransaction();

        return true;
    }

    /**
     * Ends transactional mode and enables auto commit again.
     *
223
     * @return bool Whether or not ending transactional mode succeeded.
224 225
     *
     * @throws SQLAnywhereException
226 227 228
     */
    private function endTransaction()
    {
229
        if (! sasql_set_option($this->connection, 'auto_commit', 'on')) {
Steve Müller's avatar
Steve Müller committed
230
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
231 232 233 234
        }

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