You are here

function demo_enum_tables in Demonstration site (Sandbox / Snapshot) 5

Same name and namespace in other branches
  1. 8 demo.module \demo_enum_tables()
  2. 6 demo.admin.inc \demo_enum_tables()
  3. 7 demo.admin.inc \demo_enum_tables()

Returns a list of tables in the active database.

Only returns tables whose prefix matches the configured one (or ones, if there are multiple).

2 calls to demo_enum_tables()
demo_dump_db in ./database_mysql_dump.inc
Dump active database.
demo_reset in ./demo.admin.inc

File

./demo.admin.inc, line 473
Demonstration Site administrative pages

Code

function demo_enum_tables() {
  global $db_prefix;
  $tables = array();
  if (is_array($db_prefix)) {

    // Create a regular expression for table prefix matching.
    $rx = '/^' . implode('|', array_filter($db_prefix)) . '/';
  }
  else {
    if ($db_prefix != '') {
      $rx = '/^' . $db_prefix . '/';
    }
  }
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $result = db_query("SHOW TABLES");
      break;
    case 'pgsql':
      $result = db_query("SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'", 'public');
      break;
  }
  while ($table = db_fetch_array($result)) {
    $table = reset($table);
    if (is_array($db_prefix)) {

      // Check if table name matches a configured prefix.
      if (preg_match($rx, $table, $matches)) {
        $table_prefix = $matches[0];
        $plain_table = substr($table, strlen($table_prefix));
        if ($db_prefix[$plain_table] == $table_prefix || $db_prefix['default'] == $table_prefix) {
          $tables[] = $table;
        }
      }
    }
    else {
      if ($db_prefix != '') {
        if (preg_match($rx, $table)) {
          $tables[] = $table;
        }
      }
      else {
        $tables[] = $table;
      }
    }
  }
  return $tables;
}