Skip to content

Commit ab778e9

Browse files
committed
Refactored field to something more appropriate depending on the context in which it is used.
1 parent 1abbd1d commit ab778e9

21 files changed

Lines changed: 66 additions & 66 deletions

src/Components/AlterOperation.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Parses a reference to a field.
4+
* Parses an alter operation.
55
*
66
* @package SqlParser
77
* @subpackage Components
@@ -14,7 +14,7 @@
1414
use SqlParser\TokensList;
1515

1616
/**
17-
* Parses a reference to a field.
17+
* Parses an alter operation.
1818
*
1919
* @category Components
2020
* @package SqlParser
@@ -178,10 +178,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
178178
++$brackets;
179179
} elseif ($token->value === ')') {
180180
--$brackets;
181-
} elseif ($token->value === ',') {
182-
if ($brackets === 0) {
183-
break;
184-
}
181+
} elseif (($token->value === ',') && ($brackets === 0)) {
182+
break;
185183
}
186184
}
187185
$ret->unknown[] = $token;
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Parses the definition of a field.
4+
* Parses the create definition of a column or a key.
55
*
66
* Used for parsing `CREATE TABLE` statement.
77
*
@@ -17,7 +17,7 @@
1717
use SqlParser\TokensList;
1818

1919
/**
20-
* Parses the definition of a field.
20+
* Parses the create definition of a column or a key.
2121
*
2222
* Used for parsing `CREATE TABLE` statement.
2323
*
@@ -27,7 +27,7 @@
2727
* @author Dan Ungureanu <udan1107@gmail.com>
2828
* @license http://opensource.org/licenses/GPL-2.0 GNU Public License
2929
*/
30-
class FieldDefinition extends Component
30+
class CreateDefinition extends Component
3131
{
3232

3333
/**
@@ -145,13 +145,13 @@ public function __construct($name = null, $options = null, $type = null,
145145
* @param TokensList $list The list of tokens that are being parsed.
146146
* @param array $options Parameters for parsing.
147147
*
148-
* @return FieldDefinition[]
148+
* @return CreateDefinition[]
149149
*/
150150
public static function parse(Parser $parser, TokensList $list, array $options = array())
151151
{
152152
$ret = array();
153153

154-
$expr = new FieldDefinition();
154+
$expr = new CreateDefinition();
155155

156156
/**
157157
* The state of the parser.
@@ -234,7 +234,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
234234
if ((!empty($expr->type)) || (!empty($expr->key))) {
235235
$ret[] = $expr;
236236
}
237-
$expr = new FieldDefinition();
237+
$expr = new CreateDefinition();
238238
if ($token->value === ',') {
239239
$state = 1;
240240
} elseif ($token->value === ')') {
@@ -269,7 +269,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
269269
}
270270

271271
/**
272-
* @param FieldDefinition|FieldDefinition[] $component The component to be built.
272+
* @param CreateDefinition|CreateDefinition[] $component The component to be built.
273273
*
274274
* @return string
275275
*/

src/Components/Expression.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

33
/**
4-
* Parses a reference to a field.
4+
* Parses a reference to an expression (column, table or database name, function
5+
* call, mathematical expression, etc.).
56
*
67
* @package SqlParser
78
* @subpackage Components
@@ -15,7 +16,8 @@
1516
use SqlParser\TokensList;
1617

1718
/**
18-
* Parses a reference to a field.
19+
* Parses a reference to an expression (column, table or database name, function
20+
* call, mathematical expression, etc.).
1921
*
2022
* @category Components
2123
* @package SqlParser

src/Components/ExpressionArray.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Parses a a list of fields delimited by a single comma.
4+
* Parses a a list of expression delimited by a comma.
55
*
66
* @package SqlParser
77
* @subpackage Components
@@ -14,7 +14,7 @@
1414
use SqlParser\TokensList;
1515

1616
/**
17-
* Parses a a list of fields delimited by a single comma.
17+
* Parses a a list of expression delimited by a comma.
1818
*
1919
* @category Keywords
2020
* @package SqlParser

src/Components/IntoKeyword.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class IntoKeyword extends Component
4444
*
4545
* @var array
4646
*/
47-
public $fields;
47+
public $columns;
4848

4949
/**
5050
* @param Parser $parser The parser that serves as context.
@@ -114,7 +114,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
114114
$state = 1;
115115
} elseif ($state === 1) {
116116
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
117-
$ret->fields = ArrayObj::parse($parser, $list)->values;
117+
$ret->columns = ArrayObj::parse($parser, $list)->values;
118118
++$list->idx;
119119
}
120120
break;

src/Components/OrderKeyword.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class OrderKeyword extends Component
2626
{
2727

2828
/**
29-
* The field that is used for ordering.
29+
* The expression that is used for ordering.
3030
*
3131
* @var Expression
3232
*/
33-
public $field;
33+
public $expr;
3434

3535
/**
3636
* The order type.
@@ -42,12 +42,12 @@ class OrderKeyword extends Component
4242
/**
4343
* Constructor.
4444
*
45-
* @param Expression $field The field that we are sorting by.
45+
* @param Expression $expr The expression that we are sorting by.
4646
* @param string $type The sorting type.
4747
*/
48-
public function __construct($field = null, $type = 'ASC')
48+
public function __construct($expr = null, $type = 'ASC')
4949
{
50-
$this->field = $field;
50+
$this->expr = $expr;
5151
$this->type = $type;
5252
}
5353

@@ -69,7 +69,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
6969
*
7070
* Below are the states of the parser.
7171
*
72-
* 0 ----------------------[ field ]----------------------> 1
72+
* 0 --------------------[ expression ]-------------------> 1
7373
*
7474
* 1 ------------------------[ , ]------------------------> 0
7575
* 1 -------------------[ ASC / DESC ]--------------------> 1
@@ -96,13 +96,13 @@ public static function parse(Parser $parser, TokensList $list, array $options =
9696
}
9797

9898
if ($state === 0) {
99-
$expr->field = Expression::parse($parser, $list);
99+
$expr->expr = Expression::parse($parser, $list);
100100
$state = 1;
101101
} elseif ($state === 1) {
102102
if (($token->type === Token::TYPE_KEYWORD) && (($token->value === 'ASC') || ($token->value === 'DESC'))) {
103103
$expr->type = $token->value;
104104
} elseif (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
105-
if (!empty($expr->field)) {
105+
if (!empty($expr->expr)) {
106106
$ret[] = $expr;
107107
}
108108
$expr = new OrderKeyword();
@@ -115,7 +115,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
115115
}
116116

117117
// Last iteration was not processed.
118-
if (!empty($expr->field)) {
118+
if (!empty($expr->expr)) {
119119
$ret[] = $expr;
120120
}
121121

@@ -137,7 +137,7 @@ public static function build($component)
137137
}
138138
return implode(", ", $ret);
139139
} else {
140-
return Expression::build($component->field) . ' ' . $component->type;
140+
return Expression::build($component->expr) . ' ' . $component->type;
141141
}
142142
}
143143
}

src/Components/SetOperation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
5757
*
5858
* Below are the states of the parser.
5959
*
60-
* 0 -------------------[ field name ]--------------------> 1
60+
* 0 -------------------[ column name ]-------------------> 1
6161
*
6262
* 1 ------------------------[ , ]------------------------> 0
6363
* 1 ----------------------[ value ]----------------------> 1

src/Statements/CreateStatement.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use SqlParser\TokensList;
1515
use SqlParser\Components\ArrayObj;
1616
use SqlParser\Components\DataType;
17-
use SqlParser\Components\FieldDefinition;
17+
use SqlParser\Components\CreateDefinition;
1818
use SqlParser\Components\Expression;
1919
use SqlParser\Components\OptionsArray;
2020
use SqlParser\Components\ParameterDefinition;
@@ -164,12 +164,12 @@ class CreateStatement extends Statement
164164
public $entityOptions;
165165

166166
/**
167-
* If `CREATE TABLE`, a list of fields in the new table.
167+
* If `CREATE TABLE`, a list of columns and keys.
168168
* If `CREATE VIEW`, a list of columns.
169169
*
170170
* Used by `CREATE TABLE` and `CREATE VIEW`.
171171
*
172-
* @var FieldDefinition[]|ArrayObj
172+
* @var CreateDefinition[]|ArrayObj
173173
*/
174174
public $fields;
175175

@@ -218,7 +218,7 @@ public function build()
218218
$fields = '';
219219
if (!empty($this->fields)) {
220220
if (is_array($this->fields)) {
221-
$fields = FieldDefinition::build($this->fields) . ' ';
221+
$fields = CreateDefinition::build($this->fields) . ' ';
222222
} elseif ($this->fields instanceof ArrayObj) {
223223
$fields = ArrayObj::build($this->fields);
224224
}
@@ -304,7 +304,7 @@ public function parse(Parser $parser, TokensList $list)
304304
static::$DB_OPTIONS
305305
);
306306
} elseif ($this->options->has('TABLE')) {
307-
$this->fields = FieldDefinition::parse($parser, $list);
307+
$this->fields = CreateDefinition::parse($parser, $list);
308308
if (empty($this->fields)) {
309309
$parser->error(
310310
__('At least one column definition was expected.'),

src/Utils/Query.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,40 +449,40 @@ public static function getAll($query)
449449
*/
450450
public static function getTables($statement)
451451
{
452-
$fields = array();
452+
$expressions = array();
453453

454454
if (($statement instanceof InsertStatement)
455455
|| ($statement instanceof ReplaceStatement)
456456
) {
457-
$fields = array($statement->into->dest);
457+
$expressions = array($statement->into->dest);
458458
} elseif ($statement instanceof UpdateStatement) {
459-
$fields = $statement->tables;
459+
$expressions = $statement->tables;
460460
} elseif (($statement instanceof SelectStatement)
461461
|| ($statement instanceof DeleteStatement)
462462
) {
463-
$fields = $statement->from;
463+
$expressions = $statement->from;
464464
} elseif (($statement instanceof AlterStatement)
465465
|| ($statement instanceof TruncateStatement)
466466
) {
467-
$fields = array($statement->table);
467+
$expressions = array($statement->table);
468468
} elseif ($statement instanceof DropStatement) {
469469
if (!$statement->options->has('TABLE')) {
470470
// No tables are dropped.
471471
return array();
472472
}
473-
$fields = $statement->fields;
473+
$expressions = $statement->fields;
474474
} elseif ($statement instanceof RenameStatement) {
475475
foreach ($statement->renames as $rename) {
476-
$fields[] = $rename->old;
476+
$expressions[] = $rename->old;
477477
}
478478
}
479479

480480
$ret = array();
481-
foreach ($fields as $field) {
482-
if (!empty($field->table)) {
483-
$field->expr = null; // Force rebuild.
484-
$field->alias = null; // Aliases are not required.
485-
$ret[] = Expression::build($field);
481+
foreach ($expressions as $expr) {
482+
if (!empty($expr->table)) {
483+
$expr->expr = null; // Force rebuild.
484+
$expr->alias = null; // Aliases are not required.
485+
$ret[] = Expression::build($expr);
486486
}
487487
}
488488
return $ret;

tests/Builder/CreateStatementTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use SqlParser\Parser;
66
use SqlParser\Components\DataType;
77
use SqlParser\Components\Expression;
8-
use SqlParser\Components\FieldDefinition;
8+
use SqlParser\Components\CreateDefinition;
99
use SqlParser\Components\Key;
1010
use SqlParser\Components\OptionsArray;
1111
use SqlParser\Statements\CreateStatement;
@@ -44,12 +44,12 @@ public function testBuilderTable()
4444
$stmt->name = new Expression('', 'test', '');
4545
$stmt->options = new OptionsArray(array('TABLE'));
4646
$stmt->fields = array(
47-
new FieldDefinition(
47+
new CreateDefinition(
4848
'id',
4949
new OptionsArray(array('NOT NULL', 'AUTO_INCREMENT')),
5050
new DataType('INT', array(11), new OptionsArray(array('UNSIGNED')))
5151
),
52-
new FieldDefinition(
52+
new CreateDefinition(
5353
'',
5454
null,
5555
new Key('', array('id'), 'PRIMARY KEY')

0 commit comments

Comments
 (0)