Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.QueryDb.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Query to Database
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.QueryDb.php,v 1.16 2008/08/11 10:03:29 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 
19 include_once ('Class.Log.php');
20 
21 class QueryDb
22 {
23 
24  var $nb = 0;
25  var $LastQuery = "";
26 
27  var $table;
28 
29  var $operators = array(
30  "none" => array(
31  "lib" => " --",
32  "oper" => "",
33  "param" => "NONE"
34  ) ,
35  "begin" => array(
36  "lib" => "Commence par",
37  "oper" => "like",
38  "param" => "SEMIPERCENT"
39  ) ,
40  "like" => array(
41  "lib" => "Contient",
42  "oper" => "like",
43  "param" => "PERCENT"
44  ) ,
45  "nlike" => array(
46  "lib" => "Ne Contient Pas",
47  "oper" => "not like",
48  "param" => "PERCENT"
49  ) ,
50  "=" => array(
51  "lib" => "Est égal à",
52  "oper" => "=",
53  "param" => "NORMAL"
54  ) ,
55  "!=" => array(
56  "lib" => "Est différent de",
57  "oper" => "!=",
58  "param" => "NORMAL"
59  ) ,
60  ">" => array(
61  "lib" => "Est Supérieur à",
62  "oper" => ">",
63  "param" => "NORMAL"
64  ) ,
65  "<" => array(
66  "lib" => "Est Inférieur à",
67  "oper" => "<",
68  "param" => "NORMAL"
69  ) ,
70  "notn" => array(
71  "lib" => "N'est pas Vide",
72  "oper" => "is not null",
73  "param" => "NONE"
74  ) ,
75  "null" => array(
76  "lib" => "Est Vide",
77  "oper" => "is null",
78  "param" => "NONE"
79  )
80  );
81  var $casse = "NON";
82 
83  var $criteria = "";
84  var $order_by = "";
85  var $list = array();
86  /**
87  * @var DbObj
88  */
89  public $basic_elem;
90 
92  {
93  //
94  $this->log = new Log("", "Query", "$class");
95  $this->basic_elem = new $class($dbaccess);
96  $this->dbaccess = $this->basic_elem->dbaccess;
97  $this->class = $class;
98  }
99 
100  private function initQuery($start = 0, $slice = 0, $p_query = "", $onlycont = false)
101  {
102  if ($start == "") $start = 0;
103  if ($p_query == '') {
104  // select construct
105  $select = "";
106  if (!$onlycont) {
107  foreach ($this->basic_elem->fields as $k => $v) {
108  $select = $select . " " . $this->basic_elem->dbtable . "." . $v . ",";
109  }
110 
111  foreach ($this->basic_elem->sup_fields as $k => $v) {
112  $select = $select . " " . $v . ",";
113  }
114  $select = substr($select, 0, strlen($select) - 1);
115  } else {
116  $select = 'count(*)';
117  }
118  // from
119  $from = $this->basic_elem->dbtable;
120  foreach ($this->basic_elem->sup_tables as $k => $v) {
121  $from = $from . "," . $v;
122  }
123 
124  $query = "select {$select}
125  from {$from} ";
126 
127  $nb_where = 0;
128  $where[$nb_where] = $this->CriteriaClause();
129  if ($where[$nb_where] != "") {
130  $nb_where++;
131  }
132  $where[$nb_where] = $this->AlphaClause();
133  if ($where[$nb_where] != "") {
134  $nb_where++;
135  }
136  $where[$nb_where] = $this->SupClause();
137  if ($where[$nb_where] != "") {
138  $nb_where++;
139  }
140 
141  if ($nb_where > 0) {
142  $i = 0;
143  $query = $query . ' where ';
144  reset($where);
145  while (list($k, $v) = each($where)) {
146  if ($v != "") {
147  if ($i == 0) {
148  $query = $query . $v;
149  } else {
150  $query = $query . ' AND ' . $v;
151  }
152  $i++;
153  }
154  }
155  }
156  // Order by
157  if (($this->order_by != "") && (!$onlycont)) {
158  $query = $query . " order by " . $this->order_by;
159  if (isset($this->desc) && ($this->desc == "up")) {
160  $query = $query . " desc";
161  }
162  }
163  if ($slice > 0) $query.= " limit $slice";
164  if ($start > 0) $query.= " offset $start";
165  $query.= ';';
166  } else {
167  $query = $p_query;
168  }
169 
170  $this->slice = $slice;
171  $this->start = $start;
172 
173  $this->LastQuery = $query;
174  return $query;
175  }
176  /**
177  * test CORE_QUERYPREPARE to know if can use prepare statement in queryDb
178  * @static
179  * @return bool
180  */
181  static public function usePrepare()
182  {
183 
184  static $prepare = null;
185  if ($prepare === null) {
186  include_once ('FDL/freedom_util.php');
187  $err = simpleQuery(getDbAccess() , "select val from paramv where name='CORE_QUERYPREPARE' and type='G';", $sPrepare, true, true);
188  if ($err != "") {
189  $prepare = false;
190  } else {
191  $prepare = ($sPrepare == "yes");
192  }
193  }
194  return $prepare;
195  }
196  /**
197  * Perform the query : the result can be a table or a list of objects
198  * depending on the third arg.
199  * the third ARG should be :
200  * LIST : means a table of objects
201  * LISTC : means a table of completed objects
202  * TABLE : means a table of table fields
203  * ITEM : means a ressource to step by step use table field rows
204  * ITER : return class DbObjectList
205  */
206  function Query($start = 0, $slice = 0, $res_type = "LIST", $p_query = "")
207  {
208 
209  $query = $this->initQuery($start, $slice, $p_query);
210  $this->res_type = $res_type;
211 
212  $err = $this->basic_elem->exec_query($query, 0, $this->usePrepare());
213  // print "$query $res_type $p_query<BR>\n";
214  if ($res_type == "ITER") {
215  if ($err) {
216  throw new Exception("query fail : " . $err);
217  }
218  include_once ("Class.DbObjectList.php");
219  return new DbObjectList($this->dbaccess, $this->basic_elem->res, $this->class);
220  }
221 
222  if ($err != "") return ($err);
223 
224  $this->nb = $this->basic_elem->numrows();
225 
226  if ($this->nb == 0) {
227  return FALSE;
228  }
229  if ($res_type == "ITEM") {
230  $this->cindex = 0; // current index row
231  return $this->basic_elem->res;
232  }
233  if ($start >= $this->nb) {
234  $start = 0;
235  }
236  if ($slice == 0) {
237  $slice = $this->nb;
238  }
239 
240  if (($start + $slice) >= $this->nb) {
241  $end = $this->nb;
242  } else {
243  $end = $start + $slice;
244  }
245  if ($res_type == "TABLE") {
246  $this->list = pg_fetch_all($this->basic_elem->res);
247  } else {
248  for ($c = 0; $c < $this->nb; $c++) {
249  $result = $this->basic_elem->fetch_array($c);
250  if (($res_type == "LIST") || ($res_type == "LISTC")) {
251  $this->list[$c] = new $this->class($this->dbaccess, "", $result, $this->basic_elem->dbid);
252  } else {
253  while (list($k, $v) = each($result)) {
254  $this->list[$c][$k] = $v;
255  }
256  }
257  }
258  }
259  return ($this->list);
260  }
261  /**
262  * Perform the query : return only the count fo rows returned
263  */
264  function Count($start = 0, $slice = 0)
265  {
266 
267  $query = $this->initQuery($start, $slice, "", true);
268  $this->res_type = "TABLE";
269  $err = $this->basic_elem->exec_query($query);
270  // print "$query $res_type $p_query<BR>\n";
271  if ($err != "") return ($err);
272 
273  $result = $this->basic_elem->fetch_array(0);
274  return ($result["count"]);
275  }
276 
277  function CriteriaClause()
278  {
279  $out = "";
280  if (isset($this->criteria) && ($this->criteria != "") && ($this->operator != "none")) {
281  if ($this->casse == "NON") {
282  $out = $out . " upper(" . $this->criteria . ") " . $this->operators[$this->operator]["oper"];
283  } else {
284  $out = $out . $this->criteria . " " . $this->operators[$this->operator]["oper"];
285  }
286  $string = "";
287  switch ($this->operators[$this->operator]["param"]) {
288  case "NORMAL":
289  $string = " {$this->string}";
290  break;
291 
292  case "PERCENT":
293  $string = " '%{$this->string}%'";
294  break;
295 
296  case "SEMIPERCENT":
297  $string = " '{$this->string}%'";
298  }
299  if (($this->operator != 'null') && ($this->operator != 'notn')) {
300  if ($this->casse == "NON") {
301  $out.= " upper({$string})";
302  } else {
303  $out.= $string;
304  }
305  }
306  }
307  return ($out);
308  }
309 
310  function AlphaClause()
311  {
312  }
313 
314  function SupClause()
315  {
316  $out = "";
317  if (sizeof($this->basic_elem->sup_where) > 0) {
318  reset($this->basic_elem->sup_where);
319  $count = 0;
320  while (list($k, $v) = each($this->basic_elem->sup_where)) {
321  if ($count > 0) {
322  $out = $out . " AND (" . $v . ")";
323  } else {
324  $out = "(" . $out . " " . $v . ")";
325  }
326  $count++;
327  }
328  }
329  return ($out);
330  }
331 
332  function AddQuery($contraint)
333  {
334  $this->basic_elem->sup_where[] = $contraint;
335  }
336  function resetQuery()
337  {
338  $this->basic_elem->sup_where = array();
339  unset($this->list);
340  }
341  function AddField($sqlattr, $resultname = "")
342  {
343  if ($resultname == "") $this->basic_elem->sup_fields[] = $sqlattr;
344  else $this->basic_elem->sup_fields[] = "$sqlattr as $resultname";
345  }
346 }
347 ?>
← centre documentaire © anakeen - published under CC License - Dynacase