| 39 | | protected $valid_default_values = array( |
| 40 | | 'text' => '', |
| 41 | | 'boolean' => true, |
| 42 | | 'integer' => 0, |
| 43 | | 'decimal' => 0.0, |
| 44 | | 'float' => 0.0, |
| 45 | | 'timestamp' => '1970-01-01 00:00:00', |
| 46 | | 'time' => '00:00:00', |
| 47 | | 'date' => '1970-01-01', |
| 48 | | 'clob' => '', |
| 49 | | 'blob' => '', |
| 50 | | 'string' => '', |
| 51 | | ); |
| 52 | | |
| 53 | | /** |
| 54 | | * drop an existing database |
| 55 | | * (this method is implemented by the drivers) |
| 56 | | * |
| 57 | | * @param string $name name of the database that should be dropped |
| 58 | | * @return void |
| 59 | | */ |
| 60 | | public function dropDatabase($database) |
| 61 | | { |
| 62 | | $this->conn->execute($this->dropDatabaseSql($database)); |
| 63 | | } |
| 64 | | |
| 65 | | /** |
| 66 | | * drop an existing database |
| 67 | | * (this method is implemented by the drivers) |
| 68 | | * |
| 69 | | * @param string $name name of the database that should be dropped |
| 70 | | * @return void |
| 71 | | */ |
| 72 | | public function dropDatabaseSql($database) |
| 73 | | { |
| 74 | | throw new Doctrine_Export_Exception('Drop database not supported by this driver.'); |
| 75 | | } |
| 76 | | |
| 77 | | /** |
| 78 | | * dropTableSql |
| 79 | | * drop an existing table |
| 80 | | * |
| 81 | | * @param string $table name of table that should be dropped from the database |
| 82 | | * @return string |
| 83 | | */ |
| 84 | | public function dropTableSql($table) |
| 85 | | { |
| 86 | | return 'DROP TABLE ' . $this->conn->quoteIdentifier($table); |
| 87 | | } |
| 88 | | |
| 89 | | /** |
| 90 | | * dropTable |
| 91 | | * drop an existing table |
| 92 | | * |
| 93 | | * @param string $table name of table that should be dropped from the database |
| 94 | | * @return void |
| 95 | | */ |
| 96 | | public function dropTable($table) |
| 97 | | { |
| 98 | | $this->conn->execute($this->dropTableSql($table)); |
| 99 | | } |
| 100 | | |
| 101 | | /** |
| 102 | | * drop existing index |
| 103 | | * |
| 104 | | * @param string $table name of table that should be used in method |
| 105 | | * @param string $name name of the index to be dropped |
| 106 | | * @return void |
| 107 | | */ |
| 108 | | public function dropIndex($table, $name) |
| 109 | | { |
| 110 | | return $this->conn->exec($this->dropIndexSql($table, $name)); |
| 111 | | } |
| 112 | | |
| 113 | | /** |
| 114 | | * dropIndexSql |
| 115 | | * |
| 116 | | * @param string $table name of table that should be used in method |
| 117 | | * @param string $name name of the index to be dropped |
| 118 | | * @return string SQL that is used for dropping an index |
| 119 | | */ |
| 120 | | public function dropIndexSql($table, $name) |
| 121 | | { |
| 122 | | $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)); |
| 123 | | |
| 124 | | return 'DROP INDEX ' . $name; |
| 125 | | } |
| 126 | | |
| 127 | | /** |
| 128 | | * drop existing constraint |
| 129 | | * |
| 130 | | * @param string $table name of table that should be used in method |
| 131 | | * @param string $name name of the constraint to be dropped |
| 132 | | * @param string $primary hint if the constraint is primary |
| 133 | | * @return void |
| 134 | | */ |
| 135 | | public function dropConstraint($table, $name, $primary = false) |
| 136 | | { |
| 137 | | $table = $this->conn->quoteIdentifier($table); |
| 138 | | $name = $this->conn->quoteIdentifier($name); |
| 139 | | |
| 140 | | return $this->conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name); |
| 141 | | } |
| 142 | | |
| 143 | | /** |
| 144 | | * drop existing foreign key |
| 145 | | * |
| 146 | | * @param string $table name of table that should be used in method |
| 147 | | * @param string $name name of the foreign key to be dropped |
| 148 | | * @return void |
| 149 | | */ |
| 150 | | public function dropForeignKey($table, $name) |
| 151 | | { |
| 152 | | return $this->dropConstraint($table, $name); |
| 153 | | } |
| 154 | | |
| 155 | | /** |
| 156 | | * dropSequenceSql |
| 157 | | * drop existing sequence |
| 158 | | * (this method is implemented by the drivers) |
| 159 | | * |
| 160 | | * @throws Doctrine_Connection_Exception if something fails at database level |
| 161 | | * @param string $sequenceName name of the sequence to be dropped |
| 162 | | * @return void |
| 163 | | */ |
| 164 | | public function dropSequence($sequenceName) |
| 165 | | { |
| 166 | | $this->conn->exec($this->dropSequenceSql($sequenceName)); |
| 167 | | } |
| 168 | | |
| 169 | | /** |
| 170 | | * dropSequenceSql |
| 171 | | * drop existing sequence |
| 172 | | * |
| 173 | | * @throws Doctrine_Connection_Exception if something fails at database level |
| 174 | | * @param string $sequenceName name of the sequence to be dropped |
| 175 | | * @return void |
| 176 | | */ |
| 177 | | public function dropSequenceSql($sequenceName) |
| 178 | | { |
| 179 | | throw new Doctrine_Export_Exception('Drop sequence not supported by this driver.'); |
| 180 | | } |
| 181 | | |
| 182 | | /** |
| 183 | | * create a new database |
| 184 | | * (this method is implemented by the drivers) |
| 185 | | * |
| 186 | | * @param string $name name of the database that should be created |
| 187 | | * @return void |
| 188 | | */ |
| 189 | | public function createDatabase($database) |
| 190 | | { |
| 191 | | $this->conn->execute($this->createDatabaseSql($database)); |
| 192 | | } |
| 193 | | |
| 194 | | /** |
| 195 | | * create a new database |
| 196 | | * (this method is implemented by the drivers) |
| 197 | | * |
| 198 | | * @param string $name name of the database that should be created |
| 199 | | * @return string |
| 200 | | */ |
| 201 | | public function createDatabaseSql($database) |
| 202 | | { |
| 203 | | throw new Doctrine_Export_Exception('Create database not supported by this driver.'); |
| 204 | | } |
| 205 | | |
| 206 | | /** |
| 207 | | * create a new table |
| 208 | | * |
| 209 | | * @param string $name Name of the database that should be created |
| 210 | | * @param array $fields Associative array that contains the definition of each field of the new table |
| 211 | | * The indexes of the array entries are the names of the fields of the table an |
| 212 | | * the array entry values are associative arrays like those that are meant to be |
| 213 | | * passed with the field definitions to get[Type]Declaration() functions. |
| 214 | | * array( |
| 215 | | * 'id' => array( |
| 216 | | * 'type' => 'integer', |
| 217 | | * 'unsigned' => 1 |
| 218 | | * 'notnull' => 1 |
| 219 | | * 'default' => 0 |
| 220 | | * ), |
| 221 | | * 'name' => array( |
| 222 | | * 'type' => 'text', |
| 223 | | * 'length' => 12 |
| 224 | | * ), |
| 225 | | * 'password' => array( |
| 226 | | * 'type' => 'text', |
| 227 | | * 'length' => 12 |
| 228 | | * ) |
| 229 | | * ); |
| 230 | | * @param array $options An associative array of table options: |
| 231 | | * |
| 232 | | * @return string |
| 233 | | */ |
| 234 | | public function createTableSql($name, array $fields, array $options = array()) |
| 235 | | { |
| 236 | | if ( ! $name) { |
| 237 | | throw new Doctrine_Export_Exception('no valid table name specified'); |
| 238 | | } |
| 239 | | |
| 240 | | if (empty($fields)) { |
| 241 | | throw new Doctrine_Export_Exception('no fields specified for table ' . $name); |
| 242 | | } |
| 243 | | |
| 244 | | $queryFields = $this->getFieldDeclarationList($fields); |
| 245 | | |
| 246 | | |
| 247 | | if (isset($options['primary']) && ! empty($options['primary'])) { |
| 248 | | $queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')'; |
| 249 | | } |
| 250 | | |
| 251 | | if (isset($options['indexes']) && ! empty($options['indexes'])) { |
| 252 | | foreach($options['indexes'] as $index => $definition) { |
| 253 | | $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition); |
| 254 | | } |
| 255 | | } |
| 256 | | |
| 257 | | $query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields; |
| 258 | | |
| 259 | | $check = $this->getCheckDeclaration($fields); |
| 260 | | |
| 261 | | if ( ! empty($check)) { |
| 262 | | $query .= ', ' . $check; |
| 263 | | } |
| 264 | | |
| 265 | | $query .= ')'; |
| 266 | | |
| 267 | | |
| 268 | | |
| 269 | | $sql[] = $query; |
| 270 | | |
| 271 | | if (isset($options['foreignKeys'])) { |
| 272 | | |
| 273 | | foreach ((array) $options['foreignKeys'] as $k => $definition) { |
| 274 | | if (is_array($definition)) { |
| 275 | | $sql[] = $this->createForeignKeySql($name, $definition); |
| 276 | | } |
| 277 | | } |
| 278 | | } |
| 279 | | return $sql; |
| 280 | | } |
| 281 | | |
| 282 | | /** |
| 283 | | * create a new table |
| 284 | | * |
| 285 | | * @param string $name Name of the database that should be created |
| 286 | | * @param array $fields Associative array that contains the definition of each field of the new table |
| 287 | | * @param array $options An associative array of table options: |
| 288 | | * @see Doctrine_Export::createTableSql() |
| 289 | | * |
| 290 | | * @return void |
| 291 | | */ |
| 292 | | public function createTable($name, array $fields, array $options = array()) |
| 293 | | { |
| 294 | | $sql = (array) $this->createTableSql($name, $fields, $options); |
| 295 | | |
| 296 | | foreach ($sql as $query) { |
| 297 | | $this->conn->execute($query); |
| 298 | | } |
| 299 | | } |
| 300 | | |
| 301 | | /** |
| 302 | | * create sequence |
| 303 | | * |
| 304 | | * @throws Doctrine_Connection_Exception if something fails at database level |
| 305 | | * @param string $seqName name of the sequence to be created |
| 306 | | * @param string $start start value of the sequence; default is 1 |
| 307 | | * @param array $options An associative array of table options: |
| 308 | | * array( |
| 309 | | * 'comment' => 'Foo', |
| 310 | | * 'charset' => 'utf8', |
| 311 | | * 'collate' => 'utf8_unicode_ci', |
| 312 | | * ); |
| 313 | | * @return void |
| 314 | | */ |
| 315 | | public function createSequence($seqName, $start = 1, array $options = array()) |
| 316 | | { |
| 317 | | return $this->conn->execute($this->createSequenceSql($seqName, $start = 1, $options)); |
| 318 | | } |
| 319 | | |
| 320 | | /** |
| 321 | | * return RDBMS specific create sequence statement |
| 322 | | * (this method is implemented by the drivers) |
| 323 | | * |
| 324 | | * @throws Doctrine_Connection_Exception if something fails at database level |
| 325 | | * @param string $seqName name of the sequence to be created |
| 326 | | * @param string $start start value of the sequence; default is 1 |
| 327 | | * @param array $options An associative array of table options: |
| 328 | | * array( |
| 329 | | * 'comment' => 'Foo', |
| 330 | | * 'charset' => 'utf8', |
| 331 | | * 'collate' => 'utf8_unicode_ci', |
| 332 | | * ); |
| 333 | | * @return string |
| 334 | | */ |
| 335 | | public function createSequenceSql($seqName, $start = 1, array $options = array()) |
| 336 | | { |
| 337 | | throw new Doctrine_Export_Exception('Create sequence not supported by this driver.'); |
| 338 | | } |
| 339 | | |
| 340 | | /** |
| 341 | | * create a constraint on a table |
| 342 | | * |
| 343 | | * @param string $table name of the table on which the constraint is to be created |
| 344 | | * @param string $name name of the constraint to be created |
| 345 | | * @param array $definition associative array that defines properties of the constraint to be created. |
| 346 | | * Currently, only one property named FIELDS is supported. This property |
| 347 | | * is also an associative with the names of the constraint fields as array |
| 348 | | * constraints. Each entry of this array is set to another type of associative |
| 349 | | * array that specifies properties of the constraint that are specific to |
| 350 | | * each field. |
| 351 | | * |
| 352 | | * Example |
| 353 | | * array( |
| 354 | | * 'fields' => array( |
| 355 | | * 'user_name' => array(), |
| 356 | | * 'last_login' => array() |
| 357 | | * ) |
| 358 | | * ) |
| 359 | | * @return void |
| 360 | | */ |
| 361 | | public function createConstraint($table, $name, $definition) |
| 362 | | { |
| 363 | | $sql = $this->createConstraintSql($table, $name, $definition); |
| 364 | | |
| 365 | | return $this->conn->exec($sql); |
| 366 | | } |
| 367 | | |
| 368 | | /** |
| 369 | | * create a constraint on a table |
| 370 | | * |
| 371 | | * @param string $table name of the table on which the constraint is to be created |
| 372 | | * @param string $name name of the constraint to be created |
| 373 | | * @param array $definition associative array that defines properties of the constraint to be created. |
| 374 | | * Currently, only one property named FIELDS is supported. This property |
| 375 | | * is also an associative with the names of the constraint fields as array |
| 376 | | * constraints. Each entry of this array is set to another type of associative |
| 377 | | * array that specifies properties of the constraint that are specific to |
| 378 | | * each field. |
| 379 | | * |
| 380 | | * Example |
| 381 | | * array( |
| 382 | | * 'fields' => array( |
| 383 | | * 'user_name' => array(), |
| 384 | | * 'last_login' => array() |
| 385 | | * ) |
| 386 | | * ) |
| 387 | | * @return void |
| 388 | | */ |
| 389 | | public function createConstraintSql($table, $name, $definition) |
| 390 | | { |
| 391 | | $table = $this->conn->quoteIdentifier($table); |
| 392 | | $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)); |
| 393 | | $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name; |
| 394 | | |
| 395 | | if (isset($definition['primary']) && $definition['primary']) { |
| 396 | | $query .= ' PRIMARY KEY'; |
| 397 | | } elseif (isset($definition['unique']) && $definition['unique']) { |
| 398 | | $query .= ' UNIQUE'; |
| 399 | | } |
| 400 | | |
| 401 | | $fields = array(); |
| 402 | | foreach (array_keys($definition['fields']) as $field) { |
| 403 | | $fields[] = $this->conn->quoteIdentifier($field, true); |
| 404 | | } |
| 405 | | $query .= ' ('. implode(', ', $fields) . ')'; |
| 406 | | |
| 407 | | return $query; |
| 408 | | } |
| 409 | | |
| 410 | | /** |
| 411 | | * Get the stucture of a field into an array |
| 412 | | * |
| 413 | | * @param string $table name of the table on which the index is to be created |
| 414 | | * @param string $name name of the index to be created |
| 415 | | * @param array $definition associative array that defines properties of the index to be created. |
| 416 | | * Currently, only one property named FIELDS is supported. This property |
| 417 | | * is also an associative with the names of the index fields as array |
| 418 | | * indexes. Each entry of this array is set to another type of associative |
| 419 | | * array that specifies properties of the index that are specific to |
| 420 | | * each field. |
| 421 | | * |
| 422 | | * Currently, only the sorting property is supported. It should be used |
| 423 | | * to define the sorting direction of the index. It may be set to either |
| 424 | | * ascending or descending. |
| 425 | | * |
| 426 | | * Not all DBMS support index sorting direction configuration. The DBMS |
| 427 | | * drivers of those that do not support it ignore this property. Use the |
| 428 | | * function supports() to determine whether the DBMS driver can manage indexes. |
| 429 | | * |
| 430 | | * Example |
| 431 | | * array( |
| 432 | | * 'fields' => array( |
| 433 | | * 'user_name' => array( |
| 434 | | * 'sorting' => 'ascending' |
| 435 | | * ), |
| 436 | | * 'last_login' => array() |
| 437 | | * ) |
| 438 | | * ) |
| 439 | | * @return void |
| 440 | | */ |
| 441 | | public function createIndex($table, $name, array $definition) |
| 442 | | { |
| 443 | | return $this->conn->execute($this->createIndexSql($table, $name, $definition)); |
| 444 | | } |
| 445 | | |
| 446 | | /** |
| 447 | | * Get the stucture of a field into an array |
| 448 | | * |
| 449 | | * @param string $table name of the table on which the index is to be created |
| 450 | | * @param string $name name of the index to be created |
| 451 | | * @param array $definition associative array that defines properties of the index to be created. |
| 452 | | * @see Doctrine_Export::createIndex() |
| 453 | | * @return string |
| 454 | | */ |
| 455 | | public function createIndexSql($table, $name, array $definition) |
| 456 | | { |
| 457 | | $table = $this->conn->quoteIdentifier($table); |
| 458 | | $name = $this->conn->quoteIdentifier($name); |
| 459 | | $type = ''; |
| 460 | | |
| 461 | | if (isset($definition['type'])) { |
| 462 | | switch (strtolower($definition['type'])) { |
| 463 | | case 'unique': |
| 464 | | $type = strtoupper($definition['type']) . ' '; |
| 465 | | break; |