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