From 6430573d61bc54a5f189e76cb2cfb0cdd3edf9e2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 5 Aug 2010 21:14:37 +0200 Subject: [PATCH] MySQL & MySQLi, SQLite & SQLite3 reflectors moved do external classes DibiMySqlReflector and DibiSqliteReflector --- dibi/drivers/mysql.php | 93 +--------------- dibi/drivers/mysql.reflector.php | 133 ++++++++++++++++++++++ dibi/drivers/mysqli.php | 111 +----------------- dibi/drivers/sqlite.php | 140 +---------------------- dibi/drivers/sqlite.reflector.php | 179 ++++++++++++++++++++++++++++++ dibi/drivers/sqlite3.php | 154 +------------------------ 6 files changed, 332 insertions(+), 478 deletions(-) create mode 100644 dibi/drivers/mysql.reflector.php create mode 100644 dibi/drivers/sqlite.reflector.php diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 53431dd8..86bc8b12 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -11,6 +11,9 @@ */ +require_once dirname(__FILE__) . '/mysql.reflector.php'; + + /** * The dibi driver for MySQL database. * @@ -32,7 +35,7 @@ * @copyright Copyright (c) 2005, 2010 David Grudl * @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_DUPLICATE_ENTRY = 1062; @@ -263,7 +266,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv */ public function getReflector() { - return $this; + return new DibiMySqlReflector($this); } @@ -434,90 +437,4 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv 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; - } - } diff --git a/dibi/drivers/mysql.reflector.php b/dibi/drivers/mysql.reflector.php new file mode 100644 index 00000000..aeb0d400 --- /dev/null +++ b/dibi/drivers/mysql.reflector.php @@ -0,0 +1,133 @@ +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; + } + +} diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 0bfdc45e..c38a15c2 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -11,6 +11,9 @@ */ +require_once dirname(__FILE__) . '/mysql.reflector.php'; + + /** * The dibi driver for MySQL database via improved extension. * @@ -33,7 +36,7 @@ * @copyright Copyright (c) 2005, 2010 David Grudl * @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_DUPLICATE_ENTRY = 1062; @@ -260,7 +263,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri */ public function getReflector() { - return $this; + return new DibiMySqlReflector($this); } @@ -440,108 +443,4 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri 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; - } - } diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 96dfb651..683a3e30 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -11,6 +11,9 @@ */ +require_once dirname(__FILE__) . '/sqlite.reflector.php'; + + /** * The dibi driver for SQLite database. * @@ -28,7 +31,7 @@ * @copyright Copyright (c) 2005, 2010 David Grudl * @package dibi\drivers */ -class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector +class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDriver { /** @var resource Connection resource */ private $connection; @@ -210,7 +213,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri */ 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**/ diff --git a/dibi/drivers/sqlite.reflector.php b/dibi/drivers/sqlite.reflector.php new file mode 100644 index 00000000..7aee66ee --- /dev/null +++ b/dibi/drivers/sqlite.reflector.php @@ -0,0 +1,179 @@ +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); + } + +} diff --git a/dibi/drivers/sqlite3.php b/dibi/drivers/sqlite3.php index 42fa17e1..31a656de 100644 --- a/dibi/drivers/sqlite3.php +++ b/dibi/drivers/sqlite3.php @@ -11,6 +11,9 @@ */ +require_once dirname(__FILE__) . '/sqlite.reflector.php'; + + /** * The dibi driver for SQLite3 database. * @@ -26,7 +29,7 @@ * @copyright Copyright (c) 2005, 2010 David Grudl * @package dibi\drivers */ -class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector +class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDriver { /** @var SQLite3 Connection resource */ private $connection; @@ -200,7 +203,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr */ 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**/