-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathez_sqlite3.php
More file actions
1941 lines (1656 loc) · 62.9 KB
/
Copy pathez_sqlite3.php
File metadata and controls
1941 lines (1656 loc) · 62.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*#################################################################################################################################################*/
/**********************************************************************
* Author: Justin Vincent (jv@vip.ie)
* Author: Stefanie Janine Stoelting <mail@stefanie-stoelting.de>
* Contributor: Lawrence Stubbs <technoexpressnet@gmail.com>
* Web...: http://justinvincent.com
* Name..: ezSQL
* Desc..: ezSQL Core module - database abstraction library to make
* it very easy to deal with databases. ezSQLcore can not be used by
* itself (it is designed for use by database specific modules).
*
*/
/**********************************************************************
* ezSQL Constants
*/
defined('EZSQL_VERSION') or define('EZSQL_VERSION', '3.08');
defined('OBJECT') or define('OBJECT', 'OBJECT');
defined('ARRAY_A') or define('ARRAY_A', 'ARRAY_A');
defined('ARRAY_N') or define('ARRAY_N', 'ARRAY_N');
/**********************************************************************
* Core class containing common functions to manipulate query result
* sets once returned
*/
//require_once('ezFunctions.php');
/**
* Author: Lawrence Stubbs <technoexpressnet@gmail.com>
*
* Important: Verify that every feature you use will work with your database vendor.
* ezSQL Query Builder will attempt to validate the generated SQL according to standards.
* Any errors will return an boolean false, and you will be responsible for handling.
*
* ezQuery does no validation whatsoever if certain features even work with the
* underlying database vendor.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/
// ezQuery prepare placeholder/positional tag
const _TAG = '__ez__';
// Use to set get_result output as json
const _JSON = 'json';
/*
* Operator boolean expressions.
*/
const EQ = '=';
const NEQ = '<>';
const NE = '!=';
const LT = '<';
const LTE = '<=';
const GT = '>';
const GTE = '>=';
const _IN = 'IN';
const _notIN = 'NOT IN';
const _LIKE = 'LIKE';
const _notLIKE = 'NOT LIKE';
const _BETWEEN = 'BETWEEN';
const _notBETWEEN = 'NOT BETWEEN';
const _isNULL = 'IS NULL';
const _notNULL = 'IS NOT NULL';
/*
* Combine operators .
*/
const _AND = 'AND';
const _OR = 'OR';
const _NOT = 'NOT';
const _andNOT = 'AND NOT';
// Global class instances, will be used to create and call methods directly.
$_ezQuery = null;
// $_ezCubrid = null;
$_ezMysqli = null;
// $_ezOracle8_9 = null;
// $_ezOracleTNS = null;
$_ezPdo = null;
$_ezPostgresql = null;
$_ezRecordset = null;
$_ezSqlite3 = null;
$_ezSqlsrv = null;
/**********************************************************************
* Creates an array from expressions in the following formate
* param: strings @x, The left expression.
* @operator, One of '<', '>', '=', '!=', '>=', '<=', '<>', 'IN',, 'NOT IN', 'LIKE',
* 'NOT LIKE', 'BETWEEN', 'NOT BETWEEN', 'IS', 'IS NOT', or the constants above.
* @y, The right expression.
* @and, combine additional expressions with, 'AND','OR', 'NOT', 'AND NOT'.
* @args for any extras
*
* function comparison($x, $operator, $y, $and=null, ...$args)
* {
* return array($x, $operator, $y, $and, ...$args);
* }
* @returns: array
***********************************************************************/
/**
* Creates an equality comparison expression with the given arguments.
*/
function eq($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, EQ, $y, $and, ...$args);
return $expression;
}
/**
* Creates a non equality comparison expression with the given arguments.
*/
function neq($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, NEQ, $y, $and, ...$args);
return $expression;
}
/**
* Creates the other non equality comparison expression with the given arguments.
*/
function ne($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, NE, $y, $and, ...$args);
return $expression;
}
/**
* Creates a lower-than comparison expression with the given arguments.
*/
function lt($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, LT, $y, $and, ...$args);
return $expression;
}
/**
* Creates a lower-than-equal comparison expression with the given arguments.
*/
function lte($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, LTE, $y, $and, ...$args);
return $expression;
}
/**
* Creates a greater-than comparison expression with the given arguments.
*/
function gt($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, GT, $y, $and, ...$args);
return $expression;
}
/**
* Creates a greater-than-equal comparison expression with the given arguments.
*/
function gte($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, GTE, $y, $and, ...$args);
return $expression;
}
/**
* Creates an IS NULL expression with the given arguments.
*/
function isNull($x, $y='null', $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, _isNULL, $y, $and, ...$args);
return $expression;
}
/**
* Creates an IS NOT NULL expression with the given arguments.
*/
function isNotNull($x, $y='null', $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, _notNULL, $y, $and, ...$args);
return $expression;
}
/**
* Creates a LIKE() comparison expression with the given arguments.
*/
function like($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, _LIKE, $y, $and, ...$args);
return $expression;
}
/**
* Creates a NOT LIKE() comparison expression with the given arguments.
*/
function notLike($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, _notLIKE, $y, $and, ...$args);
return $expression;
}
/**
* Creates a IN () comparison expression with the given arguments.
*/
function in($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, _IN, $y, $and, ...$args);
return $expression;
}
/**
* Creates a NOT IN () comparison expression with the given arguments.
*/
function notIn($x, $y, $and=null, ...$args)
{
$expression = array();
array_push($expression, $x, _notIN, $y, $and, ...$args);
return $expression;
}
/**
* Creates a BETWEEN () comparison expression with the given arguments.
*/
function between($x, $y, $y2, ...$args)
{
$expression = array();
array_push($expression, $x, _BETWEEN,$y, $y2, ...$args);
return $expression;
}
/**
* Creates a NOT BETWEEN () comparison expression with the given arguments.
*/
function notBetween($x, $y, $y2, ...$args)
{
$expression = array();
array_push($expression, $x, _notBETWEEN, $y, $y2, ...$args);
return $expression;
}
/**
* desc: Using global class instances, setup functions to call class methods directly.
* param: @ezSQL - string, representing class 'cubrid', 'mysqli', 'oracle8_9', 'oracletns', 'pdo', 'postgresql', 'recordset', 'sqlite3', 'sqlsrv'
* returns: boolean - true, or false for error
*/
function setQuery($ezSQL='') {
global $_ezQuery, $_ezMysqli;// $_ezCubrid, $_ezOracle8_9, $_ezOracleTNS; 'recordset' ,'oracle8_9', 'oracletns',
global $_ezPdo, $_ezPostgresql, $_ezRecordset, $_ezSqlite3, $_ezSqlsrv;
if (in_array(strtolower($ezSQL), array( 'cubrid', 'mysqli', 'pdo', 'postgresql', 'sqlite3', 'sqlsrv' ))) {
switch(strtolower($ezSQL)) {
// case 'cubrid':
// $_ezQuery = $_ezCubrid;
// break;
case 'mysqli':
$_ezQuery = $_ezMysqli;
break;
// case 'oracle8_9':
// $_ezQuery = $_ezOracle8_9;
// break;
// case 'oracletns':
// $_ezQuery = $_ezOracleTNS;
// break;
case 'pdo':
$_ezQuery = $_ezPdo;
break;
case 'postgresql':
$_ezQuery = $_ezPostgresql;
break;
case 'recordset':
$_ezQuery = $_ezRecordset;
break;
case 'sqlite3':
$_ezQuery = $_ezSqlite3;
break;
case 'sqlsrv':
$_ezQuery = $_ezSqlsrv;
break;
}
return (!empty($_ezQuery)) ? true: false;
} else {
$_ezQuery = null;
unset($_ezQuery);
return false;
}
}
function select($table='', $columns='*', ...$args) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->selecting($table, $columns, ...$args) : false;
}
function select_into($newtable, $fromcolumns='*', $oldtable=null, ...$args) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->select_into($newtable, $fromcolumns, $oldtable, ...$args) : false;
}
function insert_select($totable='', $tocolumns='*', $fromtable, $fromcolumns='*', ...$args) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->insert_select($totable, $tocolumns, $fromtable, $fromcolumns, ...$args) : false;
}
function create_select($newtable, $fromcolumns, $oldtable=null, ...$args) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->create_select($newtable, $fromcolumns, $oldtable, ...$args) : false;
}
function where( ...$args) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->where( ...$args) : false;
}
function groupBy($groupBy) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->groupBy($groupBy) : false;
}
function having( ...$args) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->having( ...$args) : false;
}
function orderBy($orderBy, $order) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->orderBy($orderBy, $order) : false;
}
function insert($table='', $keyvalue) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->insert($table, $keyvalue) : false;
}
function update($table='', $keyvalue, ...$args) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->update($table, $keyvalue, ...$args) : false;
}
function delete($table='', ...$args) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->delete($table, ...$args) : false;
}
function replace($table='', $keyvalue) {
global $_ezQuery;
return ($_ezQuery) ? $_ezQuery->replace($table, $keyvalue) : false;
}
//require_once('ezQuery.php');
/**
* Author: Lawrence Stubbs <technoexpressnet@gmail.com>
*
* Important: Verify that every feature you use will work with your database vendor.
* ezSQL Query Builder will attempt to validate the generated SQL according to standards.
* Any errors will return an boolean false, and you will be responsible for handling.
*
* ezQuery does no validation whatsoever if certain features even work with the
* underlying database vendor.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/
class ezQuery
{
protected $select_result = true;
protected $prepareActive = false;
private $fromtable = null;
private $iswhere = true;
private $isinto = false;
function __construct()
{
}
function clean($string)
{
$patterns = array( // strip out:
'@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
);
$string = preg_replace($patterns,'',$string);
$string = trim($string);
$string = stripslashes($string);
return htmlentities($string);
}
// return status of prepare function availability in method calls
function getPrepare($on=true) {
return $this->prepareActive;
}
// turn off/on prepare function availability in ezQuery method calls
function setPrepare($on=true) {
$this->prepareActive = ($on) ? true : false;
return null;
}
// returns array of parameter values for prepare function
function getParamaters() {
return $this->preparedvalues;
}
/**
* desc: add parameter values to class array variable for prepare function or clear if no value supplied
* param: @valuetoadd mixed
*
* returns int - array count
*/
function setParamaters($valuetoadd=null) {
if (empty($valuetoadd)) {
$this->preparedvalues = array();
return null;
} else
return array_push($this->preparedvalues, $valuetoadd);
}
function to_string($arrays) {
if (is_array( $arrays )) {
$columns = '';
foreach($arrays as $val) {
$columns .= $val.', ';
}
$columns = rtrim($columns, ', ');
} else
$columns = $arrays;
return $columns;
}
/**
* desc: specifies a grouping over the results of the query.
* <code>
* $this->selecting('table',
* columns,
* where(columns = values),
* groupBy(columns),
* having(columns = values),
* orderBy(order);
* </code>
* param: mixed @groupBy The grouping expression.
*
* returns: string - GROUP BY SQL statement, or false on error
*/
function groupBy($groupBy)
{
if (empty($groupBy)) {
return false;
}
$columns = $this->to_string($groupBy);
return 'GROUP BY ' .$columns;
}
/**
* desc: specifies a restriction over the groups of the query.
* formate: having( array(x, =, y, and, extra) ) or having( "x = y and extra" );
* example: having( array(key, operator, value, combine, extra) ); or having( "key operator value combine extra" );
* param: mixed @array or @string double spaced "(key, - table column
* operator, - set the operator condition, either '<','>', '=', '!=', '>=', '<=', '<>', 'in', 'like', 'between', 'not between', 'is null', 'is not null'
* value, - will be escaped
* combine, - combine additional where clauses with, either 'AND','OR', 'NOT', 'AND NOT' or carry over of @value in the case the @operator is 'between' or 'not between'
* extra - carry over of @combine in the case the operator is 'between' or 'not between')"
* @returns: string - HAVING SQL statement, or false on error
*/
function having(...$having)
{
$this->iswhere = false;
return $this->where( ...$having);
}
/**
* desc: specifies an ordering for the query results.
* param: @order The ordering direction.
* returns: string - ORDER BY SQL statement, or false on error
*/
function orderBy($orderBy, $order)
{
if (empty($orderBy)) {
return false;
}
$columns = $this->to_string($orderBy);
$order = (in_array(strtoupper($order), array( 'ASC', 'DESC'))) ? strtoupper($order) : 'ASC';
return 'ORDER BY '.$columns.' '. $order;
}
/**********************************************************************
* desc: helper returns an WHERE sql clause string
* formate: where( array(x, =, y, and, extra) ) or where( "x = y and extra" );
* example: where( array(key, operator, value, combine, extra) ); or where( "key operator value combine extra" );
* param: mixed @array or @string double spaced "(key, - table column
* operator, - set the operator condition, either '<','>', '=', '!=', '>=', '<=', '<>', 'in', 'like', 'not like', 'between', 'not between', 'is null', 'is not null'
* value, - will be escaped
* combine, - combine additional where clauses with, either 'AND','OR', 'NOT', 'AND NOT' or carry over of @value in the case the @operator is 'between' or 'not between'
* extra - carry over of @combine in the case the operator is 'between' or 'not between')"
* returns: string - WHERE SQL statement, or false on error
*/
function where( ...$getwherekeys) {
$whereorhaving = ($this->iswhere) ? 'WHERE' : 'HAVING';
$this->iswhere = true;
if (!empty($getwherekeys)){
if (is_string($getwherekeys[0])) {
foreach ($getwherekeys as $makearray)
$wherekeys[] = explode(' ',$makearray);
} else
$wherekeys = $getwherekeys;
} else
return '';
foreach ($wherekeys as $values) {
$operator[] = (isset($values[1])) ? $values[1]: '';
if (!empty($values[1])){
if (strtoupper($values[1]) == 'IN') {
$wherekey[ $values[0] ] = array_slice($values,2);
$combiner[] = (isset($values[3])) ? $values[3]: _AND;
$extra[] = (isset($values[4])) ? $values[4]: null;
} else {
$wherekey[ (isset($values[0])) ? $values[0] : '1' ] = (isset($values[2])) ? $values[2] : '' ;
$combiner[] = (isset($values[3])) ? $values[3]: _AND;
$extra[] = (isset($values[4])) ? $values[4]: null;
}
} else {
$this->setParamaters();
return false;
}
}
$where='1';
if (! isset($wherekey['1'])) {
$where='';
$i=0;
$needtoskip=false;
foreach($wherekey as $key=>$val) {
$iscondition = strtoupper($operator[$i]);
$combine = $combiner[$i];
if ( in_array(strtoupper($combine), array( 'AND', 'OR', 'NOT', 'AND NOT' )) || isset($extra[$i]))
$combinewith = (isset($extra[$i])) ? $combine : strtoupper($combine);
else
$combinewith = _AND;
if (! in_array( $iscondition, array( '<', '>', '=', '!=', '>=', '<=', '<>', 'IN', 'LIKE', 'NOT LIKE', 'BETWEEN', 'NOT BETWEEN', 'IS', 'IS NOT' ) )) {
$this->setParamaters();
return false;
} else {
if (($iscondition=='BETWEEN') || ($iscondition=='NOT BETWEEN')) {
$value = $this->escape($combinewith);
if (in_array(strtoupper($extra[$i]), array( 'AND', 'OR', 'NOT', 'AND NOT' )))
$mycombinewith = strtoupper($extra[$i]);
else
$mycombinewith = _AND;
if ($this->getPrepare()) {
$where.= "$key ".$iscondition.' '._TAG." AND "._TAG." $mycombinewith ";
$this->setParamaters($val);
$this->setParamaters($combinewith);
} else
$where.= "$key ".$iscondition." '".$this->escape($val)."' AND '".$value."' $mycombinewith ";
$combinewith = $mycombinewith;
} elseif ($iscondition=='IN') {
$value = '';
foreach ($val as $invalues) {
if ($this->getPrepare()) {
$value .= _TAG.', ';
$this->setParamaters($invalues);
} else
$value .= "'".$this->escape($invalues)."', ";
}
$where.= "$key ".$iscondition." ( ".rtrim($value, ', ')." ) $combinewith ";
} elseif(((strtolower($val)=='null') || ($iscondition=='IS') || ($iscondition=='IS NOT'))) {
$iscondition = (($iscondition=='IS') || ($iscondition=='IS NOT')) ? $iscondition : 'IS';
$where.= "$key ".$iscondition." NULL $combinewith ";
} elseif((($iscondition=='LIKE') || ($iscondition=='NOT LIKE')) && ! preg_match('/[_%?]/',$val)) return false;
else {
if ($this->getPrepare()) {
$where.= "$key ".$iscondition.' '._TAG." $combinewith ";
$this->setParamaters($val);
} else
$where.= "$key ".$iscondition." '".$this->escape($val)."' $combinewith ";
}
$i++;
}
}
$where = rtrim($where, " $combinewith ");
}
if (($this->getPrepare()) && !empty($this->getParamaters()) && ($where!='1'))
return " $whereorhaving ".$where.' ';
else
return ($where!='1') ? " $whereorhaving ".$where.' ' : ' ' ;
}
/**********************************************************************
* desc: returns an sql string or result set given the table, fields, by operator condition or conditional array
*<code>
*selecting('table',
* 'columns',
* where( eq( 'columns', values, _AND ), like( 'columns', _d ) ),
* groupBy( 'columns' ),
* having( between( 'columns', values1, values2 ) ),
* orderBy( 'columns', 'desc' );
*</code>
*
* param: @table, - database table to access
* @fields, - table columns, string or array
* @wherekey, - where clause ( array(x, =, y, and, extra) ) or ( "x = y and extra" )
* @groupby, -
* @having, - having clause ( array(x, =, y, and, extra) ) or ( "x = y and extra" )
* @orderby - *
* returns: a result set - see docs for more details, or false for error
*/
function selecting($table='', $fields='*', ...$get_args) {
$getfromtable = $this->fromtable;
$getselect_result = $this->select_result;
$getisinto = $this->isinto;
$this->fromtable = null;
$this->select_result = true;
$this->isinto = false;
$skipwhere = false;
$wherekeys = $get_args;
$where = '';
if ( ! isset($table) || $table=='' ) {
$this->setParamaters();
return false;
}
$columns = $this->to_string($fields);
if (isset($getfromtable) && ! $getisinto)
$sql="CREATE TABLE $table AS SELECT $columns FROM ".$getfromtable;
elseif (isset($getfromtable) && $getisinto)
$sql="SELECT $columns INTO $table FROM ".$getfromtable;
else
$sql="SELECT $columns FROM ".$table;
if (!empty($get_args)) {
if (is_string($get_args[0])) {
$args_by = '';
$groupbyset = false;
$havingset = false;
$orderbyset = false;
foreach ($get_args as $where_groupby_having_orderby) {
if (strpos($where_groupby_having_orderby,'WHERE')!==false ) {
$args_by .= $where_groupby_having_orderby;
$skipwhere = true;
} elseif (strpos($where_groupby_having_orderby,'GROUP BY')!==false ) {
$args_by .= ' '.$where_groupby_having_orderby;
$groupbyset = true;
} elseif (strpos($where_groupby_having_orderby,'HAVING')!==false ) {
if ($groupbyset) {
$args_by .= ' '.$where_groupby_having_orderby;
$havingset = true;
} else {
$this->setParamaters();
return false;
}
} elseif (strpos($where_groupby_having_orderby,'ORDER BY')!==false ) {
$args_by .= ' '.$where_groupby_having_orderby;
$orderbyset = true;
}
}
if ($skipwhere || $groupbyset || $havingset || $orderbyset) {
$where = $args_by;
$skipwhere = true;
}
}
} else {
$skipwhere = true;
}
if (! $skipwhere)
$where = $this->where( ...$wherekeys);
if (is_string($where)) {
$sql .= $where;
if ($getselect_result)
return (($this->getPrepare()) && !empty($this->getParamaters())) ? $this->get_results($sql, OBJECT, true) : $this->get_results($sql);
else
return $sql;
} else {
$this->setParamaters();
return false;
}
}
// Returns: string - sql statement from selecting method instead of executing get_result
function select_sql($table='', $fields='*', ...$get_args) {
$this->select_result = false;
return $this->selecting($table, $fields, ...$get_args);
}
/**********************************************************************
* desc: does an create select statement by calling selecting method
* param: @newtable, - new database table to be created
* @fromcolumns - the columns from old database table
* @oldtable - old database table
* @wherekey, - where clause ( array(x, =, y, and, extra) ) or ( "x = y and extra" )
* example: where( array(key, operator, value, combine, extra) ); or where( "key operator value combine extra" );
* returns:
*/
function create_select($newtable, $fromcolumns, $oldtable=null, ...$fromwhere) {
if (isset($oldtable))
$this->fromtable = $oldtable;
else {
$this->setParamaters();
return false;
}
$newtablefromtable = $this->select_sql($newtable, $fromcolumns, ...$fromwhere);
if (is_string($newtablefromtable))
return (($this->getPrepare()) && !empty($this->getParamaters())) ? $this->query($newtablefromtable, true) : $this->query($newtablefromtable);
else {
$this->setParamaters();
return false;
}
}
/**********************************************************************
* desc: does an select into statement by calling selecting method
* param: @newtable, - new database table to be created
* @fromcolumns - the columns from old database table
* @oldtable - old database table
* @wherekey, - where clause ( array(x, =, y, and, extra) ) or ( "x = y and extra" )
* example: where( array(key, operator, value, combine, extra) ); or where( "key operator value combine extra" );
* returns:
*/
function select_into($newtable, $fromcolumns, $oldtable=null, ...$fromwhere) {
$this->isinto = true;
if (isset($oldtable))
$this->fromtable = $oldtable;
else {
$this->setParamaters();
return false;
}
$newtablefromtable = $this->select_sql($newtable, $fromcolumns, ...$fromwhere);
if (is_string($newtablefromtable))
return (($this->getPrepare()) && !empty($this->getprepared())) ? $this->query($newtablefromtable, true) : $this->query($newtablefromtable);
else {
$this->setParamaters();
return false;
}
}
/**********************************************************************
* desc: does an update query with an array, by conditional operator array
* param: @table, - database table to access
* @keyandvalue, - table fields, assoc array with key = value (doesn't need escaped)
* @wherekey, - where clause ( array(x, =, y, and, extra) ) or ( "x = y and extra" )
* example: where( array(key, operator, value, combine, extra) ); or where( "key operator value combine extra" );
* returns: (query_id) for fetching results etc, or false for error
*/
function update($table='', $keyandvalue, ...$wherekeys) {
if ( ! is_array( $keyandvalue ) || ! isset($table) || $table=='' ) {
$this->setParamaters();
return false;
}
$sql="UPDATE $table SET ";
foreach($keyandvalue as $key=>$val) {
if(strtolower($val)=='null') {
$sql.= "$key = NULL, ";
} elseif(in_array(strtolower($val), array( 'current_timestamp()', 'date()', 'now()' ))) {
$sql.= "$key = CURRENT_TIMESTAMP(), ";
} else {
if ($this->getPrepare()) {
$sql.= "$key = "._TAG.", ";
$this->setParamaters($val);
} else
$sql.= "$key = '".$this->escape($val)."', ";
}
}
$where = $this->where(...$wherekeys);
if (is_string($where)) {
$sql = rtrim($sql, ', ') . $where;
return (($this->getPrepare()) && !empty($this->getParamaters())) ? $this->query($sql, true) : $this->query($sql) ;
} else {
$this->setParamaters();
return false;
}
}
/**********************************************************************
* desc: helper does the actual insert or replace query with an array
*/
function delete($table='', ...$wherekeys) {
if ( empty($table) ) {
$this->setParamaters();
return false;
}
$sql="DELETE FROM $table";
$where = $this->where(...$wherekeys);
if (is_string($where)) {
$sql .= $where;
return (($this->getPrepare()) && !empty($this->getParamaters())) ? $this->query($sql, true) : $this->query($sql) ;
} else {
$this->setParamaters();
return false;
}
}
/**********************************************************************
* desc: helper does the actual insert or replace query with an array
*/
function _query_insert_replace($table='', $keyandvalue, $type='', $execute=true) {
if ((! is_array($keyandvalue) && ($execute)) || $table=='' ) {
$this->setParamaters();
return false;
}
if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ))) {
$this->setParamaters();
return false;
}
$sql="$type INTO $table";
$v=''; $n='';
if ($execute) {
foreach($keyandvalue as $key=>$val) {
$n.="$key, ";
if(strtolower($val)=='null') $v.="NULL, ";
elseif(in_array(strtolower($val), array( 'current_timestamp()', 'date()', 'now()' ))) $v.="CURRENT_TIMESTAMP(), ";
else {
if ($this->getPrepare()) {
$v.= _TAG.", ";
$this->setParamaters($val);
} else
$v.= "'".$this->escape($val)."', ";
}
}
$sql .= "(". rtrim($n, ', ') .") VALUES (". rtrim($v, ', ') .");";
if (($this->getPrepare()) && !empty($this->getParamaters()))
$ok = $this->query($sql, true);
else
$ok = $this->query($sql);
if ($ok)
return $this->insert_id;
else {
$this->setParamaters();
return false;
}
} else {
if (is_array($keyandvalue)) {
if (array_keys($keyandvalue) === range(0, count($keyandvalue) - 1)) {
foreach($keyandvalue as $key) {
$n.="$key, ";
}
$sql .= " (". rtrim($n, ', ') .") ";
} else {
return false;
}
}
return $sql;
}
}
/**********************************************************************
* desc: does an replace query with an array
* param: @table, - database table to access
* @keyandvalue - table fields, assoc array with key = value (doesn't need escaped)
* returns: id of replaced record, or false for error
*/
function replace($table='', $keyandvalue) {
return $this->_query_insert_replace($table, $keyandvalue, 'REPLACE');
}
/**********************************************************************
* desc: does an insert query with an array
* param: @table, - database table to access
* @keyandvalue - table fields, assoc array with key = value (doesn't need escaped)
* returns: id of inserted record, or false for error
*/
function insert($table='', $keyandvalue) {
return $this->_query_insert_replace($table, $keyandvalue, 'INSERT');
}
/**********************************************************************
* desc: does an insert into select statement by calling insert method helper then selecting method
* param: @totable, - database table to insert table into
* @tocolumns - the receiving columns from other table columns, leave blank for all or array of column fields
* @wherekey, - where clause ( array(x, =, y, and, extra) ) or ( "x = y and extra" )
* example: where( array(key, operator, value, combine, extra) ); or where( "key operator value combine extra" );
* returns:
*/
function insert_select($totable='', $tocolumns='*', $fromtable, $fromcolumns='*', ...$fromwhere) {
$puttotable = $this->_query_insert_replace($totable, $tocolumns, 'INSERT', false);
$getfromtable = $this->select_sql($fromtable, $fromcolumns, ...$fromwhere);
if (is_string($puttotable) && is_string($getfromtable))
return (($this->getPrepare()) && !empty($this->getParamaters())) ? $this->query($puttotable." ".$getfromtable, true) : $this->query($puttotable." ".$getfromtable) ;
else {
$this->setParamaters();
return false;
}
}
}
class ezSQLcore extends ezQuery
{
public $trace = false; // same as $debug_all
public $debug_all = false; // same as $trace
public $debug_called = false;
public $vardump_called = false;
public $show_errors = true;
public $num_queries = 0;
public $conn_queries = 0;
public $last_query = null;
public $last_error = null;
public $col_info = null;
public $captured_errors = array();
public $cache_dir = false;
public $cache_queries = false;
public $cache_inserts = false;
public $use_disk_cache = false;
public $cache_timeout = 24; // hours
public $timers = array();
public $total_query_time = 0;
public $db_connect_time = 0;
public $trace_log = array();
public $use_trace_log = false;