|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2015 Repo2 |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
16
|
|
|
* copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
24
|
|
|
* SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace Repo2\QueryBuilder\Expression; |
|
28
|
|
|
|
|
29
|
|
|
use Repo2\QueryBuilder\DDL\DataType; |
|
30
|
|
|
use Repo2\QueryBuilder\DriverInterface; |
|
31
|
|
|
use Repo2\QueryBuilder\Exception\CompileException; |
|
32
|
|
|
|
|
33
|
|
|
class Column extends Reference |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var DataType */ |
|
36
|
|
|
private $type; |
|
37
|
|
|
|
|
38
|
|
|
/** @var boolean */ |
|
39
|
|
|
private $nullable = true; |
|
40
|
|
|
|
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
private $defval; |
|
43
|
|
|
|
|
44
|
|
|
/** @var boolean */ |
|
45
|
|
|
private $primary = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return Column |
|
49
|
|
|
*/ |
|
50
|
12 |
|
public function integer() |
|
51
|
|
|
{ |
|
52
|
12 |
|
return $this->type(new DataType\Scalar('integer')); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param int $length |
|
57
|
|
|
* @return Column |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function varchar($length) |
|
60
|
|
|
{ |
|
61
|
3 |
|
return $this->type(new DataType\ScalarLength('varchar', $length)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Column |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function bigint() |
|
68
|
|
|
{ |
|
69
|
3 |
|
return $this->type(new DataType\Scalar('bigint')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return Column |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public function timestamp() |
|
76
|
|
|
{ |
|
77
|
3 |
|
return $this->type(new DataType\Scalar('timestamp')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param DataType $type |
|
82
|
|
|
* @return Column |
|
83
|
|
|
*/ |
|
84
|
24 |
|
public function type(DataType $type) |
|
85
|
|
|
{ |
|
86
|
24 |
|
$this->type = $type; |
|
87
|
24 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set NOT NULL option. |
|
92
|
|
|
* |
|
93
|
|
|
* @return Column |
|
94
|
|
|
*/ |
|
95
|
3 |
|
public function required() |
|
96
|
|
|
{ |
|
97
|
3 |
|
$this->nullable = false; |
|
98
|
3 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set default value. |
|
103
|
|
|
* |
|
104
|
|
|
* @param mixed $val |
|
105
|
|
|
* @return Column |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function defval($val) |
|
108
|
|
|
{ |
|
109
|
3 |
|
$this->defval = $val; |
|
110
|
3 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Defines the column as primary key. |
|
115
|
|
|
* |
|
116
|
|
|
* @return Column |
|
117
|
|
|
*/ |
|
118
|
3 |
|
public function primary() |
|
119
|
|
|
{ |
|
120
|
3 |
|
$this->primary = true; |
|
121
|
3 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritDoc |
|
126
|
|
|
*/ |
|
127
|
27 |
|
public function compile(DriverInterface $driver) |
|
128
|
|
|
{ |
|
129
|
27 |
|
if (!$this->type) { |
|
130
|
3 |
|
throw new CompileException(sprintf('Data type for the column "%s" not defined.', $this->ref)); |
|
131
|
|
|
} |
|
132
|
24 |
|
$compiled = parent::compile($driver) . ' ' . $this->type->compile(); |
|
133
|
24 |
|
if ($this->primary) { |
|
134
|
3 |
|
return $this->compilePrimary($compiled); |
|
135
|
|
|
} |
|
136
|
21 |
|
return $this->compileRegular($driver, $compiled); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $compiled |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
3 |
|
private function compilePrimary($compiled) |
|
144
|
|
|
{ |
|
145
|
3 |
|
return $compiled . ' PRIMARY KEY'; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param DriverInterface $driver |
|
150
|
|
|
* @param string $compiled |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
21 |
|
private function compileRegular(DriverInterface $driver, $compiled) |
|
154
|
|
|
{ |
|
155
|
21 |
|
if (!$this->nullable) { |
|
156
|
3 |
|
$compiled .= ' NOT NULL'; |
|
157
|
2 |
|
} |
|
158
|
21 |
|
if (null !== $this->defval) { |
|
159
|
3 |
|
$compiled .= ' DEFAULT "' . $driver->escapeValue($this->defval) . '"'; |
|
160
|
2 |
|
} |
|
161
|
21 |
|
return $compiled; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|