Skip to content

Commit 3b218ed

Browse files
authored
Merge pull request #66 from SteadyUA/master
?string now modifier. New modifier ?hex for hexadecimal strings as in…
2 parents cee0d52 + b838538 commit 3b218ed

File tree

6 files changed

+53
-38
lines changed

6 files changed

+53
-38
lines changed

goDB/Helpers/Templater.php

+6-18
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ private function valueModification($value, array $modifiers)
152152
return $this->implementation->reprNULL($this->connection);
153153
}
154154
}
155+
if ($modifiers['h']) {
156+
return $this->implementation->reprHexadecimal($this->connection, $value);
157+
}
158+
if ($modifiers['r']) {
159+
return $this->implementation->reprString($this->connection, $value);
160+
}
155161
if ($modifiers['i']) {
156162
return $this->implementation->reprInt($this->connection, $value);
157163
} elseif ($modifiers['f']) {
@@ -186,24 +192,6 @@ protected function replacement($value, array $modifiers)
186192
}
187193
return $this->valueModification($value, $modifiers);
188194
}
189-
190-
/**
191-
* ?string
192-
* @param mixed $value
193-
* @param array $modifiers
194-
* @throws DataInvalidFormat
195-
* @return string
196-
*/
197-
protected function replacementSTRING($value, array $modifiers)
198-
{
199-
if (is_array($value)) {
200-
throw new DataInvalidFormat('', 'required scalar given');
201-
}
202-
if (($modifiers['n'] || Compat::getOpt('types')) && is_null($value)) {
203-
return $this->implementation->reprNULL($this->connection);
204-
}
205-
return $this->implementation->reprString($this->connection, $value);
206-
}
207195

208196
/**
209197
* ?l, ?list

goDB/Implementations/Base.php

+12
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ public function reprString($connection, $value)
257257
return '"'.$this->escapeString($connection, $value).'"';
258258
}
259259

260+
/**
261+
* Represents a string as a hexadecimal literal
262+
*
263+
* @param $connection
264+
* @param $value
265+
* @return string
266+
*/
267+
public function reprHexadecimal($connection, $value)
268+
{
269+
return 'x\''.$this->escapeString($connection, $value).'\'';
270+
}
271+
260272
/**
261273
* Represents a integer number as a data
262274
*

goDB/_config/placeholders.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
/* The list of long synonyms */
1616
'longs' => array(
17-
'string' => 'string',
1817
'scalar' => '',
1918
'list' => 'l',
2019
'set' => 's',
@@ -30,7 +29,7 @@
3029

3130
/* The list of modifiers (the main short form) */
3231
'modifiers' => array(
33-
'n', 'i', 'f', 'b',
32+
'n', 'i', 'f', 'b', 'h', 'r'
3433
),
3534

3635
/* The list of long synonyms of modifiers */
@@ -39,5 +38,7 @@
3938
'int' => 'i',
4039
'float' => 'f',
4140
'bool' => 'b',
41+
'hex' => 'h',
42+
'string' => 'r',
4243
),
4344
);

tests/Helpers/ParserPHTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ public function providerParse()
4040
{
4141
return array(
4242
array('', '', 'n'),
43+
array('h', '', 'hn'),
4344
array('l', 'l', 'n'),
4445
array('s', 's', 'n'),
4546
array('v', 'v', 'n'),
4647
array('t', 't', 'n'),
4748
array('c', 'c', 'n'),
4849
array('e', 'e', 'n'),
4950
array('q', 'q', 'n'),
50-
array('string', 'string', 'n'),
51+
array('string', '', 'rn'),
5152
array('scalar', '', 'n'),
53+
array('hex', '', 'hn'),
5254
array('list', 'l', 'n'),
5355
array('set', 's', 'n'),
5456
array('values', 'v', 'n'),

tests/Helpers/Templater/ListTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function providerTemplater()
3939
[$list],
4040
'INSERT INTO `table` VALUES (0, 1, NULL, 3)',
4141
],
42+
'string' => [
43+
'INSERT INTO `table` VALUES (?list-string)',
44+
[$list],
45+
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5")',
46+
],
4247
'mixed' => [
4348
'INSERT INTO `table` VALUES (?lin)',
4449
[$list],

tests/Helpers/Templater/ScalarTest.php

+24-17
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,54 @@ final class ScalarTest extends Base
1717
*/
1818
public function providerTemplater()
1919
{
20-
$data = ['str"ing', 1, null, '3.5', 2.5];
20+
$data = ['str"ing', 1, null, '3.5', 2.5, '28f7dc7206a1'];
2121
return [
2222
'escape' => [
23-
'INSERT INTO `table` VALUES (?, ?scalar, ?, ?string, ?)',
23+
'INSERT INTO `table` VALUES (?, ?scalar, ?, ?string, ?, ?)',
2424
$data,
25-
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5", 2.5)',
25+
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5", 2.5, "28f7dc7206a1")',
2626
],
2727
'null' => [
28-
'INSERT INTO `table` VALUES (?null, ?null, ?null, ?null, ?null)',
28+
'INSERT INTO `table` VALUES (?null, ?null, ?null, ?null, ?null, ?null)',
2929
$data,
30-
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5", 2.5)',
30+
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5", 2.5, "28f7dc7206a1")',
3131
],
3232
'int' => [
33-
'INSERT INTO `table` VALUES (?i, ?i, ?i, ?i, ?i)',
33+
'INSERT INTO `table` VALUES (?i, ?i, ?i, ?i, ?i, ?i)',
3434
$data,
35-
'INSERT INTO `table` VALUES (0, 1, NULL, 3, 2)',
35+
'INSERT INTO `table` VALUES (0, 1, NULL, 3, 2, 28)',
3636
],
3737
'int-null' => [
38-
'INSERT INTO `table` VALUES (?in, ?in, ?in, ?in, ?in)',
38+
'INSERT INTO `table` VALUES (?in, ?in, ?in, ?in, ?in, ?in)',
3939
$data,
40-
'INSERT INTO `table` VALUES (0, 1, NULL, 3, 2)',
40+
'INSERT INTO `table` VALUES (0, 1, NULL, 3, 2, 28)',
4141
],
4242
'float' => [
43-
'INSERT INTO `table` VALUES (?f, ?f, ?f, ?f, ?f)',
43+
'INSERT INTO `table` VALUES (?f, ?f, ?f, ?f, ?f, ?f)',
4444
$data,
45-
'INSERT INTO `table` VALUES (0, 1, NULL, 3.5, 2.5)',
45+
'INSERT INTO `table` VALUES (0, 1, NULL, 3.5, 2.5, 28)',
4646
],
4747
'string' => [
48-
'INSERT INTO `table` VALUES (?string, ?string, ?string, ?string, ?string)',
48+
'INSERT INTO `table` VALUES (?string, ?string, ?string, ?string, ?string, ?string)',
4949
$data,
50-
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5", "2.5")',
50+
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5", "2.5", "28f7dc7206a1")',
5151
],
5252
'string-null' => [
53-
'INSERT INTO `table` VALUES (?string-null, ?string-null, ?string-null, ?string-null, ?string-null)',
53+
'INSERT INTO `table`'
54+
. ' VALUES (?string-null, ?string-null, ?string-null, ?string-null, ?string-null, ?string-null)',
5455
$data,
55-
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5", "2.5")',
56+
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5", "2.5", "28f7dc7206a1")',
5657
],
5758
'full' => [
58-
'INSERT INTO `table` VALUES (?string, ?scalar-int, ?scalar-null, ?scalar-int-null, ?scalar-float)',
59+
'INSERT INTO `table`'
60+
. ' VALUES (?string, ?scalar-int, ?scalar-null, ?scalar-int, ?scalar-float, ?scalar-hex)',
5961
$data,
60-
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, 3, 2.5)',
62+
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, 3, 2.5, x\'28f7dc7206a1\')',
63+
],
64+
'hex' => [
65+
'INSERT INTO `table` VALUES (?h, ?h, ?h, ?hn, ?hex, ?scalar-hex)',
66+
$data,
67+
"INSERT INTO `table` VALUES (x'str\\\"ing', x'1', NULL, x'3.5', x'2.5', x'28f7dc7206a1')",
6168
],
6269
];
6370
}

0 commit comments

Comments
 (0)