Source of file Connection.php
Size: 8,594 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Db/Adapter/Driver/Sqlsrv/Connection.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Db\Adapter\Driver\Sqlsrv; use Zend\Db\Adapter\Driver\ConnectionInterface; use Zend\Db\Adapter\Driver\Sqlsrv\Exception\ErrorException; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; class Connection implements ConnectionInterface, Profiler\ProfilerAwareInterface { /** * @var Sqlsrv */ protected $driver = null; /** * @var Profiler\ProfilerInterface */ protected $profiler = null; /** * @var array */ protected $connectionParameters = array(); /** * @var resource */ protected $resource = null; /** * @var bool */ protected $inTransaction = false; /** * Constructor * * @param array|resource $connectionInfo * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException */ public function __construct($connectionInfo) { if (is_array($connectionInfo)) { $this->setConnectionParameters($connectionInfo); } elseif (is_resource($connectionInfo)) { $this->setResource($connectionInfo); } else { throw new Exception\InvalidArgumentException('$connection must be an array of parameters or a resource'); } } /** * Set driver * * @param Sqlsrv $driver * @return Connection */ public function setDriver(Sqlsrv $driver) { $this->driver = $driver; return $this; } /** * @param Profiler\ProfilerInterface $profiler * @return Connection */ public function setProfiler(Profiler\ProfilerInterface $profiler) { $this->profiler = $profiler; return $this; } /** * @return null|Profiler\ProfilerInterface */ public function getProfiler() { return $this->profiler; } /** * Set connection parameters * * @param array $connectionParameters * @return Connection */ public function setConnectionParameters(array $connectionParameters) { $this->connectionParameters = $connectionParameters; return $this; } /** * Get connection parameters * * @return array */ public function getConnectionParameters() { return $this->connectionParameters; } /** * Get current schema * * @return string */ public function getCurrentSchema() { if (!$this->isConnected()) { $this->connect(); } $result = sqlsrv_query($this->resource, 'SELECT SCHEMA_NAME()'); $r = sqlsrv_fetch_array($result); return $r[0]; } /** * Set resource * * @param resource $resource * @throws Exception\InvalidArgumentException * @return Connection */ public function setResource($resource) { if (get_resource_type($resource) !== 'SQL Server Connection') { throw new Exception\InvalidArgumentException('Resource provided was not of type SQL Server Connection'); } $this->resource = $resource; return $this; } /** * @return resource */ public function getResource() { if (!$this->isConnected()) { $this->connect(); } return $this->resource; } /** * Connect * * @throws Exception\RuntimeException * @return Connection */ public function connect() { if ($this->resource) { return $this; } $serverName = '.'; $params = array( 'ReturnDatesAsStrings' => true ); foreach ($this->connectionParameters as $key => $value) { switch (strtolower($key)) { case 'hostname': case 'servername': $serverName = (string) $value; break; case 'username': case 'uid': $params['UID'] = (string) $value; break; case 'password': case 'pwd': $params['PWD'] = (string) $value; break; case 'database': case 'dbname': $params['Database'] = (string) $value; break; case 'driver_options': case 'options': $params = array_merge($params, (array) $value); break; } } $this->resource = sqlsrv_connect($serverName, $params); if (!$this->resource) { throw new Exception\RuntimeException( 'Connect Error', null, new ErrorException(sqlsrv_errors()) ); } return $this; } /** * Is connected * @return bool */ public function isConnected() { return (is_resource($this->resource)); } /** * Disconnect */ public function disconnect() { sqlsrv_close($this->resource); $this->resource = null; } /** * Begin transaction */ public function beginTransaction() { // http://msdn.microsoft.com/en-us/library/cc296151.aspx /* $this->resource->autocommit(false); $this->inTransaction = true; */ } /** * In transaction * * @return bool */ public function inTransaction() { return $this->inTransaction; } /** * Commit */ public function commit() { // http://msdn.microsoft.com/en-us/library/cc296194.aspx /* if (!$this->resource) { $this->connect(); } $this->resource->commit(); $this->inTransaction = false; */ } /** * Rollback */ public function rollback() { // http://msdn.microsoft.com/en-us/library/cc296176.aspx /* if (!$this->resource) { throw new \Exception('Must be connected before you can rollback.'); } if (!$this->_inCommit) { throw new \Exception('Must call commit() before you can rollback.'); } $this->resource->rollback(); return $this; */ } /** * Execute * * @param string $sql * @throws Exception\RuntimeException * @return mixed */ public function execute($sql) { if (!$this->isConnected()) { $this->connect(); } if (!$this->driver instanceof Sqlsrv) { throw new Exception\RuntimeException('Connection is missing an instance of Sqlsrv'); } if ($this->profiler) { $this->profiler->profilerStart($sql); } $returnValue = sqlsrv_query($this->resource, $sql); if ($this->profiler) { $this->profiler->profilerFinish($sql); } // if the returnValue is something other than a Sqlsrv_result, bypass wrapping it if ($returnValue === false) { $errors = sqlsrv_errors(); // ignore general warnings if ($errors[0]['SQLSTATE'] != '01000') { throw new Exception\RuntimeException( 'An exception occurred while trying to execute the provided $sql', null, new ErrorException($errors) ); } } $result = $this->driver->createResult($returnValue); return $result; } /** * Prepare * * @param string $sql * @return string */ public function prepare($sql) { if (!$this->isConnected()) { $this->connect(); } $statement = $this->driver->createStatement($sql); return $statement; } /** * Get last generated id * * @param string $name * @return mixed */ public function getLastGeneratedValue($name = null) { if (!$this->resource) { $this->connect(); } $sql = 'SELECT @@IDENTITY as Current_Identity'; $result = sqlsrv_query($this->resource, $sql); $row = sqlsrv_fetch_array($result); return $row['Current_Identity']; } } |