* making the fracking paginator work
[vsc.git] / _res / _libs / models / tdoabstractclause.class.php
blob20ea4114f53ce8516acad5f705952c739fea7fe3
1 <?php
3 /**
4 * class to abstract a where clause in a SQL query
5 * TODO: add possibility of complex wheres: (t1 condition1 OR|AND|XOR t2.condition2)
6 * TODO: abstract the condition part of a where clause - currently string based :D
7 * @package ts_models
8 * @author Marius Orcsik <marius@habarnam.ro>
9 * @date 09.02.27
11 class tdoAbstractClause {
12 protected $subject, $predicate, $predicative;
14 /**
15 * initializing a WHERE|ON clause
17 * @param tdoAbstractClause|tdoAbstractField $subject
18 * @param string $predicate
19 * @param tdoAbstractClause|tdoAbstractField|null $complement
21 public function __construct ($subject, $predicate = null, $predicative = null) {
22 // I must be careful with the $subject == 1 because (int)object == 1
23 if (($subject === 1 || is_string($subject)) && $predicate == null && $predicative == null) {
24 $this->subject = $subject;
25 $this->predicate = '';
26 $this->predicative = '';
27 return;
30 if (($subject instanceof tdoAbstractField ) || ($subject instanceof tdoAbstractClause )) {
31 $this->subject = &$subject;
33 // $subject->set_where = true;
35 $this->predicative = &$predicative;
37 if ($this->validPredicative ($predicate))
38 $this->predicate = $predicate;
40 } else {
41 $this->subject = '';
42 $this->predicate = '';
43 $this->predicative = '';
45 return;
48 public function __destruct () {}
50 public function __toString () {
51 // var_dump($this->subject, $this->predicate, $this->predicative);
52 // echo '<br/>';
53 if ($this->subject === '1' || is_string($this->subject)) {
54 // var_dump($this->subject);
55 return (string)$this->subject;
56 } elseif ($this->subject instanceof tdoAbstractClause) {
57 $subStr = (string)$this->subject;
58 } elseif ($this->subject instanceof tdoAbstractField) {
59 // without $this->subject->table != 't' we have a bug in the delete op
60 $subStr = ($this->subject->table != 't' ? $this->subject->table.'.': '') . $this->subject->name;
61 } else {
62 return '';
65 if (is_null($this->predicative)) {
66 if ($this->validPredicative ($this->predicate)) {
67 $preStr = 'NULL';
68 } else
69 $preStr = '';
70 } elseif (is_numeric($this->predicative)) {
71 $preStr = $this->predicative;
72 } elseif (is_string($this->predicative)) {
73 $preStr = $this->predicative;
75 if ($this->predicate == 'LIKE') {
76 $preStr = '%'.$this->predicate.'%';
79 $preStr = (stripos($preStr, '"') !== 0 ? '"'.$preStr.'"' : $preStr);//'"'.$preStr.'"';
80 } elseif (is_array($this->predicative)) {
81 $preStr = '("'.implode('", "',$this->predicative).'")';
82 } elseif ($this->predicative instanceof tdoAbstractfield) {
83 $preStr = ($this->predicative->table != 't' ? $this->predicative->table.'.': '').$this->predicative->name;
84 } elseif ($this->predicative instanceof tdoAbstractClause) {
85 $subStr = $subStr;
86 $preStr = $this->predicative;
89 $retStr = $subStr.' '.$this->predicate.' '.$preStr;
90 if (($this->subject instanceof tdoAbstractClause) && ($this->predicative instanceof tdoAbstractClause))
91 return '('.$retStr.')';
93 return $retStr;
96 private function validPredicative ($predicate) {
97 // need to find a way to abstract these
98 // $validPredicates = array (
99 // 'AND',
100 // '&&',
101 // 'OR',
102 // '||',
103 // 'XOR',
104 // 'IS',
105 // 'IS NOT',
106 // '!',
107 // 'IN',
108 // 'LIKE',
109 // '=',
110 // '!=',
111 // '<>'
112 // );
113 if ($this->predicative instanceof tdoAbstractClause) {
114 // we'll have Subject AND|OR|XOR Predicative
115 $validPredicates = array (
116 'and',
117 '&&',
118 'or',
119 '||',
120 'xor'
122 } elseif (($this->predicative instanceof tdoAbstractField) || is_numeric($this->predicative)) {
123 // we'll have Subject =|!= Predicative
124 $validPredicates = array (
125 '=',
126 '!=',
127 '>',
128 '<',
129 '>=',
130 '<='
132 } elseif (is_array($this->predicative)) {
133 $validPredicates = array (
134 'in',
135 'not in'
137 } elseif (is_string($this->predicative)) {
138 $validPredicates = array (
139 '=',
140 'like',
141 // dates
142 '>',
143 '<',
144 '>=',
145 '<='
147 } elseif (is_null($this->predicative)) {
148 $validPredicates = array (
149 'is',
150 'is not'
154 return in_array(strtolower($predicate), $validPredicates);
156 // if (in_array($predicate, $validPredicates) && (($predicative instanceof tdoAbstractClause) || ($predicative instanceof tdoAbstractField)))
157 // return true;
158 // return false;