3 * StatusNet, the distributed open-source microblogging tool
5 * Database schema utilities
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
35 * A class encapsulating the structure of a column in a table.
39 * @author Evan Prodromou <evan@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
46 /** name of the column. */
48 /** type of column, e.g. 'int', 'varchar' */
50 /** size of the column. */
52 /** boolean flag; can it be null? */
55 * type of key: null = no key; 'PRI' => primary;
56 * 'UNI' => unique key; 'MUL' => multiple values.
59 /** default value if any. */
61 /** 'extra' stuff. Returned by MySQL, largely
64 /** auto increment this field if no value is specific for it during an insert **/
65 public $auto_increment;
70 * @param string $name name of the column
71 * @param string $type type of the column
72 * @param int $size size of the column
73 * @param boolean $nullable can this be null?
74 * @param string $key type of key
75 * @param value $default default value
76 * @param value $extra unused
77 * @param boolean $auto_increment
80 function __construct($name=null, $type=null, $size=null,
81 $nullable=true, $key=null, $default=null,
82 $extra=null, $auto_increment=false)
84 $this->name = strtolower($name);
85 $this->type = strtolower($type);
86 $this->size = $size+0;
87 $this->nullable = $nullable;
89 $this->default = $default;
90 $this->extra = $extra;
91 $this->auto_increment = $auto_increment;
95 * Compares this columndef with another to see
96 * if they're functionally equivalent.
98 * @param ColumnDef $other column to compare
100 * @return boolean true if equivalent, otherwise false.
103 function equals($other)
105 return ($this->name == $other->name &&
106 $this->_typeMatch($other) &&
107 $this->_defaultMatch($other) &&
108 $this->_nullMatch($other) &&
109 $this->key == $other->key &&
110 $this->auto_increment == $other->auto_increment);
114 * Does the type of this column match the
115 * type of the other column?
117 * Checks the type and size of a column. Tries
118 * to ignore differences between synonymous
119 * data types, like 'integer' and 'int'.
121 * @param ColumnDef $other other column to check
123 * @return boolean true if they're about equivalent
126 private function _typeMatch($other)
128 switch ($this->type) {
131 return ($other->type == 'integer' ||
132 $other->type == 'int');
135 return ($this->type == $other->type &&
136 $this->size == $other->size);
141 * Does the default behaviour of this column match
144 * @param ColumnDef $other other column to check
146 * @return boolean true if defaults are effectively the same.
149 private function _defaultMatch($other)
151 return ((is_null($this->default) && is_null($other->default)) ||
152 ($this->default == $other->default));
156 * Does the null behaviour of this column match
159 * @param ColumnDef $other other column to check
161 * @return boolean true if these columns 'null' the same.
164 private function _nullMatch($other)
166 return ((!is_null($this->default) && !is_null($other->default) &&
167 $this->default == $other->default) ||
168 ($this->nullable == $other->nullable));