private function _loadTables()
 {
     if ($this->_tables === null) {
         $tables = $this->_sm->listTables();
         $this->_tables = array();
         foreach ($tables as $table) {
             $this->_tables[strtolower($table->getName())] = $table;
         }
     }
 }
 public function __construct()
 {
     $this->schema = DB::getDoctrineSchemaManager();
     $this->schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
     $this->tables = $this->schema->listTables();
     $this->relationships = [];
     //first create empty ruleset for each table
     foreach ($this->tables as $table) {
         $this->relationships[$table->getName()] = ['hasMany' => [], 'hasOne' => [], 'belongsTo' => [], 'belongsToMany' => []];
     }
     // get all relationships into $this->relationships variable
     $this->getAllRelationships();
 }
 /**
  * Gets array \Doctrine\DBAL\Schema\Table
  *
  * @return array
  */
 public function get_tables()
 {
     /**
      * get tables
      */
     $tables = $this->sm->listTables();
     /**
      * unset ignore tables
      */
     $ignore_table_names = \Config::get('dbdocs.ignore_table_names', array());
     foreach ($ignore_table_names as $ignore_table_name) {
         foreach ($tables as $index => $table) {
             /* @var $table \Doctrine\DBAL\Schema\Table */
             if ($table->getName() == $ignore_table_name) {
                 unset($tables[$index]);
             }
         }
     }
     $ignore_table_name_regex = \Config::get('dbdocs.ignore_table_name_regex');
     if (!empty($ignore_table_name_regex)) {
         foreach ($tables as $index => $table) {
             /* @var $table \Doctrine\DBAL\Schema\Table */
             if (0 < preg_match($ignore_table_name_regex, $table->getName())) {
                 unset($tables[$index]);
             }
         }
     }
     return array_merge($tables, array());
 }
 public function testListTables()
 {
     $this->createTestTable('list_tables_test');
     $tables = $this->_sm->listTables();
     $this->assertType('array', $tables);
     $this->assertTrue(count($tables) > 0);
     $foundTable = false;
     foreach ($tables as $table) {
         $this->assertType('Doctrine\\DBAL\\Schema\\Table', $table);
         if ($table->getName() == 'list_tables_test') {
             $foundTable = true;
             $this->assertTrue($table->hasColumn('id'));
             $this->assertTrue($table->hasColumn('test'));
             $this->assertTrue($table->hasColumn('foreign_key_test'));
         }
     }
 }
 public function testListTables()
 {
     $this->createTestTable('list_tables_test');
     $tables = $this->_sm->listTables();
     $this->assertInternalType('array', $tables);
     $this->assertTrue(count($tables) > 0, "List Tables has to find at least one table named 'list_tables_test'.");
     $foundTable = false;
     foreach ($tables as $table) {
         $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Table', $table);
         if (strtolower($table->getName()) == 'list_tables_test') {
             $foundTable = true;
             $this->assertTrue($table->hasColumn('id'));
             $this->assertTrue($table->hasColumn('test'));
             $this->assertTrue($table->hasColumn('foreign_key_test'));
         }
     }
     $this->assertTrue($foundTable, "The 'list_tables_test' table has to be found.");
 }
 public function __construct(\Doctrine\DBAL\Schema\AbstractSchemaManager $schema_manager)
 {
     $this->schema_manager = $schema_manager;
     foreach ($schema_manager->listTables() as $table) {
         $this->tables[$table->getName()] = new TableInformation($this, $table);
     }
     foreach ($this->tables as $table) {
         $this->findRelationships($table);
     }
 }
 /**
  * @param $file
  * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $sm
  * @return bool
  */
 public static function saveSchemaToFile($file, $sm)
 {
     $xml = new SimpleXMLElement('<database/>');
     $xml->addChild('name', OC_Config::getValue("dbname", "owncloud"));
     $xml->addChild('create', 'true');
     $xml->addChild('overwrite', 'false');
     $xml->addChild('charset', 'utf8');
     foreach ($sm->listTables() as $table) {
         self::saveTable($table, $xml->addChild('table'));
     }
     file_put_contents($file, $xml->asXML());
     return true;
 }
 /**
  * @param EloquentModel $model
  * @return $this
  */
 protected function setRelations(EloquentModel $model)
 {
     $foreignKeys = $this->manager->listTableForeignKeys($this->addPrefix($model->getTableName()));
     foreach ($foreignKeys as $tableForeignKey) {
         $tableForeignColumns = $tableForeignKey->getForeignColumns();
         if (count($tableForeignColumns) !== 1) {
             continue;
         }
         $relation = new BelongsTo($this->cropPrefix($tableForeignKey->getForeignTableName()), $tableForeignKey->getLocalColumns()[0], $tableForeignColumns[0]);
         $model->addRelation($relation);
     }
     $tables = $this->manager->listTables();
     foreach ($tables as $table) {
         if ($table->getName() === $this->addPrefix($model->getTableName())) {
             continue;
         }
         $foreignKeys = $table->getForeignKeys();
         foreach ($foreignKeys as $name => $foreignKey) {
             if ($foreignKey->getForeignTableName() === $this->addPrefix($model->getTableName())) {
                 $localColumns = $foreignKey->getLocalColumns();
                 if (count($localColumns) !== 1) {
                     continue;
                 }
                 if (count($foreignKeys) === 2 && count($table->getColumns()) >= 2) {
                     $keys = array_keys($foreignKeys);
                     $key = array_search($name, $keys) === 0 ? 1 : 0;
                     $secondForeignKey = $foreignKeys[$keys[$key]];
                     $secondForeignTable = $secondForeignKey->getForeignTableName();
                     $relation = new BelongsToMany($this->cropPrefix($secondForeignTable), $table->getName(), $localColumns[0], $secondForeignKey->getLocalColumns()[0]);
                     $model->addRelation($relation);
                     break;
                 } else {
                     $tableName = $this->cropPrefix($foreignKey->getLocalTableName());
                     $foreignColumn = $localColumns[0];
                     $localColumn = $foreignKey->getForeignColumns()[0];
                     if ($this->isColumnUnique($table, $foreignColumn)) {
                         $relation = new HasOne($tableName, $foreignColumn, $localColumn);
                     } else {
                         $relation = new HasMany($tableName, $foreignColumn, $localColumn);
                     }
                     $model->addRelation($relation);
                 }
             }
         }
     }
     return $this;
 }