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