mirror of
https://github.com/dg/dibi.git
synced 2025-08-09 07:37:08 +02:00
MySQL & MySQLi, SQLite & SQLite3 reflectors moved do external classes DibiMySqlReflector and DibiSqliteReflector
This commit is contained in:
@@ -11,6 +11,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/mysql.reflector.php';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The dibi driver for MySQL database.
|
* The dibi driver for MySQL database.
|
||||||
*
|
*
|
||||||
@@ -32,7 +35,7 @@
|
|||||||
* @copyright Copyright (c) 2005, 2010 David Grudl
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||||
* @package dibi\drivers
|
* @package dibi\drivers
|
||||||
*/
|
*/
|
||||||
class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector
|
class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||||
{
|
{
|
||||||
const ERROR_ACCESS_DENIED = 1045;
|
const ERROR_ACCESS_DENIED = 1045;
|
||||||
const ERROR_DUPLICATE_ENTRY = 1062;
|
const ERROR_DUPLICATE_ENTRY = 1062;
|
||||||
@@ -263,7 +266,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
|||||||
*/
|
*/
|
||||||
public function getReflector()
|
public function getReflector()
|
||||||
{
|
{
|
||||||
return $this;
|
return new DibiMySqlReflector($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -434,90 +437,4 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
|||||||
return $this->resultSet;
|
return $this->resultSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/********************* IDibiReflector ****************d*g**/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns list of tables.
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getTables()
|
|
||||||
{
|
|
||||||
$this->query("SHOW FULL TABLES");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(FALSE)) {
|
|
||||||
$res[] = array(
|
|
||||||
'name' => $row[0],
|
|
||||||
'view' => isset($row[1]) && $row[1] === 'VIEW',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all columns in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getColumns($table)
|
|
||||||
{
|
|
||||||
$this->query("SHOW FULL COLUMNS FROM `$table`");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$type = explode('(', $row['Type']);
|
|
||||||
$res[] = array(
|
|
||||||
'name' => $row['Field'],
|
|
||||||
'table' => $table,
|
|
||||||
'nativetype' => strtoupper($type[0]),
|
|
||||||
'size' => isset($type[1]) ? (int) $type[1] : NULL,
|
|
||||||
'unsigned' => (bool) strstr($row['Type'], 'unsigned'),
|
|
||||||
'nullable' => $row['Null'] === 'YES',
|
|
||||||
'default' => $row['Default'],
|
|
||||||
'autoincrement' => $row['Extra'] === 'auto_increment',
|
|
||||||
'vendor' => $row,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all indexes in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getIndexes($table)
|
|
||||||
{
|
|
||||||
$this->query("SHOW INDEX FROM `$table`");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[$row['Key_name']]['name'] = $row['Key_name'];
|
|
||||||
$res[$row['Key_name']]['unique'] = !$row['Non_unique'];
|
|
||||||
$res[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
|
|
||||||
$res[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return array_values($res);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all foreign keys in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getForeignKeys($table)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
133
dibi/drivers/mysql.reflector.php
Normal file
133
dibi/drivers/mysql.reflector.php
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dibi - tiny'n'smart database abstraction layer
|
||||||
|
* ----------------------------------------------
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||||
|
* @license http://dibiphp.com/license dibi license
|
||||||
|
* @link http://dibiphp.com
|
||||||
|
* @package dibi\drivers
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The dibi reflector for MySQL databases.
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||||
|
* @package dibi\drivers
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class DibiMySqlReflector extends DibiObject implements IDibiReflector
|
||||||
|
{
|
||||||
|
/** @var IDibiDriver */
|
||||||
|
private $driver;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(IDibiDriver $driver)
|
||||||
|
{
|
||||||
|
$this->driver = $driver;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns list of tables.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getTables()
|
||||||
|
{
|
||||||
|
/*$this->query("
|
||||||
|
SELECT TABLE_NAME as name, TABLE_TYPE = 'VIEW' as view
|
||||||
|
FROM INFORMATION_SCHEMA.TABLES
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
");*/
|
||||||
|
$this->driver->query("SHOW FULL TABLES");
|
||||||
|
$res = array();
|
||||||
|
while ($row = $this->driver->fetch(FALSE)) {
|
||||||
|
$res[] = array(
|
||||||
|
'name' => $row[0],
|
||||||
|
'view' => isset($row[1]) && $row[1] === 'VIEW',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->driver->free();
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns metadata for all columns in a table.
|
||||||
|
* @param string
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getColumns($table)
|
||||||
|
{
|
||||||
|
/*$table = $this->escape($table, dibi::TEXT);
|
||||||
|
$this->query("
|
||||||
|
SELECT *
|
||||||
|
FROM INFORMATION_SCHEMA.COLUMNS
|
||||||
|
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
|
||||||
|
");*/
|
||||||
|
$this->driver->query("SHOW FULL COLUMNS FROM `$table`");
|
||||||
|
$res = array();
|
||||||
|
while ($row = $this->driver->fetch(TRUE)) {
|
||||||
|
$type = explode('(', $row['Type']);
|
||||||
|
$res[] = array(
|
||||||
|
'name' => $row['Field'],
|
||||||
|
'table' => $table,
|
||||||
|
'nativetype' => strtoupper($type[0]),
|
||||||
|
'size' => isset($type[1]) ? (int) $type[1] : NULL,
|
||||||
|
'unsigned' => (bool) strstr($row['Type'], 'unsigned'),
|
||||||
|
'nullable' => $row['Null'] === 'YES',
|
||||||
|
'default' => $row['Default'],
|
||||||
|
'autoincrement' => $row['Extra'] === 'auto_increment',
|
||||||
|
'vendor' => $row,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->driver->free();
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns metadata for all indexes in a table.
|
||||||
|
* @param string
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getIndexes($table)
|
||||||
|
{
|
||||||
|
/*$table = $this->escape($table, dibi::TEXT);
|
||||||
|
$this->query("
|
||||||
|
SELECT *
|
||||||
|
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||||
|
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
|
||||||
|
AND REFERENCED_COLUMN_NAME IS NULL
|
||||||
|
");*/
|
||||||
|
$this->driver->query("SHOW INDEX FROM `$table`");
|
||||||
|
$res = array();
|
||||||
|
while ($row = $this->driver->fetch(TRUE)) {
|
||||||
|
$res[$row['Key_name']]['name'] = $row['Key_name'];
|
||||||
|
$res[$row['Key_name']]['unique'] = !$row['Non_unique'];
|
||||||
|
$res[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
|
||||||
|
$res[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
|
||||||
|
}
|
||||||
|
$this->driver->free();
|
||||||
|
return array_values($res);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns metadata for all foreign keys in a table.
|
||||||
|
* @param string
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getForeignKeys($table)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -11,6 +11,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/mysql.reflector.php';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The dibi driver for MySQL database via improved extension.
|
* The dibi driver for MySQL database via improved extension.
|
||||||
*
|
*
|
||||||
@@ -33,7 +36,7 @@
|
|||||||
* @copyright Copyright (c) 2005, 2010 David Grudl
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||||
* @package dibi\drivers
|
* @package dibi\drivers
|
||||||
*/
|
*/
|
||||||
class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector
|
class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||||
{
|
{
|
||||||
const ERROR_ACCESS_DENIED = 1045;
|
const ERROR_ACCESS_DENIED = 1045;
|
||||||
const ERROR_DUPLICATE_ENTRY = 1062;
|
const ERROR_DUPLICATE_ENTRY = 1062;
|
||||||
@@ -260,7 +263,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
|||||||
*/
|
*/
|
||||||
public function getReflector()
|
public function getReflector()
|
||||||
{
|
{
|
||||||
return $this;
|
return new DibiMySqlReflector($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -440,108 +443,4 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
|||||||
return $this->resultSet;
|
return $this->resultSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/********************* IDibiReflector ****************d*g**/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns list of tables.
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getTables()
|
|
||||||
{
|
|
||||||
/*$this->query("
|
|
||||||
SELECT TABLE_NAME as name, TABLE_TYPE = 'VIEW' as view
|
|
||||||
FROM INFORMATION_SCHEMA.TABLES
|
|
||||||
WHERE TABLE_SCHEMA = DATABASE()
|
|
||||||
");*/
|
|
||||||
$this->query("SHOW FULL TABLES");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(FALSE)) {
|
|
||||||
$res[] = array(
|
|
||||||
'name' => $row[0],
|
|
||||||
'view' => isset($row[1]) && $row[1] === 'VIEW',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all columns in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getColumns($table)
|
|
||||||
{
|
|
||||||
/*$table = $this->escape($table, dibi::TEXT);
|
|
||||||
$this->query("
|
|
||||||
SELECT *
|
|
||||||
FROM INFORMATION_SCHEMA.COLUMNS
|
|
||||||
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
|
|
||||||
");*/
|
|
||||||
$this->query("SHOW FULL COLUMNS FROM `$table`");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$type = explode('(', $row['Type']);
|
|
||||||
$res[] = array(
|
|
||||||
'name' => $row['Field'],
|
|
||||||
'table' => $table,
|
|
||||||
'nativetype' => strtoupper($type[0]),
|
|
||||||
'size' => isset($type[1]) ? (int) $type[1] : NULL,
|
|
||||||
'unsigned' => (bool) strstr($row['Type'], 'unsigned'),
|
|
||||||
'nullable' => $row['Null'] === 'YES',
|
|
||||||
'default' => $row['Default'],
|
|
||||||
'autoincrement' => $row['Extra'] === 'auto_increment',
|
|
||||||
'vendor' => $row,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all indexes in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getIndexes($table)
|
|
||||||
{
|
|
||||||
/*$table = $this->escape($table, dibi::TEXT);
|
|
||||||
$this->query("
|
|
||||||
SELECT *
|
|
||||||
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
|
||||||
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
|
|
||||||
AND REFERENCED_COLUMN_NAME IS NULL
|
|
||||||
");*/
|
|
||||||
$this->query("SHOW INDEX FROM `$table`");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[$row['Key_name']]['name'] = $row['Key_name'];
|
|
||||||
$res[$row['Key_name']]['unique'] = !$row['Non_unique'];
|
|
||||||
$res[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
|
|
||||||
$res[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return array_values($res);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all foreign keys in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getForeignKeys($table)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -11,6 +11,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/sqlite.reflector.php';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The dibi driver for SQLite database.
|
* The dibi driver for SQLite database.
|
||||||
*
|
*
|
||||||
@@ -28,7 +31,7 @@
|
|||||||
* @copyright Copyright (c) 2005, 2010 David Grudl
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||||
* @package dibi\drivers
|
* @package dibi\drivers
|
||||||
*/
|
*/
|
||||||
class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector
|
class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||||
{
|
{
|
||||||
/** @var resource Connection resource */
|
/** @var resource Connection resource */
|
||||||
private $connection;
|
private $connection;
|
||||||
@@ -210,7 +213,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
|||||||
*/
|
*/
|
||||||
public function getReflector()
|
public function getReflector()
|
||||||
{
|
{
|
||||||
return $this;
|
return new DibiSqliteReflector($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -387,139 +390,6 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/********************* IDibiReflector ****************d*g**/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns list of tables.
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getTables()
|
|
||||||
{
|
|
||||||
$this->query("
|
|
||||||
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
|
|
||||||
UNION ALL
|
|
||||||
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
|
|
||||||
ORDER BY name
|
|
||||||
");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[] = $row;
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all columns in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getColumns($table)
|
|
||||||
{
|
|
||||||
$this->query("
|
|
||||||
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '$table'
|
|
||||||
UNION ALL
|
|
||||||
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = '$table'"
|
|
||||||
);
|
|
||||||
$meta = $this->fetch(TRUE);
|
|
||||||
$this->free();
|
|
||||||
|
|
||||||
$this->query("PRAGMA table_info([$table])");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$column = $row['name'];
|
|
||||||
$pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui";
|
|
||||||
$type = explode('(', $row['type']);
|
|
||||||
|
|
||||||
$res[] = array(
|
|
||||||
'name' => $column,
|
|
||||||
'table' => $table,
|
|
||||||
'fullname' => "$table.$column",
|
|
||||||
'nativetype' => strtoupper($type[0]),
|
|
||||||
'size' => isset($type[1]) ? (int) $type[1] : NULL,
|
|
||||||
'nullable' => $row['notnull'] == '0',
|
|
||||||
'default' => $row['dflt_value'],
|
|
||||||
'autoincrement' => (bool) preg_match($pattern, $meta['sql']),
|
|
||||||
'vendor' => $row,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all indexes in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getIndexes($table)
|
|
||||||
{
|
|
||||||
$this->query("PRAGMA index_list([$table])");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[$row['name']]['name'] = $row['name'];
|
|
||||||
$res[$row['name']]['unique'] = (bool) $row['unique'];
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
|
|
||||||
foreach ($res as $index => $values) {
|
|
||||||
$this->query("PRAGMA index_info([$index])");
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[$index]['columns'][$row['seqno']] = $row['name'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
|
|
||||||
$columns = $this->getColumns($table);
|
|
||||||
foreach ($res as $index => $values) {
|
|
||||||
$column = $res[$index]['columns'][0];
|
|
||||||
$primary = FALSE;
|
|
||||||
foreach ($columns as $info) {
|
|
||||||
if ($column == $info['name']) {
|
|
||||||
$primary = $info['vendor']['pk'];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$res[$index]['primary'] = (bool) $primary;
|
|
||||||
}
|
|
||||||
if (!$res) { // @see http://www.sqlite.org/lang_createtable.html#rowid
|
|
||||||
foreach ($columns as $column) {
|
|
||||||
if ($column['vendor']['pk']) {
|
|
||||||
$res[] = array(
|
|
||||||
'name' => 'ROWID',
|
|
||||||
'unique' => TRUE,
|
|
||||||
'primary' => TRUE,
|
|
||||||
'columns' => array($column['name']),
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return array_values($res);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all foreign keys in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getForeignKeys($table)
|
|
||||||
{
|
|
||||||
// @see http://www.sqlite.org/foreignkeys.html
|
|
||||||
throw new NotSupportedException;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/********************* user defined functions ****************d*g**/
|
/********************* user defined functions ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
179
dibi/drivers/sqlite.reflector.php
Normal file
179
dibi/drivers/sqlite.reflector.php
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dibi - tiny'n'smart database abstraction layer
|
||||||
|
* ----------------------------------------------
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||||
|
* @license http://dibiphp.com/license dibi license
|
||||||
|
* @link http://dibiphp.com
|
||||||
|
* @package dibi\drivers
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The dibi reflector for SQLite database.
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||||
|
* @package dibi\drivers
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class DibiSqliteReflector extends DibiObject implements IDibiReflector
|
||||||
|
{
|
||||||
|
/** @var IDibiDriver */
|
||||||
|
private $driver;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(IDibiDriver $driver)
|
||||||
|
{
|
||||||
|
$this->driver = $driver;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns list of tables.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getTables()
|
||||||
|
{
|
||||||
|
$this->driver->query("
|
||||||
|
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
|
||||||
|
UNION ALL
|
||||||
|
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
|
||||||
|
ORDER BY name
|
||||||
|
");
|
||||||
|
$res = array();
|
||||||
|
while ($row = $this->driver->fetch(TRUE)) {
|
||||||
|
$res[] = $row;
|
||||||
|
}
|
||||||
|
$this->driver->free();
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns metadata for all columns in a table.
|
||||||
|
* @param string
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getColumns($table)
|
||||||
|
{
|
||||||
|
$this->driver->query("
|
||||||
|
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '$table'
|
||||||
|
UNION ALL
|
||||||
|
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = '$table'"
|
||||||
|
);
|
||||||
|
$meta = $this->driver->fetch(TRUE);
|
||||||
|
$this->driver->free();
|
||||||
|
|
||||||
|
$this->driver->query("PRAGMA table_info([$table])");
|
||||||
|
$res = array();
|
||||||
|
while ($row = $this->driver->fetch(TRUE)) {
|
||||||
|
$column = $row['name'];
|
||||||
|
$pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui";
|
||||||
|
$type = explode('(', $row['type']);
|
||||||
|
|
||||||
|
$res[] = array(
|
||||||
|
'name' => $column,
|
||||||
|
'table' => $table,
|
||||||
|
'fullname' => "$table.$column",
|
||||||
|
'nativetype' => strtoupper($type[0]),
|
||||||
|
'size' => isset($type[1]) ? (int) $type[1] : NULL,
|
||||||
|
'nullable' => $row['notnull'] == '0',
|
||||||
|
'default' => $row['dflt_value'],
|
||||||
|
'autoincrement' => (bool) preg_match($pattern, $meta['sql']),
|
||||||
|
'vendor' => $row,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->driver->free();
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns metadata for all indexes in a table.
|
||||||
|
* @param string
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getIndexes($table)
|
||||||
|
{
|
||||||
|
$this->driver->query("PRAGMA index_list([$table])");
|
||||||
|
$res = array();
|
||||||
|
while ($row = $this->driver->fetch(TRUE)) {
|
||||||
|
$res[$row['name']]['name'] = $row['name'];
|
||||||
|
$res[$row['name']]['unique'] = (bool) $row['unique'];
|
||||||
|
}
|
||||||
|
$this->driver->free();
|
||||||
|
|
||||||
|
foreach ($res as $index => $values) {
|
||||||
|
$this->driver->query("PRAGMA index_info([$index])");
|
||||||
|
while ($row = $this->driver->fetch(TRUE)) {
|
||||||
|
$res[$index]['columns'][$row['seqno']] = $row['name'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->driver->free();
|
||||||
|
|
||||||
|
$columns = $this->getColumns($table);
|
||||||
|
foreach ($res as $index => $values) {
|
||||||
|
$column = $res[$index]['columns'][0];
|
||||||
|
$primary = FALSE;
|
||||||
|
foreach ($columns as $info) {
|
||||||
|
if ($column == $info['name']) {
|
||||||
|
$primary = $info['vendor']['pk'];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$res[$index]['primary'] = (bool) $primary;
|
||||||
|
}
|
||||||
|
if (!$res) { // @see http://www.sqlite.org/lang_createtable.html#rowid
|
||||||
|
foreach ($columns as $column) {
|
||||||
|
if ($column['vendor']['pk']) {
|
||||||
|
$res[] = array(
|
||||||
|
'name' => 'ROWID',
|
||||||
|
'unique' => TRUE,
|
||||||
|
'primary' => TRUE,
|
||||||
|
'columns' => array($column['name']),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_values($res);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns metadata for all foreign keys in a table.
|
||||||
|
* @param string
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getForeignKeys($table)
|
||||||
|
{
|
||||||
|
if (!($this->driver instanceof DibiSqlite3Driver)) {
|
||||||
|
// throw new NotSupportedException; // @see http://www.sqlite.org/foreignkeys.html
|
||||||
|
}
|
||||||
|
$this->driver->query("PRAGMA foreign_key_list([$table])");
|
||||||
|
$res = array();
|
||||||
|
while ($row = $this->driver->fetch(TRUE)) {
|
||||||
|
$res[$row['id']]['name'] = $row['id']; // foreign key name
|
||||||
|
$res[$row['id']]['local'][$row['seq']] = $row['from']; // local columns
|
||||||
|
$res[$row['id']]['table'] = $row['table']; // referenced table
|
||||||
|
$res[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns
|
||||||
|
$res[$row['id']]['onDelete'] = $row['on_delete'];
|
||||||
|
$res[$row['id']]['onUpdate'] = $row['on_update'];
|
||||||
|
|
||||||
|
if ($res[$row['id']]['foreign'][0] == NULL) {
|
||||||
|
$res[$row['id']]['foreign'] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->driver->free();
|
||||||
|
return array_values($res);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -11,6 +11,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/sqlite.reflector.php';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The dibi driver for SQLite3 database.
|
* The dibi driver for SQLite3 database.
|
||||||
*
|
*
|
||||||
@@ -26,7 +29,7 @@
|
|||||||
* @copyright Copyright (c) 2005, 2010 David Grudl
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||||
* @package dibi\drivers
|
* @package dibi\drivers
|
||||||
*/
|
*/
|
||||||
class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector
|
class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||||
{
|
{
|
||||||
/** @var SQLite3 Connection resource */
|
/** @var SQLite3 Connection resource */
|
||||||
private $connection;
|
private $connection;
|
||||||
@@ -200,7 +203,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
|||||||
*/
|
*/
|
||||||
public function getReflector()
|
public function getReflector()
|
||||||
{
|
{
|
||||||
return $this;
|
return new DibiSqliteReflector($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -373,153 +376,6 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/********************* IDibiReflector ****************d*g**/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns list of tables.
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getTables()
|
|
||||||
{
|
|
||||||
$this->query("
|
|
||||||
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
|
|
||||||
UNION ALL
|
|
||||||
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
|
|
||||||
ORDER BY name
|
|
||||||
");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[] = $row;
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all columns in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getColumns($table)
|
|
||||||
{
|
|
||||||
$this->query("
|
|
||||||
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '$table'
|
|
||||||
UNION ALL
|
|
||||||
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = '$table'"
|
|
||||||
);
|
|
||||||
$meta = $this->fetch(TRUE);
|
|
||||||
$this->free();
|
|
||||||
|
|
||||||
$this->query("PRAGMA table_info([$table])");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$column = $row['name'];
|
|
||||||
$pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui";
|
|
||||||
$type = explode('(', $row['type']);
|
|
||||||
|
|
||||||
$res[] = array(
|
|
||||||
'name' => $column,
|
|
||||||
'table' => $table,
|
|
||||||
'fullname' => "$table.$column",
|
|
||||||
'nativetype' => strtoupper($type[0]),
|
|
||||||
'size' => isset($type[1]) ? (int) $type[1] : NULL,
|
|
||||||
'nullable' => $row['notnull'] == '0',
|
|
||||||
'default' => $row['dflt_value'],
|
|
||||||
'autoincrement' => (bool) preg_match($pattern, $meta['sql']),
|
|
||||||
'vendor' => $row,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all indexes in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getIndexes($table)
|
|
||||||
{
|
|
||||||
$this->query("PRAGMA index_list([$table])");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[$row['name']]['name'] = $row['name'];
|
|
||||||
$res[$row['name']]['unique'] = (bool) $row['unique'];
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
|
|
||||||
foreach ($res as $index => $values) {
|
|
||||||
$this->query("PRAGMA index_info([$index])");
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[$index]['columns'][$row['seqno']] = $row['name'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
|
|
||||||
$columns = $this->getColumns($table);
|
|
||||||
foreach ($res as $index => $values) {
|
|
||||||
$column = $res[$index]['columns'][0];
|
|
||||||
$primary = FALSE;
|
|
||||||
foreach ($columns as $info) {
|
|
||||||
if ($column == $info['name']) {
|
|
||||||
$primary = $info['vendor']['pk'];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$res[$index]['primary'] = (bool) $primary;
|
|
||||||
}
|
|
||||||
if (!$res) { // @see http://www.sqlite.org/lang_createtable.html#rowid
|
|
||||||
foreach ($columns as $column) {
|
|
||||||
if ($column['vendor']['pk']) {
|
|
||||||
$res[] = array(
|
|
||||||
'name' => 'ROWID',
|
|
||||||
'unique' => TRUE,
|
|
||||||
'primary' => TRUE,
|
|
||||||
'columns' => array($column['name']),
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return array_values($res);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns metadata for all foreign keys in a table.
|
|
||||||
* @param string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getForeignKeys($table)
|
|
||||||
{
|
|
||||||
$this->query("PRAGMA foreign_key_list([$table])");
|
|
||||||
$res = array();
|
|
||||||
while ($row = $this->fetch(TRUE)) {
|
|
||||||
$res[$row['id']]['name'] = $row['id']; // foreign key name
|
|
||||||
$res[$row['id']]['local'][$row['seq']] = $row['from']; // local columns
|
|
||||||
$res[$row['id']]['table'] = $row['table']; // referenced table
|
|
||||||
$res[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns
|
|
||||||
$res[$row['id']]['onDelete'] = $row['on_delete'];
|
|
||||||
$res[$row['id']]['onUpdate'] = $row['on_update'];
|
|
||||||
|
|
||||||
if ($res[$row['id']]['foreign'][0] == NULL) {
|
|
||||||
$res[$row['id']]['foreign'] = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->free();
|
|
||||||
return array_values($res);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/********************* user defined functions ****************d*g**/
|
/********************* user defined functions ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user