Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 234edf6

Browse filesBrowse files
committed
[ticket/8240] Add ability to get a list of tables to db_tools.
PHPBB3-8240
1 parent 1657339 commit 234edf6
Copy full SHA for 234edf6

3 files changed

+75-49Lines changed: 75 additions & 49 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎phpBB/includes/db/db_tools.php‎

Copy file name to clipboardExpand all lines: phpBB/includes/db/db_tools.php
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,66 @@ function phpbb_db_tools(&$db, $return_statements = false)
347347
}
348348
}
349349

350+
/**
351+
* Gets a list of tables in the database.
352+
*
353+
* @return array Array of table names (all lower case)
354+
*/
355+
function sql_list_tables()
356+
{
357+
switch ($this->db->sql_layer)
358+
{
359+
case 'mysql':
360+
case 'mysql4':
361+
case 'mysqli':
362+
$sql = 'SHOW TABLES';
363+
break;
364+
365+
case 'sqlite':
366+
$sql = 'SELECT name
367+
FROM sqlite_master
368+
WHERE type = "table"';
369+
break;
370+
371+
case 'mssql':
372+
case 'mssql_odbc':
373+
case 'mssqlnative':
374+
$sql = "SELECT name
375+
FROM sysobjects
376+
WHERE type='U'";
377+
break;
378+
379+
case 'postgres':
380+
$sql = 'SELECT relname
381+
FROM pg_stat_user_tables';
382+
break;
383+
384+
case 'firebird':
385+
$sql = 'SELECT rdb$relation_name
386+
FROM rdb$relations
387+
WHERE rdb$view_source is null
388+
AND rdb$system_flag = 0';
389+
break;
390+
391+
case 'oracle':
392+
$sql = 'SELECT table_name
393+
FROM USER_TABLES';
394+
break;
395+
}
396+
397+
$result = $this->db->sql_query($sql);
398+
399+
$tables = array();
400+
while ($row = $this->db->sql_fetchrow($result))
401+
{
402+
$name = current($row);
403+
$tables[$name] = $name;
404+
}
405+
$this->db->sql_freeresult($result);
406+
407+
return $tables;
408+
}
409+
350410
/**
351411
* Check if table exists
352412
*
Collapse file

‎phpBB/includes/functions_install.php‎

Copy file name to clipboardExpand all lines: phpBB/includes/functions_install.php
+8-49Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -211,61 +211,20 @@ function dbms_select($default = '', $only_20x_options = false)
211211

212212
/**
213213
* Get tables of a database
214+
*
215+
* @deprecated
214216
*/
215-
function get_tables($db)
217+
function get_tables(&$db)
216218
{
217-
switch ($db->sql_layer)
218-
{
219-
case 'mysql':
220-
case 'mysql4':
221-
case 'mysqli':
222-
$sql = 'SHOW TABLES';
223-
break;
224-
225-
case 'sqlite':
226-
$sql = 'SELECT name
227-
FROM sqlite_master
228-
WHERE type = "table"';
229-
break;
230-
231-
case 'mssql':
232-
case 'mssql_odbc':
233-
case 'mssqlnative':
234-
$sql = "SELECT name
235-
FROM sysobjects
236-
WHERE type='U'";
237-
break;
238-
239-
case 'postgres':
240-
$sql = 'SELECT relname
241-
FROM pg_stat_user_tables';
242-
break;
243-
244-
case 'firebird':
245-
$sql = 'SELECT rdb$relation_name
246-
FROM rdb$relations
247-
WHERE rdb$view_source is null
248-
AND rdb$system_flag = 0';
249-
break;
250-
251-
case 'oracle':
252-
$sql = 'SELECT table_name
253-
FROM USER_TABLES';
254-
break;
255-
}
256-
257-
$result = $db->sql_query($sql);
258-
259-
$tables = array();
260-
261-
while ($row = $db->sql_fetchrow($result))
219+
if (!class_exists('phpbb_db_tools'))
262220
{
263-
$tables[] = current($row);
221+
global $phpbb_root_path, $phpEx;
222+
require($phpbb_root_path . 'includes/db/db_tools.' . $phpEx);
264223
}
265224

266-
$db->sql_freeresult($result);
225+
$db_tools = new phpbb_db_tools($db);
267226

268-
return $tables;
227+
return $db_tools->sql_list_tables();
269228
}
270229

271230
/**
Collapse file

‎tests/dbal/db_tools_test.php‎

Copy file name to clipboardExpand all lines: tests/dbal/db_tools_test.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ public function test_column_remove_primary()
258258
$this->assertFalse($this->tools->sql_column_exists('prefix_table_name', 'c_id'));
259259
}
260260

261+
public function test_list_tables()
262+
{
263+
$tables = $this->tools->sql_list_tables();
264+
$this->assertTrue(isset($tables['prefix_table_name']));
265+
$this->assertFalse(isset($tables['prefix_does_not_exist']));
266+
}
267+
261268
public function test_table_exists()
262269
{
263270
$this->assertTrue($this->tools->sql_table_exists('prefix_table_name'));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.