diff --git a/dibi/dibi.php b/dibi/dibi.php index f9788b19..84a59ef1 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -71,6 +71,7 @@ require_once dirname(__FILE__) . '/libs/DibiVariable.php'; require_once dirname(__FILE__) . '/libs/DibiTable.php'; require_once dirname(__FILE__) . '/libs/DibiDataSource.php'; require_once dirname(__FILE__) . '/libs/DibiFluent.php'; +require_once dirname(__FILE__) . '/libs/DibiDatabaseInfo.php'; @@ -440,6 +441,18 @@ class dibi + /** + * Gets a information about the current database - Monostate for DibiConnection::getDatabaseInfo(). + * + * @return DibiDatabaseInfo + */ + public static function getDatabaseInfo() + { + return self::getConnection()->getDatabaseInfo(); + } + + + /** * Import SQL dump from file - extreme fast! * diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index 37d8900e..5b1ed63f 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -185,6 +185,22 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver + /** + * Returns the connection resource. + * + * @return mixed + */ + public function getResource() + { + return $this->connection; + } + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -258,6 +274,10 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -324,6 +344,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver // items 'name' and 'table' are required $info = (array) mssql_fetch_field($this->resultSet, $i); $info['table'] = $info['column_source']; + $info['nativetype'] = $info['type']; $meta[] = $info; } return $meta; @@ -331,18 +352,6 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver - /** - * Returns the connection resource. - * - * @return mixed - */ - public function getResource() - { - return $this->connection; - } - - - /** * Returns the result set resource. * @@ -355,12 +364,53 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection() - {} + public function getTables() + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + public function getColumns($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + public function getIndexes($table) + { + throw new NotImplementedException; + } + + + + /** + * 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.php b/dibi/drivers/mysql.php index dd121cc6..67f5611c 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -247,6 +247,22 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver + /** + * Returns the connection resource. + * + * @return mixed + */ + public function getResource() + { + return $this->connection; + } + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -317,6 +333,10 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -387,26 +407,15 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver $count = mysql_num_fields($this->resultSet); $meta = array(); for ($i = 0; $i < $count; $i++) { - // items 'name' and 'table' are required - $meta[] = (array) mysql_fetch_field($this->resultSet, $i); + $info = (array) mysql_fetch_field($this->resultSet, $i); + $info['nativetype'] = $info['type']; + $meta[] = $info; } return $meta; } - /** - * Returns the connection resource. - * - * @return mixed - */ - public function getResource() - { - return $this->connection; - } - - - /** * Returns the result set resource. * @@ -419,12 +428,59 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection() - {} + public function getTables() + { + $this->query("SHOW TABLES"); + $res = array(); + while ($row = mysql_fetch_array($this->resultSet, MYSQL_NUM)) { + $res[] = array('name' => $row[0]); + } + $this->free(); + return $res; + } + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + public function getColumns($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + public function getIndexes($table) + { + throw new NotImplementedException; + } + + + + /** + * 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 aa9e6588..ddca60c2 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -231,6 +231,22 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver + /** + * Returns the connection resource. + * + * @return mysqli + */ + public function getResource() + { + return $this->connection; + } + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -300,6 +316,10 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -370,25 +390,15 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver $meta = array(); for ($i = 0; $i < $count; $i++) { // items 'name' and 'table' are required - $meta[] = (array) mysqli_fetch_field_direct($this->resultSet, $i); + $info = (array) mysqli_fetch_field_direct($this->resultSet, $i); + $info['nativetype'] = $info['type']; + $meta[] = $info; } return $meta; } - /** - * Returns the connection resource. - * - * @return mysqli - */ - public function getResource() - { - return $this->connection; - } - - - /** * Returns the result set resource. * @@ -401,12 +411,59 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection() - {} + public function getTables() + { + $this->query("SHOW TABLES"); + $res = array(); + while ($row = mysqli_fetch_array($this->resultSet, MYSQLI_NUM)) { + $res[] = array('name' => $row[0]); + } + $this->free(); + return $res; + } + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + public function getColumns($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + public function getIndexes($table) + { + throw new NotImplementedException; + } + + + + /** + * 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/odbc.php b/dibi/drivers/odbc.php index 6f63ba0d..770eb494 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -199,6 +199,22 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver + /** + * Returns the connection resource. + * + * @return mixed + */ + public function getResource() + { + return $this->connection; + } + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -269,6 +285,10 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -347,7 +367,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver $meta[] = array( 'name' => odbc_field_name($this->resultSet, $i), 'table' => NULL, - 'type' => odbc_field_type($this->resultSet, $i), + 'nativetype'=> odbc_field_type($this->resultSet, $i), 'length' => odbc_field_len($this->resultSet, $i), 'scale' => odbc_field_scale($this->resultSet, $i), 'precision' => odbc_field_precision($this->resultSet, $i), @@ -358,18 +378,6 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver - /** - * Returns the connection resource. - * - * @return mixed - */ - public function getResource() - { - return $this->connection; - } - - - /** * Returns the result set resource. * @@ -382,12 +390,58 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection() - {} + public function getTables() + { + $result = odbc_tables($this->connection); + $res = array(); + while (odbc_fetch_row($result)) { + $res[] = array('name' => odbc_result($result, 'TABLE_NAME')); + } + return $res; + } + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + public function getColumns($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + public function getIndexes($table) + { + throw new NotImplementedException; + } + + + + /** + * 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/oracle.php b/dibi/drivers/oracle.php index 43070e64..e7e7ec26 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -199,6 +199,22 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver + /** + * Returns the connection resource. + * + * @return mixed + */ + public function getResource() + { + return $this->connection; + } + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -266,6 +282,10 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -333,7 +353,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver $meta[] = array( 'name' => oci_field_name($this->resultSet, $i), 'table' => NULL, - 'type' => oci_field_type($this->resultSet, $i), + 'nativetype'=> oci_field_type($this->resultSet, $i), 'size' => oci_field_size($this->resultSet, $i), 'scale' => oci_field_scale($this->resultSet, $i), 'precision' => oci_field_precision($this->resultSet, $i), @@ -344,18 +364,6 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver - /** - * Returns the connection resource. - * - * @return mixed - */ - public function getResource() - { - return $this->connection; - } - - - /** * Returns the result set resource. * @@ -368,12 +376,53 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection() - {} + public function getTables() + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + public function getColumns($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + public function getIndexes($table) + { + throw new NotImplementedException; + } + + + + /** + * 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/pdo.php b/dibi/drivers/pdo.php index c80322dc..367d7145 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -222,6 +222,22 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver + /** + * Returns the connection resource. + * + * @return PDO + */ + public function getResource() + { + return $this->connection; + } + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -312,6 +328,10 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -380,6 +400,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver if ($info === FALSE) { throw new DibiDriverException('Driver does not support meta data.'); } + $info['nativetype'] = $info['native_type']; $meta[] = $info; } return $meta; @@ -387,18 +408,6 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver - /** - * Returns the connection resource. - * - * @return PDO - */ - public function getResource() - { - return $this->connection; - } - - - /** * Returns the result set resource. * @@ -411,12 +420,53 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection() - {} + public function getTables() + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + public function getColumns($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + public function getIndexes($table) + { + throw new NotImplementedException; + } + + + + /** + * 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/postgre.php b/dibi/drivers/postgre.php index 18a4bc22..ab7cf48d 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -223,6 +223,22 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver + /** + * Returns the connection resource. + * + * @return mixed + */ + public function getResource() + { + return $this->connection; + } + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -314,6 +330,10 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -382,7 +402,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver $meta[] = array( 'name' => pg_field_name($this->resultSet, $i), 'table' => $hasTable ? pg_field_table($this->resultSet, $i) : NULL, - 'type' => pg_field_type($this->resultSet, $i), + 'nativetype'=> pg_field_type($this->resultSet, $i), 'size' => pg_field_size($this->resultSet, $i), 'prtlen' => pg_field_prtlen($this->resultSet, $i), ); @@ -392,18 +412,6 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver - /** - * Returns the connection resource. - * - * @return mixed - */ - public function getResource() - { - return $this->connection; - } - - - /** * Returns the result set resource. * @@ -416,12 +424,53 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection() - {} + public function getTables() + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + public function getColumns($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + public function getIndexes($table) + { + throw new NotImplementedException; + } + + + + /** + * 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 a02d6584..555eb8d5 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -202,6 +202,22 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver + /** + * Returns the connection resource. + * + * @return mixed + */ + public function getResource() + { + return $this->connection; + } + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -267,6 +283,10 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -335,10 +355,13 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver $count = sqlite_num_fields($this->resultSet); $meta = array(); for ($i = 0; $i < $count; $i++) { - // items 'name' and 'table' are required + $pair = explode('.', sqlite_field_name($this->resultSet, $i)); + if (!isset($pair[1])) { + array_unshift($pair, NULL); + } $meta[] = array( - 'name' => sqlite_field_name($this->resultSet, $i), - 'table' => NULL, + 'name' => $pair[1], + 'table' => $pair[0], ); } return $meta; @@ -346,18 +369,6 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver - /** - * Returns the connection resource. - * - * @return mixed - */ - public function getResource() - { - return $this->connection; - } - - - /** * Returns the result set resource. * @@ -370,12 +381,57 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection() - {} + public function getTables() + { + $this->query(" + SELECT name FROM sqlite_master WHERE type='table' + UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' ORDER BY name + "); + return sqlite_fetch_all($this->resultSet, SQLITE_ASSOC); + } + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + public function getColumns($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + public function getIndexes($table) + { + throw new NotImplementedException; + } + + + + /** + * Returns metadata for all foreign keys in a table. + * @param string + * @return array + */ + public function getForeignKeys($table) + { + throw new NotImplementedException; + } } diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index fe37915d..c9afb673 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -36,8 +36,8 @@ class DibiConnection extends DibiObject private $config; /** - * IDibiDriver. - * @var array + * Driver + * @var IDibiDriver */ private $driver; @@ -557,13 +557,13 @@ class DibiConnection extends DibiObject /** - * Gets a information of the current database. + * Gets a information about the current database. * - * @return DibiReflection + * @return DibiDatabaseInfo */ - public function getDibiReflection() + public function getDatabaseInfo() { - throw new NotImplementedException; + return new DibiDatabaseInfo($this->driver, isset($this->config['database']) ? $this->config['database'] : NULL); } diff --git a/dibi/libs/DibiDatabaseInfo.php b/dibi/libs/DibiDatabaseInfo.php new file mode 100644 index 00000000..65d2a3d0 --- /dev/null +++ b/dibi/libs/DibiDatabaseInfo.php @@ -0,0 +1,564 @@ +driver = $driver; + $this->name = $name; + } + + + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + + + /** + * @return array of DibiTableInfo + */ + public function getTables() + { + $this->init(); + return array_values($this->tables); + } + + + + /** + * @return array of string + */ + public function getTableNames() + { + $this->init(); + $res = array(); + foreach ($this->tables as $table) { + $res[] = $table->getName(); + } + return $res; + } + + + + /** + * @param string + * @return DibiTableInfo + */ + public function getTable($name) + { + $this->init(); + $l = strtolower($name); + if (isset($this->tables[$l])) { + return $this->tables[$l]; + + } else { + throw new DibiException("Database '$this->name' has no table '$name'."); + } + } + + + + /** + * @param string + * @return bool + */ + public function hasTable($name) + { + $this->init(); + return isset($this->tables[strtolower($name)]); + } + + + + /** + * @return array + */ + public function getSequences() + { + throw new NotImplementedException; + } + + + + /** + * @return void + */ + protected function init() + { + if ($this->tables === NULL) { + $this->tables = array(); + foreach ($this->driver->getTables() as $info) { + $this->tables[strtolower($info['name'])] = new DibiTableInfo($this->driver, $info); + } + } + } + +} + + + + +/** + * Reflection metadata class for a database table. + * @package dibi + */ +class DibiTableInfo extends DibiObject +{ + /** @var IDibiDriver */ + private $driver; + + /** @var array */ + private $info; + + /** @var array */ + private $columns; + + /** @var array */ + private $foreignKeys; + + /** @var array */ + private $indexes; + + /** @var DibiIndexInfo */ + private $primaryKey; + + + + public function __construct(IDibiDriver $driver, array $info) + { + $this->driver = $driver; + $this->info = $info; + } + + + + /** + * @return string + */ + public function getName() + { + return $this->info['name']; + } + + + + /** + * @return bool + */ + public function isView() + { + return !empty($this->info['view']); + } + + + + /** + * @return array of DibiColumnInfo + */ + public function getColumns() + { + $this->initColumns(); + return array_values($this->columns); + } + + + + /** + * @return array of string + */ + public function getColumnNames() + { + $this->initColumns(); + $res = array(); + foreach ($this->columns as $column) { + $res[] = $column->getName(); + } + return $res; + } + + + + /** + * @param string + * @return DibiColumnInfo + */ + public function getColumn($name) + { + $this->initColumns(); + $l = strtolower($name); + if (isset($this->columns[$l])) { + return $this->columns[$l]; + + } else { + throw new DibiException("Table '$this->name' has no column '$name'."); + } + } + + + + /** + * @param string + * @return bool + */ + public function hasColumn($name) + { + $this->initColumns(); + return isset($this->columns[strtolower($name)]); + } + + + + /** + * @return array of DibiForeignKeyInfo + */ + public function getForeignKeys() + { + $this->initForeignKeys(); + return $this->foreignKeys; + } + + + + /** + * @return array of DibiIndexInfo + */ + public function getIndexes() + { + $this->initIndexes(); + return $this->indexes; + } + + + + /** + * @return DibiIndexInfo + */ + public function getPrimaryKey() + { + $this->initIndexes(); + return $this->primaryKey; + } + + + + /** + * @return void + */ + protected function initColumns() + { + if ($this->columns === NULL) { + $this->columns = array(); + foreach ($this->driver->getColumns($this->name) as $info) { + $this->columns[strtolower($info['name'])] = new DibiColumnInfo($this->driver, $info); + } + } + } + + + + /** + * @return void + */ + protected function initIndexes() + { + if ($this->indexes === NULL) { + $this->initColumns(); + $this->indexes = array(); + foreach ($this->driver->getIndexes($this->name) as $info) { + $cols = array(); + foreach ($info['columns'] as $name) { + $cols[] = $this->columns[strtolower($name)]; + } + $name = $info['name']; + $this->indexes[strtolower($name)] = new DibiIndexInfo($this, $name, $cols, $info['unique']); + if (!empty($info['primary'])) { + $this->primaryKey = $this->indexes[strtolower($name)]; + } + } + } + } + + + + /** + * @return void + */ + protected function initForeignKeys() + { + throw new NotImplementedException; + } + +} + + + + +/** + * Reflection metadata class for a table column. + * @package dibi + */ +class DibiColumnInfo extends DibiObject +{ + /** @var IDibiDriver */ + private $driver; + + /** @var array (name, table, type, nativetype, size, precision, scale, nullable, default, autoincrement) */ + private $info; + + + + public function __construct(IDibiDriver $driver, array $info) + { + $this->driver = $driver; + $this->info = $info; + } + + + + /** + * @return string + */ + public function getName() + { + return $this->info['name']; + } + + + + /** + * @return DibiTableInfo + */ + public function getTable() + { + if (empty($this->info['table'])) { + throw new DibiException("Table name is unknown."); + } + return new DibiTableInfo($this->driver, array('name' => $this->info['table'])); + } + + + + /** + * @return string + */ + public function getType() + { + return isset($this->info['type']) ? $this->info['type'] : NULL; + } + + + + /** + * @return mixed + */ + public function getNativeType() + { + return isset($this->info['nativetype']) ? $this->info['nativetype'] : NULL; + } + + + + /** + * @return int + */ + public function getSize() + { + return isset($this->info['size']) ? (int) $this->info['size'] : NULL; + } + + + + /** + * @return int + */ + public function getPrecision() + { + return isset($this->info['precision']) ? (int) $this->info['precision'] : NULL; + } + + + + /** + * @return int + */ + public function getScale() + { + return isset($this->info['scale']) ? (int) $this->info['scale'] : NULL; + } + + + + /** + * @return bool + */ + public function isNullable() + { + return !empty($this->info['nullable']); + } + + + + /** + * @return bool + */ + public function isAutoIncrement() + { + return !empty($this->info['autoincrement']); + } + + + + /** + * @return mixed + */ + public function getDefaultValue() + { + return isset($this->info['default']) ? $this->info['default'] : NULL; + } + +} + + + + +/** + * Reflection metadata class for a foreign key. + * @package dibi + */ +class DibiForeignKeyInfo extends DibiObject +{ + /** @var string */ + private $name; + + /** @var array of array(local, foreign, onDelete, onUpdate) */ + private $references; + + + + public function __construct($name, array $references) + { + $this->name = $name; + $this->references = $references; + } + + + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + + + /** + * @return array + */ + public function getReferences() + { + return $this->references; + } + +} + + + + +/** + * Reflection metadata class for a index or primary key + * @package dibi + */ +class DibiIndexInfo extends DibiObject +{ + /** @var string */ + private $name; + + /** @var array of DibiColumnInfo */ + private $columns; + + /** @var bool */ + private $unique; + + + + public function __construct($name, array $columns, $unique) + { + $this->name = $name; + $this->columns = $columns; + $this->unique = (bool) $unique; + } + + + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + + + /** + * @return array + */ + public function getColumns() + { + return $this->columns; + } + + + + /** + * @return bool + */ + public function isUnique() + { + return $this->unique; + } + +} diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index b7e532a2..50f4ac0f 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -560,18 +560,35 @@ class DibiResult extends DibiObject implements IDataSource /** * Gets an array of meta informations about column. * - * @return array + * @return array of DibiColumnInfo */ - final public function getColumnsMeta() + final public function getColumns() { if ($this->metaCache === NULL) { $this->metaCache = $this->getDriver()->getColumnsMeta(); } $cols = array(); - foreach ($this->metaCache as $col) { - $name = (!$this->withTables || $col['table'] === NULL) ? $col['name'] : ($col['table'] . '.' . $col['name']); - $cols[$name] = $col; + foreach ($this->metaCache as $info) { + $cols[] = new DibiColumnInfo($this->driver, $info); + } + return $cols; + } + + + + /** + * @param bool + * @return array of string + */ + public function getColumnNames($withTables = FALSE) + { + if ($this->metaCache === NULL) { + $this->metaCache = $this->getDriver()->getColumnsMeta(); + } + $cols = array(); + foreach ($this->metaCache as $info) { + $cols[] = (!$withTables || $info['table'] === NULL) ? $info['name'] : ($info['table'] . '.' . $info['name']); } return $cols; } diff --git a/dibi/libs/DibiTable.php b/dibi/libs/DibiTable.php index b9083d71..90c0bee9 100644 --- a/dibi/libs/DibiTable.php +++ b/dibi/libs/DibiTable.php @@ -256,7 +256,7 @@ abstract class DibiTable extends DibiObject array_shift($order); } else { $conditions = NULL; - } + } } return $this->complete($this->connection->query( 'SELECT * FROM %n', $this->name, diff --git a/dibi/libs/interfaces.php b/dibi/libs/interfaces.php index 8bc8a4fe..dbf58c46 100644 --- a/dibi/libs/interfaces.php +++ b/dibi/libs/interfaces.php @@ -141,6 +141,19 @@ interface IDibiDriver + /** + * Returns the connection resource. + * + * @return mixed + */ + function getResource(); + + + + /********************* SQL ****************d*g**/ + + + /** * Encodes data for use in an SQL statement. * @@ -177,6 +190,10 @@ interface IDibiDriver + /********************* result set ****************d*g**/ + + + /** * Returns the number of rows in a result set. * @@ -228,15 +245,6 @@ interface IDibiDriver - /** - * Returns the connection resource. - * - * @return mixed - */ - function getResource(); - - - /** * Returns the result set resource. * @@ -246,11 +254,41 @@ interface IDibiDriver + /********************* reflection ****************d*g**/ + + + /** - * Gets a information of the current database. - * - * @return DibiReflection + * Returns list of tables. + * @return array */ - function getDibiReflection(); + function getTables(); + + + + /** + * Returns metadata for all columns in a table. + * @param string + * @return array + */ + function getColumns($table); + + + + /** + * Returns metadata for all indexes in a table. + * @param string + * @return array + */ + function getIndexes($table); + + + + /** + * Returns metadata for all foreign keys in a table. + * @param string + * @return array + */ + function getForeignKeys($table); }