1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* `REFERENCES` keyword parser. |
5
|
|
|
* |
6
|
|
|
* @package SqlParser |
7
|
|
|
* @subpackage Components |
8
|
|
|
*/ |
9
|
|
|
namespace SqlParser\Components; |
10
|
|
|
|
11
|
|
|
use SqlParser\Context; |
12
|
|
|
use SqlParser\Component; |
13
|
|
|
use SqlParser\Parser; |
14
|
|
|
use SqlParser\Token; |
15
|
|
|
use SqlParser\TokensList; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* `REFERENCES` keyword parser. |
19
|
|
|
* |
20
|
|
|
* @category Keywords |
21
|
|
|
* @package SqlParser |
22
|
|
|
* @subpackage Components |
23
|
|
|
* @author Dan Ungureanu <[email protected]> |
24
|
|
|
* @license http://opensource.org/licenses/GPL-2.0 GNU Public License |
25
|
|
|
*/ |
26
|
|
|
class Reference extends Component |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* All references options. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
public static $REFERENCES_OPTIONS = array( |
35
|
|
|
'MATCH' => array(1, 'var'), |
36
|
|
|
'ON DELETE' => array(2, 'var'), |
37
|
|
|
'ON UPDATE' => array(3, 'var'), |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The referenced table. |
42
|
|
|
* |
43
|
|
|
* @var Expression |
44
|
|
|
*/ |
45
|
|
|
public $table; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The referenced columns. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
public $columns; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The options of the referencing. |
56
|
|
|
* |
57
|
|
|
* @var OptionsArray |
58
|
|
|
*/ |
59
|
|
|
public $options; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor. |
63
|
|
|
* |
64
|
|
|
* @param Expression $table The name of the table referenced. |
|
|
|
|
65
|
|
|
* @param array $columns The columns referenced. |
66
|
|
|
* @param OptionsArray $options The options. |
|
|
|
|
67
|
|
|
*/ |
68
|
7 |
|
public function __construct($table = null, array $columns = array(), $options = null) |
69
|
|
|
{ |
70
|
7 |
|
$this->table = $table; |
|
|
|
|
71
|
7 |
|
$this->columns = $columns; |
72
|
7 |
|
$this->options = $options; |
73
|
7 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Parser $parser The parser that serves as context. |
77
|
|
|
* @param TokensList $list The list of tokens that are being parsed. |
78
|
|
|
* @param array $options Parameters for parsing. |
79
|
|
|
* |
80
|
|
|
* @return Reference |
81
|
|
|
*/ |
82
|
6 |
|
public static function parse(Parser $parser, TokensList $list, array $options = array()) |
83
|
|
|
{ |
84
|
6 |
|
$ret = new Reference(); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The state of the parser. |
88
|
|
|
* |
89
|
|
|
* Below are the states of the parser. |
90
|
|
|
* |
91
|
|
|
* 0 ----------------------[ table ]---------------------> 1 |
92
|
|
|
* |
93
|
|
|
* 1 ---------------------[ columns ]--------------------> 2 |
94
|
|
|
* |
95
|
|
|
* 2 ---------------------[ options ]--------------------> (END) |
96
|
|
|
* |
97
|
|
|
* @var int $state |
98
|
|
|
*/ |
99
|
6 |
|
$state = 0; |
100
|
|
|
|
101
|
6 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
102
|
|
|
/** |
103
|
|
|
* Token parsed at this moment. |
104
|
|
|
* |
105
|
|
|
* @var Token $token |
106
|
|
|
*/ |
107
|
6 |
|
$token = $list->tokens[$list->idx]; |
108
|
|
|
|
109
|
|
|
// End of statement. |
110
|
6 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
111
|
1 |
|
break; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Skipping whitespaces and comments. |
115
|
6 |
|
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
116
|
5 |
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
6 |
|
if ($state === 0) { |
120
|
6 |
|
$ret->table = Expression::parse( |
121
|
6 |
|
$parser, |
122
|
6 |
|
$list, |
123
|
|
|
array( |
124
|
6 |
|
'parseField' => 'table', |
125
|
6 |
|
'breakOnAlias' => true, |
126
|
|
|
) |
127
|
6 |
|
); |
128
|
6 |
|
$state = 1; |
|
|
|
|
129
|
6 |
|
} elseif ($state === 1) { |
130
|
6 |
|
$ret->columns = ArrayObj::parse($parser, $list)->values; |
131
|
6 |
|
$state = 2; |
|
|
|
|
132
|
6 |
|
} elseif ($state === 2) { |
133
|
5 |
|
$ret->options = OptionsArray::parse($parser, $list, static::$REFERENCES_OPTIONS); |
134
|
5 |
|
++$list->idx; |
135
|
5 |
|
break; |
136
|
|
|
} |
137
|
|
|
|
138
|
6 |
|
} |
139
|
|
|
|
140
|
6 |
|
--$list->idx; |
141
|
6 |
|
return $ret; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param Reference $component The component to be built. |
146
|
|
|
* @param array $options Parameters for building. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
2 |
|
public static function build($component, array $options = array()) |
151
|
|
|
{ |
152
|
2 |
|
return trim( |
153
|
2 |
|
$component->table |
154
|
2 |
|
. ' (' . implode(', ', Context::escape($component->columns)) . ') ' |
155
|
2 |
|
. $component->options |
156
|
2 |
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.