CC-2166: Packaging Improvements. Moved the Zend app into airtime_mvc. It is now installed to /var/www/airtime. Storage is now set to /srv/airtime/stor. Utils are now installed to /usr/lib/airtime/utils/. Added install/airtime-dircheck.php as a simple test to see if everything is install/uninstalled correctly.
This commit is contained in:
parent
514777e8d2
commit
b11cbd8159
4546 changed files with 138 additions and 51 deletions
|
@ -0,0 +1,166 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/DataModelBuilder.php';
|
||||
|
||||
/**
|
||||
* Baseclass for SQL DDL-building classes.
|
||||
*
|
||||
* DDL-building classes are those that build all the SQL DDL for a single table.
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql
|
||||
*/
|
||||
abstract class DDLBuilder extends DataModelBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* Builds the SQL for current table and returns it as a string.
|
||||
*
|
||||
* This is the main entry point and defines a basic structure that classes should follow.
|
||||
* In most cases this method will not need to be overridden by subclasses.
|
||||
*
|
||||
* @return string The resulting SQL DDL.
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
$script = "";
|
||||
$this->addTable($script);
|
||||
$this->addIndices($script);
|
||||
$this->addForeignKeys($script);
|
||||
return $script;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name to use for creating a sequence for the current table.
|
||||
*
|
||||
* This will create a new name or use one specified in an id-method-parameter
|
||||
* tag, if specified.
|
||||
*
|
||||
* @return string Sequence name for this table.
|
||||
*/
|
||||
public function getSequenceName()
|
||||
{
|
||||
$table = $this->getTable();
|
||||
static $longNamesMap = array();
|
||||
$result = null;
|
||||
if ($table->getIdMethod() == IDMethod::NATIVE) {
|
||||
$idMethodParams = $table->getIdMethodParameters();
|
||||
$maxIdentifierLength = $table->getDatabase()->getPlatform()->getMaxColumnNameLength();
|
||||
if (empty($idMethodParams)) {
|
||||
if (strlen($table->getName() . "_SEQ") > $maxIdentifierLength) {
|
||||
if (!isset($longNamesMap[$table->getName()])) {
|
||||
$longNamesMap[$table->getName()] = strval(count($longNamesMap) + 1);
|
||||
}
|
||||
$result = substr($table->getName(), 0, $maxIdentifierLength - strlen("_SEQ_" . $longNamesMap[$table->getName()])) . "_SEQ_" . $longNamesMap[$table->getName()];
|
||||
}
|
||||
else {
|
||||
$result = substr($table->getName(), 0, $maxIdentifierLength -4) . "_SEQ";
|
||||
}
|
||||
} else {
|
||||
$result = substr($idMethodParams[0]->getValue(), 0, $maxIdentifierLength);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the DDL SQL for a Column object.
|
||||
* @return string
|
||||
*/
|
||||
public function getColumnDDL(Column $col)
|
||||
{
|
||||
$platform = $this->getPlatform();
|
||||
$domain = $col->getDomain();
|
||||
|
||||
$sb = "";
|
||||
$sb .= $this->quoteIdentifier($col->getName()) . " ";
|
||||
$sb .= $domain->getSqlType();
|
||||
if ($platform->hasSize($domain->getSqlType())) {
|
||||
$sb .= $domain->printSize();
|
||||
}
|
||||
$sb .= " ";
|
||||
$sb .= $col->getDefaultSetting() . " ";
|
||||
$sb .= $col->getNotNullString() . " ";
|
||||
$sb .= $col->getAutoIncrementString();
|
||||
|
||||
return trim($sb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a delimiter-delimited string list of column names, quoted using quoteIdentifier().
|
||||
* @param array Column[] or string[]
|
||||
* @param string $delim The delimiter to use in separating the column names.
|
||||
* @return string
|
||||
*/
|
||||
public function getColumnList($columns, $delim=',')
|
||||
{
|
||||
$list = array();
|
||||
foreach ($columns as $col) {
|
||||
if ($col instanceof Column) {
|
||||
$col = $col->getName();
|
||||
}
|
||||
$list[] = $this->quoteIdentifier($col);
|
||||
}
|
||||
return implode($delim, $list);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function adds any _database_ start/initialization SQL.
|
||||
* This is designed to be called for a database, not a specific table, hence it is static.
|
||||
* @return string The DDL is returned as astring.
|
||||
*/
|
||||
public static function getDatabaseStartDDL()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* This function adds any _database_ end/cleanup SQL.
|
||||
* This is designed to be called for a database, not a specific table, hence it is static.
|
||||
* @return string The DDL is returned as astring.
|
||||
*/
|
||||
public static function getDatabaseEndDDL()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets any static variables between building a SQL file for a database.
|
||||
*
|
||||
* Theoretically, Propel could build multiple .sql files for multiple databases; in
|
||||
* many cases we don't want static values to persist between these. This method provides
|
||||
* a way to clear out static values between iterations, if the subclasses choose to implement
|
||||
* it.
|
||||
*/
|
||||
public static function reset()
|
||||
{
|
||||
// nothing by default
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds table definition.
|
||||
* @param string &$script The script will be modified in this method.
|
||||
*/
|
||||
abstract protected function addTable(&$script);
|
||||
|
||||
/**
|
||||
* Adds index definitions.
|
||||
* @param string &$script The script will be modified in this method.
|
||||
*/
|
||||
abstract protected function addIndices(&$script);
|
||||
|
||||
/**
|
||||
* Adds foreign key constraint definitions.
|
||||
* @param string &$script The script will be modified in this method.
|
||||
*/
|
||||
abstract protected function addForeignKeys(&$script);
|
||||
|
||||
}
|
|
@ -0,0 +1,253 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/DataModelBuilder.php';
|
||||
require_once 'model/PropelTypes.php';
|
||||
|
||||
/**
|
||||
* Baseclass for SQL data dump SQL building classes.
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql
|
||||
*/
|
||||
abstract class DataSQLBuilder extends DataModelBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* Perform any reset between runs of this builder.
|
||||
*
|
||||
* This can be used, for example, to clear any stored start/end SQL.
|
||||
*/
|
||||
public static function reset()
|
||||
{
|
||||
// does nothing by default
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets any SQL to place at the start of all the row inserts.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDatabaseStartSql()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets any SQL to place at the end of all the row inserts.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDatabaseEndSql()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets any SQL to place before row inserts for a new table.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTableStartSql()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets any SQL to place at the end of row inserts for a table.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTableEndSql()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* The main method in this class, returns the SQL for INSERTing data into a row.
|
||||
* @param DataRow $row The row to process.
|
||||
* @return string
|
||||
*/
|
||||
public function buildRowSql(DataRow $row)
|
||||
{
|
||||
$sql = "";
|
||||
$platform = $this->getPlatform();
|
||||
$table = $this->getTable();
|
||||
|
||||
$sql .= "INSERT INTO ".$this->quoteIdentifier($this->getTable()->getName())." (";
|
||||
|
||||
// add column names to SQL
|
||||
$colNames = array();
|
||||
foreach ($row->getColumnValues() as $colValue) {
|
||||
$colNames[] = $this->quoteIdentifier($colValue->getColumn()->getName());
|
||||
}
|
||||
|
||||
$sql .= implode(',', $colNames);
|
||||
|
||||
$sql .= ") VALUES (";
|
||||
|
||||
$colVals = array();
|
||||
foreach ($row->getColumnValues() as $colValue) {
|
||||
$colVals[] = $this->getColumnValueSql($colValue);
|
||||
}
|
||||
|
||||
$sql .= implode(',', $colVals);
|
||||
$sql .= ");
|
||||
";
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the propertly escaped (and quoted) value for a column.
|
||||
* @param ColumnValue $colValue
|
||||
* @return mixed The proper value to be added to the string.
|
||||
*/
|
||||
protected function getColumnValueSql(ColumnValue $colValue)
|
||||
{
|
||||
$column = $colValue->getColumn();
|
||||
$method = 'get' . $column->getPhpNative() . 'Sql';
|
||||
return $this->$method($colValue->getValue());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets a representation of a binary value suitable for use in a SQL statement.
|
||||
* Default behavior is true = 1, false = 0.
|
||||
* @param boolean $value
|
||||
* @return int
|
||||
*/
|
||||
protected function getBooleanSql($value)
|
||||
{
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a representation of a BLOB/LONGVARBINARY value suitable for use in a SQL statement.
|
||||
* @param mixed $blob Blob object or string data.
|
||||
* @return string
|
||||
*/
|
||||
protected function getBlobSql($blob)
|
||||
{
|
||||
// they took magic __toString() out of PHP5.0.0; this sucks
|
||||
if (is_object($blob)) {
|
||||
return $this->getPlatform()->quote($blob->__toString());
|
||||
} else {
|
||||
return $this->getPlatform()->quote($blob);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a CLOB/LONGVARCHAR value suitable for use in a SQL statement.
|
||||
* @param mixed $clob Clob object or string data.
|
||||
* @return string
|
||||
*/
|
||||
protected function getClobSql($clob)
|
||||
{
|
||||
// they took magic __toString() out of PHP5.0.0; this sucks
|
||||
if (is_object($clob)) {
|
||||
return $this->getPlatform()->quote($clob->__toString());
|
||||
} else {
|
||||
return $this->getPlatform()->quote($clob);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a date value suitable for use in a SQL statement.
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
protected function getDateSql($value)
|
||||
{
|
||||
return "'" . date('Y-m-d', strtotime($value)) . "'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a decimal value suitable for use in a SQL statement.
|
||||
* @param double $value
|
||||
* @return float
|
||||
*/
|
||||
protected function getDecimalSql($value)
|
||||
{
|
||||
return (float) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a double value suitable for use in a SQL statement.
|
||||
* @param double $value
|
||||
* @return double
|
||||
*/
|
||||
protected function getDoubleSql($value)
|
||||
{
|
||||
return (double) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a float value suitable for use in a SQL statement.
|
||||
* @param float $value
|
||||
* @return float
|
||||
*/
|
||||
protected function getFloatSql($value)
|
||||
{
|
||||
return (float) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of an integer value suitable for use in a SQL statement.
|
||||
* @param int $value
|
||||
* @return int
|
||||
*/
|
||||
protected function getIntSql($value)
|
||||
{
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a NULL value suitable for use in a SQL statement.
|
||||
* @return null
|
||||
*/
|
||||
protected function getNullSql()
|
||||
{
|
||||
return 'NULL';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a string value suitable for use in a SQL statement.
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
protected function getStringSql($value)
|
||||
{
|
||||
return $this->getPlatform()->quote($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a time value suitable for use in a SQL statement.
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
protected function getTimeSql($paramIndex, $value)
|
||||
{
|
||||
return "'" . date('H:i:s', strtotime($value)) . "'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a representation of a timestamp value suitable for use in a SQL statement.
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
function getTimestampSql($value)
|
||||
{
|
||||
return "'" . date('Y-m-d H:i:s', strtotime($value)) . "'";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,173 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DDLBuilder.php';
|
||||
|
||||
/**
|
||||
* The SQL DDL-building class for MS SQL Server.
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.pgsql
|
||||
*/
|
||||
class MssqlDDLBuilder extends DDLBuilder
|
||||
{
|
||||
|
||||
private static $dropCount = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addDropStatement()
|
||||
*/
|
||||
protected function addDropStatements(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($table->getForeignKeys() as $fk) {
|
||||
$script .= "
|
||||
IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='".$fk->getName()."')
|
||||
ALTER TABLE ".$this->quoteIdentifier($table->getName())." DROP CONSTRAINT ".$this->quoteIdentifier($fk->getName()).";
|
||||
";
|
||||
}
|
||||
|
||||
|
||||
self::$dropCount++;
|
||||
|
||||
$script .= "
|
||||
IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = '".$table->getName()."')
|
||||
BEGIN
|
||||
DECLARE @reftable_".self::$dropCount." nvarchar(60), @constraintname_".self::$dropCount." nvarchar(60)
|
||||
DECLARE refcursor CURSOR FOR
|
||||
select reftables.name tablename, cons.name constraintname
|
||||
from sysobjects tables,
|
||||
sysobjects reftables,
|
||||
sysobjects cons,
|
||||
sysreferences ref
|
||||
where tables.id = ref.rkeyid
|
||||
and cons.id = ref.constid
|
||||
and reftables.id = ref.fkeyid
|
||||
and tables.name = '".$table->getName()."'
|
||||
OPEN refcursor
|
||||
FETCH NEXT from refcursor into @reftable_".self::$dropCount.", @constraintname_".self::$dropCount."
|
||||
while @@FETCH_STATUS = 0
|
||||
BEGIN
|
||||
exec ('alter table '+@reftable_".self::$dropCount."+' drop constraint '+@constraintname_".self::$dropCount.")
|
||||
FETCH NEXT from refcursor into @reftable_".self::$dropCount.", @constraintname_".self::$dropCount."
|
||||
END
|
||||
CLOSE refcursor
|
||||
DEALLOCATE refcursor
|
||||
DROP TABLE ".$this->quoteIdentifier($table->getName())."
|
||||
END
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* @see parent::addColumns()
|
||||
*/
|
||||
protected function addTable(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
$script .= "
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* ".$table->getName()." */
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
";
|
||||
|
||||
$this->addDropStatements($script);
|
||||
|
||||
$script .= "
|
||||
|
||||
CREATE TABLE ".$this->quoteIdentifier($table->getName())."
|
||||
(
|
||||
";
|
||||
|
||||
$lines = array();
|
||||
|
||||
foreach ($table->getColumns() as $col) {
|
||||
$lines[] = $this->getColumnDDL($col);
|
||||
}
|
||||
|
||||
if ($table->hasPrimaryKey()) {
|
||||
$lines[] = "CONSTRAINT ".$this->quoteIdentifier($table->getName()."_PK") . " PRIMARY KEY (".$this->getColumnList($table->getPrimaryKey()).")";
|
||||
}
|
||||
|
||||
foreach ($table->getUnices() as $unique ) {
|
||||
$lines[] = "UNIQUE (".$this->getColumnList($unique->getColumns()).")";
|
||||
}
|
||||
|
||||
$sep = ",
|
||||
";
|
||||
$script .= implode($sep, $lines);
|
||||
$script .= "
|
||||
);
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CREATE INDEX statements for this table.
|
||||
* @see parent::addIndices()
|
||||
*/
|
||||
protected function addIndices(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($table->getIndices() as $index) {
|
||||
$script .= "
|
||||
CREATE ";
|
||||
if ($index->getIsUnique()) {
|
||||
$script .= "UNIQUE";
|
||||
}
|
||||
$script .= "INDEX ".$this->quoteIdentifier($index->getName())." ON ".$this->quoteIdentifier($table->getName())." (".$this->getColumnList($index->getColumns()).");
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addForeignKeys()
|
||||
*/
|
||||
protected function addForeignKeys(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($table->getForeignKeys() as $fk) {
|
||||
$script .= "
|
||||
BEGIN
|
||||
ALTER TABLE ".$this->quoteIdentifier($table->getName())." ADD CONSTRAINT ".$this->quoteIdentifier($fk->getName())." FOREIGN KEY (".$this->getColumnList($fk->getLocalColumns()) .") REFERENCES ".$this->quoteIdentifier($fk->getForeignTableName())." (".$this->getColumnList($fk->getForeignColumns()).")";
|
||||
if ($fk->hasOnUpdate()) {
|
||||
if ($fk->getOnUpdate() == ForeignKey::SETNULL) { // there may be others that also won't work
|
||||
// we have to skip this because it's unsupported.
|
||||
$this->warn("MSSQL doesn't support the 'SET NULL' option for ON UPDATE (ignoring for ".$this->getColumnList($fk->getLocalColumns())." fk).");
|
||||
} else {
|
||||
$script .= " ON UPDATE ".$fk->getOnUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
if ($fk->hasOnDelete()) {
|
||||
if ($fk->getOnDelete() == ForeignKey::SETNULL) { // there may be others that also won't work
|
||||
// we have to skip this because it's unsupported.
|
||||
$this->warn("MSSQL doesn't support the 'SET NULL' option for ON DELETE (ignoring for ".$this->getColumnList($fk->getLocalColumns())." fk).");
|
||||
} else {
|
||||
$script .= " ON DELETE ".$fk->getOnDelete();
|
||||
}
|
||||
}
|
||||
$script .= "
|
||||
END
|
||||
;
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DataSQLBuilder.php';
|
||||
|
||||
/**
|
||||
* MS SQL Server class for building data dump SQL.
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.mssql
|
||||
*/
|
||||
class MssqlDataSQLBuilder extends DataSQLBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mixed $blob Blob object or string containing data.
|
||||
* @return string
|
||||
*/
|
||||
protected function getBlobSql($blob)
|
||||
{
|
||||
// they took magic __toString() out of PHP5.0.0; this sucks
|
||||
if (is_object($blob)) {
|
||||
$blob = $blob->__toString();
|
||||
}
|
||||
$data = unpack("H*hex", $blob);
|
||||
return '0x'.$data['hex']; // no surrounding quotes!
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,413 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DDLBuilder.php';
|
||||
|
||||
/**
|
||||
* DDL Builder class for MySQL.
|
||||
*
|
||||
* @author David Z<EFBFBD>lke
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.mysql
|
||||
*/
|
||||
class MysqlDDLBuilder extends DDLBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns some header SQL that disables foreign key checking.
|
||||
* @return string DDL
|
||||
*/
|
||||
public static function getDatabaseStartDDL()
|
||||
{
|
||||
$ddl = "
|
||||
# This is a fix for InnoDB in MySQL >= 4.1.x
|
||||
# It \"suspends judgement\" for fkey relationships until are tables are set.
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
";
|
||||
return $ddl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns some footer SQL that re-enables foreign key checking.
|
||||
* @return string DDL
|
||||
*/
|
||||
public static function getDatabaseEndDDL()
|
||||
{
|
||||
$ddl = "
|
||||
# This restores the fkey checks, after having unset them earlier
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
";
|
||||
return $ddl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addDropStatement()
|
||||
*/
|
||||
protected function addDropStatements(&$script)
|
||||
{
|
||||
$script .= "
|
||||
DROP TABLE IF EXISTS ".$this->quoteIdentifier($this->getTable()->getName()).";
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the SQL for current table and returns it as a string.
|
||||
*
|
||||
* This is the main entry point and defines a basic structure that classes should follow.
|
||||
* In most cases this method will not need to be overridden by subclasses.
|
||||
*
|
||||
* @return string The resulting SQL DDL.
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
$script = "";
|
||||
$this->addTable($script);
|
||||
return $script;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addColumns()
|
||||
*/
|
||||
protected function addTable(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
$script .= "
|
||||
#-----------------------------------------------------------------------------
|
||||
#-- ".$table->getName()."
|
||||
#-----------------------------------------------------------------------------
|
||||
";
|
||||
|
||||
$this->addDropStatements($script);
|
||||
|
||||
$script .= "
|
||||
|
||||
CREATE TABLE ".$this->quoteIdentifier($table->getName())."
|
||||
(
|
||||
";
|
||||
|
||||
$lines = array();
|
||||
|
||||
$databaseType = $this->getPlatform()->getDatabaseType();
|
||||
|
||||
foreach ($table->getColumns() as $col) {
|
||||
$entry = $this->getColumnDDL($col);
|
||||
$colinfo = $col->getVendorInfoForType($databaseType);
|
||||
if ( $colinfo->hasParameter('Charset') ) {
|
||||
$entry .= ' CHARACTER SET '.$platform->quote($colinfo->getParameter('Charset'));
|
||||
}
|
||||
if ( $colinfo->hasParameter('Collation') ) {
|
||||
$entry .= ' COLLATE '.$platform->quote($colinfo->getParameter('Collation'));
|
||||
} elseif ( $colinfo->hasParameter('Collate') ) {
|
||||
$entry .= ' COLLATE '.$platform->quote($colinfo->getParameter('Collate'));
|
||||
}
|
||||
if ($col->getDescription()) {
|
||||
$entry .= " COMMENT ".$platform->quote($col->getDescription());
|
||||
}
|
||||
$lines[] = $entry;
|
||||
}
|
||||
|
||||
if ($table->hasPrimaryKey()) {
|
||||
$lines[] = "PRIMARY KEY (".$this->getColumnList($table->getPrimaryKey()).")";
|
||||
}
|
||||
|
||||
$this->addIndicesLines($lines);
|
||||
$this->addForeignKeysLines($lines);
|
||||
|
||||
$sep = ",
|
||||
";
|
||||
$script .= implode($sep, $lines);
|
||||
|
||||
$script .= "
|
||||
)";
|
||||
|
||||
$vendorSpecific = $table->getVendorInfoForType($this->getPlatform()->getDatabaseType());
|
||||
if ($vendorSpecific->hasParameter('Type')) {
|
||||
$mysqlTableType = $vendorSpecific->getParameter('Type');
|
||||
} elseif ($vendorSpecific->hasParameter('Engine')) {
|
||||
$mysqlTableType = $vendorSpecific->getParameter('Engine');
|
||||
} else {
|
||||
$mysqlTableType = $this->getBuildProperty("mysqlTableType");
|
||||
}
|
||||
|
||||
$script .= sprintf(' %s=%s', $this->getBuildProperty("mysqlTableEngineKeyword"), $mysqlTableType);
|
||||
|
||||
$dbVendorSpecific = $table->getDatabase()->getVendorInfoForType($databaseType);
|
||||
$tableVendorSpecific = $table->getVendorInfoForType($databaseType);
|
||||
$vendorSpecific = $dbVendorSpecific->getMergedVendorInfo($tableVendorSpecific);
|
||||
|
||||
if ( $vendorSpecific->hasParameter('Charset') ) {
|
||||
$script .= ' CHARACTER SET '.$platform->quote($vendorSpecific->getParameter('Charset'));
|
||||
}
|
||||
if ( $vendorSpecific->hasParameter('Collate') ) {
|
||||
$script .= ' COLLATE '.$platform->quote($vendorSpecific->getParameter('Collate'));
|
||||
}
|
||||
if ( $vendorSpecific->hasParameter('Checksum') ) {
|
||||
$script .= ' CHECKSUM='.$platform->quote($vendorSpecific->getParameter('Checksum'));
|
||||
}
|
||||
if ( $vendorSpecific->hasParameter('Pack_Keys') ) {
|
||||
$script .= ' PACK_KEYS='.$platform->quote($vendorSpecific->getParameter('Pack_Keys'));
|
||||
}
|
||||
if ( $vendorSpecific->hasParameter('Delay_key_write') ) {
|
||||
$script .= ' DELAY_KEY_WRITE='.$platform->quote($vendorSpecific->getParameter('Delay_key_write'));
|
||||
}
|
||||
|
||||
if ($table->getDescription()) {
|
||||
$script .= " COMMENT=".$platform->quote($table->getDescription());
|
||||
}
|
||||
$script .= ";
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comma-separated list of column names for the index.
|
||||
* For MySQL unique indexes there is the option of specifying size, so we cannot simply use
|
||||
* the getColumnsList() method.
|
||||
* @param Index $index
|
||||
* @return string
|
||||
*/
|
||||
private function getIndexColumnList(Index $index)
|
||||
{
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
$cols = $index->getColumns();
|
||||
$list = array();
|
||||
foreach ($cols as $col) {
|
||||
$list[] = $this->quoteIdentifier($col) . ($index->hasColumnSize($col) ? '(' . $index->getColumnSize($col) . ')' : '');
|
||||
}
|
||||
return implode(', ', $list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds indexes
|
||||
*/
|
||||
protected function addIndicesLines(&$lines)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($table->getUnices() as $unique) {
|
||||
$lines[] = "UNIQUE KEY ".$this->quoteIdentifier($unique->getName())." (".$this->getIndexColumnList($unique).")";
|
||||
}
|
||||
|
||||
foreach ($table->getIndices() as $index ) {
|
||||
$vendorInfo = $index->getVendorInfoForType($platform->getDatabaseType());
|
||||
$lines[] .= (($vendorInfo && $vendorInfo->getParameter('Index_type') == 'FULLTEXT') ? 'FULLTEXT ' : '') . "KEY " . $this->quoteIdentifier($index->getName()) . "(" . $this->getIndexColumnList($index) . ")";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds foreign key declarations & necessary indexes for mysql (if they don't exist already).
|
||||
* @see parent::addForeignKeys()
|
||||
*/
|
||||
protected function addForeignKeysLines(&$lines)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
|
||||
/**
|
||||
* A collection of indexed columns. The keys is the column name
|
||||
* (concatenated with a comma in the case of multi-col index), the value is
|
||||
* an array with the names of the indexes that index these columns. We use
|
||||
* it to determine which additional indexes must be created for foreign
|
||||
* keys. It could also be used to detect duplicate indexes, but this is not
|
||||
* implemented yet.
|
||||
* @var array
|
||||
*/
|
||||
$_indices = array();
|
||||
|
||||
$this->collectIndexedColumns('PRIMARY', $table->getPrimaryKey(), $_indices, 'getName');
|
||||
|
||||
$_tableIndices = array_merge($table->getIndices(), $table->getUnices());
|
||||
foreach ($_tableIndices as $_index) {
|
||||
$this->collectIndexedColumns($_index->getName(), $_index->getColumns(), $_indices);
|
||||
}
|
||||
|
||||
// we're determining which tables have foreign keys that point to this table, since MySQL needs an index on
|
||||
// any column that is referenced by another table (yep, MySQL _is_ a PITA)
|
||||
$counter = 0;
|
||||
$allTables = $table->getDatabase()->getTables();
|
||||
foreach ($allTables as $_table) {
|
||||
foreach ($_table->getForeignKeys() as $_foreignKey) {
|
||||
if ($_foreignKey->getForeignTableName() == $table->getName()) {
|
||||
$referencedColumns = $_foreignKey->getForeignColumns();
|
||||
$referencedColumnsHash = $this->getColumnList($referencedColumns);
|
||||
if (!array_key_exists($referencedColumnsHash, $_indices)) {
|
||||
// no matching index defined in the schema, so we have to create one
|
||||
$indexName = "I_referenced_".$_foreignKey->getName()."_".(++$counter);
|
||||
$lines[] = "INDEX ".$this->quoteIdentifier($indexName)." (" .$referencedColumnsHash.")";
|
||||
// Add this new index to our collection, otherwise we might add it again (bug #725)
|
||||
$this->collectIndexedColumns($indexName, $referencedColumns, $_indices);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($table->getForeignKeys() as $fk) {
|
||||
|
||||
$indexName = $this->quoteIdentifier(substr_replace($fk->getName(), 'FI_', strrpos($fk->getName(), 'FK_'), 3));
|
||||
|
||||
$localColumns = $fk->getLocalColumns();
|
||||
$localColumnsHash = $this->getColumnList($localColumns);
|
||||
|
||||
if (!array_key_exists($localColumnsHash, $_indices)) {
|
||||
// no matching index defined in the schema, so we have to create one. MySQL needs indices on any columns that serve as foreign keys. these are not auto-created prior to 4.1.2
|
||||
$lines[] = "INDEX $indexName (".$localColumnsHash.")";
|
||||
$this->collectIndexedColumns($indexName, $localColumns, $_indices);
|
||||
}
|
||||
$str = "CONSTRAINT ".$this->quoteIdentifier($fk->getName())."
|
||||
FOREIGN KEY (".$this->getColumnList($fk->getLocalColumns()).")
|
||||
REFERENCES ".$this->quoteIdentifier($fk->getForeignTableName()) . " (".$this->getColumnList($fk->getForeignColumns()).")";
|
||||
if ($fk->hasOnUpdate()) {
|
||||
$str .= "
|
||||
ON UPDATE ".$fk->getOnUpdate();
|
||||
}
|
||||
if ($fk->hasOnDelete()) {
|
||||
$str .= "
|
||||
ON DELETE ".$fk->getOnDelete();
|
||||
}
|
||||
$lines[] = $str;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to collect indexed columns.
|
||||
* @param array $columns The column names, or objects with a $callback method
|
||||
* @param array $indexedColumns The collected indexes
|
||||
* @param string $callback The name of a method to call on each of $columns to get the column name, if needed.
|
||||
* @return unknown_type
|
||||
*/
|
||||
private function collectIndexedColumns($indexName, $columns, &$collectedIndexes, $callback = null)
|
||||
{
|
||||
// Get the actual column names, using the callback if needed.
|
||||
// DDLBuilder::getColumnList tests $col instanceof Column, and no callback - maybe we should too?
|
||||
$colnames = $columns;
|
||||
if ($callback) {
|
||||
$colnames = array();
|
||||
foreach ($columns as $col) {
|
||||
$colnames[] = $col->$callback();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* "If the table has a multiple-column index, any leftmost prefix of the
|
||||
* index can be used by the optimizer to find rows. For example, if you
|
||||
* have a three-column index on (col1, col2, col3), you have indexed search
|
||||
* capabilities on (col1), (col1, col2), and (col1, col2, col3)."
|
||||
* @link http://dev.mysql.com/doc/refman/5.5/en/mysql-indexes.html
|
||||
*/
|
||||
$indexedColumns = array();
|
||||
foreach ($colnames as $colname) {
|
||||
$indexedColumns[] = $this->quoteIdentifier($colname);
|
||||
$indexedColumnsHash = implode(',', $indexedColumns);
|
||||
if (!array_key_exists($indexedColumnsHash, $collectedIndexes)) {
|
||||
$collectedIndexes[$indexedColumnsHash] = array();
|
||||
}
|
||||
$collectedIndexes[$indexedColumnsHash][] = $indexName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether passed-in array of Column objects contains a column with specified name.
|
||||
* @param array Column[] or string[]
|
||||
* @param string $searchcol Column name to search for
|
||||
*/
|
||||
private function containsColname($columns, $searchcol)
|
||||
{
|
||||
foreach ($columns as $col) {
|
||||
if ($col instanceof Column) {
|
||||
$col = $col->getName();
|
||||
}
|
||||
if ($col == $searchcol) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Not used for MySQL since foreign keys are declared inside table declaration.
|
||||
* @see addForeignKeysLines()
|
||||
*/
|
||||
protected function addForeignKeys(&$script)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Not used for MySQL since indexes are declared inside table declaration.
|
||||
* @see addIndicesLines()
|
||||
*/
|
||||
protected function addIndices(&$script)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the DDL SQL for a Column object.
|
||||
* @return string
|
||||
*/
|
||||
public function getColumnDDL(Column $col)
|
||||
{
|
||||
$platform = $this->getPlatform();
|
||||
$domain = $col->getDomain();
|
||||
$sqlType = $domain->getSqlType();
|
||||
$notNullString = $col->getNotNullString();
|
||||
$defaultSetting = $col->getDefaultSetting();
|
||||
|
||||
// Special handling of TIMESTAMP/DATETIME types ...
|
||||
// See: http://propel.phpdb.org/trac/ticket/538
|
||||
if ($sqlType == 'DATETIME') {
|
||||
$def = $domain->getDefaultValue();
|
||||
if ($def && $def->isExpression()) { // DATETIME values can only have constant expressions
|
||||
$sqlType = 'TIMESTAMP';
|
||||
}
|
||||
} elseif ($sqlType == 'DATE') {
|
||||
$def = $domain->getDefaultValue();
|
||||
if ($def && $def->isExpression()) {
|
||||
throw new EngineException("DATE columns cannot have default *expressions* in MySQL.");
|
||||
}
|
||||
} elseif ($sqlType == 'TEXT' || $sqlType == 'BLOB') {
|
||||
if ($domain->getDefaultValue()) {
|
||||
throw new EngineException("BLOB and TEXT columns cannot have DEFAULT values. in MySQL.");
|
||||
}
|
||||
}
|
||||
|
||||
$sb = "";
|
||||
$sb .= $this->quoteIdentifier($col->getName()) . " ";
|
||||
$sb .= $sqlType;
|
||||
if ($platform->hasSize($sqlType)) {
|
||||
$sb .= $domain->printSize();
|
||||
}
|
||||
$sb .= " ";
|
||||
|
||||
if ($sqlType == 'TIMESTAMP') {
|
||||
$notNullString = $col->getNotNullString();
|
||||
$defaultSetting = $col->getDefaultSetting();
|
||||
if ($notNullString == '') {
|
||||
$notNullString = 'NULL';
|
||||
}
|
||||
if ($defaultSetting == '' && $notNullString == 'NOT NULL') {
|
||||
$defaultSetting = 'DEFAULT CURRENT_TIMESTAMP';
|
||||
}
|
||||
$sb .= $notNullString . " " . $defaultSetting . " ";
|
||||
} else {
|
||||
$sb .= $defaultSetting . " ";
|
||||
$sb .= $notNullString . " ";
|
||||
}
|
||||
$sb .= $col->getAutoIncrementString();
|
||||
|
||||
return trim($sb);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DataSQLBuilder.php';
|
||||
|
||||
/**
|
||||
* MySQL class for building data dump SQL.
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.mysql
|
||||
*/
|
||||
class MysqlDataSQLBuilder extends DataSQLBuilder
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DDLBuilder.php';
|
||||
|
||||
/**
|
||||
* The SQL DDL-building class for Oracle.
|
||||
*
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.pgsql
|
||||
*/
|
||||
class OracleDDLBuilder extends DDLBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* This function adds any _database_ start/initialization SQL.
|
||||
* This is designed to be called for a database, not a specific table, hence it is static.
|
||||
* @see parent::getDatabaseStartDDL()
|
||||
*
|
||||
* @return string The DDL is returned as astring.
|
||||
*/
|
||||
public static function getDatabaseStartDDL()
|
||||
{
|
||||
return "
|
||||
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD';
|
||||
ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS';
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addDropStatement()
|
||||
*/
|
||||
protected function addDropStatements(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
$script .= "
|
||||
DROP TABLE ".$this->quoteIdentifier($table->getName())." CASCADE CONSTRAINTS;
|
||||
";
|
||||
if ($table->getIdMethod() == "native") {
|
||||
$script .= "
|
||||
DROP SEQUENCE ".$this->quoteIdentifier($this->getSequenceName()).";
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addColumns()
|
||||
*/
|
||||
protected function addTable(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$script .= "
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- ".$table->getName()."
|
||||
-----------------------------------------------------------------------
|
||||
";
|
||||
|
||||
$this->addDropStatements($script);
|
||||
|
||||
$script .= "
|
||||
CREATE TABLE ".$this->quoteIdentifier($table->getName())."
|
||||
(
|
||||
";
|
||||
|
||||
$lines = array();
|
||||
|
||||
foreach ($table->getColumns() as $col) {
|
||||
$lines[] = $this->getColumnDDL($col);
|
||||
}
|
||||
|
||||
$sep = ",
|
||||
";
|
||||
$script .= implode($sep, $lines);
|
||||
$script .= "
|
||||
);
|
||||
";
|
||||
$this->addPrimaryKey($script);
|
||||
$this->addSequences($script);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
protected function addPrimaryKey(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
$tableName = $table->getName();
|
||||
$length = strlen($tableName);
|
||||
if ($length > 27) {
|
||||
$length = 27;
|
||||
}
|
||||
if ( is_array($table->getPrimaryKey()) && count($table->getPrimaryKey()) ) {
|
||||
$script .= "
|
||||
ALTER TABLE ".$this->quoteIdentifier($table->getName())."
|
||||
ADD CONSTRAINT ".$this->quoteIdentifier(substr($tableName,0,$length)."_PK")."
|
||||
PRIMARY KEY (";
|
||||
$delim = "";
|
||||
foreach ($table->getPrimaryKey() as $col) {
|
||||
$script .= $delim . $this->quoteIdentifier($col->getName());
|
||||
$delim = ",";
|
||||
}
|
||||
$script .= ");
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CREATE SEQUENCE statements for this table.
|
||||
*
|
||||
*/
|
||||
protected function addSequences(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
if ($table->getIdMethod() == "native") {
|
||||
$script .= "
|
||||
CREATE SEQUENCE ".$this->quoteIdentifier($this->getSequenceName())."
|
||||
INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE ORDER;
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds CREATE INDEX statements for this table.
|
||||
* @see parent::addIndices()
|
||||
*/
|
||||
protected function addIndices(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
foreach ($table->getIndices() as $index) {
|
||||
$script .= "
|
||||
CREATE ";
|
||||
if ($index->getIsUnique()) {
|
||||
$script .= "UNIQUE";
|
||||
}
|
||||
$script .= "INDEX ".$this->quoteIdentifier($index->getName()) ." ON ".$this->quoteIdentifier($table->getName())." (".$this->getColumnList($index->getColumns()).");
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addForeignKeys()
|
||||
*/
|
||||
protected function addForeignKeys(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
foreach ($table->getForeignKeys() as $fk) {
|
||||
$script .= "
|
||||
ALTER TABLE ".$this->quoteIdentifier($table->getName())."
|
||||
ADD CONSTRAINT ".$this->quoteIdentifier($fk->getName())."
|
||||
FOREIGN KEY (".$this->getColumnList($fk->getLocalColumns()) .") REFERENCES ".$this->quoteIdentifier($fk->getForeignTableName())." (".$this->getColumnList($fk->getForeignColumns()).")";
|
||||
if ($fk->hasOnUpdate()) {
|
||||
$this->warn("ON UPDATE not yet implemented for Oracle builder.(ignoring for ".$this->getColumnList($fk->getLocalColumns())." fk).");
|
||||
//$script .= " ON UPDATE ".$fk->getOnUpdate();
|
||||
}
|
||||
if ($fk->hasOnDelete()) {
|
||||
$script .= "
|
||||
ON DELETE ".$fk->getOnDelete();
|
||||
}
|
||||
$script .= ";
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DataSQLBuilder.php';
|
||||
|
||||
/**
|
||||
* Oracle class for building data dump SQL.
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.oracle
|
||||
*/
|
||||
class OracleDataSQLBuilder extends DataSQLBuilder
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,304 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DDLBuilder.php';
|
||||
|
||||
/**
|
||||
* The SQL DDL-building class for PostgreSQL.
|
||||
*
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.pgsql
|
||||
*/
|
||||
class PgsqlDDLBuilder extends DDLBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* Array that keeps track of already
|
||||
* added schema names
|
||||
*
|
||||
* @var Array of schema names
|
||||
*/
|
||||
protected static $addedSchemas = array();
|
||||
|
||||
/**
|
||||
* Queue of constraint SQL that will be added to script at the end.
|
||||
*
|
||||
* PostgreSQL seems (now?) to not like constraints for tables that don't exist,
|
||||
* so the solution is to queue up the statements and execute it at the end.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $queuedConstraints = array();
|
||||
|
||||
/**
|
||||
* Reset static vars between db iterations.
|
||||
*/
|
||||
public static function reset()
|
||||
{
|
||||
self::$addedSchemas = array();
|
||||
self::$queuedConstraints = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the ALTER TABLE ADD CONSTRAINT lines for inclusion at end of file.
|
||||
* @return string DDL
|
||||
*/
|
||||
public static function getDatabaseEndDDL()
|
||||
{
|
||||
$ddl = implode("", self::$queuedConstraints);
|
||||
return $ddl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schema for the current table
|
||||
*
|
||||
* @author Markus Lervik <markus.lervik@necora.fi>
|
||||
* @access protected
|
||||
* @return schema name if table has one, else
|
||||
* null
|
||||
**/
|
||||
protected function getSchema()
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$vi = $table->getVendorInfoForType($this->getPlatform()->getDatabaseType());
|
||||
if ($vi->hasParameter('schema')) {
|
||||
return $vi->getParameter('schema');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a schema to the generated SQL script
|
||||
*
|
||||
* @author Markus Lervik <markus.lervik@necora.fi>
|
||||
* @access protected
|
||||
* @return string with CREATE SCHEMA statement if
|
||||
* applicable, else empty string
|
||||
**/
|
||||
protected function addSchema()
|
||||
{
|
||||
|
||||
$schemaName = $this->getSchema();
|
||||
|
||||
if ($schemaName !== null) {
|
||||
|
||||
if (!in_array($schemaName, self::$addedSchemas)) {
|
||||
$platform = $this->getPlatform();
|
||||
self::$addedSchemas[] = $schemaName;
|
||||
return "\nCREATE SCHEMA " . $this->quoteIdentifier($schemaName) . ";\n";
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addDropStatement()
|
||||
*/
|
||||
protected function addDropStatements(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
$script .= "
|
||||
DROP TABLE ".$this->quoteIdentifier($table->getName())." CASCADE;
|
||||
";
|
||||
|
||||
if ($table->getIdMethod() == IDMethod::NATIVE && $table->getIdMethodParameters()) {
|
||||
$script .= "
|
||||
DROP SEQUENCE ".$this->quoteIdentifier(strtolower($this->getSequenceName())).";
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addColumns()
|
||||
*/
|
||||
protected function addTable(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
$script .= "
|
||||
-----------------------------------------------------------------------------
|
||||
-- ".$table->getName()."
|
||||
-----------------------------------------------------------------------------
|
||||
";
|
||||
|
||||
$script .= $this->addSchema();
|
||||
|
||||
$schemaName = $this->getSchema();
|
||||
if ($schemaName !== null) {
|
||||
$script .= "\nSET search_path TO " . $this->quoteIdentifier($schemaName) . ";\n";
|
||||
}
|
||||
|
||||
$this->addDropStatements($script);
|
||||
$this->addSequences($script);
|
||||
|
||||
$script .= "
|
||||
|
||||
CREATE TABLE ".$this->quoteIdentifier($table->getName())."
|
||||
(
|
||||
";
|
||||
|
||||
$lines = array();
|
||||
|
||||
foreach ($table->getColumns() as $col) {
|
||||
/* @var $col Column */
|
||||
$colDDL = $this->getColumnDDL($col);
|
||||
if ($col->isAutoIncrement() && $table->getIdMethodParameters() == null) {
|
||||
if ($col->getType() === PropelTypes::BIGINT) {
|
||||
$colDDL = str_replace($col->getDomain()->getSqlType(), 'bigserial', $colDDL);
|
||||
} else {
|
||||
$colDDL = str_replace($col->getDomain()->getSqlType(), 'serial', $colDDL);
|
||||
}
|
||||
}
|
||||
$lines[] = $colDDL;
|
||||
}
|
||||
|
||||
if ($table->hasPrimaryKey()) {
|
||||
$lines[] = "PRIMARY KEY (".$this->getColumnList($table->getPrimaryKey()).")";
|
||||
}
|
||||
|
||||
foreach ($table->getUnices() as $unique ) {
|
||||
$lines[] = "CONSTRAINT ".$this->quoteIdentifier($unique->getName())." UNIQUE (".$this->getColumnList($unique->getColumns()).")";
|
||||
}
|
||||
|
||||
$sep = ",
|
||||
";
|
||||
$script .= implode($sep, $lines);
|
||||
$script .= "
|
||||
);
|
||||
|
||||
COMMENT ON TABLE ".$this->quoteIdentifier($table->getName())." IS " . $platform->quote($table->getDescription()).";
|
||||
|
||||
";
|
||||
|
||||
$this->addColumnComments($script);
|
||||
|
||||
$script .= "\nSET search_path TO public;";
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds comments for the columns.
|
||||
*
|
||||
*/
|
||||
protected function addColumnComments(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($this->getTable()->getColumns() as $col) {
|
||||
if ( $col->getDescription() != '' ) {
|
||||
$script .= "
|
||||
COMMENT ON COLUMN ".$this->quoteIdentifier($table->getName()).".".$this->quoteIdentifier($col->getName())." IS ".$platform->quote($col->getDescription()) .";
|
||||
";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to provide sequence names that conform to postgres' standard when
|
||||
* no id-method-parameter specified.
|
||||
*
|
||||
* @see DataModelBuilder::getSequenceName()
|
||||
* @return string
|
||||
*/
|
||||
public function getSequenceName()
|
||||
{
|
||||
$table = $this->getTable();
|
||||
static $longNamesMap = array();
|
||||
$result = null;
|
||||
if ($table->getIdMethod() == IDMethod::NATIVE) {
|
||||
$idMethodParams = $table->getIdMethodParameters();
|
||||
if (empty($idMethodParams)) {
|
||||
$result = null;
|
||||
// We're going to ignore a check for max length (mainly
|
||||
// because I'm not sure how Postgres would handle this w/ SERIAL anyway)
|
||||
foreach ($table->getColumns() as $col) {
|
||||
if ($col->isAutoIncrement()) {
|
||||
$result = $table->getName() . '_' . $col->getName() . '_seq';
|
||||
break; // there's only one auto-increment column allowed
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$result = $idMethodParams[0]->getValue();
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CREATE SEQUENCE statements for this table.
|
||||
*
|
||||
*/
|
||||
protected function addSequences(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
if ($table->getIdMethod() == IDMethod::NATIVE && $table->getIdMethodParameters() != null) {
|
||||
$script .= "
|
||||
CREATE SEQUENCE ".$this->quoteIdentifier(strtolower($this->getSequenceName())).";
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds CREATE INDEX statements for this table.
|
||||
* @see parent::addIndices()
|
||||
*/
|
||||
protected function addIndices(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($table->getIndices() as $index) {
|
||||
$script .= "
|
||||
CREATE ";
|
||||
if ($index->getIsUnique()) {
|
||||
$script .= "UNIQUE";
|
||||
}
|
||||
$script .= "INDEX ".$this->quoteIdentifier($index->getName())." ON ".$this->quoteIdentifier($table->getName())." (".$this->getColumnList($index->getColumns()).");
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addForeignKeys()
|
||||
*/
|
||||
protected function addForeignKeys(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($table->getForeignKeys() as $fk) {
|
||||
$privscript = "
|
||||
ALTER TABLE ".$this->quoteIdentifier($table->getName())." ADD CONSTRAINT ".$this->quoteIdentifier($fk->getName())." FOREIGN KEY (".$this->getColumnList($fk->getLocalColumns()) .") REFERENCES ".$this->quoteIdentifier($fk->getForeignTableName())." (".$this->getColumnList($fk->getForeignColumns()).")";
|
||||
if ($fk->hasOnUpdate()) {
|
||||
$privscript .= " ON UPDATE ".$fk->getOnUpdate();
|
||||
}
|
||||
if ($fk->hasOnDelete()) {
|
||||
$privscript .= " ON DELETE ".$fk->getOnDelete();
|
||||
}
|
||||
$privscript .= ";
|
||||
";
|
||||
self::$queuedConstraints[] = $privscript;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DataSQLBuilder.php';
|
||||
|
||||
/**
|
||||
* PostgreSQL class for building data dump SQL.
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.pgsql
|
||||
*/
|
||||
class PgsqlDataSQLBuilder extends DataSQLBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* The largets serial value encountered this far.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $maxSeqVal;
|
||||
|
||||
/**
|
||||
* Construct a new PgsqlDataSQLBuilder object.
|
||||
*
|
||||
* @param Table $table
|
||||
*/
|
||||
public function __construct(Table $table)
|
||||
{
|
||||
parent::__construct($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* The main method in this class, returns the SQL for INSERTing data into a row.
|
||||
* @param DataRow $row The row to process.
|
||||
* @return string
|
||||
*/
|
||||
public function buildRowSql(DataRow $row)
|
||||
{
|
||||
$sql = parent::buildRowSql($row);
|
||||
|
||||
$table = $this->getTable();
|
||||
|
||||
if ($table->hasAutoIncrementPrimaryKey() && $table->getIdMethod() == IDMethod::NATIVE) {
|
||||
foreach ($row->getColumnValues() as $colValue) {
|
||||
if ($colValue->getColumn()->isAutoIncrement()) {
|
||||
if ($colValue->getValue() > $this->maxSeqVal) {
|
||||
$this->maxSeqVal = $colValue->getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function getTableEndSql()
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$sql = "";
|
||||
if ($table->hasAutoIncrementPrimaryKey() && $table->getIdMethod() == IDMethod::NATIVE) {
|
||||
$seqname = $this->getDDLBuilder()->getSequenceName();
|
||||
$sql .= "SELECT pg_catalog.setval('$seqname', ".((int)$this->maxSeqVal).");
|
||||
";
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SQL value to insert for Postgres BOOLEAN column.
|
||||
* @param boolean $value
|
||||
* @return string The representation of boolean for Postgres ('t' or 'f').
|
||||
*/
|
||||
protected function getBooleanSql($value)
|
||||
{
|
||||
if ($value === 'f' || $value === 'false' || $value === "0") {
|
||||
$value = false;
|
||||
}
|
||||
return ($value ? "'t'" : "'f'");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mixed $blob Blob object or string containing data.
|
||||
* @return string
|
||||
*/
|
||||
protected function getBlobSql($blob)
|
||||
{
|
||||
// they took magic __toString() out of PHP5.0.0; this sucks
|
||||
if (is_object($blob)) {
|
||||
$blob = $blob->__toString();
|
||||
}
|
||||
return "'" . pg_escape_bytea($blob) . "'";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DDLBuilder.php';
|
||||
|
||||
/**
|
||||
* The SQL DDL-building class for SQLite.
|
||||
*
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.pgsql
|
||||
*/
|
||||
class SqliteDDLBuilder extends DDLBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addDropStatement()
|
||||
*/
|
||||
protected function addDropStatements(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
$script .= "
|
||||
DROP TABLE ".$this->quoteIdentifier($table->getName()).";
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addColumns()
|
||||
*/
|
||||
protected function addTable(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
$script .= "
|
||||
-----------------------------------------------------------------------------
|
||||
-- ".$table->getName()."
|
||||
-----------------------------------------------------------------------------
|
||||
";
|
||||
|
||||
$this->addDropStatements($script);
|
||||
|
||||
$script .= "
|
||||
|
||||
CREATE TABLE ".$this->quoteIdentifier($table->getName())."
|
||||
(
|
||||
";
|
||||
|
||||
$lines = array();
|
||||
|
||||
foreach ($table->getColumns() as $col) {
|
||||
$lines[] = $this->getColumnDDL($col);
|
||||
}
|
||||
|
||||
if ($table->hasPrimaryKey() && count($table->getPrimaryKey()) > 1) {
|
||||
$lines[] = "PRIMARY KEY (".$this->getColumnList($table->getPrimaryKey()).")";
|
||||
}
|
||||
|
||||
foreach ($table->getUnices() as $unique ) {
|
||||
$lines[] = "UNIQUE (".$this->getColumnList($unique->getColumns()).")";
|
||||
}
|
||||
|
||||
$sep = ",
|
||||
";
|
||||
$script .= implode($sep, $lines);
|
||||
$script .= "
|
||||
);
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CREATE INDEX statements for this table.
|
||||
* @see parent::addIndices()
|
||||
*/
|
||||
protected function addIndices(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($table->getIndices() as $index) {
|
||||
$script .= "
|
||||
CREATE ";
|
||||
if ($index->getIsUnique()) {
|
||||
$script .= "UNIQUE";
|
||||
}
|
||||
$script .= "INDEX ".$this->quoteIdentifier($index->getName())." ON ".$this->quoteIdentifier($table->getName())." (".$this->getColumnList($index->getColumns()).");
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see parent::addForeignKeys()
|
||||
*/
|
||||
protected function addForeignKeys(&$script)
|
||||
{
|
||||
$table = $this->getTable();
|
||||
$platform = $this->getPlatform();
|
||||
|
||||
foreach ($table->getForeignKeys() as $fk) {
|
||||
$script .= "
|
||||
-- SQLite does not support foreign keys; this is just for reference
|
||||
-- FOREIGN KEY (".$this->getColumnList($fk->getLocalColumns()).") REFERENCES ".$fk->getForeignTableName()." (".$this->getColumnList($fk->getForeignColumns()).")
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Propel package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
require_once 'builder/sql/DataSQLBuilder.php';
|
||||
|
||||
/**
|
||||
* SQLite class for building data dump SQL.
|
||||
*
|
||||
* @author Hans Lellelid <hans@xmpl.org>
|
||||
* @package propel.generator.builder.sql.sqlite
|
||||
*/
|
||||
class SqliteDataSQLBuilder extends DataSQLBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns string processed by sqlite_udf_encode_binary() to ensure that binary contents will be handled correctly by sqlite.
|
||||
* @param mixed $blob Blob or string
|
||||
* @return string encoded text
|
||||
*/
|
||||
protected function getBlobSql($blob)
|
||||
{
|
||||
// they took magic __toString() out of PHP5.0.0; this sucks
|
||||
if (is_object($blob)) {
|
||||
$blob = $blob->__toString();
|
||||
}
|
||||
return "'" . sqlite_udf_encode_binary($blob) . "'";
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue