1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 06:07:39 +02:00

drivers meta function refactoring

This commit is contained in:
David Grudl
2010-08-27 01:59:54 +02:00
parent 39add9b8a3
commit b1156e54d8
13 changed files with 224 additions and 228 deletions

View File

@@ -422,21 +422,21 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getTables() public function getTables()
{ {
$this->query(" $res = $this->query("
SELECT TRIM(RDB\$RELATION_NAME), SELECT TRIM(RDB\$RELATION_NAME),
CASE RDB\$VIEW_BLR WHEN NULL THEN 'TRUE' ELSE 'FALSE' END CASE RDB\$VIEW_BLR WHEN NULL THEN 'TRUE' ELSE 'FALSE' END
FROM RDB\$RELATIONS FROM RDB\$RELATIONS
WHERE RDB\$SYSTEM_FLAG = 0;" WHERE RDB\$SYSTEM_FLAG = 0;"
); );
$res = array(); $tables = array();
while ($row = $this->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$res[] = array( $tables[] = array(
'name' => $row[0], 'name' => $row[0],
'view' => $row[1] === 'TRUE', 'view' => $row[1] === 'TRUE',
); );
} }
$this->free(); $res->free();
return $res; return $tables;
} }
@@ -449,7 +449,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
public function getColumns($table) public function getColumns($table)
{ {
$table = strtoupper($table); $table = strtoupper($table);
$this->query(" $res = $this->query("
SELECT TRIM(r.RDB\$FIELD_NAME) AS FIELD_NAME, SELECT TRIM(r.RDB\$FIELD_NAME) AS FIELD_NAME,
CASE f.RDB\$FIELD_TYPE CASE f.RDB\$FIELD_TYPE
WHEN 261 THEN 'BLOB' WHEN 261 THEN 'BLOB'
@@ -479,10 +479,10 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
ORDER BY r.RDB\$FIELD_POSITION;" ORDER BY r.RDB\$FIELD_POSITION;"
); );
$res = array(); $columns = array();
while ($row = $this->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key = $row['FIELD_NAME']; $key = $row['FIELD_NAME'];
$res[$key] = array( $columns[$key] = array(
'name' => $key, 'name' => $key,
'table' => $table, 'table' => $table,
'nativetype' => trim($row['FIELD_TYPE']), 'nativetype' => trim($row['FIELD_TYPE']),
@@ -492,8 +492,8 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
'autoincrement' => FALSE, 'autoincrement' => FALSE,
); );
} }
$this->free(); $res->free();
return $res; return $columns;
} }
@@ -506,7 +506,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
public function getIndexes($table) public function getIndexes($table)
{ {
$table = strtoupper($table); $table = strtoupper($table);
$this->query(" $res = $this->query("
SELECT TRIM(s.RDB\$INDEX_NAME) AS INDEX_NAME, SELECT TRIM(s.RDB\$INDEX_NAME) AS INDEX_NAME,
TRIM(s.RDB\$FIELD_NAME) AS FIELD_NAME, TRIM(s.RDB\$FIELD_NAME) AS FIELD_NAME,
i.RDB\$UNIQUE_FLAG AS UNIQUE_FLAG, 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' WHERE UPPER(i.RDB\$RELATION_NAME) = '$table'
ORDER BY s.RDB\$FIELD_POSITION" ORDER BY s.RDB\$FIELD_POSITION"
); );
$res = array(); $indexes = array();
while ($row = $this->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key = $row['INDEX_NAME']; $key = $row['INDEX_NAME'];
$res[$key]['name'] = $key; $indexes[$key]['name'] = $key;
$res[$key]['unique'] = $row['UNIQUE_FLAG'] === 1; $indexes[$key]['unique'] = $row['UNIQUE_FLAG'] === 1;
$res[$key]['primary'] = $row['CONSTRAINT_TYPE'] === 'PRIMARY KEY'; $indexes[$key]['primary'] = $row['CONSTRAINT_TYPE'] === 'PRIMARY KEY';
$res[$key]['table'] = $table; $indexes[$key]['table'] = $table;
$res[$key]['columns'][$row['FIELD_POSITION']] = $row['FIELD_NAME']; $indexes[$key]['columns'][$row['FIELD_POSITION']] = $row['FIELD_NAME'];
} }
$this->free(); $res->free();
return $res; return $indexes;
} }
@@ -542,7 +542,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
public function getForeignKeys($table) public function getForeignKeys($table)
{ {
$table = strtoupper($table); $table = strtoupper($table);
$this->query(" $res = $this->query("
SELECT TRIM(s.RDB\$INDEX_NAME) AS INDEX_NAME, SELECT TRIM(s.RDB\$INDEX_NAME) AS INDEX_NAME,
TRIM(s.RDB\$FIELD_NAME) AS FIELD_NAME, TRIM(s.RDB\$FIELD_NAME) AS FIELD_NAME,
FROM RDB\$INDEX_SEGMENTS s FROM RDB\$INDEX_SEGMENTS s
@@ -551,17 +551,17 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
AND r.RDB\$CONSTRAINT_TYPE = 'FOREIGN KEY' AND r.RDB\$CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY s.RDB\$FIELD_POSITION" ORDER BY s.RDB\$FIELD_POSITION"
); );
$res = array(); $keys = array();
while ($row = $this->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key = $row['INDEX_NAME']; $key = $row['INDEX_NAME'];
$res[$key] = array( $keys[$key] = array(
'name' => $key, 'name' => $key,
'column' => $row['FIELD_NAME'], 'column' => $row['FIELD_NAME'],
'table' => $table, 'table' => $table,
); );
} }
$this->free(); $res->free();
return $res; return $keys;
} }
@@ -573,19 +573,19 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getIndices($table) public function getIndices($table)
{ {
$this->query(" $res = $this->query("
SELECT TRIM(RDB\$INDEX_NAME) SELECT TRIM(RDB\$INDEX_NAME)
FROM RDB\$INDICES FROM RDB\$INDICES
WHERE RDB\$RELATION_NAME = UPPER('$table') WHERE RDB\$RELATION_NAME = UPPER('$table')
AND RDB\$UNIQUE_FLAG IS NULL AND RDB\$UNIQUE_FLAG IS NULL
AND RDB\$FOREIGN_KEY IS NULL;" AND RDB\$FOREIGN_KEY IS NULL;"
); );
$res = array(); $indices = array();
while ($row = $this->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$res[] = $row[0]; $indices[] = $row[0];
} }
$this->free(); $res->free();
return $res; return $indices;
} }
@@ -597,7 +597,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getConstraints($table) public function getConstraints($table)
{ {
$this->query(" $res = $this->query("
SELECT TRIM(RDB\$INDEX_NAME) SELECT TRIM(RDB\$INDEX_NAME)
FROM RDB\$INDICES FROM RDB\$INDICES
WHERE RDB\$RELATION_NAME = UPPER('$table') WHERE RDB\$RELATION_NAME = UPPER('$table')
@@ -606,12 +606,12 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
OR RDB\$FOREIGN_KEY IS NOT NULL OR RDB\$FOREIGN_KEY IS NOT NULL
);" );"
); );
$res = array(); $constraints = array();
while ($row = $this->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$res[] = $row[0]; $constraints[] = $row[0];
} }
$this->free(); $res->free();
return $res; return $constraints;
} }
@@ -625,7 +625,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getTriggersMeta($table = NULL) public function getTriggersMeta($table = NULL)
{ {
$this->query(" $res = $this->query("
SELECT TRIM(RDB\$TRIGGER_NAME) AS TRIGGER_NAME, SELECT TRIM(RDB\$TRIGGER_NAME) AS TRIGGER_NAME,
TRIM(RDB\$RELATION_NAME) AS TABLE_NAME, TRIM(RDB\$RELATION_NAME) AS TABLE_NAME,
CASE RDB\$TRIGGER_TYPE CASE RDB\$TRIGGER_TYPE
@@ -651,9 +651,9 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
WHERE RDB\$SYSTEM_FLAG = 0" WHERE RDB\$SYSTEM_FLAG = 0"
. ($table === NULL ? ";" : " AND RDB\$RELATION_NAME = UPPER('$table');") . ($table === NULL ? ";" : " AND RDB\$RELATION_NAME = UPPER('$table');")
); );
$res = array(); $triggers = array();
while ($row = $this->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$res[$row['TRIGGER_NAME']] = array( $triggers[$row['TRIGGER_NAME']] = array(
'name' => $row['TRIGGER_NAME'], 'name' => $row['TRIGGER_NAME'],
'table' => $row['TABLE_NAME'], 'table' => $row['TABLE_NAME'],
'type' => trim($row['TRIGGER_TYPE']), 'type' => trim($row['TRIGGER_TYPE']),
@@ -661,8 +661,8 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
'enabled' => trim($row['TRIGGER_ENABLED']) === 'TRUE', 'enabled' => trim($row['TRIGGER_ENABLED']) === 'TRUE',
); );
} }
$this->free(); $res->free();
return $res; return $triggers;
} }
@@ -680,13 +680,13 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
WHERE RDB\$SYSTEM_FLAG = 0"; WHERE RDB\$SYSTEM_FLAG = 0";
$q .= $table === NULL ? ";" : " AND RDB\$RELATION_NAME = UPPER('$table')"; $q .= $table === NULL ? ";" : " AND RDB\$RELATION_NAME = UPPER('$table')";
$this->query($q); $res = $this->query($q);
$res = array(); $triggers = array();
while ($row = $this->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$res[] = $row[0]; $triggers[] = $row[0];
} }
$this->free(); $res->free();
return $res; return $triggers;
} }
@@ -698,7 +698,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getProceduresMeta() public function getProceduresMeta()
{ {
$this->query(" $res = $this->query("
SELECT SELECT
TRIM(p.RDB\$PARAMETER_NAME) AS PARAMETER_NAME, TRIM(p.RDB\$PARAMETER_NAME) AS PARAMETER_NAME,
TRIM(p.RDB\$PROCEDURE_NAME) AS PROCEDURE_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 LEFT JOIN RDB\$FIELDS f ON f.RDB\$FIELD_NAME = p.RDB\$FIELD_SOURCE
ORDER BY p.RDB\$PARAMETER_TYPE, p.RDB\$PARAMETER_NUMBER;" ORDER BY p.RDB\$PARAMETER_TYPE, p.RDB\$PARAMETER_NUMBER;"
); );
$res = array(); $procedures = array();
while ($row = $this->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key = $row['PROCEDURE_NAME']; $key = $row['PROCEDURE_NAME'];
$io = trim($row['PARAMETER_TYPE']); $io = trim($row['PARAMETER_TYPE']);
$num = $row['PARAMETER_NUMBER']; $num = $row['PARAMETER_NUMBER'];
$res[$key]['name'] = $row['PROCEDURE_NAME']; $procedures[$key]['name'] = $row['PROCEDURE_NAME'];
$res[$key]['params'][$io][$num]['name'] = $row['PARAMETER_NAME']; $procedures[$key]['params'][$io][$num]['name'] = $row['PARAMETER_NAME'];
$res[$key]['params'][$io][$num]['type'] = trim($row['FIELD_TYPE']); $procedures[$key]['params'][$io][$num]['type'] = trim($row['FIELD_TYPE']);
$res[$key]['params'][$io][$num]['size'] = $row['FIELD_LENGTH']; $procedures[$key]['params'][$io][$num]['size'] = $row['FIELD_LENGTH'];
} }
$this->free(); $res->free();
return $res; return $procedures;
} }
@@ -752,16 +752,16 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getProcedures() public function getProcedures()
{ {
$this->query(" $res = $this->query("
SELECT TRIM(RDB\$PROCEDURE_NAME) SELECT TRIM(RDB\$PROCEDURE_NAME)
FROM RDB\$PROCEDURES;" FROM RDB\$PROCEDURES;"
); );
$res = array(); $procedures = array();
while ($row = $this->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$res[] = $row[0]; $procedures[] = $row[0];
} }
$this->free(); $res->free();
return $res; return $procedures;
} }
@@ -772,17 +772,17 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getGenerators() public function getGenerators()
{ {
$this->query(" $res = $this->query("
SELECT TRIM(RDB\$GENERATOR_NAME) SELECT TRIM(RDB\$GENERATOR_NAME)
FROM RDB\$GENERATORS FROM RDB\$GENERATORS
WHERE RDB\$SYSTEM_FLAG = 0;" WHERE RDB\$SYSTEM_FLAG = 0;"
); );
$res = array(); $generators = array();
while ($row = $this->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$res[] = $row[0]; $generators[] = $row[0];
} }
$this->free(); $res->free();
return $res; return $generators;
} }
@@ -793,17 +793,17 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getFunctions() public function getFunctions()
{ {
$this->query(" $res = $this->query("
SELECT TRIM(RDB\$FUNCTION_NAME) SELECT TRIM(RDB\$FUNCTION_NAME)
FROM RDB\$FUNCTIONS FROM RDB\$FUNCTIONS
WHERE RDB\$SYSTEM_FLAG = 0;" WHERE RDB\$SYSTEM_FLAG = 0;"
); );
$res = array(); $functions = array();
while ($row = $this->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$res[] = $row[0]; $functions[] = $row[0];
} }
$this->free(); $res->free();
return $res; return $functions;
} }
} }

View File

@@ -340,17 +340,17 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
public function getResultColumns() public function getResultColumns()
{ {
$count = mssql_num_fields($this->resultSet); $count = mssql_num_fields($this->resultSet);
$res = array(); $columns = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = (array) mssql_fetch_field($this->resultSet, $i); $row = (array) mssql_fetch_field($this->resultSet, $i);
$res[] = array( $columns[] = array(
'name' => $row['name'], 'name' => $row['name'],
'fullname' => $row['column_source'] ? $row['column_source'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['column_source'] ? $row['column_source'] . '.' . $row['name'] : $row['name'],
'table' => $row['column_source'], 'table' => $row['column_source'],
'nativetype' => $row['type'], 'nativetype' => $row['type'],
); );
} }
return $res; return $columns;
} }

View File

@@ -342,16 +342,16 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
public function getResultColumns() public function getResultColumns()
{ {
$count = sqlsrv_num_fields($this->resultSet); $count = sqlsrv_num_fields($this->resultSet);
$res = array(); $columns = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = (array) sqlsrv_field_metadata($this->resultSet, $i); $row = (array) sqlsrv_field_metadata($this->resultSet, $i);
$res[] = array( $columns[] = array(
'name' => $row['Name'], 'name' => $row['Name'],
'fullname' => $row['Name'], 'fullname' => $row['Name'],
'nativetype' => $row['Type'], 'nativetype' => $row['Type'],
); );
} }
return $res; return $columns;
} }

View File

@@ -426,10 +426,10 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
public function getResultColumns() public function getResultColumns()
{ {
$count = mysql_num_fields($this->resultSet); $count = mysql_num_fields($this->resultSet);
$res = array(); $columns = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = (array) mysql_fetch_field($this->resultSet, $i); $row = (array) mysql_fetch_field($this->resultSet, $i);
$res[] = array( $columns[] = array(
'name' => $row['name'], 'name' => $row['name'],
'table' => $row['table'], 'table' => $row['table'],
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
@@ -437,7 +437,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
'vendor' => $row, 'vendor' => $row,
); );
} }
return $res; return $columns;
} }

View File

@@ -43,16 +43,16 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
FROM INFORMATION_SCHEMA.TABLES FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE() WHERE TABLE_SCHEMA = DATABASE()
");*/ ");*/
$this->driver->query("SHOW FULL TABLES"); $res = $this->driver->query("SHOW FULL TABLES");
$res = array(); $tables = array();
while ($row = $this->driver->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$res[] = array( $tables[] = array(
'name' => $row[0], 'name' => $row[0],
'view' => isset($row[1]) && $row[1] === 'VIEW', 'view' => isset($row[1]) && $row[1] === 'VIEW',
); );
} }
$this->driver->free(); $res->free();
return $res; return $tables;
} }
@@ -70,11 +70,11 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
FROM INFORMATION_SCHEMA.COLUMNS FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE() WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
");*/ ");*/
$this->driver->query("SHOW FULL COLUMNS FROM `$table`"); $res = $this->driver->query("SHOW FULL COLUMNS FROM `$table`");
$res = array(); $columns = array();
while ($row = $this->driver->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$type = explode('(', $row['Type']); $type = explode('(', $row['Type']);
$res[] = array( $columns[] = array(
'name' => $row['Field'], 'name' => $row['Field'],
'table' => $table, 'table' => $table,
'nativetype' => strtoupper($type[0]), 'nativetype' => strtoupper($type[0]),
@@ -86,8 +86,8 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
'vendor' => $row, 'vendor' => $row,
); );
} }
$this->driver->free(); $res->free();
return $res; return $columns;
} }
@@ -106,16 +106,16 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE() WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
AND REFERENCED_COLUMN_NAME IS NULL AND REFERENCED_COLUMN_NAME IS NULL
");*/ ");*/
$this->driver->query("SHOW INDEX FROM `$table`"); $res = $this->driver->query("SHOW INDEX FROM `$table`");
$res = array(); $indexes = array();
while ($row = $this->driver->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$res[$row['Key_name']]['name'] = $row['Key_name']; $indexes[$row['Key_name']]['name'] = $row['Key_name'];
$res[$row['Key_name']]['unique'] = !$row['Non_unique']; $indexes[$row['Key_name']]['unique'] = !$row['Non_unique'];
$res[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY'; $indexes[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
$res[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name']; $indexes[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
} }
$this->driver->free(); $res->free();
return array_values($res); return array_values($indexes);
} }

View File

@@ -432,10 +432,10 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
} }
$count = mysqli_num_fields($this->resultSet); $count = mysqli_num_fields($this->resultSet);
$res = array(); $columns = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = (array) mysqli_fetch_field_direct($this->resultSet, $i); $row = (array) mysqli_fetch_field_direct($this->resultSet, $i);
$res[] = array( $columns[] = array(
'name' => $row['name'], 'name' => $row['name'],
'table' => $row['orgtable'], 'table' => $row['orgtable'],
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
@@ -443,7 +443,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
'vendor' => $row, 'vendor' => $row,
); );
} }
return $res; return $columns;
} }

View File

@@ -367,16 +367,16 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
public function getResultColumns() public function getResultColumns()
{ {
$count = odbc_num_fields($this->resultSet); $count = odbc_num_fields($this->resultSet);
$res = array(); $columns = array();
for ($i = 1; $i <= $count; $i++) { for ($i = 1; $i <= $count; $i++) {
$res[] = array( $columns[] = array(
'name' => odbc_field_name($this->resultSet, $i), 'name' => odbc_field_name($this->resultSet, $i),
'table' => NULL, 'table' => NULL,
'fullname' => odbc_field_name($this->resultSet, $i), 'fullname' => odbc_field_name($this->resultSet, $i),
'nativetype'=> odbc_field_type($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() public function getTables()
{ {
$result = odbc_tables($this->connection); $res = odbc_tables($this->connection);
$res = array(); $tables = array();
while ($row = odbc_fetch_array($result)) { while ($row = odbc_fetch_array($res)) {
if ($row['TABLE_TYPE'] === 'TABLE' || $row['TABLE_TYPE'] === 'VIEW') { if ($row['TABLE_TYPE'] === 'TABLE' || $row['TABLE_TYPE'] === 'VIEW') {
$res[] = array( $tables[] = array(
'name' => $row['TABLE_NAME'], 'name' => $row['TABLE_NAME'],
'view' => $row['TABLE_TYPE'] === 'VIEW', 'view' => $row['TABLE_TYPE'] === 'VIEW',
); );
} }
} }
odbc_free_result($result); odbc_free_result($res);
return $res; return $tables;
} }
@@ -425,11 +425,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
*/ */
public function getColumns($table) public function getColumns($table)
{ {
$result = odbc_columns($this->connection); $res = odbc_columns($this->connection);
$res = array(); $columns = array();
while ($row = odbc_fetch_array($result)) { while ($row = odbc_fetch_array($res)) {
if ($row['TABLE_NAME'] === $table) { if ($row['TABLE_NAME'] === $table) {
$res[] = array( $columns[] = array(
'name' => $row['COLUMN_NAME'], 'name' => $row['COLUMN_NAME'],
'table' => $table, 'table' => $table,
'nativetype' => $row['TYPE_NAME'], 'nativetype' => $row['TYPE_NAME'],
@@ -439,8 +439,8 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
); );
} }
} }
odbc_free_result($result); odbc_free_result($res);
return $res; return $columns;
} }

View File

@@ -133,8 +133,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
$this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL"); $row = $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL")->fetch(TRUE);
$row = $this->fetch(TRUE);
return isset($row['ID']) ? (int) $row['ID'] : FALSE; return isset($row['ID']) ? (int) $row['ID'] : FALSE;
} }
@@ -354,16 +353,16 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
public function getResultColumns() public function getResultColumns()
{ {
$count = oci_num_fields($this->resultSet); $count = oci_num_fields($this->resultSet);
$res = array(); $columns = array();
for ($i = 1; $i <= $count; $i++) { for ($i = 1; $i <= $count; $i++) {
$res[] = array( $columns[] = array(
'name' => oci_field_name($this->resultSet, $i), 'name' => oci_field_name($this->resultSet, $i),
'table' => NULL, 'table' => NULL,
'fullname' => oci_field_name($this->resultSet, $i), 'fullname' => oci_field_name($this->resultSet, $i),
'nativetype'=> oci_field_type($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() public function getTables()
{ {
$this->query('SELECT * FROM cat'); $res = $this->query('SELECT * FROM cat');
$res = array(); $tables = array();
while ($row = $this->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
if ($row[1] === 'TABLE' || $row[1] === 'VIEW') { if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
$res[] = array( $tables[] = array(
'name' => $row[0], 'name' => $row[0],
'view' => $row[1] === 'VIEW', 'view' => $row[1] === 'VIEW',
); );
} }
} }
$this->free(); $res->free();
return $res; return $tables;
} }

View File

@@ -419,7 +419,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
public function getResultColumns() public function getResultColumns()
{ {
$count = $this->resultSet->columnCount(); $count = $this->resultSet->columnCount();
$res = array(); $columns = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = @$this->resultSet->getColumnMeta($i); // intentionally @ $row = @$this->resultSet->getColumnMeta($i); // intentionally @
if ($row === FALSE) { 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 // @see: http://php.net/manual/en/pdostatement.getcolumnmeta.php#pdostatement.getcolumnmeta.changelog
$row['table'] = isset($row['table']) ? $row['table'] : NULL; $row['table'] = isset($row['table']) ? $row['table'] : NULL;
$res[] = array( $columns[] = array(
'name' => $row['name'], 'name' => $row['name'],
'table' => $row['table'], 'table' => $row['table'],
'nativetype' => $row['native_type'], 'nativetype' => $row['native_type'],
@@ -437,7 +437,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
'vendor' => $row, 'vendor' => $row,
); );
} }
return $res; return $columns;
} }

View File

@@ -155,15 +155,15 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
{ {
if ($sequence === NULL) { if ($sequence === NULL) {
// PostgreSQL 8.1 is needed // PostgreSQL 8.1 is needed
$has = $this->query("SELECT LASTVAL()"); $res = $this->query("SELECT LASTVAL()");
} else { } 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); $row = $res->fetch(FALSE);
$this->free(); $res->free();
return is_array($row) ? $row[0] : FALSE; 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', '>='); $hasTable = version_compare(PHP_VERSION , '5.2.0', '>=');
$count = pg_num_fields($this->resultSet); $count = pg_num_fields($this->resultSet);
$res = array(); $columns = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = array( $row = array(
'name' => pg_field_name($this->resultSet, $i), '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), 'nativetype'=> pg_field_type($this->resultSet, $i),
); );
$row['fullname'] = $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name']; $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.'); 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 SELECT table_name as name, CAST(table_type = 'VIEW' AS INTEGER) as view
FROM information_schema.tables FROM information_schema.tables
WHERE table_schema = current_schema() WHERE table_schema = current_schema()
"); ");
$res = pg_fetch_all($this->resultSet); $tables = pg_fetch_all($res->resultSet);
$this->free(); $res->free();
return $res ? $res : array(); return $tables ? $tables : array();
} }
@@ -457,24 +457,24 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
public function getColumns($table) public function getColumns($table)
{ {
$_table = $this->escape($table, dibi::TEXT); $_table = $this->escape($table, dibi::TEXT);
$this->query(" $res = $this->query("
SELECT indkey SELECT indkey
FROM pg_class FROM pg_class
LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
WHERE pg_class.relname = $_table 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 * SELECT *
FROM information_schema.columns FROM information_schema.columns
WHERE table_name = $_table AND table_schema = current_schema() WHERE table_name = $_table AND table_schema = current_schema()
ORDER BY ordinal_position ORDER BY ordinal_position
"); ");
$res = array(); $columns = array();
while ($row = $this->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$size = (int) max($row['character_maximum_length'], $row['numeric_precision']); $size = (int) max($row['character_maximum_length'], $row['numeric_precision']);
$res[] = array( $columns[] = array(
'name' => $row['column_name'], 'name' => $row['column_name'],
'table' => $table, 'table' => $table,
'nativetype' => strtoupper($row['udt_name']), 'nativetype' => strtoupper($row['udt_name']),
@@ -485,8 +485,8 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
'vendor' => $row, 'vendor' => $row,
); );
} }
$this->free(); $res->free();
return $res; return $columns;
} }
@@ -499,7 +499,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
public function getIndexes($table) public function getIndexes($table)
{ {
$_table = $this->escape($table, dibi::TEXT); $_table = $this->escape($table, dibi::TEXT);
$this->query(" $res = $this->query("
SELECT ordinal_position, column_name SELECT ordinal_position, column_name
FROM information_schema.columns FROM information_schema.columns
WHERE table_name = $_table AND table_schema = current_schema() WHERE table_name = $_table AND table_schema = current_schema()
@@ -507,11 +507,11 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
"); ");
$columns = array(); $columns = array();
while ($row = $this->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$columns[$row['ordinal_position']] = $row['column_name']; $columns[$row['ordinal_position']] = $row['column_name'];
} }
$this->query(" $res = $this->query("
SELECT pg_class2.relname, indisunique, indisprimary, indkey SELECT pg_class2.relname, indisunique, indisprimary, indkey
FROM pg_class FROM pg_class
LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid 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 WHERE pg_class.relname = $_table
"); ");
$res = array(); $indexes = array();
while ($row = $this->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$res[$row['relname']]['name'] = $row['relname']; $indexes[$row['relname']]['name'] = $row['relname'];
$res[$row['relname']]['unique'] = $row['indisunique'] === 't'; $indexes[$row['relname']]['unique'] = $row['indisunique'] === 't';
$res[$row['relname']]['primary'] = $row['indisprimary'] === 't'; $indexes[$row['relname']]['primary'] = $row['indisprimary'] === 't';
foreach (explode(' ', $row['indkey']) as $index) { foreach (explode(' ', $row['indkey']) as $index) {
$res[$row['relname']]['columns'][] = $columns[$index]; $indexes[$row['relname']]['columns'][] = $columns[$index];
} }
} }
$this->free(); $res->free();
return array_values($res); return array_values($indexes);
} }

View File

@@ -376,18 +376,18 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
public function getResultColumns() public function getResultColumns()
{ {
$count = sqlite_num_fields($this->resultSet); $count = sqlite_num_fields($this->resultSet);
$res = array(); $columns = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$name = str_replace(array('[', ']'), '', sqlite_field_name($this->resultSet, $i)); $name = str_replace(array('[', ']'), '', sqlite_field_name($this->resultSet, $i));
$pair = explode('.', $name); $pair = explode('.', $name);
$res[] = array( $columns[] = array(
'name' => isset($pair[1]) ? $pair[1] : $pair[0], 'name' => isset($pair[1]) ? $pair[1] : $pair[0],
'table' => isset($pair[1]) ? $pair[0] : NULL, 'table' => isset($pair[1]) ? $pair[0] : NULL,
'fullname' => $name, 'fullname' => $name,
'nativetype' => NULL, 'nativetype' => NULL,
); );
} }
return $res; return $columns;
} }

View File

@@ -38,18 +38,18 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
*/ */
public function getTables() public function getTables()
{ {
$this->driver->query(" $res = $this->driver->query("
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view') SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
UNION ALL UNION ALL
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view') SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
ORDER BY name ORDER BY name
"); ");
$res = array(); $tables = array();
while ($row = $this->driver->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$res[] = $row; $tables[] = $row;
} }
$this->driver->free(); $res->free();
return $res; return $tables;
} }
@@ -61,22 +61,19 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
*/ */
public function getColumns($table) public function getColumns($table)
{ {
$this->driver->query(" $meta = $this->driver->query("
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '$table' SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '$table'
UNION ALL UNION ALL
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = '$table'" SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = '$table'"
); )->fetch(TRUE);
$meta = $this->driver->fetch(TRUE);
$this->driver->free();
$this->driver->query("PRAGMA table_info([$table])"); $res = $this->driver->query("PRAGMA table_info([$table])");
$res = array(); $columns = array();
while ($row = $this->driver->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$column = $row['name']; $column = $row['name'];
$pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui"; $pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui";
$type = explode('(', $row['type']); $type = explode('(', $row['type']);
$columns[] = array(
$res[] = array(
'name' => $column, 'name' => $column,
'table' => $table, 'table' => $table,
'fullname' => "$table.$column", 'fullname' => "$table.$column",
@@ -88,8 +85,8 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
'vendor' => $row, 'vendor' => $row,
); );
} }
$this->driver->free(); $res->free();
return $res; return $columns;
} }
@@ -101,25 +98,25 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
*/ */
public function getIndexes($table) public function getIndexes($table)
{ {
$this->driver->query("PRAGMA index_list([$table])"); $res = $this->driver->query("PRAGMA index_list([$table])");
$res = array(); $indexes = array();
while ($row = $this->driver->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$res[$row['name']]['name'] = $row['name']; $indexes[$row['name']]['name'] = $row['name'];
$res[$row['name']]['unique'] = (bool) $row['unique']; $indexes[$row['name']]['unique'] = (bool) $row['unique'];
} }
$this->driver->free(); $res->free();
foreach ($res as $index => $values) { foreach ($indexes as $index => $values) {
$this->driver->query("PRAGMA index_info([$index])"); $res = $this->driver->query("PRAGMA index_info([$index])");
while ($row = $this->driver->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$res[$index]['columns'][$row['seqno']] = $row['name']; $indexes[$index]['columns'][$row['seqno']] = $row['name'];
} }
$res->free();
} }
$this->driver->free();
$columns = $this->getColumns($table); $columns = $this->getColumns($table);
foreach ($res as $index => $values) { foreach ($indexes as $index => $values) {
$column = $res[$index]['columns'][0]; $column = $indexes[$index]['columns'][0];
$primary = FALSE; $primary = FALSE;
foreach ($columns as $info) { foreach ($columns as $info) {
if ($column == $info['name']) { if ($column == $info['name']) {
@@ -127,12 +124,12 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
break; 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) { foreach ($columns as $column) {
if ($column['vendor']['pk']) { if ($column['vendor']['pk']) {
$res[] = array( $indexes[] = array(
'name' => 'ROWID', 'name' => 'ROWID',
'unique' => TRUE, 'unique' => TRUE,
'primary' => 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)) { if (!($this->driver instanceof DibiSqlite3Driver)) {
// throw new NotSupportedException; // @see http://www.sqlite.org/foreignkeys.html // throw new NotSupportedException; // @see http://www.sqlite.org/foreignkeys.html
} }
$this->driver->query("PRAGMA foreign_key_list([$table])"); $res = $this->driver->query("PRAGMA foreign_key_list([$table])");
$res = array(); $keys = array();
while ($row = $this->driver->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$res[$row['id']]['name'] = $row['id']; // foreign key name $keys[$row['id']]['name'] = $row['id']; // foreign key name
$res[$row['id']]['local'][$row['seq']] = $row['from']; // local columns $keys[$row['id']]['local'][$row['seq']] = $row['from']; // local columns
$res[$row['id']]['table'] = $row['table']; // referenced table $keys[$row['id']]['table'] = $row['table']; // referenced table
$res[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns $keys[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns
$res[$row['id']]['onDelete'] = $row['on_delete']; $keys[$row['id']]['onDelete'] = $row['on_delete'];
$res[$row['id']]['onUpdate'] = $row['on_update']; $keys[$row['id']]['onUpdate'] = $row['on_update'];
if ($res[$row['id']]['foreign'][0] == NULL) { if ($keys[$row['id']]['foreign'][0] == NULL) {
$res[$row['id']]['foreign'] = NULL; $keys[$row['id']]['foreign'] = NULL;
} }
} }
$this->driver->free(); $res->free();
return array_values($res); return array_values($keys);
} }
} }

View File

@@ -364,17 +364,17 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
public function getResultColumns() public function getResultColumns()
{ {
$count = $this->resultSet->numColumns(); $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'); static $types = array(SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null');
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$res[] = array( $columns[] = array(
'name' => $this->resultSet->columnName($i), 'name' => $this->resultSet->columnName($i),
'table' => NULL, 'table' => NULL,
'fullname' => $this->resultSet->columnName($i), 'fullname' => $this->resultSet->columnName($i),
'nativetype' => $types[$this->resultSet->columnType($i)], 'nativetype' => $types[$this->resultSet->columnType($i)],
); );
} }
return $res; return $columns;
} }