@@ -117,7 +117,7 @@ public function __construct($host = "", $user = "", $password = "", $database =
117
117
throw new SSQLException ("(__construct): Can’t connect to MySQL: " . $ e ->getMessage ());
118
118
}
119
119
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 ]);
121
121
$ this ->connect ->close ();
122
122
};
123
123
}
@@ -150,7 +150,7 @@ public function query($query, $flags = 0, $fnc = "Query") {
150
150
throw new SSQLException ("( " . $ fnc . "): No database selected " );
151
151
$ qr = $ this ->connect ->query ($ query );
152
152
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> " );
154
154
elseif (!$ qr )
155
155
return false ;
156
156
$ this ->result = new SSQLResult ($ qr );
@@ -199,7 +199,7 @@ public function execFnc($name, $params = [], $flags = 0) {
199
199
if (isset ($ this ->fncs [$ name ]))
200
200
$ this ->queryf ($ this ->fncs [$ name ], $ params , $ flags , $ name );
201
201
else
202
- throw new SSQLException ("( " . $ name . "): This function isn’t defined " );
202
+ throw new SSQLException ("( " . $ name . "): This function is not defined " );
203
203
}
204
204
205
205
public function dbList ($ flags = 0 ) {
@@ -219,7 +219,7 @@ public function tableList($flags = 0) {
219
219
if (empty ($ this ->db ) && !$ this ->SQLite )
220
220
throw new SSQLException ("(tableList): No database selected " );
221
221
$ 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 );
223
223
$ r = [];
224
224
while ($ f = $ this ->fetch (self ::FETCH_ARRAY )) {
225
225
$ r [] = $ f [0 ];
@@ -359,12 +359,12 @@ public function selectJoinWhere($table, $join, $on, $cond, $order = "", $cols =
359
359
$ condString = $ this ->getCondString ($ cond , $ all );
360
360
$ limitString = ($ limit ===NULL || $ limit ==="" ) ? "" : " LIMIT " . intval ($ this ->escape ($ limit ));
361
361
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 " );
363
363
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}
368
368
" , $ flags , $ name );
369
369
}
370
370
@@ -416,7 +416,7 @@ public function insert($table, $values, $flags = 0) {
416
416
$ valueString .= "0x " . bin2hex ($ value );
417
417
};
418
418
$ r = $ this ->query ("
419
- INSERT INTO $ table$ colString VALUES ( $ valueString)
419
+ INSERT INTO { $ table}{ $ colString} VALUES ( { $ valueString} )
420
420
" , $ flags , "insert " );
421
421
return (boolval ($ flags & self ::INSERT_RETURN_ID ) ? $ this ->connect ->lastInsertId () : $ r );
422
422
}
@@ -425,7 +425,7 @@ public function delete($table, $cond, $flags = 128) {
425
425
$ all = !boolval ($ flags & self ::COND_OR );
426
426
$ condString = $ this ->getCondString ($ cond , $ all );
427
427
return $ this ->query ("
428
- DELETE FROM ` $ table` WHERE $ condString
428
+ DELETE FROM ` { $ table} ` WHERE { $ condString}
429
429
" , $ flags , "delete " );
430
430
}
431
431
@@ -447,7 +447,7 @@ public function update($table, $cond, $values, $flags = 128) {
447
447
$ string .= "0x " . bin2hex ($ value );
448
448
};
449
449
return $ this ->query ("
450
- UPDATE ` $ table` SET $ string WHERE $ condString
450
+ UPDATE ` { $ table} ` SET { $ string} WHERE { $ condString}
451
451
" , $ flags , "update " );
452
452
}
453
453
@@ -457,13 +457,13 @@ public function add($table, $name, $type, $length, $null, $where, $key, $data =
457
457
$ type = strtoupper ($ type );
458
458
$ where = strtoupper ($ where );
459
459
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} '
461
461
" , $ flags , "drop " );
462
462
}
463
463
464
464
public function drop ($ table , $ name ) {
465
465
return $ this ->query ("
466
- ALTER TABLE ` $ table` DROP ' $ name'
466
+ ALTER TABLE ` { $ table} ` DROP ' { $ name} '
467
467
" , $ flags , "drop " );
468
468
}
469
469
@@ -473,7 +473,7 @@ public function change($table, $name, $newname, $type, $length, $null, $data = "
473
473
$ type = strtoupper ($ type );
474
474
$ where = strtoupper ($ where );
475
475
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
477
477
, $ flags , "change " );
478
478
}
479
479
@@ -515,13 +515,13 @@ public function createTable($table, $params, $primary = NULL, $flags = 0) {
515
515
516
516
public function renameTable ($ table , $ newname , $ flags = 0 ) {
517
517
return $ this ->query ("
518
- ALTER TABLE ` $ table` RENAME TO $ newname
518
+ ALTER TABLE ` { $ table} ` RENAME TO { $ newname}
519
519
" , $ flags , "renameTable " );
520
520
}
521
521
522
522
public function deleteTable ($ table , $ flags = 0 ) {
523
523
return $ this ->query ("
524
- DROP TABLE ` $ table`
524
+ DROP TABLE ` { $ table} `
525
525
" , $ flags , "deleteTable " );
526
526
}
527
527
@@ -627,6 +627,10 @@ public function get($table, $options = [], $flags = 4096) {
627
627
return $ this ->fetch ($ flags | $ fetch );
628
628
}
629
629
630
+ public function cond ($ cond = "" ) {
631
+ return new SSQLCond ($ this , $ cond );
632
+ }
633
+
630
634
private function getCondString ($ a , $ and , $ on = false ) {
631
635
if (!is_array ($ a ))
632
636
return $ a ;
@@ -646,7 +650,7 @@ private function getCondString($a, $and, $on = false) {
646
650
if (is_numeric ($ k ))
647
651
$ r .= $ v ;
648
652
else {
649
- $ r .= "` " . $ this ->escape ($ k ) . " ` " ;
653
+ $ r .= "` { $ this ->escape ($ k )} ` " ;
650
654
$ r .= " = " ;
651
655
if (is_numeric ($ v3 ))
652
656
$ v3 = intval ($ v3 );
@@ -655,7 +659,7 @@ private function getCondString($a, $and, $on = false) {
655
659
elseif (is_numeric ($ v3 ))
656
660
$ r .= $ v ;
657
661
else
658
- $ r .= ($ on || $ col ) ? "` $ v3` " : "' $ v3' " ;
662
+ $ r .= ($ on || $ col ) ? "` { $ v3} ` " : "' { $ v3} ' " ;
659
663
};
660
664
$ r .= $ and ? " AND " : " OR " ;
661
665
};
@@ -681,7 +685,7 @@ private function getCondString($a, $and, $on = false) {
681
685
if (is_numeric ($ v ))
682
686
$ r .= $ v ;
683
687
else
684
- $ r .= ($ on || $ col ) ? "` $ v ` " : "' $ v ' " ;
688
+ $ r .= ($ on || $ col ) ? "` { $ v } ` " : "' { $ v } ' " ;
685
689
};
686
690
$ r .= $ and ? " AND " : " OR " ;
687
691
};
@@ -727,7 +731,12 @@ public function getColumns() {
727
731
$ columns [$ i - 1 ]->name = $ meta ['name ' ];
728
732
$ columns [$ i - 1 ]->table = $ meta ['table ' ];
729
733
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
+ }
731
740
else {
732
741
switch ($ meta ['pdo_type ' ]) {
733
742
case PDO ::PARAM_BOOL : $ columns [$ i - 1 ]->type = "BOOL " ; break ;
@@ -876,6 +885,118 @@ public function run(...$qs) {
876
885
}
877
886
};
878
887
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
+
879
1000
880
1001
function SSQL ($ host , $ user , $ password , $ db , &$ object = "return " ) {
881
1002
if ($ object =="return " )
0 commit comments