Created
December 28, 2011 15:52
-
-
Save SimonRice/1528441 to your computer and use it in GitHub Desktop.
PDO DBLIB divers for Doctrine (ideal for UNIX-based servers connecting to MS SQL Server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
* | |
* This software consists of voluntary contributions made by many individuals | |
* and is licensed under the LGPL. For more information, see | |
* <http://www.doctrine-project.org>. | |
*/ | |
namespace Doctrine\DBAL\Driver\PDODblibSqlsrv; | |
/** | |
* Sqlsrv Connection implementation for PDO Dblib. | |
* | |
* @since 2.0 | |
*/ | |
class Connection extends \Doctrine\DBAL\Driver\PDOConnection implements \Doctrine\DBAL\Driver\Connection | |
{ | |
/** | |
* @override | |
*/ | |
public function quote($value, $type=\PDO::PARAM_STR) | |
{ | |
$val = parent::quote($value, $type); | |
// Fix for a driver version terminating all values with null byte | |
if (strpos($val, "\0") !== false) { | |
$val = substr($val, 0, -1); | |
} | |
return $val; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function rollback() | |
{ | |
$this->exec('ROLLBACK TRANSACTION'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function commit() | |
{ | |
$this->exec('COMMIT TRANSACTION'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function beginTransaction() | |
{ | |
$this->exec('BEGIN TRANSACTION'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function lastInsertId($name = null) | |
{ | |
$stmt = $this->query('SELECT SCOPE_IDENTITY()'); | |
$id = $stmt->fetchColumn(); | |
$stmt->closeCursor(); | |
return $id; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Description of Driver | |
* | |
* @author Simon Rice <[email protected]> | |
* @author Scott Morken <[email protected]> | |
*/ | |
namespace Doctrine\DBAL\Driver\PDODblibSqlsrv; | |
class Driver implements \Doctrine\DBAL\Driver | |
{ | |
/** | |
* Attempts to establish a connection with the underlying driver. | |
* | |
* @param array $params | |
* @param string $username | |
* @param string $password | |
* @param array $driverOptions | |
* @return Doctrine\DBAL\Driver\Connection | |
*/ | |
public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) | |
{ | |
return new Connection( | |
$this->_constructPdoDsn($params), | |
$username, | |
$password, | |
$driverOptions | |
); | |
return $conn; | |
} | |
/** | |
* Constructs the Dblib PDO DSN. | |
* | |
* @return string The DSN. | |
*/ | |
private function _constructPdoDsn(array $params) | |
{ | |
$dsn = 'dblib:'; | |
if (isset($params['host'])) { | |
$dsn .= 'host=' . $params['host'] . ';'; | |
} | |
if (isset($params['port']) && !empty($params['port'])) { | |
$dsn .= 'port=' . $params['port'] . ';'; | |
} | |
if (isset($params['dbname'])) { | |
$dsn .= 'dbname=' . $params['dbname'] . ';'; | |
} | |
return $dsn; | |
} | |
public function getDatabasePlatform() | |
{ | |
return new \Doctrine\DBAL\Platforms\MsSqlPlatform(); | |
} | |
public function getSchemaManager(\Doctrine\DBAL\Connection $conn) | |
{ | |
return new \Doctrine\DBAL\Schema\MsSqlSchemaManager($conn); | |
} | |
public function getName() | |
{ | |
return 'pdo_dblib'; | |
} | |
public function getDatabase(\Doctrine\DBAL\Connection $conn) | |
{ | |
$params = $conn->getParams(); | |
return $params['dbname']; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Doctrine/DBAL/DriverManager.php b/lib/Doctrine/DBAL/DriverManager.php | |
index b66c2f8..17fbc01 100644 | |
--- a/lib/Doctrine/DBAL/DriverManager.php | |
+++ b/lib/Doctrine/DBAL/DriverManager.php | |
@@ -44,6 +44,7 @@ final class DriverManager | |
'ibm_db2' => 'Doctrine\DBAL\Driver\IBMDB2\DB2Driver', | |
'pdo_ibm' => 'Doctrine\DBAL\Driver\PDOIbm\Driver', | |
'pdo_sqlsrv' => 'Doctrine\DBAL\Driver\PDOSqlsrv\Driver', | |
+ 'pdo_dblib' => 'Doctrine\DBAL\Driver\PDODblibSqlsrv\Driver', | |
); | |
/** Private constructor. This class cannot be instantiated. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, it is very important