Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.56 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class Musterdb_Database_PDO_Mysql extends Database_PDO
  4. {
  5.         public function list_tables($like = NULL)
  6.         {
  7.                 if (is_string($like))
  8.                 {
  9.                         // Search for table names
  10.                         $result = $this->query(Database::SELECT, 'SHOW TABLES LIKE '.$this->quote($like), FALSE);
  11.                 }
  12.                 else
  13.                 {
  14.                         // Find all table names
  15.                         $result = $this->query(Database::SELECT, 'SHOW TABLES', FALSE);
  16.                 }
  17.  
  18.                 $tables = array();
  19.                 foreach ($result as $row)
  20.                 {
  21.                         $tables[] = reset($row);
  22.                 }
  23.  
  24.                 return $tables;
  25.         }
  26.  
  27.         public function list_columns($table, $like = NULL)
  28.         {
  29.                 // Quote the table name
  30.                 $table = $this->quote_table($table);
  31.  
  32.                 if (is_string($like))
  33.                 {
  34.                         // Search for column names
  35.                         $result = $this->query(Database::SELECT, 'SHOW FULL COLUMNS FROM '.$table.' LIKE '.$this->quote($like), FALSE);
  36.                 }
  37.                 else
  38.                 {
  39.                         // Find all column names
  40.                         $result = $this->query(Database::SELECT, 'SHOW FULL COLUMNS FROM '.$table, FALSE);
  41.                 }
  42.  
  43.                 $count = 0;
  44.                 $columns = array();
  45.                 foreach ($result as $row)
  46.                 {
  47.                         var_dump($row);
  48.                         list($type, $length) = $this->_parse_type($row['Type']);
  49.  
  50.                         $column = $this->datatype($type);
  51.  
  52.                         $column['column_name']      = $row['Field'];
  53.                         $column['column_default']   = $row['Default'];
  54.                         $column['data_type']        = $type;
  55.                         $column['is_nullable']      = ($row['Null'] == 'YES');
  56.                         $column['ordinal_position'] = ++$count;
  57.  
  58.                         switch ($column['data_type']) // fixed bug, index was 'type'
  59.                         {
  60.                                 case 'float':
  61.                                         if (isset($length))
  62.                                         {
  63.                                                 list($column['numeric_precision'], $column['numeric_scale']) = explode(',', $length);
  64.                                         }
  65.                                 break;
  66.                                 case 'string':
  67.                                         switch ($column['data_type'])
  68.                                         {
  69.                                                 case 'binary':
  70.                                                 case 'varbinary':
  71.                                                         $column['character_maximum_length'] = $length;
  72.                                                 break;
  73.  
  74.                                                 case 'char':
  75.                                                 case 'varchar':
  76.                                                         $column['character_maximum_length'] = $length;
  77.                                                 case 'text':
  78.                                                 case 'tinytext':
  79.                                                 case 'mediumtext':
  80.                                                 case 'longtext':
  81.                                                         $column['collation_name'] = $row['Collation'];
  82.                                                 break;
  83.  
  84.                                                 case 'enum':
  85.                                                 case 'set':
  86.                                                         $column['collation_name'] = $row['Collation'];
  87.                                                         $column['options'] = explode('\',\'', substr($length, 1, -1));
  88.                                                 break;
  89.                                         }
  90.                                 break;
  91.                         }
  92.  
  93.                         // MySQL attributes
  94.                         $column['comment']      = $row['Comment'];
  95.                         $column['extra']        = $row['Extra'];
  96.                         $column['key']          = $row['Key'];
  97.                         $column['privileges']   = $row['Privileges'];
  98.  
  99.                         $columns[$row['Field']] = $column;
  100.                 }
  101.  
  102.                 return $columns;
  103.         }
  104. }