SQLAnywhereConnection.php 5 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 60 61 62 63 64 65 66 67
        }
    }

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

        return true;
    }

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

        $this->endTransaction();

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function errorCode()
    {
Steve Müller's avatar
Steve Müller committed
96
        return sasql_errorcode($this->connection);
97 98 99 100 101 102 103
    }

    /**
     * {@inheritdoc}
     */
    public function errorInfo()
    {
Steve Müller's avatar
Steve Müller committed
104
        return sasql_error($this->connection);
105 106 107 108 109 110 111
    }

    /**
     * {@inheritdoc}
     */
    public function exec($statement)
    {
112
        if (sasql_real_query($this->connection, $statement) === false) {
113 114
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
        }
115

116
        return sasql_affected_rows($this->connection);
117 118
    }

119 120 121 122 123
    /**
     * {@inheritdoc}
     */
    public function getServerVersion()
    {
124 125 126 127 128
        $version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();

        assert(is_string($version));

        return $version;
129 130
    }

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

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

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

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

159 160 161 162 163 164 165 166
        $stmt->execute();

        return $stmt;
    }

    /**
     * {@inheritdoc}
     */
167
    public function quote($input, $type = ParameterType::STRING)
168 169 170 171 172
    {
        if (is_int($input) || is_float($input)) {
            return $input;
        }

Steve Müller's avatar
Steve Müller committed
173
        return "'" . sasql_escape_string($this->connection, $input) . "'";
174 175
    }

176 177 178 179 180 181 182 183
    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return true;
    }

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

        $this->endTransaction();

        return true;
    }

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

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