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
8 * @author Marius Orcsik <marius@habarnam.ro>
11 class tdoAbstractClause
{
12 protected $subject, $predicate, $predicative;
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
= '';
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;
42 $this->predicate
= '';
43 $this->predicative
= '';
48 public function __destruct () {}
50 public function __toString () {
51 // var_dump($this->subject, $this->predicate, $this->predicative);
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
;
65 if (is_null($this->predicative
)) {
66 if ($this->validPredicative ($this->predicate
)) {
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
) {
86 $preStr = $this->predicative
;
89 $retStr = $subStr.' '.$this->predicate
.' '.$preStr;
90 if (($this->subject
instanceof tdoAbstractClause
) && ($this->predicative
instanceof tdoAbstractClause
))
91 return '('.$retStr.')';
96 private function validPredicative ($predicate) {
97 // need to find a way to abstract these
98 // $validPredicates = array (
113 if ($this->predicative
instanceof tdoAbstractClause
) {
114 // we'll have Subject AND|OR|XOR Predicative
115 $validPredicates = array (
122 } elseif (($this->predicative
instanceof tdoAbstractField
) ||
is_numeric($this->predicative
)) {
123 // we'll have Subject =|!= Predicative
124 $validPredicates = array (
132 } elseif (is_array($this->predicative
)) {
133 $validPredicates = array (
137 } elseif (is_string($this->predicative
)) {
138 $validPredicates = array (
147 } elseif (is_null($this->predicative
)) {
148 $validPredicates = array (
154 return in_array(strtolower($predicate), $validPredicates);
156 // if (in_array($predicate, $validPredicates) && (($predicative instanceof tdoAbstractClause) || ($predicative instanceof tdoAbstractField)))