Skip to content

Commit 31e346b

Browse files
committed
Fix parsing and building of SELECT ... INTO @var1 [,@var2...]
Signed-off-by: Deven Bansod <[email protected]>
1 parent c2d9745 commit 31e346b

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

src/Components/IntoKeyword.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class IntoKeyword extends Component
4545
*/
4646
public $columns;
4747

48+
/**
49+
* The values to be selected into (SELECT .. INTO @var1)
50+
*
51+
* @var ExpressionArray
52+
*/
53+
public $values;
54+
4855
/**
4956
* @param Parser $parser The parser that serves as context.
5057
* @param TokensList $list The list of tokens that are being parsed.
@@ -102,14 +109,22 @@ public static function parse(Parser $parser, TokensList $list, array $options =
102109
}
103110

104111
if ($state === 0) {
105-
$ret->dest = Expression::parse(
106-
$parser,
107-
$list,
108-
array(
109-
'parseField' => 'table',
110-
'breakOnAlias' => true,
111-
)
112-
);
112+
if ((isset($options['fromInsert'])
113+
&& $options['fromInsert'])
114+
|| (isset($options['fromReplace'])
115+
&& $options['fromReplace'])
116+
) {
117+
$ret->dest = Expression::parse(
118+
$parser,
119+
$list,
120+
array(
121+
'parseField' => 'table',
122+
'breakOnAlias' => true,
123+
)
124+
);
125+
} else {
126+
$ret->values = ExpressionArray::parse($parser, $list);
127+
}
113128
$state = 1;
114129
} elseif ($state === 1) {
115130
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
@@ -140,6 +155,8 @@ public static function build($component, array $options = array())
140155
$columns = !empty($component->columns) ?
141156
'(`' . implode('`, `', $component->columns) . '`)' : '';
142157
return $component->dest . $columns;
158+
} elseif (isset($component->values)) {
159+
return ExpressionArray::build($component->values);
143160
} else {
144161
return 'OUTFILE "' . $component->dest . '"';
145162
}

src/Statements/InsertStatement.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ public function parse(Parser $parser, TokensList $list)
200200
break;
201201
} else {
202202
++$list->idx;
203-
$this->into = IntoKeyword::parse($parser, $list);
203+
$this->into = IntoKeyword::parse(
204+
$parser,
205+
$list,
206+
array('fromInsert' => true)
207+
);
204208
}
205209

206210
$state = 1;

src/Statements/ReplaceStatement.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ public function parse(Parser $parser, TokensList $list)
166166
break;
167167
} else {
168168
++$list->idx;
169-
$this->into = IntoKeyword::parse($parser, $list);
169+
$this->into = IntoKeyword::parse(
170+
$parser,
171+
$list,
172+
array('fromReplace' => true)
173+
);
170174
}
171175

172176
$state = 1;

tests/Components/IntoKeywordTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public function testBuild()
2323
$this->assertEquals('tbl(`col1`, `col2`)', IntoKeyword::build($component));
2424
}
2525

26+
public function testBuildValues()
27+
{
28+
$component = IntoKeyword::parse(new Parser(), $this->getTokensList('@a1, @a2, @a3'));
29+
$this->assertEquals('@a1, @a2, @a3', IntoKeyword::build($component));
30+
}
31+
2632
public function testBuildOutfile()
2733
{
2834
$component = IntoKeyword::parse(new Parser(), $this->getTokensList('OUTFILE "/tmp/outfile.txt"'));

0 commit comments

Comments
 (0)