Skip to content

Commit ce313ae

Browse files
committed
Improved Table utilities.
Fixed coding style.
1 parent 68ad40b commit ce313ae

5 files changed

Lines changed: 48 additions & 9 deletions

File tree

src/Component.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public static function build($component)
6565
*
6666
* @return string
6767
*/
68-
public function __toString() {
68+
public function __toString()
69+
{
6970
return static::build($this);
7071
}
7172
}

src/Components/FieldDefinition.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class FieldDefinition extends Component
5353
'AS' => array(2, 'expr', array('bracketsDelimited' => true)),
5454
'VIRTUAL' => 3,
5555
'PERSISTENT' => 3,
56+
// 'UNIQUE' => 4, // common
57+
// 'UNIQUE KEY' => 4, // common
5658
);
5759

5860
/**

src/Statement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ public function after(Parser $parser, TokensList $list, Token $token)
300300
*
301301
* @return string
302302
*/
303-
public function __toString() {
303+
public function __toString()
304+
{
304305
return static::build($this);
305306
}
306307
}

src/Utils/Table.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ public static function getFields($statement)
122122
$ret[$field->name]['on_update_current_timestamp'] = true;
123123
}
124124
}
125+
126+
if (($option = $field->options->has('AS'))) {
127+
$ret[$field->name]['generated'] = true;
128+
$ret[$field->name]['expr'] = $option->expr;
129+
}
125130
}
126131

127132
}

tests/Utils/TableTest.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,33 +139,33 @@ public function getFieldsProvider()
139139
array(
140140
'address_id' => array(
141141
'type' => 'SMALLINT',
142-
'timestamp_not_null' => null,
142+
'timestamp_not_null' => false,
143143
),
144144
'address' => array(
145145
'type' => 'VARCHAR',
146-
'timestamp_not_null' => null,
146+
'timestamp_not_null' => false,
147147
),
148148
'address2' => array(
149149
'type' => 'VARCHAR',
150-
'timestamp_not_null' => null,
150+
'timestamp_not_null' => false,
151151
'default_value' => 'NULL',
152152
),
153153
'district' => array(
154154
'type' => 'VARCHAR',
155-
'timestamp_not_null' => null,
155+
'timestamp_not_null' => false,
156156
),
157157
'city_id' => array(
158158
'type' => 'SMALLINT',
159-
'timestamp_not_null' => null,
159+
'timestamp_not_null' => false,
160160
),
161161
'postal_code' => array(
162162
'type' => 'VARCHAR',
163-
'timestamp_not_null' => null,
163+
'timestamp_not_null' => false,
164164
'default_value' => 'NULL',
165165
),
166166
'phone' => array(
167167
'type' => 'VARCHAR',
168-
'timestamp_not_null' => null,
168+
'timestamp_not_null' => false,
169169
),
170170
'last_update' => array(
171171
'type' => 'TIMESTAMP',
@@ -176,6 +176,36 @@ public function getFieldsProvider()
176176
)
177177
)
178178
),
179+
array(
180+
'CREATE TABLE table1 (
181+
a INT NOT NULL,
182+
b VARCHAR(32),
183+
c INT AS (a mod 10) VIRTUAL,
184+
d VARCHAR(5) AS (left(b,5)) PERSISTENT
185+
)',
186+
array(
187+
'a' => array(
188+
'type' => 'INT',
189+
'timestamp_not_null' => false,
190+
),
191+
'b' => array(
192+
'type' => 'VARCHAR',
193+
'timestamp_not_null' => false,
194+
),
195+
'c' => array(
196+
'type' => 'INT',
197+
'timestamp_not_null' => false,
198+
'generated' => true,
199+
'expr' => '(a mod 10)',
200+
),
201+
'd' => array(
202+
'type' => 'VARCHAR',
203+
'timestamp_not_null' => false,
204+
'generated' => true,
205+
'expr' => '(left(b,5))',
206+
),
207+
),
208+
),
179209
);
180210
}
181211
}

0 commit comments

Comments
 (0)