1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-06 15:45:34 +02:00

Merge branch 'develop-olympus' into develop

* develop-olympus:
  [ticket/8240] Add ability to get a list of columns of a tables to db_tools.
  [ticket/8240] Add ability to get a list of tables to db_tools.
This commit is contained in:
Nils Adermann 2011-10-14 17:48:15 +02:00
commit 8a82ec95a5
3 changed files with 120 additions and 123 deletions

View File

@ -347,6 +347,66 @@ class phpbb_db_tools
} }
} }
/**
* Gets a list of tables in the database.
*
* @return array Array of table names (all lower case)
*/
function sql_list_tables()
{
switch ($this->db->sql_layer)
{
case 'mysql':
case 'mysql4':
case 'mysqli':
$sql = 'SHOW TABLES';
break;
case 'sqlite':
$sql = 'SELECT name
FROM sqlite_master
WHERE type = "table"';
break;
case 'mssql':
case 'mssql_odbc':
case 'mssqlnative':
$sql = "SELECT name
FROM sysobjects
WHERE type='U'";
break;
case 'postgres':
$sql = 'SELECT relname
FROM pg_stat_user_tables';
break;
case 'firebird':
$sql = 'SELECT rdb$relation_name
FROM rdb$relations
WHERE rdb$view_source is null
AND rdb$system_flag = 0';
break;
case 'oracle':
$sql = 'SELECT table_name
FROM USER_TABLES';
break;
}
$result = $this->db->sql_query($sql);
$tables = array();
while ($row = $this->db->sql_fetchrow($result))
{
$name = current($row);
$tables[$name] = $name;
}
$this->db->sql_freeresult($result);
return $tables;
}
/** /**
* Check if table exists * Check if table exists
* *
@ -1011,34 +1071,21 @@ class phpbb_db_tools
} }
/** /**
* Check if a specified column exist * Gets a list of columns of a table.
* *
* @param string $table Table to check the column at * @param string $table Table name
* @param string $column_name The column to check
* *
* @return bool True if column exists, else false * @return array Array of column names (all lower case)
*/ */
function sql_column_exists($table, $column_name) function sql_list_columns($table)
{ {
$columns = array();
switch ($this->sql_layer) switch ($this->sql_layer)
{ {
case 'mysql_40': case 'mysql_40':
case 'mysql_41': case 'mysql_41':
$sql = "SHOW COLUMNS FROM $table"; $sql = "SHOW COLUMNS FROM $table";
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// lower case just in case
if (strtolower($row['Field']) == $column_name)
{
$this->db->sql_freeresult($result);
return true;
}
}
$this->db->sql_freeresult($result);
return false;
break; break;
// PostgreSQL has a way of doing this in a much simpler way but would // PostgreSQL has a way of doing this in a much simpler way but would
@ -1049,19 +1096,6 @@ class phpbb_db_tools
WHERE c.relname = '{$table}' WHERE c.relname = '{$table}'
AND a.attnum > 0 AND a.attnum > 0
AND a.attrelid = c.oid"; AND a.attrelid = c.oid";
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// lower case just in case
if (strtolower($row['attname']) == $column_name)
{
$this->db->sql_freeresult($result);
return true;
}
}
$this->db->sql_freeresult($result);
return false;
break; break;
// same deal with PostgreSQL, we must perform more complex operations than // same deal with PostgreSQL, we must perform more complex operations than
@ -1072,62 +1106,26 @@ class phpbb_db_tools
FROM syscolumns c FROM syscolumns c
LEFT JOIN sysobjects o ON c.id = o.id LEFT JOIN sysobjects o ON c.id = o.id
WHERE o.name = '{$table}'"; WHERE o.name = '{$table}'";
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// lower case just in case
if (strtolower($row['name']) == $column_name)
{
$this->db->sql_freeresult($result);
return true;
}
}
$this->db->sql_freeresult($result);
return false;
break; break;
case 'oracle': case 'oracle':
$sql = "SELECT column_name $sql = "SELECT column_name
FROM user_tab_columns FROM user_tab_columns
WHERE LOWER(table_name) = '" . strtolower($table) . "'"; WHERE LOWER(table_name) = '" . strtolower($table) . "'";
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// lower case just in case
if (strtolower($row['column_name']) == $column_name)
{
$this->db->sql_freeresult($result);
return true;
}
}
$this->db->sql_freeresult($result);
return false;
break; break;
case 'firebird': case 'firebird':
$sql = "SELECT RDB\$FIELD_NAME as FNAME $sql = "SELECT RDB\$FIELD_NAME as FNAME
FROM RDB\$RELATION_FIELDS FROM RDB\$RELATION_FIELDS
WHERE RDB\$RELATION_NAME = '" . strtoupper($table) . "'"; WHERE RDB\$RELATION_NAME = '" . strtoupper($table) . "'";
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// lower case just in case
if (strtolower($row['fname']) == $column_name)
{
$this->db->sql_freeresult($result);
return true;
}
}
$this->db->sql_freeresult($result);
return false;
break; break;
// ugh, SQLite
case 'sqlite': case 'sqlite':
$sql = "SELECT sql $sql = "SELECT sql
FROM sqlite_master FROM sqlite_master
WHERE type = 'table' WHERE type = 'table'
AND name = '{$table}'"; AND name = '{$table}'";
$result = $this->db->sql_query($sql); $result = $this->db->sql_query($sql);
if (!$result) if (!$result)
@ -1151,14 +1149,39 @@ class phpbb_db_tools
continue; continue;
} }
if (strtolower($entities[0]) == $column_name) $column = strtolower($entities[0]);
{ $columns[$column] = $column;
return true;
}
} }
return false;
return $columns;
break; break;
} }
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$column = strtolower(current($row));
$columns[$column] = $column;
}
$this->db->sql_freeresult($result);
return $columns;
}
/**
* Check whether a specified column exist in a table
*
* @param string $table Table to check
* @param string $column_name Column to check
*
* @return bool True if column exists, false otherwise
*/
function sql_column_exists($table, $column_name)
{
$columns = $this->sql_list_columns($table);
return isset($columns[$column_name]);
} }
/** /**

View File

@ -211,61 +211,20 @@ function dbms_select($default = '', $only_20x_options = false)
/** /**
* Get tables of a database * Get tables of a database
*
* @deprecated
*/ */
function get_tables($db) function get_tables(&$db)
{ {
switch ($db->sql_layer) if (!class_exists('phpbb_db_tools'))
{ {
case 'mysql': global $phpbb_root_path, $phpEx;
case 'mysql4': require($phpbb_root_path . 'includes/db/db_tools.' . $phpEx);
case 'mysqli':
$sql = 'SHOW TABLES';
break;
case 'sqlite':
$sql = 'SELECT name
FROM sqlite_master
WHERE type = "table"';
break;
case 'mssql':
case 'mssql_odbc':
case 'mssqlnative':
$sql = "SELECT name
FROM sysobjects
WHERE type='U'";
break;
case 'postgres':
$sql = 'SELECT relname
FROM pg_stat_user_tables';
break;
case 'firebird':
$sql = 'SELECT rdb$relation_name
FROM rdb$relations
WHERE rdb$view_source is null
AND rdb$system_flag = 0';
break;
case 'oracle':
$sql = 'SELECT table_name
FROM USER_TABLES';
break;
} }
$result = $db->sql_query($sql); $db_tools = new phpbb_db_tools($db);
$tables = array(); return $db_tools->sql_list_tables();
while ($row = $db->sql_fetchrow($result))
{
$tables[] = current($row);
}
$db->sql_freeresult($result);
return $tables;
} }
/** /**

View File

@ -234,6 +234,14 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case
$this->assertEquals($row2, $row_actual); $this->assertEquals($row2, $row_actual);
} }
public function test_list_columns()
{
$this->assertEquals(
array_keys($this->table_data['COLUMNS']),
array_values($this->tools->sql_list_columns('prefix_table_name'))
);
}
public function test_column_exists() public function test_column_exists()
{ {
$this->assertTrue($this->tools->sql_column_exists('prefix_table_name', 'c_id')); $this->assertTrue($this->tools->sql_column_exists('prefix_table_name', 'c_id'));
@ -258,6 +266,13 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case
$this->assertFalse($this->tools->sql_column_exists('prefix_table_name', 'c_id')); $this->assertFalse($this->tools->sql_column_exists('prefix_table_name', 'c_id'));
} }
public function test_list_tables()
{
$tables = $this->tools->sql_list_tables();
$this->assertTrue(isset($tables['prefix_table_name']));
$this->assertFalse(isset($tables['prefix_does_not_exist']));
}
public function test_table_exists() public function test_table_exists()
{ {
$this->assertTrue($this->tools->sql_table_exists('prefix_table_name')); $this->assertTrue($this->tools->sql_table_exists('prefix_table_name'));