/** * @return void */ protected function _before() { $config = new QuickGeneratorConfig(); $table = new Table('Foo'); $column = new Column('testColumn', PropelTypes::INTEGER); $table->addColumn($column); $table->setNamespace('Unit\\Spryker\\Zed\\Propel\\Business\\Builder\\QueryBuilder'); $table->setDatabase(new Database('TestDB', new DefaultPlatform())); foreach ($this->getFilesToGenerate() as $fileName => $builderClass) { $builder = new $builderClass($table); $builder->setGeneratorConfig($config); $this->writePropelFile($builder, $fileName); } }
/** * Adds a new table to this database. * * @param Table|array $table * @return Table */ public function addTable($table) { if (!$table instanceof Table) { $tbl = new Table(); $tbl->setDatabase($this); $tbl->setSchema($this->getSchema()); $tbl->loadMapping($table); return $this->addTable($tbl); } $table->setDatabase($this); if (isset($this->tablesByName[$table->getName()])) { throw new EngineException(sprintf('Table "%s" declared twice', $table->getName())); } if (null === $table->getSchema()) { $table->setSchema($this->getSchema()); } $this->tables[] = $table; $this->tablesByName[$table->getName()] = $table; $this->tablesByLowercaseName[strtolower($table->getName())] = $table; $this->tablesByPhpName[$table->getPhpName()] = $table; $this->computeTableNamespace($table); if (null === $table->getPackage()) { $table->setPackage($this->getPackage()); } return $table; }
public function testGetBaseClassesFromDatabase() { $database = $this->getDatabaseMock('bookstore'); $database->expects($this->once())->method('getBaseClass')->will($this->returnValue('BaseObject')); $table = new Table(); $table->setDatabase($database); $this->assertSame('BaseObject', $table->getBaseClass()); }
public function testAppendXmlNamespaceWithAutoPackage() { $schema = <<<EOF <?xml version="1.0"?> <table name="test" namespace="\\testNs"/> EOF; $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $config = new GeneratorConfig(); $config->setBuildProperties(array('propel.namespace.autoPackage' => 'true')); $appData = new AppData(); $appData->setGeneratorConfig($config); $db = new Database('testDb'); $db->setAppData($appData); $table = new Table('test'); $table->setDatabase($db); $table->setNamespace('\\testNs'); $table->appendXml($doc); $xmlstr = trim($doc->saveXML()); $this->assertSame($schema, $xmlstr); $schema = <<<EOF <?xml version="1.0"?> <table name="test" namespace="\\testNs" package="testPkg"/> EOF; $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $table->setPackage('testPkg'); $table->appendXml($doc); $xmlstr = trim($doc->saveXML()); $this->assertSame($schema, $xmlstr); }
public function testReverseDiffHasModifiedIndices() { $table = new Table(); $table->setDatabase(new Database('foo', new DefaultPlatform())); $fromIndex = new Index('i1'); $fromIndex->setTable($table); $toIndex = new Index('i1'); $toIndex->setTable($table); $diff = $this->createTableDiff(); $diff->addModifiedIndex('i1', $fromIndex, $toIndex); $reverseDiff = $diff->getReverseDiff(); $this->assertTrue($reverseDiff->hasModifiedIndices()); $this->assertSame(['i1' => [$toIndex, $fromIndex]], $reverseDiff->getModifiedIndices()); }
/** * An utility method to add a new table from an xml attribute. */ public function addTable($data) { if ($data instanceof Table) { $tbl = $data; // alias if (isset($this->tablesByName[$tbl->getName()])) { throw new EngineException(sprintf('Table "%s" declared twice', $tbl->getName())); } $tbl->setDatabase($this); 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 } }
protected function parseTables(&$tableWraps, Database $database, Table $filterTable = null) { $stmt = null; $params = []; $sql = "\n SELECT c.oid, 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%'"; if ($filterTable) { if ($schema = $filterTable->getSchema()) { $sql .= ' AND n.nspname = ?'; $params[] = $schema; } $sql .= ' AND c.relname = ?'; $params[] = $filterTable->getCommonName(); } else { if (!$database->getSchema()) { $stmt = $this->dbh->query('SELECT schema_name FROM information_schema.schemata'); $searchPath = []; while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $searchPath[] = $row['schema_name']; } foreach ($searchPath as &$path) { $params[] = $path; $path = '?'; } $searchPath = implode(', ', $searchPath); $sql .= "\n AND n.nspname IN ({$searchPath})"; } elseif ($database->getSchema()) { $sql .= "\n AND n.nspname = ?"; $params[] = $database->getSchema(); } } $sql .= "\n ORDER BY relname"; $stmt = $this->dbh->prepare($sql); $stmt->execute($params); // First load the tables (important that this happen before filling out details of tables) while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $name = $row['relname']; $namespaceName = $row['nspname']; if ($name == $this->getMigrationTable()) { continue; } $oid = $row['oid']; $table = new Table($name); if ('public' !== $namespaceName) { $table->setSchema($namespaceName); } $table->setIdMethod($database->getDefaultIdMethod()); $table->setDatabase($database); if (!$database->hasTable($table->getName())) { $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; } } }