3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Driver\SQLAnywhere
;
22 use Doctrine\DBAL\Driver\Connection
;
23 use Doctrine\DBAL\Driver\ServerInfoAwareConnection
;
26 * SAP Sybase SQL Anywhere implementation of the Connection interface.
28 * @author Steve Müller <st.mueller@dzh-online.de>
29 * @link www.doctrine-project.org
32 class SQLAnywhereConnection
implements Connection
, ServerInfoAwareConnection
35 * @var resource The SQL Anywhere connection resource.
42 * Connects to database with given connection string.
44 * @param string $dsn The connection string.
45 * @param boolean $persistent Whether or not to establish a persistent connection.
47 * @throws SQLAnywhereException
49 public function __construct($dsn, $persistent = false)
51 $this->connection
= $persistent ? @sasql_pconnect
($dsn) : @sasql_connect
($dsn);
53 if ( ! is_resource($this->connection
)) {
54 throw SQLAnywhereException
::fromSQLAnywhereError();
57 // Disable PHP warnings on error.
58 if ( ! sasql_set_option($this->connection
, 'verbose_errors', false)) {
59 throw SQLAnywhereException
::fromSQLAnywhereError($this->connection
);
62 // Enable auto committing by default.
63 if ( ! sasql_set_option($this->connection
, 'auto_commit', 'on')) {
64 throw SQLAnywhereException
::fromSQLAnywhereError($this->connection
);
67 // Enable exact, non-approximated row count retrieval.
68 if ( ! sasql_set_option($this->connection
, 'row_counts', true)) {
69 throw SQLAnywhereException
::fromSQLAnywhereError($this->connection
);
76 * @throws SQLAnywhereException
78 public function beginTransaction()
80 if ( ! sasql_set_option($this->connection
, 'auto_commit', 'off')) {
81 throw SQLAnywhereException
::fromSQLAnywhereError($this->connection
);
90 * @throws SQLAnywhereException
92 public function commit()
94 if ( ! sasql_commit($this->connection
)) {
95 throw SQLAnywhereException
::fromSQLAnywhereError($this->connection
);
98 $this->endTransaction();
106 public function errorCode()
108 return sasql_errorcode($this->connection
);
114 public function errorInfo()
116 return sasql_error($this->connection
);
122 public function exec($statement)
124 $stmt = $this->prepare($statement);
128 return $stmt->rowCount();
134 public function getServerVersion()
136 return $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
142 public function lastInsertId($name = null)
144 if (null === $name) {
145 return sasql_insert_id($this->connection
);
148 return $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();
154 public function prepare($prepareString)
156 return new SQLAnywhereStatement($this->connection
, $prepareString);
162 public function query()
164 $args = func_get_args();
165 $stmt = $this->prepare($args[0]);
175 public function quote($input, $type = \PDO
::PARAM_STR
)
177 if (is_int($input) ||
is_float($input)) {
181 return "'" . sasql_escape_string($this->connection
, $input) . "'";
187 public function requiresQueryForServerVersion()
195 * @throws SQLAnywhereException
197 public function rollBack()
199 if ( ! sasql_rollback($this->connection
)) {
200 throw SQLAnywhereException
::fromSQLAnywhereError($this->connection
);
203 $this->endTransaction();
209 * Ends transactional mode and enables auto commit again.
211 * @throws SQLAnywhereException
213 * @return boolean Whether or not ending transactional mode succeeded.
215 private function endTransaction()
217 if ( ! sasql_set_option($this->connection
, 'auto_commit', 'on')) {
218 throw SQLAnywhereException
::fromSQLAnywhereError($this->connection
);