columndef.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Database schema utilities
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Database
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * A class encapsulating the structure of a column in a table.
  34. *
  35. * @category Database
  36. * @package StatusNet
  37. * @author Evan Prodromou <evan@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. */
  41. class ColumnDef
  42. {
  43. public $widgetOpts;
  44. public $scoped;
  45. /** name of the column. */
  46. public $name;
  47. /** type of column, e.g. 'int', 'varchar' */
  48. public $type;
  49. /** size of the column. */
  50. public $size;
  51. /** boolean flag; can it be null? */
  52. public $nullable;
  53. /**
  54. * type of key: null = no key; 'PRI' => primary;
  55. * 'UNI' => unique key; 'MUL' => multiple values.
  56. */
  57. public $key;
  58. /** default value if any. */
  59. public $default;
  60. /** 'extra' stuff. Returned by MySQL, largely
  61. * unused. */
  62. public $extra;
  63. /** auto increment this field if no value is specific for it during an insert **/
  64. public $auto_increment;
  65. /**
  66. * Constructor.
  67. *
  68. * @param string $name name of the column
  69. * @param string $type type of the column
  70. * @param int $size size of the column
  71. * @param boolean $nullable can this be null?
  72. * @param string $key type of key
  73. * @param value $default default value
  74. * @param value $extra unused
  75. * @param boolean $auto_increment
  76. */
  77. function __construct($name=null, $type=null, $size=null,
  78. $nullable=true, $key=null, $default=null,
  79. $extra=null, $auto_increment=false)
  80. {
  81. $this->name = strtolower($name);
  82. $this->type = strtolower($type);
  83. $this->size = $size+0;
  84. $this->nullable = $nullable;
  85. $this->key = $key;
  86. $this->default = $default;
  87. $this->extra = $extra;
  88. $this->auto_increment = $auto_increment;
  89. }
  90. /**
  91. * Compares this columndef with another to see
  92. * if they're functionally equivalent.
  93. *
  94. * @param ColumnDef $other column to compare
  95. *
  96. * @return boolean true if equivalent, otherwise false.
  97. */
  98. function equals($other)
  99. {
  100. return ($this->name == $other->name &&
  101. $this->_typeMatch($other) &&
  102. $this->_defaultMatch($other) &&
  103. $this->_nullMatch($other) &&
  104. $this->key == $other->key &&
  105. $this->auto_increment == $other->auto_increment);
  106. }
  107. /**
  108. * Does the type of this column match the
  109. * type of the other column?
  110. *
  111. * Checks the type and size of a column. Tries
  112. * to ignore differences between synonymous
  113. * data types, like 'integer' and 'int'.
  114. *
  115. * @param ColumnDef $other other column to check
  116. *
  117. * @return boolean true if they're about equivalent
  118. */
  119. private function _typeMatch($other)
  120. {
  121. switch ($this->type) {
  122. case 'integer':
  123. case 'int':
  124. return ($other->type == 'integer' ||
  125. $other->type == 'int');
  126. break;
  127. default:
  128. return ($this->type == $other->type &&
  129. $this->size == $other->size);
  130. }
  131. }
  132. /**
  133. * Does the default behaviour of this column match
  134. * the other?
  135. *
  136. * @param ColumnDef $other other column to check
  137. *
  138. * @return boolean true if defaults are effectively the same.
  139. */
  140. private function _defaultMatch($other)
  141. {
  142. return ((is_null($this->default) && is_null($other->default)) ||
  143. ($this->default == $other->default));
  144. }
  145. /**
  146. * Does the null behaviour of this column match
  147. * the other?
  148. *
  149. * @param ColumnDef $other other column to check
  150. *
  151. * @return boolean true if these columns 'null' the same.
  152. */
  153. private function _nullMatch($other)
  154. {
  155. return ((!is_null($this->default) && !is_null($other->default) &&
  156. $this->default == $other->default) ||
  157. ($this->nullable == $other->nullable));
  158. }
  159. }