Included vendor/ to the project

This commit is contained in:
2017-09-13 09:35:32 +07:00
parent e72205ff09
commit 17f1643875
2483 changed files with 179073 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database;
use PDO;
use PHPUnit\DbUnit\Database\Metadata\Metadata;
use PHPUnit\DbUnit\DataSet\IDataSet;
use PHPUnit\DbUnit\DataSet\ITable;
/**
* Provides a basic interface for communicating with a database.
*/
interface Connection
{
/**
* Close this connection.
*/
public function close();
/**
* Creates a dataset containing the specified table names. If no table
* names are specified then it will created a dataset over the entire
* database.
*
* @param array $tableNames
*
* @return IDataSet
*/
public function createDataSet(array $tableNames = null);
/**
* Creates a table with the result of the specified SQL statement.
*
* @param string $resultName
* @param string $sql
*
* @return ITable
*/
public function createQueryTable($resultName, $sql);
/**
* Returns a PDO Connection
*
* @return PDO
*/
public function getConnection();
/**
* Returns a database metadata object that can be used to retrieve table
* meta data from the database.
*
* @return Metadata
*/
public function getMetaData();
/**
* Returns the number of rows in the given table. You can specify an
* optional where clause to return a subset of the table.
*
* @param string $tableName
* @param string $whereClause
* @param int
*/
public function getRowCount($tableName, $whereClause = null);
/**
* Returns the schema for the connection.
*
* @return string
*/
public function getSchema();
/**
* Returns a quoted schema object. (table name, column name, etc)
*
* @param string $object
*
* @return string
*/
public function quoteSchemaObject($object);
/**
* Returns the command used to truncate a table.
*
* @return string
*/
public function getTruncateCommand();
/**
* Returns true if the connection allows cascading
*
* @return bool
*/
public function allowsCascading();
/**
* Disables primary keys if connection does not allow setting them otherwise
*
* @param string $tableName
*/
public function disablePrimaryKeys($tableName);
/**
* Reenables primary keys after they have been disabled
*
* @param string $tableName
*/
public function enablePrimaryKeys($tableName);
}

View File

@@ -0,0 +1,142 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database;
use PHPUnit\DbUnit\DataSet\AbstractDataSet;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\ITableMetadata;
use PHPUnit\DbUnit\InvalidArgumentException;
use PHPUnit\DbUnit\RuntimeException;
/**
* Provides access to a database instance as a data set.
*/
class DataSet extends AbstractDataSet
{
/**
* An array of ITable objects.
*
* @var array
*/
protected $tables = [];
/**
* The database connection this dataset is using.
*
* @var Connection
*/
protected $databaseConnection;
/**
* Creates a new dataset using the given database connection.
*
* @param Connection $databaseConnection
*/
public function __construct(Connection $databaseConnection)
{
$this->databaseConnection = $databaseConnection;
}
/**
* Creates the query necessary to pull all of the data from a table.
*
* @param ITableMetadata $tableMetaData
*
* @return string
*/
public static function buildTableSelect(ITableMetadata $tableMetaData, Connection $databaseConnection = null)
{
if ($tableMetaData->getTableName() == '') {
$e = new RuntimeException('Empty Table Name');
echo $e->getTraceAsString();
throw $e;
}
$columns = $tableMetaData->getColumns();
if ($databaseConnection) {
$columns = array_map([$databaseConnection, 'quoteSchemaObject'], $columns);
}
$columnList = implode(', ', $columns);
if ($databaseConnection) {
$tableName = $databaseConnection->quoteSchemaObject($tableMetaData->getTableName());
} else {
$tableName = $tableMetaData->getTableName();
}
$primaryKeys = $tableMetaData->getPrimaryKeys();
if ($databaseConnection) {
$primaryKeys = array_map([$databaseConnection, 'quoteSchemaObject'], $primaryKeys);
}
if (count($primaryKeys)) {
$orderBy = 'ORDER BY ' . implode(' ASC, ', $primaryKeys) . ' ASC';
} else {
$orderBy = '';
}
return "SELECT {$columnList} FROM {$tableName} {$orderBy}";
}
/**
* Creates an iterator over the tables in the data set. If $reverse is
* true a reverse iterator will be returned.
*
* @param bool $reverse
*
* @return TableIterator
*/
protected function createIterator($reverse = false)
{
return new TableIterator($this->getTableNames(), $this, $reverse);
}
/**
* Returns a table object for the given table.
*
* @param string $tableName
*
* @return Table
*/
public function getTable($tableName)
{
if (!in_array($tableName, $this->getTableNames())) {
throw new InvalidArgumentException("$tableName is not a table in the current database.");
}
if (empty($this->tables[$tableName])) {
$this->tables[$tableName] = new Table($this->getTableMetaData($tableName), $this->databaseConnection);
}
return $this->tables[$tableName];
}
/**
* Returns a table meta data object for the given table.
*
* @param string $tableName
*
* @return DefaultTableMetadata
*/
public function getTableMetaData($tableName)
{
return new DefaultTableMetadata($tableName, $this->databaseConnection->getMetaData()->getTableColumns($tableName), $this->databaseConnection->getMetaData()->getTablePrimaryKeys($tableName));
}
/**
* Returns a list of table names for the database
*
* @return array
*/
public function getTableNames()
{
return $this->databaseConnection->getMetaData()->getTableNames();
}
}

View File

@@ -0,0 +1,199 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database;
use PDO;
use PHPUnit\DbUnit\Database\Metadata\AbstractMetadata;
use PHPUnit\DbUnit\Database\Metadata\Metadata;
use PHPUnit\DbUnit\DataSet\IDataSet;
use PHPUnit\DbUnit\DataSet\QueryTable;
/**
* Provides a basic interface for communicating with a database.
*/
class DefaultConnection implements Connection
{
/**
* @var PDO
*/
protected $connection;
/**
* The metadata object used to retrieve table meta data from the database.
*
* @var Metadata
*/
protected $metaData;
/**
* Creates a new database connection
*
* @param PDO $connection
* @param string $schema - The name of the database schema you will be testing against.
*/
public function __construct(PDO $connection, $schema = '')
{
$this->connection = $connection;
$this->metaData = AbstractMetadata::createMetaData($connection, $schema);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
/**
* Close this connection.
*/
public function close()
{
unset($this->connection);
}
/**
* Returns a database metadata object that can be used to retrieve table
* meta data from the database.
*
* @return Metadata
*/
public function getMetaData()
{
return $this->metaData;
}
/**
* Returns the schema for the connection.
*
* @return string
*/
public function getSchema()
{
return $this->getMetaData()->getSchema();
}
/**
* Creates a dataset containing the specified table names. If no table
* names are specified then it will created a dataset over the entire
* database.
*
* @param array $tableNames
*
* @return IDataSet
*
* @todo Implement the filtered data set.
*/
public function createDataSet(array $tableNames = null)
{
if (empty($tableNames)) {
return new DataSet($this);
} else {
return new FilteredDataSet($this, $tableNames);
}
}
/**
* Creates a table with the result of the specified SQL statement.
*
* @param string $resultName
* @param string $sql
*
* @return Table
*/
public function createQueryTable($resultName, $sql)
{
return new QueryTable($resultName, $sql, $this);
}
/**
* Returns this connection database configuration
*/
public function getConfig()
{
}
/**
* Returns a PDO Connection
*
* @return PDO
*/
public function getConnection()
{
return $this->connection;
}
/**
* Returns the number of rows in the given table. You can specify an
* optional where clause to return a subset of the table.
*
* @param string $tableName
* @param string $whereClause
*
* @return int
*/
public function getRowCount($tableName, $whereClause = null)
{
$query = 'SELECT COUNT(*) FROM ' . $this->quoteSchemaObject($tableName);
if (isset($whereClause)) {
$query .= " WHERE {$whereClause}";
}
return (int) $this->connection->query($query)->fetchColumn();
}
/**
* Returns a quoted schema object. (table name, column name, etc)
*
* @param string $object
*
* @return string
*/
public function quoteSchemaObject($object)
{
return $this->getMetaData()->quoteSchemaObject($object);
}
/**
* Returns the command used to truncate a table.
*
* @return string
*/
public function getTruncateCommand()
{
return $this->getMetaData()->getTruncateCommand();
}
/**
* Returns true if the connection allows cascading
*
* @return bool
*/
public function allowsCascading()
{
return $this->getMetaData()->allowsCascading();
}
/**
* Disables primary keys if connection does not allow setting them otherwise
*
* @param string $tableName
*/
public function disablePrimaryKeys($tableName)
{
$this->getMetaData()->disablePrimaryKeys($tableName);
}
/**
* Reenables primary keys after they have been disabled
*
* @param string $tableName
*/
public function enablePrimaryKeys($tableName)
{
$this->getMetaData()->enablePrimaryKeys($tableName);
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database;
/**
* Provides access to a database instance as a data set.
*/
class FilteredDataSet extends DataSet
{
/**
* @var array
*/
protected $tableNames;
/**
* Creates a new dataset using the given database connection.
*
* @param Connection $databaseConnection
*/
public function __construct(Connection $databaseConnection, array $tableNames)
{
parent::__construct($databaseConnection);
$this->tableNames = $tableNames;
}
/**
* Returns a list of table names for the database
*
* @return array
*/
public function getTableNames()
{
return $this->tableNames;
}
}

View File

@@ -0,0 +1,217 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
use PDO;
use PHPUnit\DbUnit\RuntimeException;
use ReflectionClass;
/**
* Provides a basic constructor for all meta data classes and a factory for
* generating the appropriate meta data class.
*/
abstract class AbstractMetadata implements Metadata
{
protected static $metaDataClassMap = [
'pgsql' => PgSQL::class,
'mysql' => MySQL::class,
'oci' => Oci::class,
'sqlite' => Sqlite::class,
'sqlite2' => Sqlite::class,
'sqlsrv' => SqlSrv::class,
'firebird' => Firebird::class,
'dblib' => Dblib::class
];
/**
* The PDO connection used to retreive database meta data
*
* @var PDO
*/
protected $pdo;
/**
* The default schema name for the meta data object.
*
* @var string
*/
protected $schema;
/**
* The character used to quote schema objects.
*/
protected $schemaObjectQuoteChar = '"';
/**
* The command used to perform a TRUNCATE operation.
*/
protected $truncateCommand = 'TRUNCATE';
/**
* Creates a new database meta data object using the given pdo connection
* and schema name.
*
* @param PDO $pdo
* @param string $schema
*/
final public function __construct(PDO $pdo, $schema = '')
{
$this->pdo = $pdo;
$this->schema = $schema;
}
/**
* Creates a meta data object based on the driver of given $pdo object and
* $schema name.
*
* @param PDO $pdo
* @param string $schema
*
* @return AbstractMetadata
*/
public static function createMetaData(PDO $pdo, $schema = '')
{
$driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
if (isset(self::$metaDataClassMap[$driverName])) {
$className = self::$metaDataClassMap[$driverName];
if ($className instanceof ReflectionClass) {
return $className->newInstance($pdo, $schema);
} else {
return self::registerClassWithDriver($className, $driverName)->newInstance($pdo, $schema);
}
} else {
throw new RuntimeException("Could not find a meta data driver for {$driverName} pdo driver.");
}
}
/**
* Validates and registers the given $className with the given $pdoDriver.
* It should be noted that this function will not attempt to include /
* require the file. The $pdoDriver can be determined by the value of the
* PDO::ATTR_DRIVER_NAME attribute for a pdo object.
*
* A reflection of the $className is returned.
*
* @param string $className
* @param string $pdoDriver
*
* @return ReflectionClass
*/
public static function registerClassWithDriver($className, $pdoDriver)
{
if (!class_exists($className)) {
throw new RuntimeException("Specified class for {$pdoDriver} driver ({$className}) does not exist.");
}
$reflection = new ReflectionClass($className);
if ($reflection->isSubclassOf(self::class)) {
return self::$metaDataClassMap[$pdoDriver] = $reflection;
} else {
throw new RuntimeException("Specified class for {$pdoDriver} driver ({$className}) does not extend PHPUnit_Extensions_Database_DB_MetaData.");
}
}
/**
* Returns the schema for the connection.
*
* @return string
*/
public function getSchema()
{
return $this->schema;
}
/**
* Returns a quoted schema object. (table name, column name, etc)
*
* @param string $object
*
* @return string
*/
public function quoteSchemaObject($object)
{
$parts = explode('.', $object);
$quotedParts = [];
foreach ($parts as $part) {
$quotedParts[] = $this->schemaObjectQuoteChar .
str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar . $this->schemaObjectQuoteChar, $part) .
$this->schemaObjectQuoteChar;
}
return implode('.', $quotedParts);
}
/**
* Seperates the schema and the table from a fully qualified table name.
*
* Returns an associative array containing the 'schema' and the 'table'.
*
* @param string $fullTableName
*
* @return array
*/
public function splitTableName($fullTableName)
{
if (($dot = strpos($fullTableName, '.')) !== false) {
return [
'schema' => substr($fullTableName, 0, $dot),
'table' => substr($fullTableName, $dot + 1)
];
} else {
return [
'schema' => null,
'table' => $fullTableName
];
}
}
/**
* Returns the command for the database to truncate a table.
*
* @return string
*/
public function getTruncateCommand()
{
return $this->truncateCommand;
}
/**
* Returns true if the rdbms allows cascading
*
* @return bool
*/
public function allowsCascading()
{
return false;
}
/**
* Disables primary keys if the rdbms does not allow setting them otherwise
*
* @param string $tableName
*/
public function disablePrimaryKeys($tableName)
{
return;
}
/**
* Reenables primary keys after they have been disabled
*
* @param string $tableName
*/
public function enablePrimaryKeys($tableName)
{
return;
}
}

View File

@@ -0,0 +1,128 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
/**
* Provides functionality to retrieve meta data from an Dblib (SQL Server) database.
*/
class Dblib extends AbstractMetadata
{
/**
* No character used to quote schema objects.
*
* @var string
*/
protected $schemaObjectQuoteChar = '';
/**
* The command used to perform a TRUNCATE operation.
*
* @var string
*/
protected $truncateCommand = 'TRUNCATE TABLE';
/**
* @var array
*/
protected $columns = [];
/**
* @var array
*/
protected $keys = [];
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames()
{
$tableNames = [];
$query = 'SELECT name
FROM sys.tables
ORDER BY name';
$result = $this->pdo->query($query);
while ($tableName = $result->fetchColumn(0)) {
$tableNames[] = $tableName;
}
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)
{
if (!isset($this->columns[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->columns[$tableName];
}
/**
* 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)
{
if (!isset($this->keys[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->keys[$tableName];
}
/**
* Loads column info from a sql server database.
*
* @param string $tableName
*/
protected function loadColumnInfo($tableName)
{
$query = "SELECT name
FROM sys.columns
WHERE object_id = OBJECT_ID('" . $tableName . "')
ORDER BY column_id";
$result = $this->pdo->query($query);
while ($columnName = $result->fetchColumn(0)) {
$this->columns[$tableName][] = $columnName;
}
$keyQuery = "SELECT COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i INNER JOIN
sys.index_columns AS ic ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1 AND OBJECT_NAME(ic.OBJECT_ID) = '" . $tableName . "'";
$result = $this->pdo->query($keyQuery);
while ($columnName = $result->fetchColumn(0)) {
$this->keys[$tableName][] = $columnName;
}
}
}

View File

@@ -0,0 +1,214 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
/**
* Provides functionality to retrieve meta data from a Firebird database.
*/
class Firebird extends AbstractMetadata
{
/**
* The command used to perform a TRUNCATE operation.
*
* @var string
*/
protected $truncateCommand = 'DELETE FROM';
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames()
{
$query = "
SELECT DISTINCT
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE='BASE TABLE' AND
TABLE_SCHEMA = ?
ORDER BY TABLE_NAME
";
$query = "
select
RDB$RELATION_NAME as TABLE_NAME
from RDB$RELATIONS
where
((RDB$RELATION_TYPE = 0) or
(RDB$RELATION_TYPE is null)) and
(RDB$SYSTEM_FLAG = 0)
order by (RDB$RELATION_NAME)
";
$statement = $this->pdo->prepare($query);
$statement->execute([$this->getSchema()]);
$tableNames = [];
while ($tableName = $statement->fetchColumn(0)) {
$tableNames[] = $tableName;
}
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)
{
if (!isset($this->columns[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->columns[$tableName];
}
/**
* 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)
{
if (!isset($this->keys[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->keys[$tableName];
}
/**
* Loads column info from a database table.
*
* @param string $tableName
*/
protected function loadColumnInfo($tableName)
{
$this->columns[$tableName] = [];
$this->keys[$tableName] = [];
$columnQuery = '
SELECT DISTINCT
COLUMN_NAME, ORDINAL_POSITION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = ? AND
TABLE_SCHEMA = ?
ORDER BY ORDINAL_POSITION
';
$columnQuery = '
select
rf.RDB$FIELD_NAME as COLUMN_NAME,
rf.RDB$FIELD_POSITION as ORDINAL_POSITION
from RDB$RELATION_FIELDS as rf
where
upper(RDB$RELATION_NAME) = upper(?)
order by
ORDINAL_POSITION
';
$columnStatement = $this->pdo->prepare($columnQuery);
$columnStatement->execute([$tableName]);
while ($columName = $columnStatement->fetchColumn(0)) {
$this->columns[$tableName][] = $columName;
}
$keyQuery = "
SELECT
KCU.COLUMN_NAME,
KCU.ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC
ON TC.TABLE_NAME = KCU.TABLE_NAME
WHERE
TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.TABLE_NAME = ? AND
TC.TABLE_SCHEMA = ?
ORDER BY
KCU.ORDINAL_POSITION ASC
";
$keyQuery = "
select
idseg.rdb\$field_name as COLUMN_NAME,
idseg.rdb\$field_position as ORDINAL_POSITION,
rc.rdb\$relation_name as tablename,
rc.rdb\$constraint_name as pk_name
from
RDB\$RELATION_CONSTRAINTS AS rc
left join
rdb\$index_segments as idseg on
(rc.rdb\$index_name = idseg.rdb\$index_name)
where
rc.RDB\$CONSTRAINT_TYPE = 'PRIMARY KEY'
and upper(rc.RDB\$RELATION_NAME) = upper(?)
order by
rc.rdb\$constraint_name, idseg.rdb\$field_position
";
$keyStatement = $this->pdo->prepare($keyQuery);
$keyStatement->execute([$tableName]);
while ($columName = $keyStatement->fetchColumn(0)) {
$this->keys[$tableName][] = $columName;
}
}
/**
* Returns the schema for the connection.
*
* @return string
*/
public function getSchema()
{
if (empty($this->schema)) {
return 'public';
} else {
return $this->schema;
}
}
/**
* Returns true if the rdbms allows cascading
*
* @return bool
*/
public function allowsCascading()
{
return false;
}
/**
* Returns a quoted schema object. (table name, column name, etc)
*
* @param string $object
*
* @return string
*/
public function quoteSchemaObject($object)
{
return $object; //firebird does not allow object quoting
}
}

View File

@@ -0,0 +1,135 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
/**
* Provides functionality to retrieve meta data from a database with information_schema support.
*/
class InformationSchema extends AbstractMetadata
{
protected $columns = [];
protected $keys = [];
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames()
{
$query = "
SELECT DISTINCT
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE='BASE TABLE' AND
TABLE_SCHEMA = ?
ORDER BY TABLE_NAME
";
$statement = $this->pdo->prepare($query);
$statement->execute([$this->getSchema()]);
$tableNames = [];
while ($tableName = $statement->fetchColumn(0)) {
$tableNames[] = $tableName;
}
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)
{
if (!isset($this->columns[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->columns[$tableName];
}
/**
* 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)
{
if (!isset($this->keys[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->keys[$tableName];
}
/**
* Loads column info from a sqlite database.
*
* @param string $tableName
*/
protected function loadColumnInfo($tableName)
{
$this->columns[$tableName] = [];
$this->keys[$tableName] = [];
$columnQuery = '
SELECT DISTINCT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = ? AND
TABLE_SCHEMA = ?
ORDER BY ORDINAL_POSITION
';
$columnStatement = $this->pdo->prepare($columnQuery);
$columnStatement->execute([$tableName, $this->getSchema()]);
while ($columName = $columnStatement->fetchColumn(0)) {
$this->columns[$tableName][] = $columName;
}
$keyQuery = "
SELECT
KCU.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU
WHERE
TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND
TC.TABLE_NAME = KCU.TABLE_NAME AND
TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA AND
TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.TABLE_NAME = ? AND
TC.TABLE_SCHEMA = ?
ORDER BY
KCU.ORDINAL_POSITION ASC
";
$keyStatement = $this->pdo->prepare($keyQuery);
$keyStatement->execute([$tableName, $this->getSchema()]);
while ($columName = $keyStatement->fetchColumn(0)) {
$this->keys[$tableName][] = $columName;
}
}
}

View File

@@ -0,0 +1,81 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
/**
* Provides a basic interface for retreiving metadata from a database.
*/
interface Metadata
{
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames();
/**
* Returns an array containing the names of all the columns in the
* $tableName table,
*
* @param string $tableName
*
* @return array
*/
public function getTableColumns($tableName);
/**
* 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);
/**
* Returns the name of the default schema.
*
* @return string
*/
public function getSchema();
/**
* Returns a quoted schema object. (table name, column name, etc)
*
* @param string $object
*
* @return string
*/
public function quoteSchemaObject($object);
/**
* Returns true if the rdbms allows cascading
*
* @return bool
*/
public function allowsCascading();
/**
* Disables primary keys if rdbms does not allow setting them otherwise
*
* @param string $tableName
*/
public function disablePrimaryKeys($tableName);
/**
* Reenables primary keys after they have been disabled
*
* @param string $tableName
*/
public function enablePrimaryKeys($tableName);
}

View File

@@ -0,0 +1,87 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
use PDO;
/**
* Provides functionality to retrieve meta data from a MySQL database.
*/
class MySQL extends AbstractMetadata
{
protected $schemaObjectQuoteChar = '`';
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames()
{
$query = 'SHOW TABLES';
$statement = $this->pdo->prepare($query);
$statement->execute();
$tableNames = [];
while (($tableName = $statement->fetchColumn(0))) {
$tableNames[] = $tableName;
}
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)
{
$query = 'SHOW COLUMNS FROM ' . $this->quoteSchemaObject($tableName);
$statement = $this->pdo->prepare($query);
$statement->execute();
$columnNames = [];
while (($columnName = $statement->fetchColumn(0))) {
$columnNames[] = $columnName;
}
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)
{
$query = 'SHOW INDEX FROM ' . $this->quoteSchemaObject($tableName);
$statement = $this->pdo->prepare($query);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
$columnNames = [];
while (($column = $statement->fetch())) {
if ($column['Key_name'] == 'PRIMARY') {
$columnNames[] = $column['Column_name'];
}
}
return $columnNames;
}
}

View File

@@ -0,0 +1,143 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
/**
* Provides functionality to retrieve meta data from an Oracle database.
*/
class Oci extends AbstractMetadata
{
/**
* No character used to quote schema objects.
*
* @var string
*/
protected $schemaObjectQuoteChar = '';
/**
* The command used to perform a TRUNCATE operation.
*
* @var string
*/
protected $truncateCommand = 'TRUNCATE TABLE';
/**
* @var array
*/
protected $columns = [];
/**
* @var array
*/
protected $keys = [];
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames()
{
$tableNames = [];
$query = "SELECT table_name
FROM cat
WHERE table_type='TABLE'
ORDER BY table_name";
$result = $this->pdo->query($query);
while ($tableName = $result->fetchColumn(0)) {
$tableNames[] = $tableName;
}
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)
{
if (!isset($this->columns[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->columns[$tableName];
}
/**
* 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)
{
if (!isset($this->keys[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->keys[$tableName];
}
/**
* Loads column info from a oracle database.
*
* @param string $tableName
*/
protected function loadColumnInfo($tableName)
{
$ownerQuery = '';
$conOwnerQuery = '';
$tableParts = $this->splitTableName($tableName);
$this->columns[$tableName] = [];
$this->keys[$tableName] = [];
if (!empty($tableParts['schema'])) {
$ownerQuery = " AND OWNER = '{$tableParts['schema']}'";
$conOwnerQuery = " AND a.owner = '{$tableParts['schema']}'";
}
$query = "SELECT DISTINCT COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME='" . $tableParts['table'] . "'
$ownerQuery
ORDER BY COLUMN_NAME";
$result = $this->pdo->query($query);
while ($columnName = $result->fetchColumn(0)) {
$this->columns[$tableName][] = $columnName;
}
$keyQuery = "SELECT b.column_name
FROM user_constraints a, user_cons_columns b
WHERE a.constraint_type='P'
AND a.constraint_name=b.constraint_name
$conOwnerQuery
AND a.table_name = '" . $tableParts['table'] . "' ";
$result = $this->pdo->query($keyQuery);
while ($columnName = $result->fetchColumn(0)) {
$this->keys[$tableName][] = $columnName;
}
}
}

View File

@@ -0,0 +1,155 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
/**
* Provides functionality to retrieve meta data from a PostgreSQL database.
*/
class PgSQL extends AbstractMetadata
{
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames()
{
$query = "
SELECT DISTINCT
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE='BASE TABLE' AND
TABLE_SCHEMA = ?
ORDER BY TABLE_NAME
";
$statement = $this->pdo->prepare($query);
$statement->execute([$this->getSchema()]);
$tableNames = [];
while ($tableName = $statement->fetchColumn(0)) {
$tableNames[] = $tableName;
}
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)
{
if (!isset($this->columns[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->columns[$tableName];
}
/**
* 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)
{
if (!isset($this->keys[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->keys[$tableName];
}
/**
* Loads column info from a database table.
*
* @param string $tableName
*/
protected function loadColumnInfo($tableName)
{
$this->columns[$tableName] = [];
$this->keys[$tableName] = [];
$columnQuery = '
SELECT DISTINCT
COLUMN_NAME, ORDINAL_POSITION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = ? AND
TABLE_SCHEMA = ?
ORDER BY ORDINAL_POSITION
';
$columnStatement = $this->pdo->prepare($columnQuery);
$columnStatement->execute([$tableName, $this->getSchema()]);
while ($columName = $columnStatement->fetchColumn(0)) {
$this->columns[$tableName][] = $columName;
}
$keyQuery = "
SELECT
KCU.COLUMN_NAME,
KCU.ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC
ON TC.TABLE_NAME = KCU.TABLE_NAME AND
TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
WHERE
TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.TABLE_NAME = ? AND
TC.TABLE_SCHEMA = ?
ORDER BY
KCU.ORDINAL_POSITION ASC
";
$keyStatement = $this->pdo->prepare($keyQuery);
$keyStatement->execute([$tableName, $this->getSchema()]);
while ($columName = $keyStatement->fetchColumn(0)) {
$this->keys[$tableName][] = $columName;
}
}
/**
* Returns the schema for the connection.
*
* @return string
*/
public function getSchema()
{
if (empty($this->schema)) {
return 'public';
} else {
return $this->schema;
}
}
/**
* Returns true if the rdbms allows cascading
*
* @return bool
*/
public function allowsCascading()
{
return true;
}
}

View File

@@ -0,0 +1,137 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
use PDO;
use PDOException;
/**
* Provides functionality to retrieve meta data from a Microsoft SQL Server database.
*/
class SqlSrv extends AbstractMetadata
{
/**
* No character used to quote schema objects.
*
* @var string
*/
protected $schemaObjectQuoteChar = '';
/**
* The command used to perform a TRUNCATE operation.
*
* @var string
*/
protected $truncateCommand = 'TRUNCATE TABLE';
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames()
{
$query = "SELECT name
FROM sysobjects
WHERE type='U'";
$statement = $this->pdo->prepare($query);
$statement->execute();
$tableNames = [];
while (($tableName = $statement->fetchColumn(0))) {
$tableNames[] = $tableName;
}
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)
{
$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 = [];
while (($columnName = $statement->fetchColumn(0))) {
$columnNames[] = $columnName;
}
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)
{
$query = "EXEC sp_statistics '$tableName'";
$statement = $this->pdo->prepare($query);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
$columnNames = [];
while (($column = $statement->fetch())) {
if ($column['TYPE'] == 1) {
$columnNames[] = $column['COLUMN_NAME'];
}
}
return $columnNames;
}
/**
* Allow overwriting identities for the given table.
*
* @param string $tableName
*/
public function disablePrimaryKeys($tableName)
{
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
}
}
/**
* Reenable auto creation of identities for the given table.
*
* @param string $tableName
*/
public function enablePrimaryKeys($tableName)
{
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
}
}
}

View File

@@ -0,0 +1,110 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
use PDO;
use PDOStatement;
/**
* Provides functionality to retrieve meta data from an Sqlite database.
*/
class Sqlite extends AbstractMetadata
{
protected $columns = [];
protected $keys = [];
protected $truncateCommand = 'DELETE FROM';
/**
* Returns an array containing the names of all the tables in the database.
*
* @return array
*/
public function getTableNames()
{
$query = "
SELECT name
FROM sqlite_master
WHERE
type='table' AND
name <> 'sqlite_sequence'
ORDER BY name
";
$result = $this->pdo->query($query);
$tableNames = [];
while ($tableName = $result->fetchColumn(0)) {
$tableNames[] = $tableName;
}
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)
{
if (!isset($this->columns[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->columns[$tableName];
}
/**
* 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)
{
if (!isset($this->keys[$tableName])) {
$this->loadColumnInfo($tableName);
}
return $this->keys[$tableName];
}
/**
* Loads column info from a sqlite database.
*
* @param string $tableName
*/
protected function loadColumnInfo($tableName)
{
$query = "PRAGMA table_info('{$tableName}')";
$statement = $this->pdo->query($query);
/* @var $statement PDOStatement */
$this->columns[$tableName] = [];
$this->keys[$tableName] = [];
while ($columnData = $statement->fetch(PDO::FETCH_NUM)) {
$this->columns[$tableName][] = $columnData[1];
if ((int) $columnData[5] !== 0) {
$this->keys[$tableName][] = $columnData[1];
}
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database\Metadata;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
/**
* This class loads a table metadata object with database metadata.
*/
class Table extends DefaultTableMetadata
{
public function __construct($tableName, Metadata $databaseMetaData)
{
$this->tableName = $tableName;
$this->columns = $databaseMetaData->getTableColumns($tableName);
$this->primaryKeys = $databaseMetaData->getTablePrimaryKeys($tableName);
}
}

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database;
use PDO;
use PDOStatement;
use PHPUnit\DbUnit\DataSet\AbstractTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
/**
* Provides the functionality to represent a database result set as a DBUnit
* table.
*
* @deprecated The PHPUnit_Extension_Database_DataSet_QueryTable should be used instead
* @see PHPUnit_Extension_Database_DataSet_QueryTable
* @see PHPUnit_Extension_Database_DataSet_QueryDataSet
*/
class ResultSetTable extends AbstractTable
{
/**
* Creates a new result set table.
*
* @param string $tableName
* @param PDOStatement $pdoStatement
*/
public function __construct($tableName, PDOStatement $pdoStatement)
{
$this->data = $pdoStatement->fetchAll(PDO::FETCH_ASSOC);
if (count($this->data)) {
$columns = array_keys($this->data[0]);
} else {
$columns = [];
}
$this->setTableMetaData(new DefaultTableMetadata($tableName, $columns));
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database;
use PDO;
use PHPUnit\DbUnit\DataSet\AbstractTable;
use PHPUnit\DbUnit\DataSet\ITableMetadata;
/**
* Provides the functionality to represent a database table.
*/
class Table extends AbstractTable
{
/**
* Creates a new database table object.
*
* @param ITableMetadata $tableMetaData
* @param Connection $databaseConnection
*/
public function __construct(ITableMetadata $tableMetaData, Connection $databaseConnection)
{
$this->setTableMetaData($tableMetaData);
$pdoStatement = $databaseConnection->getConnection()->prepare(DataSet::buildTableSelect($tableMetaData, $databaseConnection));
$pdoStatement->execute();
$this->data = $pdoStatement->fetchAll(PDO::FETCH_ASSOC);
}
}

View File

@@ -0,0 +1,128 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\DbUnit\Database;
use PHPUnit\DbUnit\DataSet\ITable;
use PHPUnit\DbUnit\DataSet\ITableIterator;
use PHPUnit\DbUnit\DataSet\ITableMetadata;
/**
* Provides iterative access to tables from a database instance.
*/
class TableIterator implements ITableIterator
{
/**
* An array of tablenames.
*
* @var array
*/
protected $tableNames;
/**
* If this property is true then the tables will be iterated in reverse
* order.
*
* @var bool
*/
protected $reverse;
/**
* The database dataset that this iterator iterates over.
*
* @var DataSet
*/
protected $dataSet;
public function __construct($tableNames, DataSet $dataSet, $reverse = false)
{
$this->tableNames = $tableNames;
$this->dataSet = $dataSet;
$this->reverse = $reverse;
$this->rewind();
}
/**
* Returns the current table.
*
* @return ITable
*/
public function getTable()
{
return $this->current();
}
/**
* Returns the current table's meta data.
*
* @return ITableMetadata
*/
public function getTableMetaData()
{
return $this->current()->getTableMetaData();
}
/**
* Returns the current table.
*
* @return ITable
*/
public function current()
{
$tableName = current($this->tableNames);
return $this->dataSet->getTable($tableName);
}
/**
* Returns the name of the current table.
*
* @return string
*/
public function key()
{
return $this->current()->getTableMetaData()->getTableName();
}
/**
* advances to the next element.
*/
public function next()
{
if ($this->reverse) {
prev($this->tableNames);
} else {
next($this->tableNames);
}
}
/**
* Rewinds to the first element
*/
public function rewind()
{
if ($this->reverse) {
end($this->tableNames);
} else {
reset($this->tableNames);
}
}
/**
* Returns true if the current index is valid
*
* @return bool
*/
public function valid()
{
return (current($this->tableNames) !== false);
}
}