-
Notifications
You must be signed in to change notification settings - Fork 8
/
underQL.mini.php
1505 lines (1112 loc) · 49 KB
/
underQL.mini.php
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
/****************************************************************************************
* Copyright (c) 2012, Abdullah E. Almehmadi - www.abdullaheid.net *
* All rights reserved. *
****************************************************************************************
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
Neither the name of the underQL nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written
permission.
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 HOLDER 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.
*****************************************************************************************/
define ( 'UQL_VERSION', '1.0.0' );
define ( 'UQL_VERSION_ID', 20120512 );
//define('UQL_VERSION_CODE_NAME','Eid');
define ( 'UQL_DIR_FILTER', 'filters/' );
define ( 'UQL_DIR_FILTER_API', 'filters_api/' );
define ( 'UQL_DIR_RULE', 'rules/' );
define ( 'UQL_DIR_RULE_API', 'rules_api/' );
//%s represents the table name
//define ('UQL_ABSTRACT_E_OBJECT_SYNTAX','the_%s_abstract');
define ( 'UQL_FILTER_IN', 0xA );
define ( 'UQL_FILTER_OUT', 0xC );
//%s represents the table name
define ( 'UQL_FILTER_OBJECT_SYNTAX', '%s_filter' );
define ( 'UQL_FILTER_FUNCTION_NAME', 'ufilter_%s' );
//define ( 'UQL_FILTER_FILE_NAME', 'ufilter_%s' );
//%s represents the table name
define ( 'UQL_RULE_OBJECT_SYNTAX', '%s_rule' );
define ( 'UQL_RULE_FUNCTION_NAME', 'urule_%s' );
//define ( 'UQL_RULE_FILE_NAME', 'urule_%s' );
define ( 'UQL_RULE_SUCCESS', 0x0D );
define ( 'UQL_ENTITY_OBJECT_SYNTAX', '%s' );
/* Database connection information */
define ( 'UQL_DB_HOST', 'localhost' );
define ( 'UQL_DB_USER', 'root' );
define ( 'UQL_DB_PASSWORD', 'root' );
define ( 'UQL_DB_NAME', 'abdullaheid_db' );
define ( 'UQL_DB_CHARSET', 'utf8' );
define ( 'UQL_CONFIG_USE_INVOKE_CALL', true );
// to use __invoke magic method
class UQLBase {
//public function freeResources(){}
public function the_uql_error($message) {
die ( '<h3><code><b style = "color:#FF0000">UnderQL Error: </b>' . $message . '</h3>' );
}
public function _() {
$params_count = func_num_args ();
if ($params_count < 1)
$this->error ( 'You must pass one parameter at least for _ method' );
$params = func_get_args ();
$func_name = 'the_uql_' . $params [0];
if (! method_exists ( $this, $func_name ))
$this->the_uql_error ( $params [0] . ' is not a valid action' );
$params = array_slice ( $params, 1 );
return call_user_func_array ( array ($this, $func_name ), $params );
}
}
class UQLConnection extends UQLBase {
private $uql_connection_handle;
private $uql_database_host;
private $uql_database_user_name;
private $uql_database_password;
private $uql_database_name;
private $uql_operations_charset;
public function __construct($host, $database_name, $user = 'root', $password = '', $charset = 'utf8') {
$this->uql_database_host = $host;
$this->uql_database_name = $database_name;
$this->uql_database_user_name = $user;
$this->uql_database_password = $password;
$this->uql_operations_charset = $charset;
$this->uql_connection_handle = null;
$this->the_uql_start_connection ();
}
public function the_uql_start_connection() {
$this->uql_connection_handle = mysql_connect ( $this->uql_database_host, $this->uql_database_user_name, $this->uql_database_password );
if (! $this->uql_connection_handle) {
$this->the_uql_error ( 'Unable to connect' );
return false;
}
$this->the_uql_set_database_name ( $this->uql_database_name );
$local_charset_query = sprintf ( "SET NAMES '%s'", $this->uql_operations_charset );
mysql_query ( $local_charset_query );
return $this->uql_connection_handle;
}
public function the_uql_restart_connection() {
$this->the_uql_close_connection ();
$this->the_uql_start_connection ();
}
public function the_uql_get_connection_handle() {
return $this->uql_connection_handle;
}
public function the_uql_set_database_host($host) {
$this->uql_database_host = $host;
}
public function the_uql_get_database_host() {
return $this->uql_database_host;
}
public function the_uql_set_database_name($db_name) {
$this->uql_database_name = $db_name;
$local_result = mysql_select_db ( $this->uql_database_name );
if (! $local_result) {
$this->the_uql_close_connection ();
$this->the_uql_error ( 'Unable to select database' );
return false;
}
return true;
}
public function the_uql_get_database_name() {
return $this->uql_database_name;
}
public function the_uql_set_database_user_name($user) {
$this->uql_database_user_name = $user;
}
public function the_uql_get_database_user_name() {
return $this->uql_database_user_name;
}
public function the_uql_set_database_password($password) {
$this->uql_database_password = $password;
}
public function the_uql_get_database_password() {
return $this->uql_database_password;
}
public function the_uql_set_database_charset($charset, $without_restart = false) {
/* $without_restart : if true, run a query to change charset without need to restarting the connection*/
$this->uql_operations_charset = $charset;
if ($without_restart) {
$local_charset_query = sprintf ( "SET NAMES '%s'", $this->uql_operations_charset );
mysql_query ( $local_charset_query );
}
}
public function the_uql_get_database_charset() {
return $this->uql_operations_charset;
}
public function the_uql_close_connection() {
if ($this->uql_connection_handle)
mysql_close ( $this->uql_connection_handle );
$this->uql_connection_handle = false;
}
public function __destruct() {
//Clean up
// $this -> closeConnection();
$this->uql_database_host = null;
$this->uql_database_name = null;
$this->uql_database_user_name = null;
$this->uql_database_password = null;
$this->uql_operations_charset = null;
$this->uql_connection_handle = null;
}
}
class UQLMap extends UQLBase {
private $uql_map_list;
private $uql_elements_count;
public function __construct() {
$this->uql_map_list = array ();
$this->uql_elements_count = 0;
}
public function the_uql_add_element($key, $value) {
if ($this->the_uql_find_element ( $key ) == null)
$this->uql_elements_count ++;
$this->uql_map_list [$key] = $value;
}
public function the_uql_find_element($key) {
if ($this->the_uql_is_element_exist ( $key ))
return $this->uql_map_list [$key];
return null;
}
public function the_uql_is_element_exist($key) {
if ($this->uql_elements_count <= 0)
return false;
if (@array_key_exists ( $key, $this->uql_map_list ))
return true;
return false;
}
public function the_uql_get_count() {
return count ( $this->uql_map_list );
}
public function the_uql_remove_element($key) {
if ($this->the_uql_is_element_exist ( $key )) {
unset ( $this->uql_map_list [$key] );
$this->uql_elements_count --;
}
}
public function the_uql_is_empty() {
return $this->uql_elements_count == 0;
}
public function mapCallback($callback) {
if (! $this->isEmpty ())
return array_map ( $callback, $this->map_list );
}
public function the_uql_get_map() {
return $this->uql_map_list;
}
public function __destruct() {
$this->uql_map_list = null;
$this->uql_elements_count = 0;
}
}
class UQLAbstractEntity extends UQLBase {
private $uql_entity_name;
private $uql_fields;
private $uql_fields_count;
public function __construct($entity_name, &$database_handle) {
$this->uql_entity_name = null;
$this->uql_fields = null;
$this->uql_fields_count = 0;
$this->the_uql_set_entity_name ( $entity_name, $database_handle );
}
public function the_uql_set_entity_name($entity_name, &$database_handle) {
if (($database_handle instanceof UQLConnection)) {
$this->uql_entity_name = $entity_name;
$local_string_query = sprintf ( "SHOW COLUMNS FROM `%s`", $this->uql_entity_name );
$local_query_result = mysql_query ( $local_string_query/*, $database_handle -> getConnectionHandle()*/);
if ($local_query_result) {
$this->uql_fields_count = mysql_num_rows ( $local_query_result );
@mysql_free_result ( $local_query_result );
$local_fields_list = mysql_list_fields ( $database_handle->the_uql_get_database_name (), $this->uql_entity_name );
$this->uql_fields = array ();
$local_i = 0;
while ( $local_i < $this->uql_fields_count ) {
$local_field = mysql_fetch_field ( $local_fields_list );
$this->uql_fields [$local_field->name] = $local_field;
$local_i ++;
}
@mysql_free_result ( $local_fields_list );
} else {
$this->error ( mysql_error(/*$database_handle -> getConnectionHandle()*/) );
}
}
}
public function the_uql_get_entity_name() {
return $this->uql_entity_name;
}
public function the_uql_is_field_exist($name) {
return (($this->uql_fields != null) && (array_key_exists ( $name, $this->uql_fields )));
}
public function the_uql_get_field_object($name) {
if ($this->the_uql_is_field_exist ( $name ))
return $this->uql_fields [$name];
return null;
}
public function the_uql_get_all_fields() {
return $this->uql_fields;
}
public function the_uql_get_fields_count() {
return $this->uql_fields_count;
}
public function __destruct() {
$this->uql_entity_name = null;
$this->uql_fields = null;
$this->uql_fields_count = 0;
}
}
class UQLFilter extends UQLBase {
private $uql_entity_name;
private $uql_filters_map;
public function __construct($entity_name) {
$this->uql_entity_name = $entity_name;
$this->uql_filters_map = new UQLMap ();
}
public function __call($function_name, $parameters) {
$local_params_count = count ( $parameters );
if ($local_params_count < 1 /*filter_name [AT LEAST]*/)
$this->the_uql_error ( $function_name . ' filter must have 1 parameter at least' );
if ($local_params_count == 1)
$this->the_uql_add_filter ( $function_name, array ($parameters [0], 'inout' ) );
else
$this->the_uql_add_filter ( $function_name, $parameters );
return $this;
}
protected function the_uql_add_filter($field, $filter) {
if (! $this->uql_filters_map->the_uql_is_element_exist ( $field ))
$this->uql_filters_map->the_uql_add_element ( $field, new UQLMap () );
$local_filter = $this->uql_filters_map->the_uql_find_element ( $field );
$local_filter->the_uql_add_element ( $local_filter->the_uql_get_count (), array ('filter' => $filter, 'is_active' => true ) );
$this->uql_filters_map->the_uql_add_element ( $field, $local_filter );
}
protected function the_uql_set_filter_activation($field_name, $filter_name, $activation) {
$local_filter = $this->uql_filters_map->the_uql_find_element ( $field_name );
if (! $local_filter)
$this->the_uql_error ( 'You can not stop a filter for unknown field (' . $field_name . ')' );
/* $target_filter = $local_filter->the_uql_find_element($filter_name);
if(!$target_filter)
$this->the_uql_error('You can not stop unknown filter ('.$filter_name.')'); */
for($i = 0; $i < $local_filter->the_uql_get_count (); $i ++) {
$target_filter = $local_filter->the_uql_find_element ( $i );
if (strcmp ( $target_filter ['filter'] [0], $filter_name ) == 0) {
$target_filter ['is_active'] = $activation;
$local_filter->the_uql_add_element ( $i, array ('filter' => $target_filter ['filter'], 'is_active' => $activation ) );
$this->uql_filters_map->the_uql_add_element ( $field_name, $local_filter );
}
}
}
public function the_uql_start_filters(/*$field_name,$filter_name*/) {
$params_count = func_num_args ();
if ($params_count < 2)
$this->the_uql_error ( 'start_filters needs 2 parameters at least' );
$filters_counts = $params_count - 1; // remove field name
$parameters = func_get_args ();
if ($filters_counts == 1) {
$this->the_uql_set_filter_activation ( $parameters [0], $parameters [1], true );
return;
} else {
for($i = 0; $i < $filters_counts - 1; $i ++)
$this->the_uql_set_filter_activation ( $parameters [0], $parameters [$i + 1], true );
}
}
public function the_uql_stop_filters(/*$field_name,$filter_name*/) {
$params_count = func_num_args ();
if ($params_count < 2)
$this->the_uql_error ( 'stop_filters needs 2 parameters at least' );
$filters_counts = $params_count - 1; // remove field name
$parameters = func_get_args ();
if ($filters_counts == 1) {
$this->the_uql_set_filter_activation ( $parameters [0], $parameters [1], false );
return;
} else {
for($i = 0; $i < $filters_counts - 1; $i ++)
$this->the_uql_set_filter_activation ( $parameters [0], $parameters [$i + 1], false );
}
}
public function the_uql_get_filters_by_field_name($field_name) {
return $this->uql_filters_map->the_uql_find_element ( $field_name );
}
public function the_uql_get_filters() {
return $this->uql_filters_map;
}
public function the_uql_get_entity_name() {
return $this->uql_entity_name;
}
public static function the_uql_find_filter_object($entity) {
$filter_object_name = sprintf ( UQL_FILTER_OBJECT_SYNTAX, $entity );
if (isset ( $GLOBALS [$filter_object_name] ))
$filter_object = $GLOBALS [$filter_object_name];
else
$filter_object = null;
return $filter_object;
}
public function __destruct() {
$this->uql_entity_name = null;
$this->uql_filters_map = null;
}
}
class UQLFilterEngine extends UQLBase {
private $uql_filter_object;
private $uql_values_map;
//current inserted | updated $key => $value pairs
private $uql_in_out_flag;
// specify if the engine for input or output
public function __construct(&$filter_object, $in_out_flag) {
$this->uql_filter_object = $filter_object;
$this->uql_values_map = null;
$this->uql_in_out_flag = $in_out_flag;
}
public function the_uql_set_values_map(&$values_map) {
$this->uql_values_map = $values_map;
}
public function the_uql_apply_filter($field_name, $value) {
if ($this->uql_filter_object != null)
$filters = $this->uql_filter_object->the_uql_get_filters_by_field_name ( $field_name );
else
return $value;
if ($filters == null)
return $value;
$tmp_value = $value;
foreach ( $filters->the_uql_get_map () as $filter_id => $filter_value ) {
$filter_name = $filter_value ['filter'] [0];
$filter_flag = $filter_value ['filter'] [1];
// echo $filter_flag;
if (strcmp ( strtolower ( $filter_flag ), 'in' ) == 0)
$filter_flag = UQL_FILTER_IN;
else if (strcmp ( strtolower ( $filter_flag ), 'out' ) == 0)
$filter_flag = UQL_FILTER_OUT;
else
$filter_flag = UQL_FILTER_IN | UQL_FILTER_OUT;
if ((! $filter_value ['is_active']) || (($filter_flag != $this->uql_in_out_flag) && ($filter_flag != UQL_FILTER_IN | UQL_FILTER_OUT)))
continue;
$include_filter_api = 'include_filters';
$include_filter_api ( $filter_name );
$filter_api_function = sprintf ( UQL_FILTER_FUNCTION_NAME, $filter_name );
if (! function_exists ( $filter_api_function ))
die ( $filter_name . ' is not a valid filter' );
if (@count ( $filter_value ['filter'] ) == 2) // the filter has no parameter(s)
$tmp_value = $filter_api_function ( $field_name, $tmp_value, $filter_flag );
else {
$params = array_slice ( $filter_value ['filter'], 2 );
$tmp_value = $filter_api_function ( $field_name, $tmp_value, $filter_flag, $params );
}
}
return $tmp_value;
}
public function the_uql_run_engine() {
if (! $this->uql_values_map || $this->uql_values_map->the_uql_get_count () == 0)
return null;
foreach ( $this->uql_values_map->the_uql_get_map () as $name => $value ) {
$this->uql_values_map->the_uql_add_element ( $name, $this->the_uql_apply_filter ( $name, $value ) );
}
return $this->uql_values_map;
}
public function __destruct() {
$this->uql_values_map = null;
$this->uql_filter_object = null;
}
}
class UQLRule extends UQLBase {
private $uql_entity_name;
private $uql_alises_map;
private $uql_rules_map;
public function __construct($entity_name) {
$this->uql_entity_name = $entity_name;
$this->uql_alises_map = new UQLMap ();
$this->uql_rules_map = new UQLMap ();
}
public function __call($function_name, $parameters) {
$local_params_count = count ( $parameters );
if ($local_params_count == 0)
return;
$this->the_uql_add_rule ( $function_name, $parameters );
return $this;
}
protected function the_uql_add_rule($field, $rule) {
if (! $this->uql_rules_map->the_uql_is_element_exist ( $field ))
$this->uql_rules_map->the_uql_add_element ( $field, new UQLMap () );
$local_rule = $this->uql_rules_map->the_uql_find_element ( $field );
$local_rule->the_uql_add_element ( $local_rule->the_uql_get_count (), array ('rule' => $rule, 'is_active' => true ) );
$this->uql_rules_map->the_uql_add_element ( $field, $local_rule );
}
protected function the_uql_set_rule_activation($field_name, $rule_name, $activation) {
$local_rule = $this->uql_rules_map->the_uql_find_element ( $field_name );
if (! $local_rule)
$this->the_uql_error ( 'You can not stop a rule for unknown field (' . $field_name . ')' );
/*$target_rule = $local_rule->the_uql_find_element($rule_name);
if(!$target_rule)
$this->the_uql_error('You can not stop unknown rule ('.$rule_name.')');*/
for($i = 0; $i < $local_rule->the_uql_get_count (); $i ++) {
$target_rule = $local_rule->the_uql_find_element ( $i );
if (strcmp ( $target_rule ['rule'] [0], $rule_name ) == 0) {
$target_rule ['is_active'] = $activation;
$local_rule->the_uql_add_element ( $i, array ('rule' => $target_rule ['rule'], 'is_active' => $activation ) );
$this->uql_rules_map->the_uql_add_element ( $field_name, $local_rule );
}
}
}
public function the_uql_start_rules(/*$field_name,$rule_name*/) {
$params_count = func_num_args ();
if ($params_count < 2)
$this->error ( 'start_rules needs 2 parameters at least' );
$rules_counts = $params_count - 1;
// remove field name
$parameters = func_get_args ();
if ($rules_counts == 1) {
$this->the_uql_set_rule_activation ( $parameters [0], $parameters [1], true );
return;
} else {
for($i = 0; $i < $rules_counts - 1; $i ++)
$this->the_uql_set_rule_activation ( $parameters [0], $parameters [$i + 1], true );
}
}
public function the_uql_stop_rules(/*$field_name,$rule_name*/) {
$params_count = func_num_args ();
if ($params_count < 2)
$this->the_uql_error ( 'stop_rules needs 2 parameters at least' );
$rules_counts = $params_count - 1;
// remove field name
$parameters = func_get_args ();
if ($rules_counts == 1) {
$this->the_uql_set_rule_activation ( $parameters [0], $parameters [1], false );
return;
} else {
for($i = 0; $i < $rules_counts - 1; $i ++)
$this->the_uql_set_rule_activation ( $parameters [0], $parameters [$i + 1], false );
}
}
public function the_uql_get_rules_by_field_name($field_name) {
return $this->uql_rules_map->the_uql_find_element ( $field_name );
}
public function the_uql_add_alias($key, $value) {
$this->uql_alises_map->the_uql_add_element ( $key, $value );
}
public function the_uql_get_alias($key) {
return $this->uql_alises_map->the_uql_find_element ( $key );
}
public function the_uql_get_rules() {
return $this->uql_alises_map;
}
public function the_uql_get_entity_name() {
return $this->uql_entity_name;
}
public function the_uql_get_aliases() {
return $this->uql_alises_map;
}
public static function the_uql_find_rule_object($entity) {
$rule_object_name = sprintf ( UQL_RULE_OBJECT_SYNTAX, $entity );
if (isset ( $GLOBALS [$rule_object_name] ))
$rule_object = $GLOBALS [$rule_object_name];
else
$rule_object = null;
return $rule_object;
}
public function __destruct() {
$this->uql_entity_name = null;
$this->uql_rules_map = null;
$this->uql_alises_map = null;
}
}
class UQLRuleMessagesHandler extends UQLBase {
private $uql_messages;
public function __construct(&$the_msgs_list) {
if (! $the_msgs_list)
$this->uql_messages = array ();
else
$this->uql_messages = $the_msgs_list;
}
public function in($field_name) {
return isset ( $this->uql_messages [$field_name] );
}
public function at($field_name, $rule_name) {
return isset ( $this->uql_messages [$field_name] [$rule_name] );
}
public function get($field_name, $rule_name) {
return $this->uql_messages [$field_name] [$rule_name];
}
public function __destruct() {
$this->uql_messages = null;
}
}
class UQLRuleEngine extends UQLBase {
private $uql_rule_object;
private $uql_values_map;
//current inserted | updated $key => $value pairs
private $uql_false_rule_flag;
// true if there is at least one rule failed.
private $uql_fail_rules_list;
// list of error messages about each field that fail in one or more rules
public function __construct(&$rule_object, &$values_map) {
$this->uql_rule_object = $rule_object;
$this->uql_values_map = $values_map;
$this->uql_false_rule_flag = false;
$this->uql_fail_rules_list = new UQLMap ();
}
protected function the_uql_apply_rule($field_name, $value) {
$rules = $this->uql_rule_object->the_uql_get_rules_by_field_name ( $field_name );
$the_results = array ();
if ($rules == null)
return true;
foreach ( $rules->the_uql_get_map () as $rule_id => $rule_value ) {
if (! $rule_value ['is_active'])
continue;
$rule_name = $rule_value ['rule'] [0];
$include_rule_api = 'include_rules';
$include_rule_api ( $rule_name );
$rule_api_function = sprintf ( UQL_RULE_FUNCTION_NAME, $rule_name );
if (! function_exists ( $rule_api_function ))
$this->the_uql_error ( $rule_name . ' is not a valid rule' );
$alias = $this->uql_rule_object->the_uql_get_alias ( $field_name );
if (@count ( $rule_value ['rule'] ) == 1) // the rule has no parameter(s)
$result = $rule_api_function ( $field_name, $value, $alias );
else {
$params = array_alice ( $rule_value ['rule'] );
// remove rule name
$result = $rule_api_function ( $field_name, $value, $alias, $params );
}
if ($result != UQL_RULE_SUCCESS) {
$the_results [$rule_name] = $result;
// message
$this->uql_false_rule_flag = true;
} else
$the_results [$rule_name] = $result;
// OK
}
return $the_results;
}
public function the_uql_are_rules_passed() {
return $this->uql_false_rule_flag == false;
}
public function the_uql_run_engine() {
if (! $this->uql_values_map || $this->uql_values_map->the_uql_get_count () == 0)
return null;
$result = true;
foreach ( $this->uql_values_map->the_uql_get_map () as $name => $value ) {
$result = $this->the_uql_apply_rule ( $name, $value );
if ($result != UQL_RULE_SUCCESS)
$this->uql_fail_rules_list->the_uql_add_element ( $name, $result );
}
if ($this->the_uql_are_rules_passed ())
return true;
$the_map = $this->uql_fail_rules_list->the_uql_get_map ();
return new UQLRuleMessagesHandler ( $the_map );
}
public function __destruct() {
$this->uql_values_map = null;
$this->uql_rule_object = null;
}
}
class UQLQuery extends UQLBase {
private $uql_database_handle;
private $uql_query_result;
private $uql_current_row_object;
private $uql_current_query_fields;
public function __construct(&$database_handle) {
$this->uql_database_handle = (($database_handle instanceof UQLConnection) ? $database_handle : null);
$this->uql_query_result = null;
$this->uql_current_row_object = null;
$this->uql_current_query_fields = array ();
}
public function the_uql_set_database_handle($database_handle) {
$this->database_handle ( ($database_handle instanceof UQLConnection) ? $database_handle : null );
}
public function the_uql_get_database_handle() {
return $this->uql_database_handle;
}
public function the_uql_execute_query($query) {
if ($this->uql_database_handle instanceof UQLConnection) {
$this->uql_query_result = mysql_query ( $query /*,$this -> database_handle*/);
$this->the_uql_is_there_any_error ();
if (! $this->uql_query_result)
return false;
return true;
}
return false;
}
public function the_uql_get_current_query_fields() {
if (! $this->uql_query_result)
return null;
$local_fields_count = @mysql_num_fields ( $this->uql_query_result );
if ($local_fields_count == 0)
return null;
for($local_i = 0; $local_i < $local_fields_count; $local_i ++)
$this->uql_current_query_fields [$local_i] = mysql_field_name ( $this->uql_query_result, $local_i );
return $this->uql_current_query_fields;
}
public function the_uql_fetch_row() {
if ($this->uql_query_result) {
$this->uql_current_row_object = mysql_fetch_object ( $this->uql_query_result );
return $this->uql_current_row_object;
}
return false;
}
public function the_uql_reset_result() {
if ($this->uql_query_result)
return mysql_data_seek ( $this->uql_query_result, 0 );
return false;
}
public function the_uql_get_current_row() {
return $this->uql_current_row_object;
}
public function the_uql_get_count() {
if ($this->uql_query_result)
return mysql_num_rows ( $this->uql_query_result );
return 0;
}
public function the_uql_get_affected_rows() {
if (($this->uql_database_handle instanceof UQLConnection) && ($this->uql_query_result))
return mysql_affected_rows ( $this->uql_database_handle );
return 0;
}
public function the_uql_get_last_inserted_id() {
if (($this->uql_database_handle instanceof UQLConnection) && ($this->uql_query_result))
return mysql_insert_id ( $this->uql_database_handle );
return 0;
}
public function the_uql_free_result() {
if ($this->uql_query_result)
@mysql_free_result ( $this->uql_query_result );
$this->uql_current_row_object = null;
$this->uql_query_result = null;
$this->uql_current_query_fields = array ();
}
public function the_uql_is_there_any_error() {
if (mysql_errno () != 0)
$this->the_uql_error ( '[MySQL query error - ' . mysql_errno () . '] - ' . mysql_error () );
}
public function __destruct() {
$this->the_uql_free_result ();
$this->uql_query_result = null;
$this->uql_current_query_fields = null;
$this->uql_current_row_object = null;
$this->uql_database_handle = null;
}
}
class UQLQueryPath extends UQLBase {
public $uql_abstract_entity;
// reference to the abstract table's data
public $uql_query_object;
public $uql_filter_engine;
public function __construct(&$database_handle, &$abstract_entity) {
if ($abstract_entity instanceof UQLAbstractEntity)
$this->uql_abstract_entity = $abstract_entity;
else
$this->error ( 'You must provide a appropriate value for abstract_entity parameter' );
$this->uql_query_object = new UQLQuery ( $database_handle );
$filter_object = UQLFilter::the_uql_find_filter_object ( $this->uql_abstract_entity->the_uql_get_entity_name () );
$this->uql_filter_engine = new UQLFilterEngine ( $filter_object, UQL_FILTER_OUT );
}
public function the_uql_execute_query($query) {
if ($this->uql_query_object->the_uql_execute_query ( $query )) {
/*if ($this->uql_query_object->the_uql_get_count () > 0) {
$this->the_uql_get_next ();
}*/
return true;
}
return false;
}
public function the_uql_get_next() {
return $this->uql_query_object->the_uql_fetch_row ();
}
public function the_uql_reset_result() {
return $this->uql_query_object->the_uql_reset_result();
}
public function the_uql_get_count() {
return $this->uql_query_object->the_uql_get_count ();
}
public function the_uql_get_query_object() {
return $this->uql_query_object;
}
public function the_uql_get_abstract_entity() {
return $this->uql_abstract_entity;
}
public function __get($key) {
if (! $this->uql_abstract_entity->the_uql_is_field_exist ( $key ))
$this->the_uql_error ( "[$key] does not exist in ".$this->uql_abstract_entity->the_uql_get_entity_name());
$local_current_query_fields = $this->uql_query_object->the_uql_get_current_query_fields ();
if ($local_current_query_fields == null)
$this->the_uql_error ( "[$key] does not exist in the current query fields" );
foreach ( $local_current_query_fields as $local_field_name ) {
if (strcmp ( $key, $local_field_name ) == 0) {
$local_current_row = $this->uql_query_object->the_uql_get_current_row ();
if ($local_current_row == null)
return null;