columndef.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /** name of the column. */
  44. public $name;
  45. /** type of column, e.g. 'int', 'varchar' */
  46. public $type;
  47. /** size of the column. */
  48. public $size;
  49. /** boolean flag; can it be null? */
  50. public $nullable;
  51. /**
  52. * type of key: null = no key; 'PRI' => primary;
  53. * 'UNI' => unique key; 'MUL' => multiple values.
  54. */
  55. public $key;
  56. /** default value if any. */
  57. public $default;
  58. /** 'extra' stuff. Returned by MySQL, largely
  59. * unused. */
  60. public $extra;
  61. /** auto increment this field if no value is specific for it during an insert **/
  62. public $auto_increment;
  63. /**
  64. * Constructor.
  65. *
  66. * @param string $name name of the column
  67. * @param string $type type of the column
  68. * @param int $size size of the column
  69. * @param boolean $nullable can this be null?
  70. * @param string $key type of key
  71. * @param value $default default value
  72. * @param value $extra unused
  73. * @param boolean $auto_increment
  74. */
  75. function __construct($name=null, $type=null, $size=null,
  76. $nullable=true, $key=null, $default=null,
  77. $extra=null, $auto_increment=false)
  78. {
  79. $this->name = strtolower($name);
  80. $this->type = strtolower($type);
  81. $this->size = $size+0;
  82. $this->nullable = $nullable;
  83. $this->key = $key;
  84. $this->default = $default;
  85. $this->extra = $extra;
  86. $this->auto_increment = $auto_increment;
  87. }
  88. /**
  89. * Compares this columndef with another to see
  90. * if they're functionally equivalent.
  91. *
  92. * @param ColumnDef $other column to compare
  93. *
  94. * @return boolean true if equivalent, otherwise false.
  95. */
  96. function equals($other)
  97. {
  98. return ($this->name == $other->name &&
  99. $this->_typeMatch($other) &&
  100. $this->_defaultMatch($other) &&
  101. $this->_nullMatch($other) &&
  102. $this->key == $other->key &&
  103. $this->auto_increment == $other->auto_increment);
  104. }
  105. /**
  106. * Does the type of this column match the
  107. * type of the other column?
  108. *
  109. * Checks the type and size of a column. Tries
  110. * to ignore differences between synonymous
  111. * data types, like 'integer' and 'int'.
  112. *
  113. * @param ColumnDef $other other column to check
  114. *
  115. * @return boolean true if they're about equivalent
  116. */
  117. private function _typeMatch($other)
  118. {
  119. switch ($this->type) {
  120. case 'integer':
  121. case 'int':
  122. return ($other->type == 'integer' ||
  123. $other->type == 'int');
  124. break;
  125. default:
  126. return ($this->type == $other->type &&
  127. $this->size == $other->size);
  128. }
  129. }
  130. /**
  131. * Does the default behaviour of this column match
  132. * the other?
  133. *
  134. * @param ColumnDef $other other column to check
  135. *
  136. * @return boolean true if defaults are effectively the same.
  137. */
  138. private function _defaultMatch($other)
  139. {
  140. return ((is_null($this->default) && is_null($other->default)) ||
  141. ($this->default == $other->default));
  142. }
  143. /**
  144. * Does the null behaviour of this column match
  145. * the other?
  146. *
  147. * @param ColumnDef $other other column to check
  148. *
  149. * @return boolean true if these columns 'null' the same.
  150. */
  151. private function _nullMatch($other)
  152. {
  153. return ((!is_null($this->default) && !is_null($other->default) &&
  154. $this->default == $other->default) ||
  155. ($this->nullable == $other->nullable));
  156. }
  157. }