1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-22 21:54:05 +02:00

improved reflection skills

This commit is contained in:
David Grudl
2008-10-28 01:03:50 +00:00
parent e221a13dda
commit ab892255d3
15 changed files with 242 additions and 140 deletions

View File

@@ -339,15 +339,18 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver
public function getColumnsMeta()
{
$count = mssql_num_fields($this->resultSet);
$meta = array();
$res = array();
for ($i = 0; $i < $count; $i++) {
// 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;
$row = (array) mssql_fetch_field($this->resultSet, $i);
$res[] = array(
'name' => $row['name'],
'fullname' => $row['column_source'] ? $row['column_source'] . '.' . $row['name'] : $row['name'],
'table' => $row['column_source'],
'type' => NULL,
'nativetype' => $row['type'],
) + $row;
}
return $meta;
return $res;
}

View File

@@ -405,13 +405,18 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
public function getColumnsMeta()
{
$count = mysql_num_fields($this->resultSet);
$meta = array();
$res = array();
for ($i = 0; $i < $count; $i++) {
$info = (array) mysql_fetch_field($this->resultSet, $i);
$info['nativetype'] = $info['type'];
$meta[] = $info;
$row = (array) mysql_fetch_field($this->resultSet, $i);
$res[] = array(
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
'type' => NULL,
'nativetype' => strtoupper($row['type']),
'nullable' => !($row['not_null']),
'default' => $row['def'],
) + $row;
}
return $meta;
return $res;
}
@@ -438,10 +443,13 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
*/
public function getTables()
{
$this->query("SHOW TABLES");
$this->query("SHOW FULL TABLES");
$res = array();
while ($row = mysql_fetch_array($this->resultSet, MYSQL_NUM)) {
$res[] = array('name' => $row[0]);
while ($row = $this->fetch(FALSE)) {
$res[] = array(
'name' => $row[0],
'view' => isset($row[1]) && $row[1] === 'VIEW',
);
}
$this->free();
return $res;
@@ -456,7 +464,22 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
*/
public function getColumns($table)
{
throw new NotImplementedException;
$this->query("SHOW COLUMNS FROM `$table`");
$res = array();
while ($row = $this->fetch(TRUE)) {
$type = explode('(', $row['Type']);
$res[] = array(
'name' => $row['Field'],
'table' => $table,
'nativetype' => strtoupper($type[0]),
'size' => isset($type[1]) ? (int) $type[1] : NULL,
'nullable' => $row['Null'] === 'YES',
'default' => $row['Default'],
'autoincrement' => $row['Extra'] === 'auto_increment',
);
}
$this->free();
return $res;
}
@@ -468,7 +491,16 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
*/
public function getIndexes($table)
{
throw new NotImplementedException;
$this->query("SHOW INDEX FROM `$table`");
$res = array();
while ($row = $this->fetch(TRUE)) {
$res[$row['Key_name']]['name'] = $row['Key_name'];
$res[$row['Key_name']]['unique'] = !$row['Non_unique'];
$res[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
$res[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
}
$this->free();
return array_values($res);
}

View File

@@ -387,14 +387,22 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
public function getColumnsMeta()
{
$count = mysqli_num_fields($this->resultSet);
$meta = array();
$res = array();
for ($i = 0; $i < $count; $i++) {
// items 'name' and 'table' are required
$info = (array) mysqli_fetch_field_direct($this->resultSet, $i);
$info['nativetype'] = $info['type'];
$meta[] = $info;
$row = (array) mysqli_fetch_field_direct($this->resultSet, $i);
$res[] = array(
'name' => $row['name'],
'column' => $row['orgname'],
'alias' => $row['table'],
'table' => $row['orgtable'],
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
'type' => NULL,
'nativetype' => $row['type'],
'nullable' => !($row['flags'] & MYSQLI_NOT_NULL_FLAG),
'default' => $row['def'],
) + $row;
}
return $meta;
return $res;
}
@@ -421,10 +429,18 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
*/
public function getTables()
{
$this->query("SHOW TABLES");
/*$this->query("
SELECT TABLE_NAME as name, TABLE_TYPE = 'VIEW' as view
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE()
");*/
$this->query("SHOW FULL TABLES");
$res = array();
while ($row = mysqli_fetch_array($this->resultSet, MYSQLI_NUM)) {
$res[] = array('name' => $row[0]);
while ($row = $this->fetch(FALSE)) {
$res[] = array(
'name' => $row[0],
'view' => isset($row[1]) && $row[1] === 'VIEW',
);
}
$this->free();
return $res;
@@ -439,7 +455,28 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
*/
public function getColumns($table)
{
throw new NotImplementedException;
/*$table = $this->escape($table, dibi::FIELD_TEXT);
$this->query("
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
");*/
$this->query("SHOW COLUMNS FROM `$table`");
$res = array();
while ($row = $this->fetch(TRUE)) {
$type = explode('(', $row['Type']);
$res[] = array(
'name' => $row['Field'],
'table' => $table,
'nativetype' => strtoupper($type[0]),
'size' => isset($type[1]) ? (int) $type[1] : NULL,
'nullable' => $row['Null'] === 'YES',
'default' => $row['Default'],
'autoincrement' => $row['Extra'] === 'auto_increment',
);
}
$this->free();
return $res;
}
@@ -451,7 +488,23 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
*/
public function getIndexes($table)
{
throw new NotImplementedException;
/*$table = $this->escape($table, dibi::FIELD_TEXT);
$this->query("
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
AND REFERENCED_COLUMN_NAME IS NULL
");*/
$this->query("SHOW INDEX FROM `$table`");
$res = array();
while ($row = $this->fetch(TRUE)) {
$res[$row['Key_name']]['name'] = $row['Key_name'];
$res[$row['Key_name']]['unique'] = !$row['Non_unique'];
$res[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
$res[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
}
$this->free();
return array_values($res);
}

View File

@@ -361,19 +361,16 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
public function getColumnsMeta()
{
$count = odbc_num_fields($this->resultSet);
$meta = array();
$res = array();
for ($i = 1; $i <= $count; $i++) {
// items 'name' and 'table' are required
$meta[] = array(
$res[] = array(
'name' => odbc_field_name($this->resultSet, $i),
'table' => NULL,
'fullname' => odbc_field_name($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),
);
}
return $meta;
return $res;
}
@@ -402,9 +399,15 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
{
$result = odbc_tables($this->connection);
$res = array();
while (odbc_fetch_row($result)) {
$res[] = array('name' => odbc_result($result, 'TABLE_NAME'));
while ($row = odbc_fetch_array($result)) {
if ($row['TABLE_TYPE'] === 'TABLE' || $row['TABLE_TYPE'] === 'VIEW') {
$res[] = array(
'name' => $row['TABLE_NAME'],
'view' => $row['TABLE_TYPE'] === 'VIEW',
);
}
}
odbc_free_result($result);
return $res;
}
@@ -417,7 +420,22 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
*/
public function getColumns($table)
{
throw new NotImplementedException;
$result = odbc_columns($this->connection);
$res = array();
while ($row = odbc_fetch_array($result)) {
if ($row['TABLE_NAME'] === $table) {
$res[] = array(
'name' => $row['COLUMN_NAME'],
'table' => $table,
'nativetype' => $row['TYPE_NAME'],
'size' => $row['COLUMN_SIZE'],
'nullable' => (bool) $row['NULLABLE'],
'default' => $row['COLUMN_DEF'],
);
}
}
odbc_free_result($result);
return $res;
}

View File

@@ -347,19 +347,16 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver
public function getColumnsMeta()
{
$count = oci_num_fields($this->resultSet);
$meta = array();
$res = array();
for ($i = 1; $i <= $count; $i++) {
// items 'name' and 'table' are required
$meta[] = array(
$res[] = array(
'name' => oci_field_name($this->resultSet, $i),
'table' => NULL,
'fullname' => oci_field_name($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),
);
}
return $meta;
return $res;
}

View File

@@ -393,17 +393,19 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver
public function getColumnsMeta()
{
$count = $this->resultSet->columnCount();
$meta = array();
$res = array();
for ($i = 0; $i < $count; $i++) {
// items 'name' and 'table' are required
$info = @$this->resultSet->getColumnsMeta($i); // intentionally @
if ($info === FALSE) {
$row = @$this->resultSet->getColumnMeta($i); // intentionally @
if ($row === FALSE) {
throw new DibiDriverException('Driver does not support meta data.');
}
$info['nativetype'] = $info['native_type'];
$meta[] = $info;
$res[] = array(
'type' => NULL,
'nativetype' => $row['native_type'],
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
) + $row;
}
return $meta;
return $res;
}

View File

@@ -396,18 +396,17 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
{
$hasTable = version_compare(PHP_VERSION , '5.2.0', '>=');
$count = pg_num_fields($this->resultSet);
$meta = array();
$res = array();
for ($i = 0; $i < $count; $i++) {
// items 'name' and 'table' are required
$meta[] = array(
$row = array(
'name' => pg_field_name($this->resultSet, $i),
'table' => $hasTable ? pg_field_table($this->resultSet, $i) : NULL,
'nativetype'=> pg_field_type($this->resultSet, $i),
'size' => pg_field_size($this->resultSet, $i),
'prtlen' => pg_field_prtlen($this->resultSet, $i),
);
$row['fullname'] = $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'];
$res[] = $row;
}
return $meta;
return $res;
}
@@ -434,7 +433,14 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
*/
public function getTables()
{
throw new NotImplementedException;
$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;
}

View File

@@ -361,18 +361,17 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
public function getColumnsMeta()
{
$count = sqlite_num_fields($this->resultSet);
$meta = array();
$res = array();
for ($i = 0; $i < $count; $i++) {
$pair = explode('.', str_replace(array('[', ']'), '', sqlite_field_name($this->resultSet, $i)));
if (!isset($pair[1])) {
array_unshift($pair, NULL);
}
$meta[] = array(
'name' => $pair[1],
'table' => $pair[0],
$name = str_replace(array('[', ']'), '', sqlite_field_name($this->resultSet, $i));
$pair = explode('.', $name);
$res[] = array(
'name' => isset($pair[1]) ? $pair[1] : $pair[0],
'table' => isset($pair[1]) ? $pair[0] : NULL,
'fullname' => $name,
);
}
return $meta;
return $res;
}
@@ -400,10 +399,14 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
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
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
");
return sqlite_fetch_all($this->resultSet, SQLITE_ASSOC);
$res = sqlite_fetch_all($this->resultSet, SQLITE_ASSOC);
$this->free();
return $res;
}