/
Where.php
148 lines (139 loc) · 3.99 KB
/
Where.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
<?php
/**
* Sql Conditions generator.
*
* @package Tsukiyo
* @author Satoshi Nishimura <nishim314@gmail.com>
* @copyright Copyright (c) 2012 Satoshi Nishimura
*/
/**
* Sql Conditions generator interface.
*
* @package Tsukiyo
* @author Satoshi Nishimura <nishim314@gmail.com>
* @copyright Copyright (c) 2012 Satoshi Nishimura
*/
interface Tsukiyo_Where {
public function add(Tsukiyo_Where $where);
public function getString();
public function getParams();
public function isNoParam();
}
/**
* Single Condition
* @package Tsukiyo
*/
class Tsukiyo_WhereNode implements Tsukiyo_Where{
private $op;
private $name;
private $value;
private $noParam;
public function __construct($op, $voName, $value, $isNoParam = false){
$this->op = $op;
$this->name = Tsukiyo_Util::toDbName($voName);
$this->value = $value;
$this->noParam = $isNoParam;
}
public function add(Tsukiyo_Where $where){
$ret = new Tsukiyo_WhereTree('and');
$ret->add($this);
$ret->add($where);
return $ret;
}
public function getString(){
$ret = " $this->name $this->op ";
if ($this->noParam)
return $ret;
return $ret . ' ? ';
}
public function isNoParam(){
return $this->noParam;
}
public function getParams(){
return $this->value;
}
}
/**
* Conditions Tree
* @package Tsukiyo
*/
class Tsukiyo_WhereTree implements Tsukiyo_Where{
private $children = array();
private $andOr;
public function __construct($andOr){
$this->andOr = $andOr;
}
public function add(Tsukiyo_Where $where){
$this->children[] = $where;
return $this;
}
public function getString(){
if (count($this->children) === 0)
return null;
$strings = array();
foreach ($this->children as $child){
$strings[] = $child->getString();
}
return '(' . implode(' '.$this->andOr.' ', $strings) . ')';
}
public function isNoParam(){
return false;
}
public function getParams(){
$ret = array();
foreach ($this->children as $child){
if ($child->isNoParam())
continue;
$params = $child->getParams();
if (is_array($params)){
foreach($params as $param)
$ret[] = $param;
}else{
$ret[] = $params;
}
}
return $ret;
}
public function eq($where){
return $this->add(Tsukiyo_Helper::where('=', $where));
}
public function ne($where){
return $this->add(Tsukiyo_Helper::where('<>', $where));
}
public function lt($where){
return $this->add(Tsukiyo_Helper::where('<', $where));
}
public function le($where){
return $this->add(Tsukiyo_Helper::where('<=', $where));
}
public function gt($where){
return $this->add(Tsukiyo_Helper::where('>', $where));
}
public function ge($where){
return $this->add(Tsukiyo_Helper::where('>=', $where));
}
public function like($where){
return $this->add(Tsukiyo_Helper::like($where, true, true));
}
public function notLike($where){
return $this->add(Tsukiyo_Helper::like($where, true, true, true));
}
public function starts($where){
return $this->add(Tsukiyo_Helper::like($where, false, true));
}
public function notStarts($where){
return $this->add(Tsukiyo_Helper::like($where, false, true, true));
}
public function ends($where){
return $this->add(Tsukiyo_Helper::like($where, true, false));
}
public function notEnds($where){
return $this->add(Tsukiyo_Helper::like($where, true, false, true));
}
public function isNull($where){
return $this->add(Tsukiyo_Helper::noParamWhere('is null', $where));
}
public function isNotNull($where){
return $this->add(Tsukiyo_Helper::noParamWhere('is not null', $where));
}
}