You are here

function views_database_connector_get_database_schema_sqlite in Views Database Connector 7

Same name and namespace in other branches
  1. 8 views_database_connector.views.inc \views_database_connector_get_database_schema_sqlite()

Gathers appropriate information from SQLite driver type databases.

1 call to views_database_connector_get_database_schema_sqlite()
views_database_connector_get_database_schemas in ./views_database_connector.views.inc
Gathers appropriate information from each potential database driver type.

File

./views_database_connector.views.inc, line 239
Responsible for hooking views to add each database and its tables.

Code

function views_database_connector_get_database_schema_sqlite($key) {

  // Load the appropriate data type groups.
  $types = views_database_connector_get_data_types();

  // Switch to database in question.
  db_set_active($key);

  // Get a list of the tables in this database.
  $q = 'SELECT name FROM sqlite_master WHERE type=\'table\';';
  $tables = db_query($q);

  // Switch back to the main database.
  db_set_active('default');
  $tablelist = array();

  // Fetch a row, each with a table name.
  while ($row = $tables
    ->fetchAssoc()) {
    foreach ($row as $v) {

      // Check that the table name is safe to substitute in the query.
      if ($v == db_escape_table($v)) {

        // Switch to database in question.
        db_set_active($key);

        // Fetch column names and their data type from said table.
        $q = 'PRAGMA table_info(:table);';
        $cols = db_query(str_ireplace(':table', $v, $q));

        // Switch back to the main database.
        db_set_active('default');
        $collist = array();

        // Fetch a row, each with a column name.
        while ($r = $cols
          ->fetchAssoc()) {
          $t = 'broken';

          // Add column to column list.
          if (isset($r['name'])) {
            foreach ($types as $type => $matches) {
              foreach ($matches as $match) {
                if (stristr($r['type'], $match)) {
                  $t = $type;
                }
              }
            }
            $collist[] = array(
              $t,
              $r['name'],
            );
          }
        }

        // Add table and its columns to the table list.
        $tablelist[] = array(
          $v,
          $collist,
        );
      }
    }
  }
  return $tablelist;
}