PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/sabel/db/ibase/Migration.php

https://github.com/reoring/sabel
PHP | 144 lines | 116 code | 19 blank | 9 comment | 20 complexity | 70b04fcf78adc78b092a092f7327c1a1 MD5 | raw file
  1. <?php
  2. /**
  3. * Sabel_Db_Ibase_Migration
  4. *
  5. * @category DB
  6. * @package org.sabel.db
  7. * @author Ebine Yutaka <ebine.yutaka@sabel.jp>
  8. * @copyright 2004-2008 Mori Reo <mori.reo@sabel.jp>
  9. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  10. */
  11. class Sabel_Db_Ibase_Migration extends Sabel_Db_Abstract_Migration
  12. {
  13. protected $types = array(Sabel_Db_Type::INT => "INTEGER",
  14. Sabel_Db_Type::BIGINT => "BIGINT",
  15. Sabel_Db_Type::SMALLINT => "SMALLINT",
  16. Sabel_Db_Type::FLOAT => "FLOAT",
  17. Sabel_Db_Type::DOUBLE => "DOUBLE PRECISION",
  18. Sabel_Db_Type::BOOL => "CHAR(1)",
  19. Sabel_Db_Type::STRING => "VARCHAR",
  20. Sabel_Db_Type::TEXT => "BLOB SUB_TYPE TEXT",
  21. Sabel_Db_Type::DATETIME => "TIMESTAMP",
  22. Sabel_Db_Type::DATE => "DATE",
  23. Sabel_Db_Type::BINARY => "BLOB SUB_TYPE 2");
  24. protected function create()
  25. {
  26. $tblName = convert_to_tablename($this->mdlName);
  27. $schema = $this->getSchema();
  28. $tables = $schema->getTableList();
  29. if (Sabel_Db_Migration_Manager::isUpgrade()) {
  30. if (in_array($tblName, $tables)) {
  31. Sabel_Console::warning("table '{$tblName}' already exists. (SKIP)");
  32. } else {
  33. $this->createTable($this->filePath);
  34. }
  35. } else {
  36. if (in_array($tblName, $tables)) {
  37. $this->dropSequence($schema->getTable($tblName)->getSequenceColumn());
  38. $this->executeQuery("DROP TABLE " . $this->quoteIdentifier($tblName));
  39. } else {
  40. Sabel_Console::warning("unknown table '{$tblName}'. (SKIP)");
  41. }
  42. }
  43. }
  44. protected function createTable($filePath)
  45. {
  46. $create = $this->getReader($filePath)->readCreate();
  47. $this->executeQuery($this->getCreateSql($create));
  48. foreach ($create->getColumns() as $column) {
  49. if ($column->increment) {
  50. $tblName = convert_to_tablename($this->mdlName);
  51. $seqName = strtoupper($tblName) . "_" . strtoupper($column->name) . "_SEQ";
  52. $this->executeQuery("CREATE SEQUENCE " . $seqName);
  53. }
  54. }
  55. }
  56. protected function drop()
  57. {
  58. $restore = $this->getRestoreFileName();
  59. if (Sabel_Db_Migration_Manager::isUpgrade()) {
  60. if (is_file($restore)) unlink($restore);
  61. $schema = $this->getSchema()->getTable(convert_to_tablename($this->mdlName));
  62. $writer = new Sabel_Db_Migration_Writer($restore);
  63. $writer->writeTable($schema);
  64. $tblName = $this->quoteIdentifier($schema->getTableName());
  65. $this->executeQuery("DROP TABLE $tblName");
  66. $this->dropSequence($schema->getSequenceColumn());
  67. } else {
  68. $this->createTable($restore);
  69. }
  70. }
  71. private function dropSequence($incCol)
  72. {
  73. if ($incCol !== null) {
  74. $tblName = convert_to_tablename($this->mdlName);
  75. $seqName = strtoupper($tblName) . "_" . strtoupper($incCol) . "_SEQ";
  76. $this->executeQuery("DROP SEQUENCE " . $seqName);
  77. }
  78. }
  79. protected function changeColumnUpgrade($columns, $schema)
  80. {
  81. throw new Sabel_Exception_Runtime("change column not supported.");
  82. }
  83. protected function changeColumnDowngrade($columns, $schema)
  84. {
  85. throw new Sabel_Exception_Runtime("change column not supported.");
  86. }
  87. protected function createColumnAttributes($col)
  88. {
  89. $line = array();
  90. $line[] = $this->quoteIdentifier($col->name);
  91. $line[] = $this->getTypeString($col);
  92. $line[] = $this->getDefaultValue($col);
  93. if (($nullable = $this->getNullableString($col)) !== "") {
  94. $line[] = $nullable;
  95. }
  96. return preg_replace("/[ ]{2,}/", " ", implode(" ", $line));
  97. }
  98. private function getTypeString($col, $withLength = true)
  99. {
  100. if ($col->isString() && $withLength) {
  101. return $this->types[$col->type] . "({$col->max})";
  102. } else {
  103. return $this->types[$col->type];
  104. }
  105. }
  106. private function getNullableString($column)
  107. {
  108. return ($column->nullable === false) ? "NOT NULL" : "";
  109. }
  110. private function valueCheck($column, $default)
  111. {
  112. if ($default === null) return true;
  113. if (($column->isBool() && !is_bool($default)) ||
  114. ($column->isNumeric() && !is_numeric($default))) {
  115. throw new Sabel_Db_Exception("invalid default value.");
  116. } else {
  117. return true;
  118. }
  119. }
  120. protected function getBooleanAttr($value)
  121. {
  122. $v = ($value === true) ? "'1'" : "'0'";
  123. return "DEFAULT " . $v;
  124. }
  125. }