From b1156e54d8b5e9b3b58b64a62eb5ce7c01fd64fb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 27 Aug 2010 01:59:54 +0200 Subject: [PATCH] drivers meta function refactoring --- dibi/drivers/firebird.php | 158 +++++++++++++++--------------- dibi/drivers/mssql.php | 6 +- dibi/drivers/mssql2005.php | 6 +- dibi/drivers/mysql.php | 6 +- dibi/drivers/mysql.reflector.php | 42 ++++---- dibi/drivers/mysqli.php | 6 +- dibi/drivers/odbc.php | 30 +++--- dibi/drivers/oracle.php | 27 +++-- dibi/drivers/pdo.php | 6 +- dibi/drivers/postgre.php | 62 ++++++------ dibi/drivers/sqlite.php | 6 +- dibi/drivers/sqlite.reflector.php | 91 +++++++++-------- dibi/drivers/sqlite3.php | 6 +- 13 files changed, 224 insertions(+), 228 deletions(-) diff --git a/dibi/drivers/firebird.php b/dibi/drivers/firebird.php index 51ecdabf..eed0f340 100644 --- a/dibi/drivers/firebird.php +++ b/dibi/drivers/firebird.php @@ -422,21 +422,21 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD */ public function getTables() { - $this->query(" + $res = $this->query(" SELECT TRIM(RDB\$RELATION_NAME), CASE RDB\$VIEW_BLR WHEN NULL THEN 'TRUE' ELSE 'FALSE' END FROM RDB\$RELATIONS WHERE RDB\$SYSTEM_FLAG = 0;" ); - $res = array(); - while ($row = $this->fetch(FALSE)) { - $res[] = array( + $tables = array(); + while ($row = $res->fetch(FALSE)) { + $tables[] = array( 'name' => $row[0], 'view' => $row[1] === 'TRUE', ); } - $this->free(); - return $res; + $res->free(); + return $tables; } @@ -449,7 +449,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD public function getColumns($table) { $table = strtoupper($table); - $this->query(" + $res = $this->query(" SELECT TRIM(r.RDB\$FIELD_NAME) AS FIELD_NAME, CASE f.RDB\$FIELD_TYPE WHEN 261 THEN 'BLOB' @@ -479,10 +479,10 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD ORDER BY r.RDB\$FIELD_POSITION;" ); - $res = array(); - while ($row = $this->fetch(TRUE)) { + $columns = array(); + while ($row = $res->fetch(TRUE)) { $key = $row['FIELD_NAME']; - $res[$key] = array( + $columns[$key] = array( 'name' => $key, 'table' => $table, 'nativetype' => trim($row['FIELD_TYPE']), @@ -492,8 +492,8 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD 'autoincrement' => FALSE, ); } - $this->free(); - return $res; + $res->free(); + return $columns; } @@ -506,7 +506,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD public function getIndexes($table) { $table = strtoupper($table); - $this->query(" + $res = $this->query(" SELECT TRIM(s.RDB\$INDEX_NAME) AS INDEX_NAME, TRIM(s.RDB\$FIELD_NAME) AS FIELD_NAME, i.RDB\$UNIQUE_FLAG AS UNIQUE_FLAG, @@ -519,17 +519,17 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD WHERE UPPER(i.RDB\$RELATION_NAME) = '$table' ORDER BY s.RDB\$FIELD_POSITION" ); - $res = array(); - while ($row = $this->fetch(TRUE)) { + $indexes = array(); + while ($row = $res->fetch(TRUE)) { $key = $row['INDEX_NAME']; - $res[$key]['name'] = $key; - $res[$key]['unique'] = $row['UNIQUE_FLAG'] === 1; - $res[$key]['primary'] = $row['CONSTRAINT_TYPE'] === 'PRIMARY KEY'; - $res[$key]['table'] = $table; - $res[$key]['columns'][$row['FIELD_POSITION']] = $row['FIELD_NAME']; + $indexes[$key]['name'] = $key; + $indexes[$key]['unique'] = $row['UNIQUE_FLAG'] === 1; + $indexes[$key]['primary'] = $row['CONSTRAINT_TYPE'] === 'PRIMARY KEY'; + $indexes[$key]['table'] = $table; + $indexes[$key]['columns'][$row['FIELD_POSITION']] = $row['FIELD_NAME']; } - $this->free(); - return $res; + $res->free(); + return $indexes; } @@ -542,7 +542,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD public function getForeignKeys($table) { $table = strtoupper($table); - $this->query(" + $res = $this->query(" SELECT TRIM(s.RDB\$INDEX_NAME) AS INDEX_NAME, TRIM(s.RDB\$FIELD_NAME) AS FIELD_NAME, FROM RDB\$INDEX_SEGMENTS s @@ -551,17 +551,17 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD AND r.RDB\$CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY s.RDB\$FIELD_POSITION" ); - $res = array(); - while ($row = $this->fetch(TRUE)) { + $keys = array(); + while ($row = $res->fetch(TRUE)) { $key = $row['INDEX_NAME']; - $res[$key] = array( + $keys[$key] = array( 'name' => $key, 'column' => $row['FIELD_NAME'], 'table' => $table, ); } - $this->free(); - return $res; + $res->free(); + return $keys; } @@ -573,19 +573,19 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD */ public function getIndices($table) { - $this->query(" + $res = $this->query(" SELECT TRIM(RDB\$INDEX_NAME) FROM RDB\$INDICES WHERE RDB\$RELATION_NAME = UPPER('$table') AND RDB\$UNIQUE_FLAG IS NULL AND RDB\$FOREIGN_KEY IS NULL;" ); - $res = array(); - while ($row = $this->fetch(FALSE)) { - $res[] = $row[0]; + $indices = array(); + while ($row = $res->fetch(FALSE)) { + $indices[] = $row[0]; } - $this->free(); - return $res; + $res->free(); + return $indices; } @@ -597,7 +597,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD */ public function getConstraints($table) { - $this->query(" + $res = $this->query(" SELECT TRIM(RDB\$INDEX_NAME) FROM RDB\$INDICES WHERE RDB\$RELATION_NAME = UPPER('$table') @@ -606,12 +606,12 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD OR RDB\$FOREIGN_KEY IS NOT NULL );" ); - $res = array(); - while ($row = $this->fetch(FALSE)) { - $res[] = $row[0]; + $constraints = array(); + while ($row = $res->fetch(FALSE)) { + $constraints[] = $row[0]; } - $this->free(); - return $res; + $res->free(); + return $constraints; } @@ -625,7 +625,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD */ public function getTriggersMeta($table = NULL) { - $this->query(" + $res = $this->query(" SELECT TRIM(RDB\$TRIGGER_NAME) AS TRIGGER_NAME, TRIM(RDB\$RELATION_NAME) AS TABLE_NAME, CASE RDB\$TRIGGER_TYPE @@ -651,9 +651,9 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD WHERE RDB\$SYSTEM_FLAG = 0" . ($table === NULL ? ";" : " AND RDB\$RELATION_NAME = UPPER('$table');") ); - $res = array(); - while ($row = $this->fetch(TRUE)) { - $res[$row['TRIGGER_NAME']] = array( + $triggers = array(); + while ($row = $res->fetch(TRUE)) { + $triggers[$row['TRIGGER_NAME']] = array( 'name' => $row['TRIGGER_NAME'], 'table' => $row['TABLE_NAME'], 'type' => trim($row['TRIGGER_TYPE']), @@ -661,8 +661,8 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD 'enabled' => trim($row['TRIGGER_ENABLED']) === 'TRUE', ); } - $this->free(); - return $res; + $res->free(); + return $triggers; } @@ -680,13 +680,13 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD WHERE RDB\$SYSTEM_FLAG = 0"; $q .= $table === NULL ? ";" : " AND RDB\$RELATION_NAME = UPPER('$table')"; - $this->query($q); - $res = array(); - while ($row = $this->fetch(FALSE)) { - $res[] = $row[0]; + $res = $this->query($q); + $triggers = array(); + while ($row = $res->fetch(FALSE)) { + $triggers[] = $row[0]; } - $this->free(); - return $res; + $res->free(); + return $triggers; } @@ -698,7 +698,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD */ public function getProceduresMeta() { - $this->query(" + $res = $this->query(" SELECT TRIM(p.RDB\$PARAMETER_NAME) AS PARAMETER_NAME, TRIM(p.RDB\$PROCEDURE_NAME) AS PROCEDURE_NAME, @@ -730,18 +730,18 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD LEFT JOIN RDB\$FIELDS f ON f.RDB\$FIELD_NAME = p.RDB\$FIELD_SOURCE ORDER BY p.RDB\$PARAMETER_TYPE, p.RDB\$PARAMETER_NUMBER;" ); - $res = array(); - while ($row = $this->fetch(TRUE)) { + $procedures = array(); + while ($row = $res->fetch(TRUE)) { $key = $row['PROCEDURE_NAME']; $io = trim($row['PARAMETER_TYPE']); $num = $row['PARAMETER_NUMBER']; - $res[$key]['name'] = $row['PROCEDURE_NAME']; - $res[$key]['params'][$io][$num]['name'] = $row['PARAMETER_NAME']; - $res[$key]['params'][$io][$num]['type'] = trim($row['FIELD_TYPE']); - $res[$key]['params'][$io][$num]['size'] = $row['FIELD_LENGTH']; + $procedures[$key]['name'] = $row['PROCEDURE_NAME']; + $procedures[$key]['params'][$io][$num]['name'] = $row['PARAMETER_NAME']; + $procedures[$key]['params'][$io][$num]['type'] = trim($row['FIELD_TYPE']); + $procedures[$key]['params'][$io][$num]['size'] = $row['FIELD_LENGTH']; } - $this->free(); - return $res; + $res->free(); + return $procedures; } @@ -752,16 +752,16 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD */ public function getProcedures() { - $this->query(" + $res = $this->query(" SELECT TRIM(RDB\$PROCEDURE_NAME) FROM RDB\$PROCEDURES;" ); - $res = array(); - while ($row = $this->fetch(FALSE)) { - $res[] = $row[0]; + $procedures = array(); + while ($row = $res->fetch(FALSE)) { + $procedures[] = $row[0]; } - $this->free(); - return $res; + $res->free(); + return $procedures; } @@ -772,17 +772,17 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD */ public function getGenerators() { - $this->query(" + $res = $this->query(" SELECT TRIM(RDB\$GENERATOR_NAME) FROM RDB\$GENERATORS WHERE RDB\$SYSTEM_FLAG = 0;" ); - $res = array(); - while ($row = $this->fetch(FALSE)) { - $res[] = $row[0]; + $generators = array(); + while ($row = $res->fetch(FALSE)) { + $generators[] = $row[0]; } - $this->free(); - return $res; + $res->free(); + return $generators; } @@ -793,17 +793,17 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD */ public function getFunctions() { - $this->query(" + $res = $this->query(" SELECT TRIM(RDB\$FUNCTION_NAME) FROM RDB\$FUNCTIONS WHERE RDB\$SYSTEM_FLAG = 0;" ); - $res = array(); - while ($row = $this->fetch(FALSE)) { - $res[] = $row[0]; + $functions = array(); + while ($row = $res->fetch(FALSE)) { + $functions[] = $row[0]; } - $this->free(); - return $res; + $res->free(); + return $functions; } } diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index f00a1109..88dc51f7 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -340,17 +340,17 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv public function getResultColumns() { $count = mssql_num_fields($this->resultSet); - $res = array(); + $columns = array(); for ($i = 0; $i < $count; $i++) { $row = (array) mssql_fetch_field($this->resultSet, $i); - $res[] = array( + $columns[] = array( 'name' => $row['name'], 'fullname' => $row['column_source'] ? $row['column_source'] . '.' . $row['name'] : $row['name'], 'table' => $row['column_source'], 'nativetype' => $row['type'], ); } - return $res; + return $columns; } diff --git a/dibi/drivers/mssql2005.php b/dibi/drivers/mssql2005.php index d3439c4f..625b5ff7 100644 --- a/dibi/drivers/mssql2005.php +++ b/dibi/drivers/mssql2005.php @@ -342,16 +342,16 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult public function getResultColumns() { $count = sqlsrv_num_fields($this->resultSet); - $res = array(); + $columns = array(); for ($i = 0; $i < $count; $i++) { $row = (array) sqlsrv_field_metadata($this->resultSet, $i); - $res[] = array( + $columns[] = array( 'name' => $row['Name'], 'fullname' => $row['Name'], 'nativetype' => $row['Type'], ); } - return $res; + return $columns; } diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 312e6153..71d683dd 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -426,10 +426,10 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv public function getResultColumns() { $count = mysql_num_fields($this->resultSet); - $res = array(); + $columns = array(); for ($i = 0; $i < $count; $i++) { $row = (array) mysql_fetch_field($this->resultSet, $i); - $res[] = array( + $columns[] = array( 'name' => $row['name'], 'table' => $row['table'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], @@ -437,7 +437,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv 'vendor' => $row, ); } - return $res; + return $columns; } diff --git a/dibi/drivers/mysql.reflector.php b/dibi/drivers/mysql.reflector.php index aeb0d400..be02ba79 100644 --- a/dibi/drivers/mysql.reflector.php +++ b/dibi/drivers/mysql.reflector.php @@ -43,16 +43,16 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() ");*/ - $this->driver->query("SHOW FULL TABLES"); - $res = array(); - while ($row = $this->driver->fetch(FALSE)) { - $res[] = array( + $res = $this->driver->query("SHOW FULL TABLES"); + $tables = array(); + while ($row = $res->fetch(FALSE)) { + $tables[] = array( 'name' => $row[0], 'view' => isset($row[1]) && $row[1] === 'VIEW', ); } - $this->driver->free(); - return $res; + $res->free(); + return $tables; } @@ -70,11 +70,11 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector 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)) { + $res = $this->driver->query("SHOW FULL COLUMNS FROM `$table`"); + $columns = array(); + while ($row = $res->fetch(TRUE)) { $type = explode('(', $row['Type']); - $res[] = array( + $columns[] = array( 'name' => $row['Field'], 'table' => $table, 'nativetype' => strtoupper($type[0]), @@ -86,8 +86,8 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector 'vendor' => $row, ); } - $this->driver->free(); - return $res; + $res->free(); + return $columns; } @@ -106,16 +106,16 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector 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']; + $res = $this->driver->query("SHOW INDEX FROM `$table`"); + $indexes = array(); + while ($row = $res->fetch(TRUE)) { + $indexes[$row['Key_name']]['name'] = $row['Key_name']; + $indexes[$row['Key_name']]['unique'] = !$row['Non_unique']; + $indexes[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY'; + $indexes[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name']; } - $this->driver->free(); - return array_values($res); + $res->free(); + return array_values($indexes); } diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 099f45f0..8a2001f8 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -432,10 +432,10 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri } $count = mysqli_num_fields($this->resultSet); - $res = array(); + $columns = array(); for ($i = 0; $i < $count; $i++) { $row = (array) mysqli_fetch_field_direct($this->resultSet, $i); - $res[] = array( + $columns[] = array( 'name' => $row['name'], 'table' => $row['orgtable'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], @@ -443,7 +443,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri 'vendor' => $row, ); } - return $res; + return $columns; } diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index c093e47d..4d8ff8c4 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -367,16 +367,16 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive public function getResultColumns() { $count = odbc_num_fields($this->resultSet); - $res = array(); + $columns = array(); for ($i = 1; $i <= $count; $i++) { - $res[] = array( + $columns[] = array( 'name' => odbc_field_name($this->resultSet, $i), 'table' => NULL, 'fullname' => odbc_field_name($this->resultSet, $i), 'nativetype'=> odbc_field_type($this->resultSet, $i), ); } - return $res; + return $columns; } @@ -402,18 +402,18 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive */ public function getTables() { - $result = odbc_tables($this->connection); - $res = array(); - while ($row = odbc_fetch_array($result)) { + $res = odbc_tables($this->connection); + $tables = array(); + while ($row = odbc_fetch_array($res)) { if ($row['TABLE_TYPE'] === 'TABLE' || $row['TABLE_TYPE'] === 'VIEW') { - $res[] = array( + $tables[] = array( 'name' => $row['TABLE_NAME'], 'view' => $row['TABLE_TYPE'] === 'VIEW', ); } } - odbc_free_result($result); - return $res; + odbc_free_result($res); + return $tables; } @@ -425,11 +425,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive */ public function getColumns($table) { - $result = odbc_columns($this->connection); - $res = array(); - while ($row = odbc_fetch_array($result)) { + $res = odbc_columns($this->connection); + $columns = array(); + while ($row = odbc_fetch_array($res)) { if ($row['TABLE_NAME'] === $table) { - $res[] = array( + $columns[] = array( 'name' => $row['COLUMN_NAME'], 'table' => $table, 'nativetype' => $row['TYPE_NAME'], @@ -439,8 +439,8 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive ); } } - odbc_free_result($result); - return $res; + odbc_free_result($res); + return $columns; } diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index 6b495c92..298261e0 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -133,8 +133,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri */ public function getInsertId($sequence) { - $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL"); - $row = $this->fetch(TRUE); + $row = $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL")->fetch(TRUE); return isset($row['ID']) ? (int) $row['ID'] : FALSE; } @@ -354,16 +353,16 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri public function getResultColumns() { $count = oci_num_fields($this->resultSet); - $res = array(); + $columns = array(); for ($i = 1; $i <= $count; $i++) { - $res[] = array( - 'name' => oci_field_name($this->resultSet, $i), - 'table' => NULL, - 'fullname' => oci_field_name($this->resultSet, $i), + $columns[] = array( + 'name' => oci_field_name($this->resultSet, $i), + 'table' => NULL, + 'fullname' => oci_field_name($this->resultSet, $i), 'nativetype'=> oci_field_type($this->resultSet, $i), ); } - return $res; + return $columns; } @@ -389,18 +388,18 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri */ public function getTables() { - $this->query('SELECT * FROM cat'); - $res = array(); - while ($row = $this->fetch(FALSE)) { + $res = $this->query('SELECT * FROM cat'); + $tables = array(); + while ($row = $res->fetch(FALSE)) { if ($row[1] === 'TABLE' || $row[1] === 'VIEW') { - $res[] = array( + $tables[] = array( 'name' => $row[0], 'view' => $row[1] === 'VIEW', ); } } - $this->free(); - return $res; + $res->free(); + return $tables; } diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 891d9d1f..719984f7 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -419,7 +419,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver public function getResultColumns() { $count = $this->resultSet->columnCount(); - $res = array(); + $columns = array(); for ($i = 0; $i < $count; $i++) { $row = @$this->resultSet->getColumnMeta($i); // intentionally @ if ($row === FALSE) { @@ -429,7 +429,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver // @see: http://php.net/manual/en/pdostatement.getcolumnmeta.php#pdostatement.getcolumnmeta.changelog $row['table'] = isset($row['table']) ? $row['table'] : NULL; - $res[] = array( + $columns[] = array( 'name' => $row['name'], 'table' => $row['table'], 'nativetype' => $row['native_type'], @@ -437,7 +437,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver 'vendor' => $row, ); } - return $res; + return $columns; } diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index e9344a74..d867ce0e 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -155,15 +155,15 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr { if ($sequence === NULL) { // PostgreSQL 8.1 is needed - $has = $this->query("SELECT LASTVAL()"); + $res = $this->query("SELECT LASTVAL()"); } else { - $has = $this->query("SELECT CURRVAL('$sequence')"); + $res = $this->query("SELECT CURRVAL('$sequence')"); } - if (!$has) return FALSE; + if (!$res) return FALSE; - $row = $this->fetch(FALSE); - $this->free(); + $row = $res->fetch(FALSE); + $res->free(); return is_array($row) ? $row[0] : FALSE; } @@ -396,7 +396,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr { $hasTable = version_compare(PHP_VERSION , '5.2.0', '>='); $count = pg_num_fields($this->resultSet); - $res = array(); + $columns = array(); for ($i = 0; $i < $count; $i++) { $row = array( 'name' => pg_field_name($this->resultSet, $i), @@ -404,9 +404,9 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr 'nativetype'=> pg_field_type($this->resultSet, $i), ); $row['fullname'] = $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name']; - $res[] = $row; + $columns[] = $row; } - return $res; + return $columns; } @@ -437,14 +437,14 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr throw new DibiDriverException('Reflection requires PostgreSQL 8.'); } - $this->query(" + $res = $this->query(" SELECT table_name as name, CAST(table_type = 'VIEW' AS INTEGER) as view FROM information_schema.tables WHERE table_schema = current_schema() "); - $res = pg_fetch_all($this->resultSet); - $this->free(); - return $res ? $res : array(); + $tables = pg_fetch_all($res->resultSet); + $res->free(); + return $tables ? $tables : array(); } @@ -457,24 +457,24 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr public function getColumns($table) { $_table = $this->escape($table, dibi::TEXT); - $this->query(" + $res = $this->query(" SELECT indkey FROM pg_class LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary WHERE pg_class.relname = $_table "); - $primary = (int) pg_fetch_object($this->resultSet)->indkey; + $primary = (int) pg_fetch_object($res->resultSet)->indkey; - $this->query(" + $res = $this->query(" SELECT * FROM information_schema.columns WHERE table_name = $_table AND table_schema = current_schema() ORDER BY ordinal_position "); - $res = array(); - while ($row = $this->fetch(TRUE)) { + $columns = array(); + while ($row = $res->fetch(TRUE)) { $size = (int) max($row['character_maximum_length'], $row['numeric_precision']); - $res[] = array( + $columns[] = array( 'name' => $row['column_name'], 'table' => $table, 'nativetype' => strtoupper($row['udt_name']), @@ -485,8 +485,8 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr 'vendor' => $row, ); } - $this->free(); - return $res; + $res->free(); + return $columns; } @@ -499,7 +499,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr public function getIndexes($table) { $_table = $this->escape($table, dibi::TEXT); - $this->query(" + $res = $this->query(" SELECT ordinal_position, column_name FROM information_schema.columns WHERE table_name = $_table AND table_schema = current_schema() @@ -507,11 +507,11 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr "); $columns = array(); - while ($row = $this->fetch(TRUE)) { + while ($row = $res->fetch(TRUE)) { $columns[$row['ordinal_position']] = $row['column_name']; } - $this->query(" + $res = $this->query(" SELECT pg_class2.relname, indisunique, indisprimary, indkey FROM pg_class LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid @@ -519,17 +519,17 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr WHERE pg_class.relname = $_table "); - $res = array(); - while ($row = $this->fetch(TRUE)) { - $res[$row['relname']]['name'] = $row['relname']; - $res[$row['relname']]['unique'] = $row['indisunique'] === 't'; - $res[$row['relname']]['primary'] = $row['indisprimary'] === 't'; + $indexes = array(); + while ($row = $res->fetch(TRUE)) { + $indexes[$row['relname']]['name'] = $row['relname']; + $indexes[$row['relname']]['unique'] = $row['indisunique'] === 't'; + $indexes[$row['relname']]['primary'] = $row['indisprimary'] === 't'; foreach (explode(' ', $row['indkey']) as $index) { - $res[$row['relname']]['columns'][] = $columns[$index]; + $indexes[$row['relname']]['columns'][] = $columns[$index]; } } - $this->free(); - return array_values($res); + $res->free(); + return array_values($indexes); } diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index fa33d232..391ff9e3 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -376,18 +376,18 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri public function getResultColumns() { $count = sqlite_num_fields($this->resultSet); - $res = array(); + $columns = array(); for ($i = 0; $i < $count; $i++) { $name = str_replace(array('[', ']'), '', sqlite_field_name($this->resultSet, $i)); $pair = explode('.', $name); - $res[] = array( + $columns[] = array( 'name' => isset($pair[1]) ? $pair[1] : $pair[0], 'table' => isset($pair[1]) ? $pair[0] : NULL, 'fullname' => $name, 'nativetype' => NULL, ); } - return $res; + return $columns; } diff --git a/dibi/drivers/sqlite.reflector.php b/dibi/drivers/sqlite.reflector.php index 7aee66ee..0aa14cac 100644 --- a/dibi/drivers/sqlite.reflector.php +++ b/dibi/drivers/sqlite.reflector.php @@ -38,18 +38,18 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector */ public function getTables() { - $this->driver->query(" + $res = $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; + $tables = array(); + while ($row = $res->fetch(TRUE)) { + $tables[] = $row; } - $this->driver->free(); - return $res; + $res->free(); + return $tables; } @@ -61,22 +61,19 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector */ public function getColumns($table) { - $this->driver->query(" + $meta = $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(); + )->fetch(TRUE); - $this->driver->query("PRAGMA table_info([$table])"); - $res = array(); - while ($row = $this->driver->fetch(TRUE)) { + $res = $this->driver->query("PRAGMA table_info([$table])"); + $columns = array(); + while ($row = $res->fetch(TRUE)) { $column = $row['name']; $pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui"; $type = explode('(', $row['type']); - - $res[] = array( + $columns[] = array( 'name' => $column, 'table' => $table, 'fullname' => "$table.$column", @@ -88,8 +85,8 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector 'vendor' => $row, ); } - $this->driver->free(); - return $res; + $res->free(); + return $columns; } @@ -101,25 +98,25 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector */ 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']; + $res = $this->driver->query("PRAGMA index_list([$table])"); + $indexes = array(); + while ($row = $res->fetch(TRUE)) { + $indexes[$row['name']]['name'] = $row['name']; + $indexes[$row['name']]['unique'] = (bool) $row['unique']; } - $this->driver->free(); + $res->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']; + foreach ($indexes as $index => $values) { + $res = $this->driver->query("PRAGMA index_info([$index])"); + while ($row = $res->fetch(TRUE)) { + $indexes[$index]['columns'][$row['seqno']] = $row['name']; } + $res->free(); } - $this->driver->free(); $columns = $this->getColumns($table); - foreach ($res as $index => $values) { - $column = $res[$index]['columns'][0]; + foreach ($indexes as $index => $values) { + $column = $indexes[$index]['columns'][0]; $primary = FALSE; foreach ($columns as $info) { if ($column == $info['name']) { @@ -127,12 +124,12 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector break; } } - $res[$index]['primary'] = (bool) $primary; + $indexes[$index]['primary'] = (bool) $primary; } - if (!$res) { // @see http://www.sqlite.org/lang_createtable.html#rowid + if (!$indexes) { // @see http://www.sqlite.org/lang_createtable.html#rowid foreach ($columns as $column) { if ($column['vendor']['pk']) { - $res[] = array( + $indexes[] = array( 'name' => 'ROWID', 'unique' => TRUE, 'primary' => TRUE, @@ -143,7 +140,7 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector } } - return array_values($res); + return array_values($indexes); } @@ -158,22 +155,22 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector 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']; + $res = $this->driver->query("PRAGMA foreign_key_list([$table])"); + $keys = array(); + while ($row = $res->fetch(TRUE)) { + $keys[$row['id']]['name'] = $row['id']; // foreign key name + $keys[$row['id']]['local'][$row['seq']] = $row['from']; // local columns + $keys[$row['id']]['table'] = $row['table']; // referenced table + $keys[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns + $keys[$row['id']]['onDelete'] = $row['on_delete']; + $keys[$row['id']]['onUpdate'] = $row['on_update']; - if ($res[$row['id']]['foreign'][0] == NULL) { - $res[$row['id']]['foreign'] = NULL; + if ($keys[$row['id']]['foreign'][0] == NULL) { + $keys[$row['id']]['foreign'] = NULL; } } - $this->driver->free(); - return array_values($res); + $res->free(); + return array_values($keys); } } diff --git a/dibi/drivers/sqlite3.php b/dibi/drivers/sqlite3.php index 8598f81a..6d7194f6 100644 --- a/dibi/drivers/sqlite3.php +++ b/dibi/drivers/sqlite3.php @@ -364,17 +364,17 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr public function getResultColumns() { $count = $this->resultSet->numColumns(); - $res = array(); + $columns = array(); static $types = array(SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null'); for ($i = 0; $i < $count; $i++) { - $res[] = array( + $columns[] = array( 'name' => $this->resultSet->columnName($i), 'table' => NULL, 'fullname' => $this->resultSet->columnName($i), 'nativetype' => $types[$this->resultSet->columnType($i)], ); } - return $res; + return $columns; }