8000 Update ssql.php · ratajs/SuperSQL@5a9bdc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a9bdc8

Browse files
authored
Update ssql.php
• Uncommon characters are passed to database in hex. • SQ::NO_ERROR makes query function return false on error. • db parameter added to __debugInfo() (either SQLite or MySQL)
1 parent df07c83 commit 5a9bdc8

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

ssql.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public function escape($string) {
141141
}
142142

143143
public function reload() {
144+
$this->connect = NULL;
144145
$this->__construct();
145146
}
146147

@@ -150,6 +151,8 @@ public function query($query, $flags = 0, $fnc = "Query") {
150151
$qr = $this->connect->query($query);
151152
if(!$qr && !($flags & self::NO_ERROR))
152153
throw new SSQLException("(" . $fnc . "): Database error: <em>" . $this->connect->errorInfo()[2] . "</em> <strong>SQL command:</strong> <code>" . (empty($query) ? "<em>Missing</em>" : $query) . "</code>");
154+
elseif(!$qr)
155+
return false;
153156
$this->result = new SSQLResult($qr);
154157
return $this->result;
155158
}
@@ -403,7 +406,14 @@ public function insert($table, $values, $flags = 0) {
403406
$valueString = "";
404407
foreach($values as $key => $value) {
405408
if($key!=array_keys($values, array_values($values)[0])[0]) $valueString.= ", ";
406-
$valueString.= $value===NULL ? "NULL" : ("'" . $this->escape($value) . "'");
409+
if($value===NULL)
410+
$valueString.= "NULL";
411+
elseif(preg_match("/^[\x{0020}-\x{007E}\x{00A0}-ſ]*$/", $value))
412+
$valueString.= "'" . $this->escape($value) . "'";
413+
elseif($this->SQLite)
414+
$valueString.= "x'" . bin2hex($value) . "'";
415+
else
416+
$valueString.= "0x" . bin2hex($value);
407417
};
408418
$r = $this->query("
409419
INSERT INTO $table$colString VALUES ($valueString)
@@ -426,7 +436,15 @@ public function update($table, $cond, $values, $flags = 128) {
426436
foreach($values as $key => $value) {
427437
if($string!="")
428438
$string.= ", ";
429-
$string.= "`" . $this->escape($key) . "`=" . ($value===NULL ? "NULL" : ("'" . $this->escape($value) . "'"));
439+
$string.= "`" . $this->escape($key) . "`=";
440+
if($value===NULL)
441+
$string.= "NULL";
442+
elseif(preg_match("/^[\x{0020}-\x{007E}\x{00A0}-ſ]*$/", $value))
443+
$string.= "'" . $this->escape($value) . "'";
444+
elseif($this->SQLite)
445+
$string.= "x'" . bin2hex($value) . "'";
446+
else
447+
$string.= "0x" . bin2hex($value);
430448
};
431449
return $this->query("
432450
UPDATE `$table` SET $string WHERE $condString
@@ -671,7 +689,7 @@ private function getCondString($a, $and, $on = false) {
671689
return rtrim($r, $and ? " AND " : " OR ");
672690
}
673691
public function __debugInfo() {
674-
return ['host' => $this->host, 'user' => $this->user, 'password' => preg_replace("/./", "*", $this->password), 'database' => $this->db, 'lastError' => $this->connect->errorInfo()];
692+
return ['db' => ($this->SQLite ? "SQLite" : "MySQL"), ($this->SQLite ? 'file' : 'host') => $this->host, 'user' => $this->user, 'password' => preg_replace("/./", "*", $this->password), 'database' => $this->db, 'lastError' => $this->connect->errorInfo()];
675693
}
676694
public function __sleep() {
677695
if($this->result instanceof PDOStatement)

0 commit comments

Comments
 (0)
0