|
1 <?php |
|
2 /* |
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14 * |
|
15 * This software consists of voluntary contributions made by many individuals |
|
16 * and is licensed under the LGPL. For more information, see |
|
17 * <http://www.doctrine-project.org>. |
|
18 */ |
|
19 |
|
20 namespace Doctrine\DBAL\Schema; |
|
21 |
|
22 use \Doctrine\DBAL\Types\Type; |
|
23 use Doctrine\DBAL\Schema\Visitor\Visitor; |
|
24 |
|
25 /** |
|
26 * Object representation of a database column |
|
27 * |
|
28 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
29 * @link www.doctrine-project.org |
|
30 * @since 2.0 |
|
31 * @version $Revision$ |
|
32 * @author Benjamin Eberlei <kontakt@beberlei.de> |
|
33 */ |
|
34 class Column extends AbstractAsset |
|
35 { |
|
36 /** |
|
37 * @var \Doctrine\DBAL\Types\Type |
|
38 */ |
|
39 protected $_type; |
|
40 |
|
41 /** |
|
42 * @var int |
|
43 */ |
|
44 protected $_length = null; |
|
45 |
|
46 /** |
|
47 * @var int |
|
48 */ |
|
49 protected $_precision = 10; |
|
50 |
|
51 /** |
|
52 * @var int |
|
53 */ |
|
54 protected $_scale = 0; |
|
55 |
|
56 /** |
|
57 * @var bool |
|
58 */ |
|
59 protected $_unsigned = false; |
|
60 |
|
61 /** |
|
62 * @var bool |
|
63 */ |
|
64 protected $_fixed = false; |
|
65 |
|
66 /** |
|
67 * @var bool |
|
68 */ |
|
69 protected $_notnull = true; |
|
70 |
|
71 /** |
|
72 * @var string |
|
73 */ |
|
74 protected $_default = null; |
|
75 |
|
76 /** |
|
77 * @var bool |
|
78 */ |
|
79 protected $_autoincrement = false; |
|
80 |
|
81 /** |
|
82 * @var array |
|
83 */ |
|
84 protected $_platformOptions = array(); |
|
85 |
|
86 /** |
|
87 * @var string |
|
88 */ |
|
89 protected $_columnDefinition = null; |
|
90 |
|
91 /** |
|
92 * @var string |
|
93 */ |
|
94 protected $_comment = null; |
|
95 |
|
96 /** |
|
97 * Create a new Column |
|
98 * |
|
99 * @param string $columnName |
|
100 * @param Doctrine\DBAL\Types\Type $type |
|
101 * @param int $length |
|
102 * @param bool $notNull |
|
103 * @param mixed $default |
|
104 * @param bool $unsigned |
|
105 * @param bool $fixed |
|
106 * @param int $precision |
|
107 * @param int $scale |
|
108 * @param array $platformOptions |
|
109 */ |
|
110 public function __construct($columnName, Type $type, array $options=array()) |
|
111 { |
|
112 $this->_setName($columnName); |
|
113 $this->setType($type); |
|
114 $this->setOptions($options); |
|
115 } |
|
116 |
|
117 /** |
|
118 * @param array $options |
|
119 * @return Column |
|
120 */ |
|
121 public function setOptions(array $options) |
|
122 { |
|
123 foreach ($options AS $name => $value) { |
|
124 $method = "set".$name; |
|
125 if (method_exists($this, $method)) { |
|
126 $this->$method($value); |
|
127 } |
|
128 } |
|
129 return $this; |
|
130 } |
|
131 |
|
132 /** |
|
133 * @param Type $type |
|
134 * @return Column |
|
135 */ |
|
136 public function setType(Type $type) |
|
137 { |
|
138 $this->_type = $type; |
|
139 return $this; |
|
140 } |
|
141 |
|
142 /** |
|
143 * @param int $length |
|
144 * @return Column |
|
145 */ |
|
146 public function setLength($length) |
|
147 { |
|
148 if($length !== null) { |
|
149 $this->_length = (int)$length; |
|
150 } else { |
|
151 $this->_length = null; |
|
152 } |
|
153 return $this; |
|
154 } |
|
155 |
|
156 /** |
|
157 * @param int $precision |
|
158 * @return Column |
|
159 */ |
|
160 public function setPrecision($precision) |
|
161 { |
|
162 if (!is_numeric($precision)) { |
|
163 $precision = 10; // defaults to 10 when no valid precision is given. |
|
164 } |
|
165 |
|
166 $this->_precision = (int)$precision; |
|
167 return $this; |
|
168 } |
|
169 |
|
170 /** |
|
171 * @param int $scale |
|
172 * @return Column |
|
173 */ |
|
174 public function setScale($scale) |
|
175 { |
|
176 if (!is_numeric($scale)) { |
|
177 $scale = 0; |
|
178 } |
|
179 |
|
180 $this->_scale = (int)$scale; |
|
181 return $this; |
|
182 } |
|
183 |
|
184 /** |
|
185 * |
|
186 * @param bool $unsigned |
|
187 * @return Column |
|
188 */ |
|
189 public function setUnsigned($unsigned) |
|
190 { |
|
191 $this->_unsigned = (bool)$unsigned; |
|
192 return $this; |
|
193 } |
|
194 |
|
195 /** |
|
196 * |
|
197 * @param bool $fixed |
|
198 * @return Column |
|
199 */ |
|
200 public function setFixed($fixed) |
|
201 { |
|
202 $this->_fixed = (bool)$fixed; |
|
203 return $this; |
|
204 } |
|
205 |
|
206 /** |
|
207 * @param bool $notnull |
|
208 * @return Column |
|
209 */ |
|
210 public function setNotnull($notnull) |
|
211 { |
|
212 $this->_notnull = (bool)$notnull; |
|
213 return $this; |
|
214 } |
|
215 |
|
216 /** |
|
217 * |
|
218 * @param mixed $default |
|
219 * @return Column |
|
220 */ |
|
221 public function setDefault($default) |
|
222 { |
|
223 $this->_default = $default; |
|
224 return $this; |
|
225 } |
|
226 |
|
227 /** |
|
228 * |
|
229 * @param array $platformOptions |
|
230 * @return Column |
|
231 */ |
|
232 public function setPlatformOptions(array $platformOptions) |
|
233 { |
|
234 $this->_platformOptions = $platformOptions; |
|
235 return $this; |
|
236 } |
|
237 |
|
238 /** |
|
239 * |
|
240 * @param string $name |
|
241 * @param mixed $value |
|
242 * @return Column |
|
243 */ |
|
244 public function setPlatformOption($name, $value) |
|
245 { |
|
246 $this->_platformOptions[$name] = $value; |
|
247 return $this; |
|
248 } |
|
249 |
|
250 /** |
|
251 * |
|
252 * @param string |
|
253 * @return Column |
|
254 */ |
|
255 public function setColumnDefinition($value) |
|
256 { |
|
257 $this->_columnDefinition = $value; |
|
258 return $this; |
|
259 } |
|
260 |
|
261 public function getType() |
|
262 { |
|
263 return $this->_type; |
|
264 } |
|
265 |
|
266 public function getLength() |
|
267 { |
|
268 return $this->_length; |
|
269 } |
|
270 |
|
271 public function getPrecision() |
|
272 { |
|
273 return $this->_precision; |
|
274 } |
|
275 |
|
276 public function getScale() |
|
277 { |
|
278 return $this->_scale; |
|
279 } |
|
280 |
|
281 public function getUnsigned() |
|
282 { |
|
283 return $this->_unsigned; |
|
284 } |
|
285 |
|
286 public function getFixed() |
|
287 { |
|
288 return $this->_fixed; |
|
289 } |
|
290 |
|
291 public function getNotnull() |
|
292 { |
|
293 return $this->_notnull; |
|
294 } |
|
295 |
|
296 public function getDefault() |
|
297 { |
|
298 return $this->_default; |
|
299 } |
|
300 |
|
301 public function getPlatformOptions() |
|
302 { |
|
303 return $this->_platformOptions; |
|
304 } |
|
305 |
|
306 public function hasPlatformOption($name) |
|
307 { |
|
308 return isset($this->_platformOptions[$name]); |
|
309 } |
|
310 |
|
311 public function getPlatformOption($name) |
|
312 { |
|
313 return $this->_platformOptions[$name]; |
|
314 } |
|
315 |
|
316 public function getColumnDefinition() |
|
317 { |
|
318 return $this->_columnDefinition; |
|
319 } |
|
320 |
|
321 public function getAutoincrement() |
|
322 { |
|
323 return $this->_autoincrement; |
|
324 } |
|
325 |
|
326 public function setAutoincrement($flag) |
|
327 { |
|
328 $this->_autoincrement = $flag; |
|
329 return $this; |
|
330 } |
|
331 |
|
332 public function setComment($comment) |
|
333 { |
|
334 $this->_comment = $comment; |
|
335 return $this; |
|
336 } |
|
337 |
|
338 public function getComment() |
|
339 { |
|
340 return $this->_comment; |
|
341 } |
|
342 |
|
343 /** |
|
344 * @param Visitor $visitor |
|
345 */ |
|
346 public function visit(\Doctrine\DBAL\Schema\Visitor $visitor) |
|
347 { |
|
348 $visitor->accept($this); |
|
349 } |
|
350 |
|
351 /** |
|
352 * @return array |
|
353 */ |
|
354 public function toArray() |
|
355 { |
|
356 return array_merge(array( |
|
357 'name' => $this->_name, |
|
358 'type' => $this->_type, |
|
359 'default' => $this->_default, |
|
360 'notnull' => $this->_notnull, |
|
361 'length' => $this->_length, |
|
362 'precision' => $this->_precision, |
|
363 'scale' => $this->_scale, |
|
364 'fixed' => $this->_fixed, |
|
365 'unsigned' => $this->_unsigned, |
|
366 'autoincrement' => $this->_autoincrement, |
|
367 'columnDefinition' => $this->_columnDefinition, |
|
368 'comment' => $this->_comment, |
|
369 ), $this->_platformOptions); |
|
370 } |
|
371 } |