Example #1
0
 public function testTableInheritsSchema()
 {
     $database = new Database();
     $database->setPlatform(new SchemaPlatform());
     $database->setSchema("Foo");
     $table = new Table("Bar");
     $database->addTable($table);
     $this->assertTrue($database->hasTable("Foo.Bar"));
     $this->assertFalse($database->hasTable("Bar"));
     $database = new Database();
     $database->setPlatform(new NoSchemaPlatform());
     $database->addTable($table);
     $this->assertFalse($database->hasTable("Foo.Bar"));
     $this->assertTrue($database->hasTable("Bar"));
 }
Example #2
0
 /**
  * Gets fields with specified key type.
  */
 public static function getFields($tables, $key = null, $nameOnly = true)
 {
     $tables = Utility::wrapAssoc($tables);
     $cache =& self::$schemaCache;
     // Clear the cache on expire
     if (@$cache['timestamp'] < strtotime('-30min')) {
         unset($cache['collections']);
         $cache['timestamp'] = microtime(1);
     }
     array_walk($tables, function ($tableName) use(&$cache) {
         if (!Database::hasTable($tableName)) {
             throw new \PDOException("Table {$tableName} doesn't exists!");
         }
         if (@$cache['collections'][$tableName]) {
             return;
         }
         $res = Database::fetchArray('SHOW COLUMNS FROM ' . static::escapeField($tableName));
         $res = array_combine(array_map(prop('Field'), $res), array_map(removes('Field'), $res));
         $res = array_map(function ($info) {
             $info['Key'] = preg_split('/\\s*,\\s*/', $info['Key']);
             foreach ($info as &$value) {
                 switch ($value) {
                     case 'YES':
                         $value = true;
                         break;
                     case 'NO':
                         $value = false;
                         break;
                 }
             }
             return $info;
         }, $res);
         $cache['collections'][$tableName] = $res;
     });
     $tables = array_map(function ($tableName) use($cache, $key, $nameOnly) {
         $cache = $cache['collections'][$tableName];
         if ($key !== null) {
             $key = Utility::wrapAssoc($key);
             $cache = array_filter($cache, propHas('Key', $key));
         }
         return $cache;
     }, $tables);
     $tables = array_reduce($tables, function ($result, $fields) {
         return array_merge($result, (array) $fields);
     }, array());
     if ($nameOnly) {
         $tables = array_unique(array_keys($tables));
     }
     return $tables;
 }