KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
sqlite.php
Ir a la documentación de este archivo.
1 <?php
29 class DbSQLite extends DbBase implements DbBaseInterface
30 {
31 
49  protected $last_query;
55  public $last_error;
56 
61  const DB_ASSOC = SQLITE_ASSOC;
62 
67  const DB_BOTH = SQLITE_BOTH;
68 
73  const DB_NUM = SQLITE_NUM;
74 
75 
80  const TYPE_INTEGER = 'INTEGER';
81 
86  const TYPE_DATE = 'DATE';
87 
92  const TYPE_VARCHAR = 'VARCHAR';
93 
98  const TYPE_DECIMAL = 'DECIMAL';
99 
104  const TYPE_DATETIME = 'DATETIME';
105 
110  const TYPE_CHAR = 'CHAR';
111 
118  public function connect($config)
119  {
120 
121  if (!extension_loaded('sqlite')) {
122  throw new KumbiaException('Debe cargar la extensión de PHP llamada sqlite');
123  }
124  if ($this->id_connection = sqlite_open(APP_PATH . 'config/sql/' . $config['name'])) {
125  return true;
126  } else {
127  throw new KumbiaException($this->error('No se puede conectar a la base de datos'));
128  }
129  }
130 
137  function query($sqlQuery)
138  {
139  $this->debug($sqlQuery);
140  if ($this->logger) {
141  Logger::debug($sqlQuery);
142  }
143 
144  $this->last_query = $sqlQuery;
145  if ($resultQuery = @sqlite_query($this->id_connection, $sqlQuery)) {
146  $this->last_result_query = $resultQuery;
147  return $resultQuery;
148  } else {
149  throw new KumbiaException($this->error(" al ejecutar <em>'$sqlQuery'</em>"));
150  }
151  }
152 
156  function close()
157  {
158  if ($this->id_connection) {
159  return sqlite_close($this->id_connection);
160  } else {
161  return false;
162  }
163  }
164 
172  function fetch_array($resultQuery='', $opt=SQLITE_BOTH)
173  {
174 
175  if (!$resultQuery) {
176  $resultQuery = $this->last_result_query;
177  if (!$resultQuery) {
178  return false;
179  }
180  }
181 
182  return sqlite_fetch_array($resultQuery, $opt);
183  }
184 
191  {
192  $this->connect($config);
193  }
194 
198  function num_rows($resultQuery='')
199  {
200  if (!$resultQuery) {
201  $resultQuery = $this->last_result_query;
202  if (!$resultQuery) {
203  return false;
204  }
205  }
206  if (($numberRows = sqlite_num_rows($resultQuery)) !== false) {
207  return $numberRows;
208  } else {
209  throw new KumbiaException($this->error());
210  }
211  }
212 
220  function field_name($number, $resultQuery='')
221  {
222 
223  if (!$resultQuery) {
224  $resultQuery = $this->last_result_query;
225  if (!$resultQuery) {
226  return false;
227  }
228  }
229  if (($fieldName = sqlite_field_name($resultQuery, $number)) !== false) {
230  return $fieldName;
231  } else {
232  throw new KumbiaException($this->error());
233  }
234  }
235 
243  function data_seek($number, $resultQuery='')
244  {
245  if (!$resultQuery) {
246  $resultQuery = $this->last_result_query;
247  if (!$resultQuery) {
248  return false;
249  }
250  }
251  if (($success = sqlite_rewind($resultQuery, $number)) !== false) {
252  return $success;
253  } else {
254  throw new KumbiaException($this->error());
255  }
256  }
257 
264  function affected_rows($resultQuery='')
265  {
266 
267  if (!$resultQuery) {
268  $resultQuery = $this->last_result_query;
269  if (!$resultQuery) {
270  return false;
271  }
272  }
273  if (($numberRows = pg_affected_rows($resultQuery)) !== false) {
274  return $numberRows;
275  } else {
276  throw new KumbiaException($this->error());
277  }
278  }
279 
285  function error($err='')
286  {
287  if (!$this->id_connection) {
288  $this->last_error = sqlite_last_error($this->id_connection) ? sqlite_last_error($this->id_connection) . $err : "[Error Desconocido en SQLite \"$err\"]";
289  if ($this->logger) {
290  Logger::error($this->last_error);
291  }
292  return $this->last_error;
293  }
294  $this->last_error = 'SQLite error: ' . sqlite_error_string(sqlite_last_error($this->id_connection));
295  $this->last_error.= $err;
296  if ($this->logger) {
297  Logger::error($this->last_error);
298  }
299  return $this->last_error;
300  }
301 
307  function no_error()
308  {
309  return 0; //Codigo de Error?
310  }
311 
317  public function last_insert_id($table='', $primary_key='')
318  {
319  $last_id = $this->fetch_one("SELECT COUNT(*) FROM $table");
320  return $last_id[0];
321  }
322 
329  function table_exists($table, $schema='')
330  {
331  $table = addslashes(strtolower($table));
332  if (strpos($table, ".")) {
333  list($schema, $table) = explode(".", $table);
334  }
335  $num = $this->fetch_one("SELECT COUNT(*) FROM sqlite_master WHERE name = '$table'");
336  return $num[0];
337  }
338 
345  public function limit($sql)
346  {
347  $params = Util::getParams(func_get_args());
348  $sql_new = $sql;
349 
350  if (isset($params['limit']) && is_numeric($params['limit'])) {
351  $sql_new.=" LIMIT $params[limit]";
352  }
353 
354  if (isset($params['offset']) && is_numeric($params['offset'])) {
355  $sql_new.=" OFFSET $params[offset]";
356  }
357 
358  return $sql_new;
359  }
360 
367  public function drop_table($table, $if_exists=true)
368  {
369  if ($if_exists) {
370  if ($this->table_exists($table)) {
371  return $this->query("DROP TABLE $table");
372  } else {
373  return true;
374  }
375  } else {
376  return $this->query("DROP TABLE $table");
377  }
378  }
379 
387  public function create_table($table, $definition, $index=array())
388  {
389 
390  }
391 
397  public function list_tables()
398  {
399  return $this->fetch_all("SELECT name FROM sqlite_master WHERE type='table' " .
400  "UNION ALL SELECT name FROM sqlite_temp_master " .
401  "WHERE type='table' ORDER BY name");
402  }
403 
410  public function describe_table($table, $schema='')
411  {
412  $fields = array();
413  $results = $this->fetch_all("PRAGMA table_info($table)");
414  //var_dump($results); die();
415  foreach ($results as $field) {
416  $fields[] = array(
417  "Field" => $field["name"],
418  "Type" => $field["type"],
419  "Null" => $field["notnull"] == '0' ? "YES" : "NO",
420  "Key" => $field['pk'] == 1 ? "PRI" : ""
421  );
422  }
423  return $fields;
424  }
425 
431  public function last_sql_query()
432  {
433  return $this->last_query;
434  }
435 
436 }