Created
February 26, 2012 12:32
-
-
Save Noxwizard/1916504 to your computer and use it in GitHub Desktop.
ODBC metadata file for phpunit
This file contains 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 | |
/** | |
* Wrapper around the PDO object to be able to specify information about the underlying driver | |
* | |
* @package DbUnit | |
* @author Patrick Webster <noxwizard@gmail.com> | |
* @copyright 2002-2011 Sebastian Bergmann <sb@sebastian-bergmann.de> | |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License | |
* @version Release: @package_version@ | |
* @link http://www.phpunit.de/ | |
* @since Class available since Release 1.1.0 | |
*/ | |
if(!class_exists('phpbb_database_connection_ODBC_PDO_wrapper')) | |
{ | |
class phpbb_database_connection_ODBC_PDO_wrapper extends PDO | |
{ | |
public $driver = ''; | |
public $version = 0; | |
function __construct($dbms, $version, $dsn, $user, $pass) | |
{ | |
$this->driver = $dbms; | |
$this->version = (double)$version; | |
parent::__construct($dsn, $user, $pass); | |
} | |
} | |
} | |
/** | |
* Provides functionality to retrieve meta data from an ODBC connection for the following database drivers: | |
* Firebird | |
* Microsoft SQL Server | |
* | |
* @package DbUnit | |
* @author Patrick Webster <noxwizard@gmail.com> | |
* @copyright 2002-2011 Sebastian Bergmann <sb@sebastian-bergmann.de> | |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License | |
* @version Release: @package_version@ | |
* @link http://www.phpunit.de/ | |
* @since Class available since Release 1.1.0 | |
*/ | |
class PHPUnit_Extensions_Database_DB_MetaData_ODBC extends PHPUnit_Extensions_Database_DB_MetaData | |
{ | |
/** | |
* The name of the underlying database system we are connecting to. | |
* @var string | |
*/ | |
protected $realDriver = ''; | |
/** | |
* The ODBC driver version | |
* @var string | |
*/ | |
protected $version = 0; | |
/** | |
* Constructor | |
* @var phpbb_database_connection_ODBC_PDO_wrapper | |
* @var string - schema | |
*/ | |
function __construct($args) | |
{ | |
parent::__construct($args); | |
$pdo_obj = func_get_arg(0); | |
if(!($pdo_obj instanceof phpbb_database_connection_ODBC_PDO_wrapper)) | |
{ | |
throw new Exception('ODBC MetaDriver requires a phpbb_database_connection_ODBC_PDO_wrapper PDO object and not a regular PDO object.'); | |
} | |
$this->realDriver = strtolower($pdo_obj->driver); | |
$this->version = (double)$pdo_obj->version; | |
switch($this->realDriver) | |
{ | |
case 'firebird': | |
$this->schemaObjectQuoteChar = '"'; | |
$this->truncateCommand = 'DELETE FROM'; | |
break; | |
case 'mssql': | |
$this->schemaObjectQuoteChar = ''; | |
$this->truncateCommand = 'TRUNCATE TABLE'; | |
break; | |
default: | |
throw new Exception('The ODBC MetaDriver does not support your underlying DBMS: ' . $this->realDriver); | |
break; | |
} | |
} | |
/** | |
* Returns an array containing the names of all the tables in the database. | |
* | |
* @return array | |
*/ | |
public function getTableNames() | |
{ | |
switch($this->realDriver) | |
{ | |
case 'firebird': | |
$query = 'SELECT RDB$RELATION_NAME | |
FROM RDB$RELATIONS | |
WHERE RDB$VIEW_SOURCE is null | |
AND RDB$SYSTEM_FLAG = 0'; | |
$result = $this->pdo->query($query); | |
$tableNames = array(); | |
while (($tableName = $statement->fetchColumn(0))) { | |
$tableNames[] = $tableName; | |
} | |
break; | |
case 'mssql': | |
$query = "SELECT name | |
FROM sysobjects | |
WHERE type='U'"; | |
$statement = $this->pdo->prepare($query); | |
$statement->execute(); | |
$tableNames = array(); | |
while (($tableName = $statement->fetchColumn(0))) { | |
$tableNames[] = $tableName; | |
} | |
break; | |
} | |
return $tableNames; | |
} | |
/** | |
* Returns an array containing the names of all the columns in the | |
* $tableName table. | |
* | |
* @param string $tableName | |
* @return array | |
*/ | |
public function getTableColumns($tableName) | |
{ | |
switch($this->realDriver) | |
{ | |
case 'firebird': | |
$query = 'SELECT RDB$FIELD_NAME | |
FROM RDB$RELATION_FIELDS | |
WHERE RDB$RELATION_NAME = \'' . strtoupper($tableName) . "'"; | |
$result = $this->pdo->query($query); | |
$columnNames = array(); | |
while (($columnName = $result->fetchColumn(0))) { | |
$columnNames[] = $columnName; | |
} | |
break; | |
case 'mssql': | |
$query = "SELECT c.name | |
FROM syscolumns c | |
LEFT JOIN sysobjects o ON c.id = o.id | |
WHERE o.name = '$tableName'"; | |
$statement = $this->pdo->prepare($query); | |
$statement->execute(); | |
$columnNames = array(); | |
while (($columnName = $statement->fetchColumn(0))) { | |
$columnNames[] = $columnName; | |
} | |
break; | |
} | |
return $columnNames; | |
} | |
/** | |
* Returns an array containing the names of all the primary key columns in | |
* the $tableName table. | |
* | |
* @param string $tableName | |
* @return array | |
*/ | |
public function getTablePrimaryKeys($tableName) | |
{ | |
switch($this->realDriver) | |
{ | |
case 'firebird': | |
$query = "SELECT RDB\$FIELD_NAME FROM RDB\$INDICES i | |
LEFT JOIN RDB\$INDEX_SEGMENTS i2 ON (i2.RDB\$INDEX_NAME = i.RDB\$INDEX_NAME) | |
LEFT JOIN RDB\$RELATION_CONSTRAINTS c ON (c.RDB\$INDEX_NAME = i2.RDB\$INDEX_NAME) | |
WHERE c.RDB\$CONSTRAINT_TYPE = 'PRIMARY KEY' | |
AND i.RDB\$RELATION_NAME = '" . strtoupper($tableName) . "'"; | |
$result = $this->pdo->query($query); | |
$columnNames = array(); | |
while (($columnName = $result->fetchColumn(0))) { | |
$columnNames[] = $columnName; | |
} | |
break; | |
case 'mssql': | |
$query = "EXEC sp_statistics '$tableName'"; | |
$statement = $this->pdo->prepare($query); | |
$statement->execute(); | |
$statement->setFetchMode(PDO::FETCH_ASSOC); | |
$columnNames = array(); | |
while (($column = $statement->fetch())) { | |
if ($column['TYPE'] == 1) { | |
$columnNames[] = $column['COLUMN_NAME']; | |
} | |
} | |
break; | |
} | |
return $columnNames; | |
} | |
/** | |
* Allow overwriting identities for the given table. | |
* | |
* @param string $tableName | |
*/ | |
public function disablePrimaryKeys($tableName) | |
{ | |
switch($this->realDriver) | |
{ | |
case 'mssql': | |
try { | |
$query = "SET IDENTITY_INSERT $tableName ON"; | |
$this->pdo->exec($query); | |
} | |
catch (PDOException $e) { | |
// ignore the error here - can happen if primary key is not an identity | |
} | |
break; | |
} | |
} | |
/** | |
* Reenable auto creation of identities for the given table. | |
* | |
* @param string $tableName | |
*/ | |
public function enablePrimaryKeys($tableName) | |
{ | |
switch($this->realDriver) | |
{ | |
case 'mssql': | |
try { | |
$query = "SET IDENTITY_INSERT $tableName OFF"; | |
$this->pdo->exec($query); | |
} | |
catch (PDOException $e) { | |
// ignore the error here - can happen if primary key is not an identity | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment