protected function createName() { $inputs[] = $this->table->getDatabase(); $inputs[] = $this->table->getCommonName(); $inputs[] = 'I'; $inputs[] = count($this->table->getIndices()) + 1; // @TODO replace the factory by a real object $this->name = NameFactory::generateName(NameFactory::CONSTRAINT_GENERATOR, $inputs); }
/** * Adds a unique constraint to the table to enforce uniqueness of the slug_column * * @param Table $table */ protected function addUniqueConstraint(Table $table) { $unique = new Unique($this->getColumnForParameter('slug_column')); $unique->setName($table->getCommonName() . '_slug'); $unique->addColumn($table->getColumn($this->getParameter('slug_column'))); if ($this->getParameter('scope_column')) { $unique->addColumn($table->getColumn($this->getParameter('scope_column'))); } $table->addUnique($unique); }
public function testCreateNewTable() { $table = new Table('books'); $this->assertSame('books', $table->getCommonName()); $this->assertFalse($table->isAllowPkInsert()); $this->assertFalse($table->isCrossRef()); $this->assertFalse($table->isReloadOnInsert()); $this->assertFalse($table->isReloadOnUpdate()); $this->assertFalse($table->isSkipSql()); $this->assertFalse($table->isReadOnly()); $this->assertSame(0, $table->getNumLazyLoadColumns()); $this->assertNull($table->getChildrenNames()); $this->assertFalse($table->hasForeignKeys()); }
protected function doNaming() { if (!$this->name || $this->autoNaming) { $newName = 'fk_'; $hash = []; $hash[] = $this->foreignSchemaName . '.' . $this->foreignTableCommonName; $hash[] = implode(',', (array) $this->localColumns); $hash[] = implode(',', (array) $this->foreignColumns); $newName .= substr(md5(strtolower(implode(':', $hash))), 0, 6); if ($this->parentTable) { $newName = $this->parentTable->getCommonName() . '_' . $newName; } $this->name = $newName; $this->autoNaming = true; } }
protected function parseTables(Database $database, Table $filterTable = null) { $sql = "\n SELECT name\n FROM sqlite_master\n WHERE type='table'\n %filter%\n UNION ALL\n SELECT name\n FROM sqlite_temp_master\n WHERE type='table'\n %filter%\n ORDER BY name;"; $filter = ''; if ($filterTable) { if ($schema = $filterTable->getSchema()) { $filter = sprintf(" AND name LIKE '%s§%%'", $schema); } $filter .= sprintf(" AND (name = '%s' OR name LIKE '%%§%1\$s')", $filterTable->getCommonName()); } else { if ($schema = $database->getSchema()) { $filter = sprintf(" AND name LIKE '%s§%%'", $schema); } } $sql = str_replace('%filter%', $filter, $sql); $dataFetcher = $this->dbh->query($sql); // First load the tables (important that this happen before filling out details of tables) foreach ($dataFetcher as $row) { $tableName = $row[0]; $tableSchema = ''; if ('sqlite_' == substr($tableName, 0, 7)) { continue; } if (false !== ($pos = strpos($tableName, '§'))) { $tableSchema = substr($tableName, 0, $pos); $tableName = substr($tableName, $pos + 2); } $table = new Table($tableName); if ($filterTable && $filterTable->getSchema()) { $table->setSchema($filterTable->getSchema()); } else { if (!$database->getSchema() && $tableSchema) { //we have no schema to filter, but this belongs to one, so next continue; } } if ($tableName === $this->getMigrationTable()) { continue; } $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); } }
/** * Returns the name of a table primary key. * * @return string */ public function getPrimaryKeyName(Table $table) { $tableName = $table->getCommonName(); return $tableName . '_pk'; }
/** * Adds Columns to the specified table. * * @param Table $table The Table model class to add columns to. * @param int $oid The table OID */ protected function addColumns(Table $table, $oid) { // Get the columns, types, etc. // Based on code from pgAdmin3 (http://www.pgadmin.org/) $searchPath = '?'; $params = [$table->getDatabase()->getSchema()]; if ($schema = $table->getSchema()) { $searchPath = '?'; $params = [$schema]; } else { if (!$table->getDatabase()->getSchema()) { $stmt = $this->dbh->query('SHOW search_path'); $searchPathString = $stmt->fetchColumn(); $params = []; $searchPath = explode(',', $searchPathString); foreach ($searchPath as &$path) { $params[] = $path; $path = '?'; } $searchPath = implode(', ', $searchPath); } } $stmt = $this->dbh->prepare("\n SELECT\n column_name,\n data_type,\n column_default,\n is_nullable,\n numeric_precision,\n numeric_scale,\n character_maximum_length\n FROM information_schema.columns\n WHERE\n table_schema IN ({$searchPath}) AND table_name = ?\n "); $params[] = $table->getCommonName(); $stmt->execute($params); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $size = $row['character_maximum_length']; if (!$size) { $size = $row['numeric_precision']; } $scale = $row['numeric_scale']; $name = $row['column_name']; $type = $row['data_type']; $default = $row['column_default']; $isNullable = true === $row['is_nullable'] || 'YES' === strtoupper($row['is_nullable']); // Check to ensure that this column isn't an array data type if ('ARRAY' === $type) { $this->warn(sprintf('Array datatypes are not currently supported [%s.%s]', $table->getName(), $name)); continue; } $autoincrement = null; // if column has a default if (strlen(trim($default)) > 0) { if (!preg_match('/^nextval\\(/', $default)) { $strDefault = preg_replace('/::[\\W\\D]*/', '', $default); } else { $autoincrement = true; $default = null; } } else { $default = null; } $propelType = $this->getMappedPropelType($type); if (!$propelType) { $propelType = Column::DEFAULT_TYPE; $this->warn('Column [' . $table->getName() . '.' . $name . '] has a column type (' . $type . ') that Propel does not support.'); } if (isset(static::$defaultTypeSizes[$type]) && $size == static::$defaultTypeSizes[$type]) { $size = null; } if ('SERIAL' === substr(strtoupper($type), 0, 6)) { $autoincrement = true; $default = null; } $column = new Column($name); $column->setTable($table); $column->setDomainForType($propelType); $column->getDomain()->replaceSize($size); if ($scale) { $column->getDomain()->replaceScale($scale); } if (null !== $default) { if ("'" !== substr($default, 0, 1) && strpos($default, '(')) { $defaultType = ColumnDefaultValue::TYPE_EXPR; } else { $defaultType = ColumnDefaultValue::TYPE_VALUE; $default = str_replace("'", '', $strDefault); } $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, $defaultType)); } $column->setAutoIncrement($autoincrement); $column->setNotNull(!$isNullable); $table->addColumn($column); } }
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"); }
/** * Appends the generated <table> XML node to its parent node. * * @param Table $table The Table model instance * @param \DOMNode $parentNode The parent DOMNode object */ private function appendTableNode(Table $table, \DOMNode $parentNode) { $tableNode = $parentNode->appendChild($this->document->createElement('table')); $tableNode->setAttribute('name', $table->getCommonName()); $database = $table->getDatabase(); $schema = $table->getSchema(); if ($schema && $schema !== $database->getSchema()) { $tableNode->setAttribute('schema', $schema); } if (IdMethod::NO_ID_METHOD !== ($idMethod = $table->getIdMethod())) { $tableNode->setAttribute('idMethod', $idMethod); } if ($phpName = $table->getPhpName()) { $tableNode->setAttribute('phpName', $phpName); } $package = $table->getPackage(); if ($package && !$table->isPackageOverriden()) { $tableNode->setAttribute('package', $package); } if ($namespace = $table->getNamespace()) { $tableNode->setAttribute('namespace', $namespace); } if ($table->isSkipSql()) { $tableNode->setAttribute('skipSql', 'true'); } if ($table->isAbstract()) { $tableNode->setAttribute('abstract', 'true'); } if ($interface = $table->getInterface()) { $tableNode->setAttribute('interface', $interface); } if ($table->isCrossRef()) { $tableNode->setAttribute('isCrossRef', 'true'); } $phpNamingMethod = $table->getPhpNamingMethod(); if ($phpNamingMethod && $phpNamingMethod !== $database->getDefaultPhpNamingMethod()) { $tableNode->setAttribute('phpNamingMethod', $phpNamingMethod); } if ($baseClass = $table->getBaseClass()) { $tableNode->setAttribute('baseClass', $baseClass); } if ($baseQueryClass = $table->getBaseQueryClass()) { $tableNode->setAttribute('baseQueryClass', $baseQueryClass); } if ($table->isReadOnly()) { $tableNode->setAttribute('readOnly', 'true'); } if ($table->isReloadOnInsert()) { $tableNode->setAttribute('reloadOnInsert', 'true'); } if ($table->isReloadOnUpdate()) { $tableNode->setAttribute('reloadOnUpdate', 'true'); } if (null !== ($referenceOnly = $table->isForReferenceOnly())) { $tableNode->setAttribute('forReferenceOnly', $referenceOnly ? 'true' : 'false'); } if ($alias = $table->getAlias()) { $tableNode->setAttribute('alias', $alias); } if ($description = $table->getDescription()) { $tableNode->setAttribute('description', $description); } $defaultStringFormat = $table->getDefaultStringFormat(); if (Table::DEFAULT_STRING_FORMAT !== $defaultStringFormat) { $tableNode->setAttribute('defaultStringFormat', $defaultStringFormat); } $defaultAccessorVisibility = $table->getDefaultAccessorVisibility(); if ($defaultAccessorVisibility !== Table::VISIBILITY_PUBLIC) { $tableNode->setAttribute('defaultAccessorVisibility', $defaultAccessorVisibility); } $defaultMutatorVisibility = $table->getDefaultMutatorVisibility(); if ($defaultMutatorVisibility !== Table::VISIBILITY_PUBLIC) { $tableNode->setAttribute('defaultMutatorVisibility', $defaultMutatorVisibility); } foreach ($table->getColumns() as $column) { $this->appendColumnNode($column, $tableNode); } foreach ($table->getForeignKeys() as $foreignKey) { $this->appendForeignKeyNode($foreignKey, $tableNode); } foreach ($table->getIdMethodParameters() as $parameter) { $this->appendIdMethodParameterNode($parameter, $tableNode); } foreach ($table->getIndices() as $index) { $this->appendIndexNode($index, $tableNode); } foreach ($table->getUnices() as $index) { $this->appendUniqueIndexNode($index, $tableNode); } foreach ($table->getVendorInformation() as $vendorInformation) { $this->appendVendorInformationNode($vendorInformation, $tableNode); } foreach ($table->getBehaviors() as $behavior) { $this->appendBehaviorNode($behavior, $tableNode); } }