Skip to content

Commit a1264fb

Browse files
committed
prepared statements: more types
1 parent 85f39e7 commit a1264fb

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

.vscode/c_cpp_properties.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"compilerPath": "/usr/bin/g++",
1010
"cStandard": "gnu11",
1111
"cppStandard": "gnu++11",
12-
"intelliSenseMode": "clang-x64",
12+
"intelliSenseMode": "gcc-x64",
1313
"compilerArgs": [
1414
"-lssl"
1515
]

src/Connection.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,23 @@ private function WritePreparedStatement(string $sql, array $params) {
506506
$escaped[] = preg_replace('/[^0-9\.]/', '', $param);
507507
}
508508
else if ($type == "timestamp") {
509-
$escaped[] = "TIMESTAMP '".preg_replace('/[^0-9\.\-\: ]/', '', $param)."'";
509+
$escaped[] = "TIMESTAMP '".$this->Escape($param)."'";
510510
}
511+
else if ($type == "int" || $type == "bigint") {
512+
$escaped[] = (string)((int)$param);
513+
}
514+
else if ($type == "double" || $type == "real") {
515+
$escaped[] = (string)((float)$param);
516+
}
517+
518+
// TODO: TIME, INTERVAL, boolean, binary ?
519+
511520
else {
512-
$escaped[] = @"'".$this->Escape((string)$param)."'";
521+
$escaped[] = "'".$this->Escape($param)."'";
513522
}
514523
}
515-
elseif (is_numeric($param) && !is_string($param)) {
516-
$escaped[] = $param + 0;
524+
elseif (is_float($param) || is_integer($param)) {
525+
$escaped[] = (string)$param;
517526
}
518527
elseif ($param instanceof DateTime) {
519528
if ($type == "date") {
@@ -523,11 +532,17 @@ private function WritePreparedStatement(string $sql, array $params) {
523532
}
524533
}
525534
else {
526-
$escaped[] = @"'".$this->Escape((string)$param)."'";
535+
$gotType = gettype($param);
536+
if ($gotType == "object") {
537+
$gotType = get_class($param);
538+
}
539+
540+
throw new MonetException("Parameter ".($index + 1)." has invalid PHP type: '{$gotType}'. "
541+
."(Expected SQL type: '{$type}'.)");
527542
}
528543
}
529544

530-
$this->Write("sEXECUTE {$id}(".implode(",", $escaped).");");
545+
$this->Write("sEXECUTE {$id}(".implode(", ", $escaped).");");
531546
}
532547

533548
/**

tests/preparedTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ public function testDec38(): void {
4646

4747
public function testInt32(): void {
4848
$this->InsertValuePrepared('i', 123456, '123456');
49+
$this->InsertValuePrepared('i', '123456', '123456');
4950
}
5051

5152
public function testInt64(): void {
5253
$this->InsertValuePrepared('b', 1234567890987654321, '1234567890987654321');
54+
$this->InsertValuePrepared('b', '1234567890987654321', '1234567890987654321');
5355
}
5456

5557
public function testTimestamp(): void {
@@ -69,4 +71,20 @@ public function testDate(): void {
6971
$ts = new DateTime();
7072
$this->InsertValuePrepared('dat', $ts, $ts->format('Y-m-d'));
7173
}
74+
75+
public function testDouble(): void {
76+
$this->InsertValuePrepared('f', 3.14159265, '3.14159265');
77+
$this->InsertValuePrepared('f', '3.14159265', '3.14159265');
78+
79+
$this->InsertValuePrepared('dbl', 3.14159265, '3.14159265');
80+
$this->InsertValuePrepared('dbl', '3.14159265', '3.14159265');
81+
82+
$this->InsertValuePrepared('dbl2', 3.14159265, '3.14159265');
83+
$this->InsertValuePrepared('dbl2', '3.14159265', '3.14159265');
84+
}
85+
86+
public function testReal(): void {
87+
$this->InsertValuePrepared('r', 3.141592, '3.141592');
88+
$this->InsertValuePrepared('r', '3.141592', '3.141592');
89+
}
7290
}

0 commit comments

Comments
 (0)