/** * */ public function parse(Database $database, Task $task = null) { $stmt = $this->dbh->query("SELECT version() as ver"); $nativeVersion = $stmt->fetchColumn(); if (!$nativeVersion) { throw new EngineException("Failed to get database version"); } $arrVersion = sscanf($nativeVersion, '%*s %d.%d'); $version = sprintf("%d.%d", $arrVersion[0], $arrVersion[1]); // Clean up $stmt = null; $stmt = $this->dbh->query("SELECT c.oid,\n c.relname, n.nspname\n FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)\n WHERE c.relkind = 'r'\n AND n.nspname NOT IN ('information_schema','pg_catalog')\n AND n.nspname NOT LIKE 'pg_temp%'\n AND n.nspname NOT LIKE 'pg_toast%'\n ORDER BY relname"); $tableWraps = array(); // First load the tables (important that this happen before filling out details of tables) if ($task) { $task->log("Reverse Engineering Tables", Project::MSG_VERBOSE); } while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $name = $row['relname']; $namespacename = $row['nspname']; if ($name == $this->getMigrationTable()) { continue; } if ($task) { $task->log(" Adding table '" . $name . "' in schema '" . $namespacename . "'", Project::MSG_VERBOSE); } $oid = $row['oid']; $table = new Table($name); if ($namespacename != 'public') { $table->setSchema($namespacename); } $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); // Create a wrapper to hold these tables and their associated OID $wrap = new stdClass(); $wrap->table = $table; $wrap->oid = $oid; $tableWraps[] = $wrap; } // Now populate only columns. if ($task) { $task->log("Reverse Engineering Columns", Project::MSG_VERBOSE); } foreach ($tableWraps as $wrap) { if ($task) { $task->log(" Adding columns for table '" . $wrap->table->getName() . "'", Project::MSG_VERBOSE); } $this->addColumns($wrap->table, $wrap->oid, $version); } // Now add indexes and constraints. if ($task) { $task->log("Reverse Engineering Indices And Constraints", Project::MSG_VERBOSE); } foreach ($tableWraps as $wrap) { if ($task) { $task->log(" Adding indices and constraints for table '" . $wrap->table->getName() . "'", Project::MSG_VERBOSE); } $this->addForeignKeys($wrap->table, $wrap->oid, $version); $this->addIndexes($wrap->table, $wrap->oid, $version); $this->addPrimaryKey($wrap->table, $wrap->oid, $version); } // TODO - Handle Sequences ... return count($tableWraps); }
public function testQualifiedName() { $table = new Table(); $table->setSchema("foo"); $table->setCommonName("bar"); $this->assertEquals($table->getName(), "bar"); $this->assertEquals($table->getCommonName(), "bar"); $database = new Database(); $database->addTable($table); $database->setPlatform(new NoSchemaPlatform()); $this->assertEquals($table->getName(), "bar"); $database->setPlatform(new SchemaPlatform()); $this->assertEquals($table->getName(), "foo.bar"); }
/** * An utility method to add a new table from an xml attribute. */ public function addTable($data) { if ($data instanceof Table) { $tbl = $data; // alias $tbl->setDatabase($this); if (isset($this->tablesByName[$tbl->getName()])) { throw new EngineException(sprintf('Table "%s" declared twice', $tbl->getName())); } if ($tbl->getSchema() === null) { $tbl->setSchema($this->getSchema()); } $this->tableList[] = $tbl; $this->tablesByName[$tbl->getName()] = $tbl; $this->tablesByLowercaseName[strtolower($tbl->getName())] = $tbl; $this->tablesByPhpName[$tbl->getPhpName()] = $tbl; if (strpos($tbl->getNamespace(), '\\') === 0) { $tbl->setNamespace(substr($tbl->getNamespace(), 1)); } elseif ($namespace = $this->getNamespace()) { if ($tbl->getNamespace() === null) { $tbl->setNamespace($namespace); } else { $tbl->setNamespace($namespace . '\\' . $tbl->getNamespace()); } } if ($tbl->getPackage() === null) { $tbl->setPackage($this->getPackage()); } return $tbl; } else { $tbl = new Table(); $tbl->setDatabase($this); $tbl->setSchema($this->getSchema()); $tbl->loadFromXML($data); return $this->addTable($tbl); // call self w/ different param } }
public function testAddTableWithSameNameOnDifferentSchema() { $db = new Database(); $db->setPlatform(new SchemaPlatform()); $t1 = new Table('t1'); $db->addTable($t1); $this->assertEquals('t1', $t1->getName()); $t1b = new Table('t1'); $t1b->setSchema('bis'); $db->addTable($t1b); $this->assertEquals('bis.t1', $t1b->getName()); }