You are here

function _optimizedb_tables_list in OptimizeDB 8

Same name and namespace in other branches
  1. 6 optimizedb.module \_optimizedb_tables_list()
  2. 7 optimizedb.module \_optimizedb_tables_list()

List and the size of the database tables.

Return value

array An array with a list of database tables.

5 calls to _optimizedb_tables_list()
GeneralTest::testTablesList in tests/src/Functional/GeneralTest.php
Sizes tables.
ListTablesOperationExecuteTest::testListTablesOperationExecute in tests/src/Functional/ListTablesOperationExecuteTest.php
Performing operations on tables.
OptimizedbAdminForm::optimizeTablesSubmit in src/Form/OptimizedbAdminForm.php
Optimize all tables in database.
OptimizedbListTablesForm::buildForm in src/Form/OptimizedbListTablesForm.php
Form constructor.
optimizedb_module_preinstall in ./optimizedb.install
Implements hook_module_preinstall().

File

./optimizedb.module, line 187
Database Optimization.

Code

function _optimizedb_tables_list() {
  $tables = FALSE;
  switch (\Drupal::database()
    ->driver()) {
    case 'mysql':
      $tables = \Drupal::database()
        ->query("SHOW TABLE STATUS")
        ->fetchAll();
      break;
    case 'pgsql':
      $tables = \Drupal::database()
        ->query("SELECT table_name as \"Name\",\n        pg_total_relation_size(table_name) AS \"Data_length\",\n        0 as \"Index_length\"\n        FROM information_schema.tables\n        WHERE table_schema = 'public'\n        ORDER BY table_name")
        ->fetchAll();
      break;
  }
  $result = [];
  if ($tables) {
    $size_tables = 0;
    foreach ($tables as $table) {
      $length = $table->Data_length + $table->Index_length;
      $result[$table->Name] = [
        'name' => $table->Name,
        'size' => format_size($length),
        'size_byte' => $length,
      ];
      $size_tables += $length;
    }

    // The total size of the tables.
    \Drupal::configFactory()
      ->getEditable('optimizedb.settings')
      ->set('tables_size', $size_tables)
      ->save();
  }
  return (array) $result;
}