]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/columndef.php
Always naming it 'plugin' is not good, it can easily confuse. So better name it
[quix0rs-gnu-social.git] / lib / columndef.php
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
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * A class encapsulating the structure of a column in a table.
36  *
37  * @category Database
38  * @package  StatusNet
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/
42  */
43 class ColumnDef
44 {
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     /**
67      * Constructor.
68      *
69      * @param string  $name     name of the column
70      * @param string  $type     type of the column
71      * @param int     $size     size of the column
72      * @param boolean $nullable can this be null?
73      * @param string  $key      type of key
74      * @param value   $default  default value
75      * @param value   $extra    unused
76      * @param boolean $auto_increment
77      */
78     function __construct($name=null, $type=null, $size=null,
79                          $nullable=true, $key=null, $default=null,
80                          $extra=null, $auto_increment=false)
81     {
82         $this->name     = strtolower($name);
83         $this->type     = strtolower($type);
84         $this->size     = $size+0;
85         $this->nullable = $nullable;
86         $this->key      = $key;
87         $this->default  = $default;
88         $this->extra    = $extra;
89         $this->auto_increment = $auto_increment;
90     }
91
92     /**
93      * Compares this columndef with another to see
94      * if they're functionally equivalent.
95      *
96      * @param ColumnDef $other column to compare
97      *
98      * @return boolean true if equivalent, otherwise false.
99      */
100     function equals($other)
101     {
102         return ($this->name == $other->name &&
103                 $this->_typeMatch($other) &&
104                 $this->_defaultMatch($other) &&
105                 $this->_nullMatch($other) &&
106                 $this->key == $other->key &&
107                 $this->auto_increment == $other->auto_increment);
108     }
109
110     /**
111      * Does the type of this column match the
112      * type of the other column?
113      *
114      * Checks the type and size of a column. Tries
115      * to ignore differences between synonymous
116      * data types, like 'integer' and 'int'.
117      *
118      * @param ColumnDef $other other column to check
119      *
120      * @return boolean true if they're about equivalent
121      */
122     private function _typeMatch($other)
123     {
124         switch ($this->type) {
125         case 'integer':
126         case 'int':
127             return ($other->type == 'integer' ||
128                     $other->type == 'int');
129             break;
130         default:
131             return ($this->type == $other->type &&
132                     $this->size == $other->size);
133         }
134     }
135
136     /**
137      * Does the default behaviour of this column match
138      * the other?
139      *
140      * @param ColumnDef $other other column to check
141      *
142      * @return boolean true if defaults are effectively the same.
143      */
144     private function _defaultMatch($other)
145     {
146         return ((is_null($this->default) && is_null($other->default)) ||
147                 ($this->default == $other->default));
148     }
149
150     /**
151      * Does the null behaviour of this column match
152      * the other?
153      *
154      * @param ColumnDef $other other column to check
155      *
156      * @return boolean true if these columns 'null' the same.
157      */
158     private function _nullMatch($other)
159     {
160         return ((!is_null($this->default) && !is_null($other->default) &&
161                  $this->default == $other->default) ||
162                 ($this->nullable == $other->nullable));
163     }
164 }