KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
db_base.php
Ir a la documentación de este archivo.
1 <?php
35 class DbBase
36 {
37 
43  public $debug = false;
49  public $logger = false;
55  protected $last_query;
56 
66  public function find($table, $where="1=1", $fields="*", $orderBy="1")
67  {
71  $q = $this->query("SELECT $fields FROM $table WHERE $where ORDER BY $orderBy");
72  $results = array();
73  while ($row = $this->fetch_array($q)) {
74  $results[] = $row;
75  }
76  return $results;
77  }
78 
86  public function in_query($sql)
87  {
88  $q = $this->query($sql);
89  $results = array();
90  if ($q) {
91  while ($row = $this->fetch_array($q)) {
92  $results[] = $row;
93  }
94  }
95  return $results;
96  }
97 
105  public function fetch_all($sql)
106  {
107  return $this->in_query($sql);
108  }
109 
117  public function in_query_assoc($sql)
118  {
119  $q = $this->query($sql);
120  $results = array();
121  if ($q) {
122  while ($row = $this->fetch_array($q, db::DB_ASSOC)) {
123  $results[] = $row;
124  }
125  }
126  return $results;
127  }
128 
136  public function in_query_num($sql)
137  {
138  $q = $this->query($sql);
139  $results = array();
140  if ($q) {
141  while ($row = $this->fetch_array($q, db::DB_NUM)) {
142  $results[] = $row;
143  }
144  }
145  return $results;
146  }
147 
154  public function fetch_one($sql)
155  {
156  $q = $this->query($sql);
157  if ($q) {
158  if ($this->num_rows($q) > 1) {
159  Flash::warning("Una sentencia SQL: \"$sql\" retorno mas de una fila cuando se esperaba una sola");
160  }
161  return $this->fetch_array($q);
162  } else {
163  return array();
164  }
165  }
166 
175  public function insert($table, $values, $fields=null)
176  {
177  //$insert_sql = "";
178  if (is_array($values)) {
179  if (!count($values)) {
180  new KumbiaException("Imposible realizar inserci&oacute;n en $table sin datos");
181  }
182  if (is_array($fields)) {
183  $insert_sql = "INSERT INTO $table (" . join(",", $fields) . ") VALUES (" . join(",", $values) . ")";
184  } else {
185  $insert_sql = "INSERT INTO $table VALUES (" . join(",", $values) . ")";
186  }
187  return $this->query($insert_sql);
188  } else {
189  throw new KumbiaException('El segundo parametro para insert no es un Array');
190  }
191  }
192 
202  public function update($table, $fields, $values, $where_condition=null)
203  {
204  $update_sql = "UPDATE $table SET ";
205  if (count($fields) != count($values)) {
206  throw new KumbiaException('Los n&uacute;mero de valores a actualizar no es el mismo de los campos');
207  }
208  $i = 0;
209  $update_values = array();
210  foreach ($fields as $field) {
211  $update_values[] = $field . ' = ' . $values[$i];
212  $i++;
213  }
214  $update_sql.= join(',', $update_values);
215  if ($where_condition != null) {
216  $update_sql.= " WHERE $where_condition";
217  }
218  return $this->query($update_sql);
219  }
220 
227  public function delete($table, $where_condition)
228  {
229  if (trim($where_condition)) {
230  return $this->query("DELETE FROM $table WHERE $where_condition");
231  } else {
232  return $this->query("DELETE FROM $table");
233  }
234  }
235 
240  public function begin()
241  {
242  return $this->query('BEGIN');
243  }
244 
249  public function rollback()
250  {
251  return $this->query('ROLLBACK');
252  }
253 
258  public function commit()
259  {
260  return $this->query('COMMIT');
261  }
262 
268  static public function add_quotes($value)
269  {
270  return "'" . addslashes($value) . "'";
271  }
272 
279  protected function log($msg, $type)
280  {
281  if ($this->logger) {
282  Logger::log($this->logger, $msg, $type);
283  }
284  }
285 
291  protected function debug($sql)
292  {
293  if ($this->debug) {
294  Flash::info($sql);
295  }
296  }
297 
304  public function query($sql)
305  {
306  }
307 
315  public function fetch_array($resultQuery = NULL, $opt = '')
316  {
317  }
318 }