/** * Adds Columns to the specified table. * * @param Table $table The Table model class to add columns to. */ protected function addColumns(Table $table) { $tableName = $table->getName(); // var_dump("PRAGMA table_info('$tableName') //"); $stmt = $this->dbh->query("PRAGMA table_info('{$tableName}')"); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $name = $row['name']; $fulltype = $row['type']; $size = null; $scale = null; if (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) { $type = $matches[1]; $size = $matches[2]; $scale = $matches[3]; } elseif (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) { $type = $matches[1]; $size = $matches[2]; } else { $type = $fulltype; } $notNull = $row['notnull']; $default = $row['dflt_value']; $propelType = $this->getMappedPropelType(strtolower($type)); if (!$propelType) { $propelType = Column::DEFAULT_TYPE; $this->warn('Column [' . $table->getName() . '.' . $name . '] has a column type (' . $type . ') that Propel does not support.'); } $column = new Column($name); $column->setTable($table); $column->setDomainForType($propelType); // We may want to provide an option to include this: // $column->getDomain()->replaceSqlType($type); $column->getDomain()->replaceSize($size); $column->getDomain()->replaceScale($scale); if (null !== $default) { if ("'" !== substr($default, 0, 1) && strpos($default, '(')) { $defaultType = ColumnDefaultValue::TYPE_EXPR; if ('datetime(CURRENT_TIMESTAMP, \'localtime\')' === $default) { $default = 'CURRENT_TIMESTAMP'; } } else { $defaultType = ColumnDefaultValue::TYPE_VALUE; $default = str_replace("'", '', $default); } $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, $defaultType)); } $column->setNotNull($notNull); if (0 < $row['pk'] + 0) { $column->setPrimaryKey(true); } if ($column->isPrimaryKey()) { // check if autoIncrement $autoIncrementStmt = $this->dbh->prepare(' SELECT tbl_name FROM sqlite_master WHERE tbl_name = ? AND sql LIKE "%AUTOINCREMENT%" '); $autoIncrementStmt->execute([$table->getName()]); $autoincrementRow = $autoIncrementStmt->fetch(\PDO::FETCH_ASSOC); if ($autoincrementRow && $autoincrementRow['tbl_name'] == $table->getName()) { $column->setAutoIncrement(true); } } $table->addColumn($column); } }
/** * Appends the generated <column> XML node to its parent node. * * @param Column $column The Column model instance * @param \DOMNode $parentNode The parent DOMNode object */ private function appendColumnNode(Column $column, \DOMNode $parentNode) { $columnNode = $parentNode->appendChild($this->document->createElement('column')); $columnNode->setAttribute('name', $column->getName()); if ($phpName = $column->getPhpName()) { $columnNode->setAttribute('phpName', $phpName); } $columnNode->setAttribute('type', $column->getType()); $domain = $column->getDomain(); if ($size = $domain->getSize()) { $columnNode->setAttribute('size', $size); } if (null !== ($scale = $domain->getScale())) { $columnNode->setAttribute('scale', $scale); } $platform = $column->getPlatform(); if ($platform && !$column->isDefaultSqlType($platform)) { $columnNode->setAttribute('sqlType', $domain->getSqlType()); } if ($description = $column->getDescription()) { $columnNode->setAttribute('description', $description); } if ($column->isPrimaryKey()) { $columnNode->setAttribute('primaryKey', 'true'); } if ($column->isAutoIncrement()) { $columnNode->setAttribute('autoIncrement', 'true'); } if ($column->isNotNull()) { $columnNode->setAttribute('required', 'true'); } $defaultValue = $domain->getDefaultValue(); if ($defaultValue) { $type = $defaultValue->isExpression() ? 'defaultExpr' : 'defaultValue'; $columnNode->setAttribute($type, $defaultValue->getValue()); } if ($column->isInheritance()) { $columnNode->setAttribute('inheritance', $column->getInheritanceType()); foreach ($column->getInheritanceList() as $inheritance) { $this->appendInheritanceNode($inheritance, $columnNode); } } if ($column->isNodeKey()) { $columnNode->setAttribute('nodeKey', 'true'); if ($nodeKeySeparator = $column->getNodeKeySep()) { $columnNode->setAttribute('nodeKeySep', $nodeKeySeparator); } } foreach ($column->getVendorInformation() as $vendorInformation) { $this->appendVendorInformationNode($vendorInformation, $columnNode); } }
/** * Add an added Pk column * * @param string $columnName * @param Column $addedPkColumn */ public function addAddedPkColumn($columnName, Column $addedPkColumn) { if (!$addedPkColumn->isPrimaryKey()) { throw new DiffException(sprintf('Column %s is not a valid primary key column.', $columnName)); } $this->addedPkColumns[$columnName] = $addedPkColumn; }
public function testSetupObjectWithDomain() { $database = $this->getDatabaseMock('bookstore'); $database->expects($this->once())->method('getDomain')->with($this->equalTo('BOOLEAN'))->will($this->returnValue($this->getDomainMock('INTEGER'))); $table = $this->getTableMock('books', array('database' => $database)); $column = new Column(); $column->setTable($table); $column->setDomain($this->getDomainMock('BOOLEAN')); $column->loadMapping(array('domain' => 'BOOLEAN', 'name' => 'is_published', 'phpName' => 'IsPublished', 'phpType' => 'boolean', 'tableMapName' => 'IS_PUBLISHED', 'prefix' => 'col_', 'accessorVisibility' => 'public', 'mutatorVisibility' => 'public', 'primaryString' => 'false', 'primaryKey' => 'false', 'nodeKey' => 'false', 'nestedSetLeftKey' => 'false', 'nestedSetRightKey' => 'false', 'treeScopeKey' => 'false', 'required' => 'false', 'autoIncrement' => 'false', 'lazyLoad' => 'true', 'sqlType' => 'TINYINT', 'size' => 1, 'defaultValue' => 'true', 'valueSet' => 'FOO, BAR, BAZ')); $this->assertSame('is_published', $column->getName()); $this->assertSame('IsPublished', $column->getPhpName()); $this->assertSame('boolean', $column->getPhpType()); $this->assertSame('IS_PUBLISHED', $column->getTableMapName()); $this->assertSame('public', $column->getAccessorVisibility()); $this->assertSame('public', $column->getMutatorVisibility()); $this->assertFalse($column->isPrimaryString()); $this->assertFalse($column->isPrimaryKey()); $this->assertFalse($column->isNodeKey()); $this->assertFalse($column->isNestedSetLeftKey()); $this->assertFalse($column->isNestedSetRightKey()); $this->assertFalse($column->isTreeScopeKey()); $this->assertTrue($column->isLazyLoad()); $this->assertCount(3, $column->getValueSet()); }