Skip to content

Commit 223e596

Browse files
authored
Update ssql.php
Added SSQLCond class, improved column type recognition for SQLite.
1 parent 21e9d28 commit 223e596

File tree

1 file changed

+142
-21
lines changed

1 file changed

+142
-21
lines changed

ssql.php

+142-21
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function __construct($host = "", $user = "", $password = "", $database =
117117
throw new SSQLException("(__construct): Can’t connect to MySQL: " . $e->getMessage());
118118
}
119119
if($this->connect->errorCode() && $this->connect) {
120-
throw new SSQLException("(__construct): Can’t select MySQL database (" . $this->connect->errorInfo()[2] . ")");
120+
throw new SSQLException("(__construct): Can’t select MySQL database:" . $this->connect->errorInfo()[2]);
121121
$this->connect->close();
122122
};
123123
}
@@ -150,7 +150,7 @@ public function query($query, $flags = 0, $fnc = "Query") {
150150
throw new SSQLException("(" . $fnc . "): No database selected");
151151
$qr = $this->connect->query($query);
152152
if(!$qr && !($flags & self::NO_ERROR))
153-
throw new SSQLException("(" . $fnc . "): Database error: <em>" . $this->connect->errorInfo()[2] . "</em> <strong>SQL command:</strong> <code>" . (empty($query) ? "<em>Missing</em>" : $query) . "</code>");
153+
throw new SSQLException("(" . $fnc . "): Database error: <em>{$this->connect->errorInfo()[2]}</em> <strong>SQL command:</strong> <code>" . (empty($query) ? "<em>Missing</em>" : $query) . "</code>");
154154
elseif(!$qr)
155155
return false;
156156
$this->result = new SSQLResult($qr);
@@ -199,7 +199,7 @@ public function execFnc($name, $params = [], $flags = 0) {
199199
if(isset($this->fncs[$name]))
200200
$this->queryf($this->fncs[$name], $params, $flags, $name);
201201
else
202-
throw new SSQLException("(" . $name . "): This function isn’t defined");
202+
throw new SSQLException("(" . $name . "): This function is not defined");
203203
}
204204

205205
public function dbList($flags = 0) {
@@ -219,7 +219,7 @@ public function tableList($flags = 0) {
219219
if(empty($this->db) && !$this->SQLite)
220220
throw new SSQLException("(tableList): No database selected");
221221
$result = $this->result;
222-
$this->query(($this->SQLite ? "SELECT `name` FROM `sqlite_master` WHERE type='table' ORDER BY `name`" : ("SHOW TABLES FROM `" . $this->db . "`")), "tableList", $flags);
222+
$this->query(($this->SQLite ? "SELECT `name` FROM `sqlite_master` WHERE type='table' ORDER BY `name`" : ("SHOW TABLES FROM `{$this->db}`")), "tableList", $flags);
223223
$r = [];
224224
while($f = $this->fetch(self::FETCH_ARRAY)) {
225225
$r[] = $f[0];
@@ -359,12 +359,12 @@ public function selectJoinWhere($table, $join, $on, $cond, $order = "", $cols =
359359
$condString = $this->getCondString($cond, $all);
360360
$limitString = ($limit===NULL || $limit==="") ? "" : " LIMIT " . intval($this->escape($limit));
361361
if(!empty($order))
362-
$order = " ORDER BY `" . $order . "` " . (boolval($flags & self::ORDER_DESC) ? " DESC" : " ASC");
362+
$order = " ORDER BY `{$order}` " . (boolval($flags & self::ORDER_DESC) ? " DESC" : " ASC");
363363
return $this->query("
364-
SELECT $colsValue
365-
FROM `$table`
366-
$jt JOIN `$join` ON $onString
367-
WHERE $condString$limitString$order
364+
SELECT {$colsValue}
365+
FROM `{$table}`
366+
{$jt} JOIN `{$join}` ON {$onString}
367+
WHERE {$condString}{$limitString}{$order}
368368
", $flags, $name);
369369
}
370370

@@ -416,7 +416,7 @@ public function insert($table, $values, $flags = 0) {
416416
$valueString.= "0x" . bin2hex($value);
417417
};
418418
$r = $this->query("
419-
INSERT INTO $table$colString VALUES ($valueString)
419+
INSERT INTO {$table}{$colString} VALUES ({$valueString})
420420
", $flags, "insert");
421421
return (boolval($flags & self::INSERT_RETURN_ID) ? $this->connect->lastInsertId() : $r);
422422
}
@@ -425,7 +425,7 @@ public function delete($table, $cond, $flags = 128) {
425425
$all = !boolval($flags & self::COND_OR);
426426
$condString = $this->getCondString($cond, $all);
427427
return $this->query("
428-
DELETE FROM `$table` WHERE $condString
428+
DELETE FROM `{$table}` WHERE {$condString}
429429
", $flags, "delete");
430430
}
431431

@@ -447,7 +447,7 @@ public function update($table, $cond, $values, $flags = 128) {
447447
$string.= "0x" . bin2hex($value);
448448
};
449449
return $this->query("
450-
UPDATE `$table` SET $string WHERE $condString
450+
UPDATE `{$table}` SET {$string} WHERE {$condString}
451451
", $flags, "update");
452452
}
453453

@@ -457,13 +457,13 @@ public function add($table, $name, $type, $length, $null, $where, $key, $data =
457457
$type = strtoupper($type);
458458
$where = strtoupper($where);
459459
return $this->query("
460-
ALTER TABLE `$table` ADD '$name' $type($length) " . ($null ? "NULL" : "NOT NULL") . "$data $where '$key'
460+
ALTER TABLE `{$table}` ADD '{$name}' {$type}({$length}) " . ($null ? "NULL" : "NOT NULL") . "{$data} {$where} '{$key}'
461461
", $flags, "drop");
462462
}
463463

464464
public function drop($table, $name) {
465465
return $this->query("
466-
ALTER TABLE `$table` DROP '$name'
466+
ALTER TABLE `{$table}` DROP '{$name}'
467467
", $flags, "drop");
468468
}
469469

@@ -473,7 +473,7 @@ public function change($table, $name, $newname, $type, $length, $null, $data = "
473473
$type = strtoupper($type);
474474
$where = strtoupper($where);
475475
return $this->query("
476-
ALTER TABLE `$table` CHANGE '$name' $newname $type($length) " . ($null ? "NULL" : "NOT NULL") . $data
476+
ALTER TABLE `{$table}` CHANGE '{$name}' {$newname} {$type}({$length}) " . ($null ? "NULL" : "NOT NULL") . $data
477477
, $flags, "change");
478478
}
479479

@@ -515,13 +515,13 @@ public function createTable($table, $params, $primary = NULL, $flags = 0) {
515515

516516
public function renameTable($table, $newname, $flags = 0) {
517517
return $this->query("
518-
ALTER TABLE `$table` RENAME TO $newname
518+
ALTER TABLE `{$table}` RENAME TO {$newname}
519519
", $flags, "renameTable");
520520
}
521521

522522
public function deleteTable($table, $flags = 0) {
523523
return $this->query("
524-
DROP TABLE `$table`
524+
DROP TABLE `{$table}`
525525
", $flags, "deleteTable");
526526
}
527527

@@ -627,6 +627,10 @@ public function get($table, $options = [], $flags = 4096) {
627627
return $this->fetch($flags | $fetch);
628628
}
629629

630+
public function cond($cond = "") {
631+
return new SSQLCond($this, $cond);
632+
}
633+
630634
private function getCondString($a, $and, $on = false) {
631635
if(!is_array($a))
632636
return $a;
@@ -646,7 +650,7 @@ private function getCondString($a, $and, $on = false) {
646650
if(is_numeric($k))
647651
$r.= $v;
648652
else {
649-
$r.= "`" . $this->escape($k) . "`";
653+
$r.= "`{$this->escape($k)}`";
650654
$r.= " = ";
651655
if(is_numeric($v3))
652656
$v3 = intval($v3);
@@ -655,7 +659,7 @@ private function getCondString($a, $and, $on = false) {
655659
elseif(is_numeric($v3))
656660
$r.= $v;
657661
else
658-
$r.= ($on || $col) ? "`$v3`" : "'$v3'";
662+
$r.= ($on || $col) ? "`{$v3}`" : "'{$v3}'";
659663
};
660664
$r.= $and ? " AND " : " OR ";
661665
};
@@ -681,7 +685,7 @@ private function getCondString($a, $and, $on = false) {
681685
if(is_numeric($v))
682686
$r.= $v;
683687
else
684-
$r.= ($on || $col) ? "`$v`" : "'$v'";
688+
$r.= ($on || $col) ? "`{$v}`" : "'{$v}'";
685689
};
686690
$r.= $and ? " AND " : " OR ";
687691
};
@@ -727,7 +731,12 @@ public function getColumns() {
727731
$columns[$i - 1]->name = $meta['name'];
728732
$columns[$i - 1]->table = $meta['table'];
729733
if(isset($meta['mysql:decl_type']))
730-
$columns[$i - 1]->type = $meta['mysql:decl_type'];
734+
$columns[$i - 1]->type = strtoupper($meta['mysql:decl_type']);
735+
elseif(isset($meta['sqlite:decl_type'])) {
736+
$columns[$i - 1]->type = strtoupper(explode("(", $meta['sqlite:decl_type'])[0]);
737+
if(count(explode("(", $meta['sqlite:decl_type'])) > 1)
738+
$columns[$i - 1]->len = rtrim(explode("(", $meta['sqlite:decl_type'])[1], ")");
739+
}
731740
else {
732741
switch($meta['pdo_type']) {
733742
case PDO::PARAM_BOOL: $columns[$i - 1]->type = "BOOL"; break;
@@ -876,6 +885,118 @@ public function run(...$qs) {
876885
}
877886
};
878887

888+
class SSQLCond {
889+
private $c;
890+
private $text;
891+
public function __construct($c, $cond = "") {
892+
$this->c = $c;
893+
$this->cond = $cond;
894+
}
895+
public function eq($k, $v, $flags = 128) {
896+
$append = !empty($this->cond);
897+
$quotes = "\"";
898+
if(is_int($v) || is_float($v)) {
899+
$v = [$v];
900+
$quotes = "";
901+
};
902+
if(is_array($v) && count($v) < 1)
903+
return false;
904+
if(is_array($v))
905+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ((count($v) < 2) ? ("`" . $this->c->escape($k) . "` = " . $quotes . $this->c->escape($v[0]) . $quotes) : ("(`" . $this->c->escape($k) . "` = " . $quotes . $this->c->escape(array_shift($v)) . $quotes . ") OR (" . $this->c->cond()->eq($k, $v) . ")")) . ($append ? ")" : "");
906+
else
907+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("`" . $this->c->escape($k) . "` = `" . $this->c->escape($v) . "`") . ($append ? ")" : "");
908+
return $this;
909+
}
910+
public function lt($k, $v, $flags = 128) {
911+
$append = !empty($this->cond);
912+
if(is_int($v) || is_float($v))
913+
$v = [$v];
914+
if(is_array($v) && count($v) < 1)
915+
return false;
916+
if(is_array($v))
917+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ((count($v) < 2) ? ("`" . $this->c->escape($k) . "` < " . floatval($v[0])) : ("(`" . $this->c->escape($k) . "` < " . floatval(array_shift($v)) . ") OR (" . $this->c->cond()->lt($k, $v) . ")")) . ($append ? ")" : "");
918+
else
919+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("`" . $this->c->escape($k) . "` < `" . $this->c->escape($v) . "`") . ($append ? ")" : "");
920+
return $this;
921+
}
922+
public function gt($k, $v, $flags = 128) {
923+
$append = !empty($this->cond);
924+
if(is_int($v) || is_float($v))
925+
$v = [$v];
926+
if(is_array($v) && count($v) < 1)
927+
return false;
928+
if(is_array($v))
929+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ((count($v) < 2) ? ("`" . $this->c->escape($k) . "` > " . floatval($v[0])) : ("(`" . $this->c->escape($k) . "` > " . floatval(array_shift($v)) . ") OR (" . $this->c->cond()->gt($k, $v) . ")")) . ($append ? ")" : "");
930+
else
931+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("`" . $this->c->escape($k) . "` > `" . $this->c->escape($v) . "`") . ($append ? ")" : "");
932+
return $this;
933+
}
934+
public function lte($k, $v, $flags = 128) {
935+
$append = !empty($this->cond);
936+
if(is_int($v) || is_float($v))
937+
$v = [$v];
938+
if(is_array($v) && count($v) < 1)
939+
return false;
940+
if(is_array($v))
941+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ((count($v) < 2) ? ("`" . $this->c->escape($k) . "` <= " . floatval($v[0])) : ("(`" . $this->c->escape($k) . "` <= " . floatval(array_shift($v)) . ") OR (" . $this->c->cond()->lte($k, $v) . ")")) . ($append ? ")" : "");
942+
else
943+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("`" . $this->c->escape($k) . "` <= `" . $this->c->escape($v) . "`") . ($append ? ")" : "");
944+
return $this;
945+
}
946+
public function gte($k, $v, $flags = 128) {
947+
$append = !empty($this->cond);
948+
if(is_int($v) || is_float($v))
949+
$v = [$v];
950+
if(is_array($v) && count($v) < 1)
951+
return false;
952+
if(is_array($v))
953+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ((count($v) < 2) ? ("`" . $this->c->escape($k) . "` >= " . floatval($v[0])) : ("(`" . $this->c->escape($k) . "` >= " . floatval(array_shift($v)) . ") OR (" . $this->c->cond()->gte($k, $v) . ")")) . ($append ? ")" : "");
954+
else
955+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("`" . $this->c->escape($k) . "` >= `" . $this->c->escape($v) . "`") . ($append ? ")" : "");
956+
return $this;
957+
}
958+
public function like($k, $v, $flags = 128) {
959+
$append = !empty($this->cond);
960+
if(is_array($v) && count($v) < 1)
961+
return false;
962+
if(is_array($v))
963+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ((count($v) < 2) ? ("`" . $this->c->escape($k) . "` LIKE \"" . $this->c->escape($v[0]) . "\"") : ("(`" . $this->c->escape($k) . "` LIKE \"" . $this->c->escape(array_shift($v)) . "\") OR (" . $this->c->cond()->like($k, $v) . ")")) . ($append ? ")" : "");
964+
else
965+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("`" . $this->c->escape($k) . "` LIKE `" . $this->c->escape($v) . "`") . ($append ? ")" : "");
966+
return $this;
967+
}
968+
public function between($k, $a, $b, $flags = 128) {
969+
$append = !empty($this->cond);
970+
if((is_int($a) || is_float($a)) && (is_int($b) || is_float($b))) {
971+
$a = [$a];
972+
$b = [$b];
973+
};
974+
if(is_array($a) && is_array($b) && (count($a) < 1 || count($b) < 1))
975+
return false;
976+
elseif(is_array($a) && is_array($b) && (count($a) < 2 || count($b) < 2))
977+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("`" . $this->c->escape($k) . "` BETWEEN " . floatval($a[0]) . " AND " . floatval($b[0])) . ($append ? ")" : "");
978+
elseif(is_array($a) && is_array($b))
979+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("(`" . $this->c->escape($k) . "` BETWEEN " . floatval(array_shift($a)) . " AND " . floatval(array_shift($b)) . ") " . ($flags & SQ::COND_AND ? "AND" : "OR") . " (" . $this->c->cond()->between($k, $a, $b) . ")") . ($append ? ")" : "");
980+
else
981+
$this->cond = ($append ? ("({$this->cond}) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " (") : "") . ("`" . $this->c->escape($k) . "` BETWEEN `" . $this->c->escape($a) . "` AND `" . $this->c->escape($b) . "`") . ($append ? ")" : "");
982+
return $this;
983+
}
984+
985+
public function not($cond = "", $flags = 128) {
986+
if(!empty($cond) && empty($this->cond))
987+
$this->cond = "NOT ({$cond})";
988+
elseif(!empty($cond))
989+
$this->cond = "($this->cond) " . ($flags & SQ::COND_OR ? "OR" : "AND") . " NOT ({$cond})";
990+
else
991+
$this->cond = "NOT ({$this->cond})";
992+
return $this;
993+
}
994+
995+
public function __toString() {
996+
return $this->cond;
997+
}
998+
}
999+
8791000

8801001
function SSQL($host, $user, $password, $db, &$object = "return") {
8811002
if($object=="return")

0 commit comments

Comments
 (0)