-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.php
208 lines (201 loc) · 6.17 KB
/
Search.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
<?php
namespace BlueSeed;
/**
*
* This class make possible SQL Commands to retrieve data
* @author ivonascimento <ivo@o8o.com.br>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @package system
*/
Class Search{
/**
*
* stored the sql sentence are created
* @var string
*/
private $sqlExp = "";
private $countis = false;
/**
* the count constant
* @var unknown_type
*/
const COUNT = 0;
/**
*
* store a instance of VO with the parameter used in search
* @var VO
*/
private $vo;
/**
*
* Constructor
* @param VO $vo this VO have the basic parameters used to perform the search
* @param string $fields Optional Parameter where are set the request fields into Result
* @access private
*/
private function __construct($vo, $fields='*'){
$this->vo = $vo;
if ($fields === Search::COUNT) {
$fields = " count({$vo->getIndexName()}) ";
$this->countis = true;
}
else if ($fields != '*'){
$fields = explode(',', $fields);
foreach ($fields as &$field){
$field = trim($field);
}
$fields = implode(',', $fields);
}else
$fields = $vo->getTableName().'.*';
$this->sqlExp = "Select {$fields} from {$vo->getTableName()}";
}
/**
*
* Request a New Class of Search
* @param activeRecord $vo a instance of a VO
* @param string $fields a list of fields
* @access public
* @return Search
*/
public static function Select(activeRecord $vo, $fields='*'){
return new Search( $vo, $fields );
}
/**
*
* Add the expression isNull to Search
* @param string $param a field from VO where the value is setted like '%value%'
* @return Search
* @access public
*/
public function isnull($param){
$this->sqlExp.= " {$this->vo->getTableName()}.{$param} is null ";
return $this;
}
public function in($param){
$this->sqlExp.= " {$this->vo->getTableName()}.{$param} in ({$this->vo->$param}) ";
return $this;
}
public function equal($param){
$this->sqlExp.= " {$this->vo->getTableName()}.{$param} = '{$this->vo->$param}' ";
return $this;
}
public function _($literal){
$this->sqlExp.= " {$literal} ";
return $this;
}
public function like( $param){
$this->sqlExp.= " {$this->vo->getTableName()}.{$param} like '{$this->vo->$param}' ";
return $this;
}
public function notlike( $param){
$this->sqlExp.= " {$this->vo->getTableName()}.{$param} not like '{$this->vo->$param}' ";
return $this;
}
public function ilike( $param){
$this->sqlExp.= " {$this->vo->getTableName()}.{$param} ilike '{$this->vo->$param}' ";
return $this;
}
/**
*
* used to mapping common SQL instruction
* @param string $name
* @param Array $arg
*/
public function __call($name, $arg){
$this->sqlExp.=" {$name} ";
return $this;
}
/**
*
* configure Subquerys
* @param string $field a field name used to construct the 'in' SQL instruction
* @param Search $search
* @return Search
* @access public
*/
public function inSearch( $field, Search $search){
$this->sqlExp .= "{$this->vo->getTableName()}.{$field} in (". $search->sqlExp .") ";
return $this;
}
public function between($field, $de, $ate) {
$this->sqlExp .= "{$this->vo->getTableName()}.{$field} BETWEEN '{$de}' and '{$ate}' ";
return $this;
}
public function EqualMoreThen($field) {
$this->sqlExp.= " {$this->vo->getTableName()}.{$field} >= '{$this->vo->$field}' ";
return $this;
}
public function MoreThen($field) {
$this->sqlExp.= " {$this->vo->getTableName()}.{$field} > '{$this->vo->$field}' ";
return $this;
}
public function EqualLessThen($field) {
$this->sqlExp.= " {$this->vo->getTableName()}.{$field} <= '{$this->vo->$field}' ";
return $this;
}
public function LessThen($field) {
$this->sqlExp.= " {$this->vo->getTableName()}.{$field} < '{$this->vo->$field}' ";
return $this;
}
/**
*
* configure Subquerys
* @param string $field a field name used to construct the 'in' SQL instruction
* @param Search $search
* @return Search
* @access public
*/
public function notInSearch( $field, Search $search){
$this->sqlExp .= "{$this->vo->getTableName()}.{$field} not in (". $search->sqlExp .") ";
return $this;
}
public function unionDistinct(Search $search){
$this->sqlExp .= "union distinct {$search->getSQL()} ";
return $this;
}
public function getSQL()
{
return $this->sqlExp;
}
/**
*
* perform the Search into DatabaseConnection configured into VO
* @throws Exception
* @return PDOStatement
* @access public
*/
public function exec($asStdClass = false){
$dataModel = get_class($this->vo);
$obj = new $dataModel();
try{
$con = Database::getInstance()->
get( $this->vo->getConnectionName() )->
get();
$data = $con->query($this->sqlExp);
$err = $con->ErrorInfo();
if ($err[0]!='0000') {
throw New \PDOException($err[2], $err[0]);
}
if ( $this->countis)
$dados = $data->fetchAll( \PDO::FETCH_NUM);
else
$dados = $data->fetchAll( \PDO::FETCH_ASSOC );
}catch( \Exception $e){
throw New \Exception ("Erro ao executar a pesquisa: {$e->getMessage()}");
}
if ($this->countis) {
return $dados[0][0];
}
$return = Array();
foreach ($dados as $idxRec => $Rec){
$obj = (!$asStdClass)?new $dataModel(): New \StdClass();
foreach ($Rec as $idx => $value){
$obj->$idx = $value;
}
$obj->$idx = $value;
array_push($return, $obj);
}
return $return;
}
}
?>