aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB
diff options
context:
space:
mode:
authorNils Adermann <naderman@naderman.de>2011-10-14 17:48:15 +0200
committerNils Adermann <naderman@naderman.de>2011-10-14 17:48:15 +0200
commit8a82ec95a56f70b7b5ea10cfe9e98574e5230195 (patch)
treecf302fef439e6816b09e532e47cd75bf5d51e8d5 /phpBB
parent207a73537b2bb7f4608509f3ec79aaa313608ccb (diff)
parent4effe8fb8bdf493e539b7bf883fbf1e290bf7ab2 (diff)
downloadforums-8a82ec95a56f70b7b5ea10cfe9e98574e5230195.tar
forums-8a82ec95a56f70b7b5ea10cfe9e98574e5230195.tar.gz
forums-8a82ec95a56f70b7b5ea10cfe9e98574e5230195.tar.bz2
forums-8a82ec95a56f70b7b5ea10cfe9e98574e5230195.tar.xz
forums-8a82ec95a56f70b7b5ea10cfe9e98574e5230195.zip
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.
Diffstat (limited to 'phpBB')
-rw-r--r--phpBB/includes/db/db_tools.php171
-rw-r--r--phpBB/includes/functions_install.php57
2 files changed, 105 insertions, 123 deletions
diff --git a/phpBB/includes/db/db_tools.php b/phpBB/includes/db/db_tools.php
index 1d740121e4..dafa5d0d1e 100644
--- a/phpBB/includes/db/db_tools.php
+++ b/phpBB/includes/db/db_tools.php
@@ -348,6 +348,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
*
*
@@ -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 $column_name The column to check
+ * @param string $table Table name
*
- * @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)
{
case 'mysql_40':
case 'mysql_41':
-
$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;
// 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}'
AND a.attnum > 0
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;
// same deal with PostgreSQL, we must perform more complex operations than
@@ -1072,62 +1106,26 @@ class phpbb_db_tools
FROM syscolumns c
LEFT JOIN sysobjects o ON c.id = o.id
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;
case 'oracle':
$sql = "SELECT column_name
FROM user_tab_columns
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;
case 'firebird':
$sql = "SELECT RDB\$FIELD_NAME as FNAME
FROM RDB\$RELATION_FIELDS
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;
- // ugh, SQLite
case 'sqlite':
$sql = "SELECT sql
FROM sqlite_master
WHERE type = 'table'
AND name = '{$table}'";
+
$result = $this->db->sql_query($sql);
if (!$result)
@@ -1151,14 +1149,39 @@ class phpbb_db_tools
continue;
}
- if (strtolower($entities[0]) == $column_name)
- {
- return true;
- }
+ $column = strtolower($entities[0]);
+ $columns[$column] = $column;
}
- return false;
+
+ return $columns;
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]);
}
/**
diff --git a/phpBB/includes/functions_install.php b/phpBB/includes/functions_install.php
index 7742bb9263..7a99bca0e2 100644
--- a/phpBB/includes/functions_install.php
+++ b/phpBB/includes/functions_install.php
@@ -211,61 +211,20 @@ function dbms_select($default = '', $only_20x_options = false)
/**
* Get tables of a database
+*
+* @deprecated
*/
-function get_tables($db)
+function get_tables(&$db)
{
- switch ($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 = $db->sql_query($sql);
-
- $tables = array();
-
- while ($row = $db->sql_fetchrow($result))
+ if (!class_exists('phpbb_db_tools'))
{
- $tables[] = current($row);
+ global $phpbb_root_path, $phpEx;
+ require($phpbb_root_path . 'includes/db/db_tools.' . $phpEx);
}
- $db->sql_freeresult($result);
+ $db_tools = new phpbb_db_tools($db);
- return $tables;
+ return $db_tools->sql_list_tables();
}
/**