[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: PgsqlDDLBuilder.php 1310 2006-05-04 07:36:54Z fabien $ 5 * 6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 17 * 18 * This software consists of voluntary contributions made by many individuals 19 * and is licensed under the LGPL. For more information please see 20 * <http://propel.phpdb.org>. 21 */ 22 23 require_once 'propel/engine/builder/sql/DDLBuilder.php'; 24 25 /** 26 * The SQL DDL-building class for PostgreSQL. 27 * 28 * 29 * @author Hans Lellelid <hans@xmpl.org> 30 * @package propel.engine.builder.sql.pgsql 31 */ 32 class PgsqlDDLBuilder extends DDLBuilder { 33 34 35 /** 36 * Array that keeps track of already 37 * added schema names 38 * 39 * @var Array of schema names 40 */ 41 protected static $addedSchemas = array(); 42 43 /** 44 * Get the schema for the current table 45 * 46 * @author Markus Lervik <markus.lervik@necora.fi> 47 * @access protected 48 * @return schema name if table has one, else 49 * null 50 **/ 51 protected function getSchema() 52 { 53 54 $table = $this->getTable(); 55 $schema = $table->getVendorSpecificInfo(); 56 if (!empty($schema) && isset($schema['schema'])) { 57 return $schema['schema']; 58 } 59 60 return null; 61 62 } 63 64 /** 65 * Add a schema to the generated SQL script 66 * 67 * @author Markus Lervik <markus.lervik@necora.fi> 68 * @access protected 69 * @return string with CREATE SCHEMA statement if 70 * applicable, else empty string 71 **/ 72 protected function addSchema() 73 { 74 75 $schemaName = $this->getSchema(); 76 77 if ($schemaName !== null) { 78 79 if (!in_array($schemaName, self::$addedSchemas)) { 80 $platform = $this->getPlatform(); 81 self::$addedSchemas[] = $schemaName; 82 return "\nCREATE SCHEMA " . $this->quoteIdentifier($schemaName) . ";\n"; 83 } 84 } 85 86 return ''; 87 88 } 89 90 /** 91 * 92 * @see parent::addDropStatement() 93 */ 94 protected function addDropStatements(&$script) 95 { 96 $table = $this->getTable(); 97 $platform = $this->getPlatform(); 98 99 $script .= " 100 DROP TABLE ".$this->quoteIdentifier($table->getName())." CASCADE; 101 "; 102 if ($table->getIdMethod() == "native") { 103 $script .= " 104 DROP SEQUENCE ".$this->quoteIdentifier(strtolower($table->getSequenceName()))."; 105 "; 106 } 107 } 108 109 /** 110 * 111 * @see parent::addColumns() 112 */ 113 protected function addTable(&$script) 114 { 115 $table = $this->getTable(); 116 $platform = $this->getPlatform(); 117 118 $script .= " 119 ----------------------------------------------------------------------------- 120 -- ".$table->getName()." 121 ----------------------------------------------------------------------------- 122 "; 123 124 $script .= $this->addSchema(); 125 126 $schemaName = $this->getSchema(); 127 if ($schemaName !== null) { 128 $script .= "\nSET search_path TO " . $this->quoteIdentifier($schemaName) . ";\n"; 129 } 130 131 $this->addDropStatements($script); 132 $this->addSequences($script); 133 134 $script .= " 135 136 CREATE TABLE ".$this->quoteIdentifier($table->getName())." 137 ( 138 "; 139 140 $lines = array(); 141 142 foreach ($table->getColumns() as $col) { 143 $lines[] = $this->getColumnDDL($col); 144 } 145 146 if ($table->hasPrimaryKey()) { 147 $lines[] = "PRIMARY KEY (".$this->getColumnList($table->getPrimaryKey()).")"; 148 } 149 150 foreach ($table->getUnices() as $unique ) { 151 $lines[] = "CONSTRAINT ".$this->quoteIdentifier($unique->getName())." UNIQUE (".$this->getColumnList($unique->getColumns()).")"; 152 } 153 154 $sep = ", 155 "; 156 $script .= implode($sep, $lines); 157 $script .= " 158 ); 159 160 COMMENT ON TABLE ".$this->quoteIdentifier($table->getName())." IS '" . $platform->escapeText($table->getDescription())."'; 161 162 "; 163 164 $this->addColumnComments($script); 165 166 $script .= "\nSET search_path TO public;"; 167 168 } 169 170 /** 171 * Adds comments for the columns. 172 * 173 */ 174 protected function addColumnComments(&$script) 175 { 176 $table = $this->getTable(); 177 $platform = $this->getPlatform(); 178 179 foreach ($this->getTable()->getColumns() as $col) { 180 if( $col->getDescription() != '' ) { 181 $script .= " 182 COMMENT ON COLUMN ".$this->quoteIdentifier($table->getName()).".".$this->quoteIdentifier($col->getName())." IS '".$platform->escapeText($col->getDescription()) ."'; 183 "; 184 } 185 } 186 } 187 188 /** 189 * Adds CREATE SEQUENCE statements for this table. 190 * 191 */ 192 protected function addSequences(&$script) 193 { 194 $table = $this->getTable(); 195 $platform = $this->getPlatform(); 196 197 if ($table->getIdMethod() == "native") { 198 $script .= " 199 CREATE SEQUENCE ".$this->quoteIdentifier(strtolower($table->getSequenceName()))."; 200 "; 201 } 202 } 203 204 205 /** 206 * Adds CREATE INDEX statements for this table. 207 * @see parent::addIndices() 208 */ 209 protected function addIndices(&$script) 210 { 211 $table = $this->getTable(); 212 $platform = $this->getPlatform(); 213 214 foreach ($table->getIndices() as $index) { 215 $script .= " 216 CREATE "; 217 if($index->getIsUnique()) { 218 $script .= "UNIQUE"; 219 } 220 $script .= "INDEX ".$this->quoteIdentifier($index->getName())." ON ".$this->quoteIdentifier($table->getName())." (".$this->getColumnList($index->getColumns())."); 221 "; 222 } 223 } 224 225 /** 226 * 227 * @see parent::addForeignKeys() 228 */ 229 protected function addForeignKeys(&$script) 230 { 231 $table = $this->getTable(); 232 $platform = $this->getPlatform(); 233 234 foreach ($table->getForeignKeys() as $fk) { 235 $script .= " 236 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()).")"; 237 if ($fk->hasOnUpdate()) { 238 $script .= " ON UPDATE ".$fk->getOnUpdate(); 239 } 240 if ($fk->hasOnDelete()) { 241 $script .= " ON DELETE ".$fk->getOnDelete(); 242 } 243 $script .= "; 244 "; 245 } 246 } 247 248 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |