1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* KumbiaPHP web & app Framework |
4
|
|
|
* |
5
|
|
|
* LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the new BSD license that is bundled |
8
|
|
|
* with this package in the file LICENSE.txt. |
9
|
|
|
* It is also available through the world-wide-web at this URL: |
10
|
|
|
* http://wiki.kumbiaphp.com/Licencia |
11
|
|
|
* If you did not receive a copy of the license and are unable to |
12
|
|
|
* obtain it through the world-wide-web, please send an email |
13
|
|
|
* to [email protected] so we can send you a copy immediately. |
14
|
|
|
* |
15
|
|
|
* @category Kumbia |
16
|
|
|
* @package Db |
17
|
|
|
* @subpackage Adapters |
18
|
|
|
* @copyright Copyright (c) 2005 - 2017 Kumbia Team (http://www.kumbiaphp.com) |
19
|
|
|
* @license http://wiki.kumbiaphp.com/Licencia New BSD License |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Firebird/Interbase Database Support |
24
|
|
|
* |
25
|
|
|
* @category Kumbia |
26
|
|
|
* @package Db |
27
|
|
|
* @subpackage Adapters |
28
|
|
|
*/ |
29
|
|
|
class DbFirebird extends DbBase implements DbBaseInterface |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Resource de la Conexion a Firebird |
34
|
|
|
* |
35
|
|
|
* @var resource |
36
|
|
|
*/ |
37
|
|
|
public $id_connection; |
38
|
|
|
/** |
39
|
|
|
* Ultimo Resultado de una Query |
40
|
|
|
* |
41
|
|
|
* @var resource |
42
|
|
|
*/ |
43
|
|
|
public $last_result_query; |
44
|
|
|
/** |
45
|
|
|
* Ultima sentencia SQL enviada a Firebird |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $last_query; |
50
|
|
|
/** |
51
|
|
|
* Ultimo error generado por Firebird |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
public $last_error; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Resultado de Array Asociativo |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
|
|
const DB_ASSOC = MYSQL_ASSOC; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Resultado de Array Asociativo y Numerico |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
|
|
const DB_BOTH = MYSQL_BOTH; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Resultado de Array Numerico |
71
|
|
|
* |
72
|
|
|
*/ |
73
|
|
|
const DB_NUM = MYSQL_NUM; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tipo de Dato Integer |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
|
|
const TYPE_INTEGER = 'INTEGER'; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Tipo de Dato Date |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
|
|
const TYPE_DATE = 'DATE'; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tipo de Dato Varchar |
89
|
|
|
* |
90
|
|
|
*/ |
91
|
|
|
const TYPE_VARCHAR = 'VARCHAR'; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Tipo de Dato Decimal |
95
|
|
|
* |
96
|
|
|
*/ |
97
|
|
|
const TYPE_DECIMAL = 'DECIMAL'; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Tipo de Dato Datetime |
101
|
|
|
* |
102
|
|
|
*/ |
103
|
|
|
const TYPE_DATETIME = 'DATETIME'; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Tipo de Dato Char |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
|
|
const TYPE_CHAR = 'CHAR'; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Hace una conexion a la base de datos de Firebird |
113
|
|
|
* |
114
|
|
|
* @param array $config |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function connect($config) |
118
|
|
|
{ |
119
|
|
|
|
120
|
|
|
if (!extension_loaded('interbase')) { |
121
|
|
|
throw new KumbiaException('Debe cargar la extensión de PHP llamada php_interbase'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (isset($config['host']) && $config['host']) { |
125
|
|
|
$id_con = ibase_connect("{$config['host']}:{$config['name']}", $config['username'], $config['password']); |
126
|
|
|
} else { |
127
|
|
|
$id_con = ibase_connect($config['name'], $config['username'], $config['password']); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($this->id_connection = $id_con) { |
131
|
|
|
return true; |
132
|
|
|
} else { |
133
|
|
|
throw new KumbiaException($this->error()); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Efectua operaciones SQL sobre la base de datos |
139
|
|
|
* |
140
|
|
|
* @param string $sql_query |
141
|
|
|
* @return resource or false |
142
|
|
|
*/ |
143
|
|
|
public function query($sql_query) |
144
|
|
|
{ |
145
|
|
|
$this->debug($sql_query); |
146
|
|
|
if ($this->logger) { |
147
|
|
|
Logger::debug($sql_query); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->last_query = $sql_query; |
151
|
|
|
if ($result_query = ibase_query($this->id_connection, $sql_query)) { |
152
|
|
|
$this->last_result_query = $result_query; |
153
|
|
|
return $result_query; |
154
|
|
|
} else { |
155
|
|
|
throw new KumbiaException($this->error(" al ejecutar <em>\"$sql_query\"</em>")); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Cierra la Conexión al Motor de Base de datos |
161
|
|
|
* |
162
|
|
|
*/ |
163
|
|
|
public function close() |
164
|
|
|
{ |
165
|
|
|
if ($this->id_connection) { |
166
|
|
|
return ibase_close(); |
167
|
|
|
} |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Devuelve fila por fila el contenido de un select |
173
|
|
|
* |
174
|
|
|
* @param resource $result_query |
175
|
|
|
* @param int $opt |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function fetch_array($result_query=NULL, $opt=MYSQL_BOTH) |
179
|
|
|
{ |
180
|
|
|
$result=array(); |
181
|
|
|
if (!$result_query) { |
182
|
|
|
$result_query = $this->last_result_query; |
183
|
|
|
if (!$result_query) { |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
if ($opt == db::DB_BOTH) { |
188
|
|
|
$fetch = ibase_fetch_assoc($result_query); |
189
|
|
|
$result = array(); |
190
|
|
|
$i = 0; |
191
|
|
|
foreach ($fetch as $key => $value) { |
192
|
|
|
$result[$key] = $value; |
193
|
|
|
$result[$i++] = $value; |
194
|
|
|
} |
195
|
|
|
return $result; |
196
|
|
|
} |
197
|
|
|
if ($opt == db::DB_ASSOC) { |
198
|
|
|
return ibase_fetch_assoc($result_query); |
199
|
|
|
} |
200
|
|
|
if ($opt == db::DB_NUM) { |
201
|
|
|
return ibase_fetch_row($result_query); |
202
|
|
|
} |
203
|
|
|
return $result; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Constructor de la Clase |
208
|
|
|
* |
209
|
|
|
* @param array $config |
210
|
|
|
*/ |
211
|
|
|
public function __construct($config) |
212
|
|
|
{ |
213
|
|
|
$this->connect($config); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Devuelve el numero de filas de un select |
218
|
|
|
*/ |
219
|
|
|
public function num_rows($result_query='') |
220
|
|
|
{ |
221
|
|
|
// GDS Interbase no soporta esta funcion (No debe ser usada) |
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Devuelve el nombre de un campo en el resultado de un select |
227
|
|
|
* |
228
|
|
|
* @param int $number |
229
|
|
|
* @param resource $result_query |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function field_name($number, $result_query='') |
233
|
|
|
{ |
234
|
|
|
|
235
|
|
|
if (!$result_query) { |
236
|
|
|
$result_query = $this->last_result_query; |
237
|
|
|
if (!$result_query) { |
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
if (($fieldName = ibase_field_name($result_query, $number)) !== false) { |
242
|
|
|
return $fieldName; |
243
|
|
|
} else { |
244
|
|
|
throw new KumbiaException($this->error()); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Se Mueve al resultado indicado por $number en un select |
250
|
|
|
* |
251
|
|
|
* @param int $number |
252
|
|
|
* @param resource $result_query |
253
|
|
|
* @return boolean |
254
|
|
|
*/ |
255
|
|
|
public function data_seek($number, $result_query=NULL) |
256
|
|
|
{ |
257
|
|
|
if (!$result_query) { |
258
|
|
|
$result_query = $this->last_result_query; |
259
|
|
|
if (!$result_query) { |
260
|
|
|
return false; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
if (($success = ibase_data_seek($result_query, $number)) !== false) { |
264
|
|
|
return $success; |
265
|
|
|
} else { |
266
|
|
|
throw new KumbiaException($this->error()); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Numero de Filas afectadas en un insert, update o delete |
272
|
|
|
* |
273
|
|
|
* @param resource $result_query |
274
|
|
|
* @return int |
275
|
|
|
*/ |
276
|
|
|
public function affected_rows($result_query=NULL) |
277
|
|
|
{ |
278
|
|
|
if (($numberRows = ibase_affected_rows()) !== false) { |
279
|
|
|
return $numberRows; |
280
|
|
|
} else { |
281
|
|
|
throw new KumbiaException($this->error()); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Devuelve el error de Firebird |
287
|
|
|
* |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
|
public function error($err='') |
291
|
|
|
{ |
292
|
|
|
if (!$this->id_connection) { |
293
|
|
|
$this->last_error = ibase_errmsg() ? ibase_errmsg() : "[Error Desconocido en Firebird: $err]"; |
294
|
|
|
if ($this->logger) { |
295
|
|
|
Logger::error($this->last_error); |
296
|
|
|
} |
297
|
|
|
return $this->last_error; |
298
|
|
|
} |
299
|
|
|
$this->last_error = ibase_errmsg() ? ibase_errmsg() : "[Error Desconocido en Firebird: $err]"; |
300
|
|
|
$this->last_error.= $err; |
301
|
|
|
if ($this->logger) { |
302
|
|
|
Logger::error($this->last_error); |
303
|
|
|
} |
304
|
|
|
return $this->last_error; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Devuelve un array del resultado de un select de un solo registro. Esta implementacion no |
309
|
|
|
* |
310
|
|
|
* |
311
|
|
|
* @param string $sql |
312
|
|
|
* @return array |
313
|
|
|
*/ |
314
|
|
|
public function fetch_one($sql) |
315
|
|
|
{ |
316
|
|
|
$q = $this->query($sql); |
317
|
|
|
if ($q) { |
318
|
|
|
return $this->fetch_array($q); |
319
|
|
|
} else { |
320
|
|
|
return array(); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Devuelve el no error de Firebird |
326
|
|
|
* |
327
|
|
|
* @return int |
328
|
|
|
*/ |
329
|
|
|
public function no_error() |
330
|
|
|
{ |
331
|
|
|
return ibase_errcode(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Devuelve el ultimo id autonumerico generado en la BD |
336
|
|
|
* |
337
|
|
|
* @return int |
338
|
|
|
*/ |
339
|
|
|
public function last_insert_id($table='', $primary_key='') |
340
|
|
|
{ |
341
|
|
|
return ibase_insert_id($this->id_connection); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Verifica si una tabla existe o no |
346
|
|
|
* |
347
|
|
|
* @param string $table |
348
|
|
|
* @return boolean |
349
|
|
|
*/ |
350
|
|
|
public function table_exists($table, $schema='') |
351
|
|
|
{ |
352
|
|
|
$table = strtoupper(addslashes("$table")); |
353
|
|
|
// NOT LIKE 'RDB\$%' |
354
|
|
|
$num = $this->fetch_one("SELECT COUNT(*) FROM rdb\$relations WHERE rdb\$relation_name = '$table'"); |
355
|
|
|
return $num[0]; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Devuelve un LIMIT valido para un SELECT del RBDM |
360
|
|
|
* |
361
|
|
|
* @param string $sql consulta sql |
362
|
|
|
* @return string |
363
|
|
|
*/ |
364
|
|
|
public function limit($sql) |
365
|
|
|
{ |
366
|
|
|
$params = Util::getParams(func_get_args()); |
367
|
|
|
$sql_new = $sql; |
368
|
|
|
|
369
|
|
|
if (isset($params['limit']) && is_numeric($params['limit'])) { |
370
|
|
|
$sql_new.=" FIRST $params[limit]"; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if (isset($params['offset']) && is_numeric($params['offset'])) { |
374
|
|
|
$sql_new.=" SKIP $params[offset]"; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return $sql_new; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Borra una tabla de la base de datos |
382
|
|
|
* |
383
|
|
|
* @param string $table |
384
|
|
|
* @return boolean |
385
|
|
|
*/ |
386
|
|
|
public function drop_table($table, $if_exists=true) |
387
|
|
|
{ |
388
|
|
|
if ($if_exists) { |
389
|
|
|
if ($this->table_exists($table)) { |
390
|
|
|
return $this->query("DROP TABLE $table"); |
391
|
|
|
} else { |
392
|
|
|
return true; |
393
|
|
|
} |
394
|
|
|
} else { |
395
|
|
|
return $this->query("DROP TABLE $table"); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Crea una tabla utilizando SQL nativo del RDBM |
401
|
|
|
* |
402
|
|
|
* TODO: |
403
|
|
|
* - Falta que el parametro index funcione. Este debe listar indices compuestos multipes y unicos |
404
|
|
|
* - Agregar el tipo de tabla que debe usarse (Firebird) |
405
|
|
|
* - Soporte para campos autonumericos |
406
|
|
|
* - Soporte para llaves foraneas |
407
|
|
|
* |
408
|
|
|
* @param string $table |
409
|
|
|
* @param array $definition |
410
|
|
|
* @return resource |
411
|
|
|
*/ |
412
|
|
|
public function create_table($table, $definition, $index=array()) |
413
|
|
|
{ |
414
|
|
|
$create_sql = "CREATE TABLE $table ("; |
415
|
|
|
if (!is_array($definition)) { |
416
|
|
|
throw new KumbiaException("Definición invalida para crear la tabla '$table'"); |
417
|
|
|
} |
418
|
|
|
$create_lines = array(); |
419
|
|
|
$index = array(); |
420
|
|
|
$unique_index = array(); |
421
|
|
|
$primary = array(); |
422
|
|
|
//$not_null = ""; |
423
|
|
|
//$size = ""; |
424
|
|
|
foreach ($definition as $field => $field_def) { |
425
|
|
|
if (isset($field_def['not_null'])) { |
426
|
|
|
$not_null = $field_def['not_null'] ? 'NOT NULL' : ''; |
427
|
|
|
} else { |
428
|
|
|
$not_null = ""; |
429
|
|
|
} |
430
|
|
|
if (isset($field_def['size'])) { |
431
|
|
|
$size = $field_def['size'] ? '(' . $field_def['size'] . ')' : ''; |
432
|
|
|
} else { |
433
|
|
|
$size = ""; |
434
|
|
|
} |
435
|
|
|
if (isset($field_def['index'])) { |
436
|
|
|
if ($field_def['index']) { |
437
|
|
|
$index[] = "INDEX($field)"; |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
if (isset($field_def['unique_index'])) { |
441
|
|
|
if ($field_def['unique_index']) { |
442
|
|
|
$index[] = "UNIQUE($field)"; |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
if (isset($field_def['primary'])) { |
446
|
|
|
if ($field_def['primary']) { |
447
|
|
|
$primary[] = "$field"; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
if (isset($field_def['auto'])) { |
451
|
|
|
if ($field_def['auto']) { |
452
|
|
|
$gen = $this->fetch_one("SELECT COUNT(*) FROM RDB\$GENERATORS WHERE RDB\$GENERATOR_NAME = UPPER('{$table}_{$field}_seq')"); |
453
|
|
|
if (!$gen[0]) { |
454
|
|
|
$this->query("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('{$table}_{$field}_seq'))"); |
455
|
|
|
} |
456
|
|
|
$this->query("SET GENERATOR {$table}_{$field}_seq TO 1;"); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
if (isset($field_def['extra'])) { |
460
|
|
|
$extra = $field_def['extra']; |
461
|
|
|
} else { |
462
|
|
|
$extra = ""; |
463
|
|
|
} |
464
|
|
|
$create_lines[] = "$field " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra; |
465
|
|
|
} |
466
|
|
|
$create_sql.= join(',', $create_lines); |
467
|
|
|
$last_lines = array(); |
468
|
|
|
if (count($primary)) { |
469
|
|
|
$last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')'; |
470
|
|
|
} |
471
|
|
|
if (count($index)) { |
472
|
|
|
$last_lines[] = join(',', $index); |
473
|
|
|
} |
474
|
|
|
if (count($unique_index)) { |
475
|
|
|
$last_lines[] = join(',', $unique_index); |
476
|
|
|
} |
477
|
|
|
if (count($last_lines)) { |
478
|
|
|
$create_sql.= ',' . join(',', $last_lines) . ')'; |
479
|
|
|
} |
480
|
|
|
return $this->query($create_sql); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Listar las tablas en la base de datos |
485
|
|
|
* |
486
|
|
|
* @return array |
487
|
|
|
*/ |
488
|
|
|
public function list_tables() |
489
|
|
|
{ |
490
|
|
|
return $this->fetch_all("SHOW TABLES"); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Listar los campos de una tabla |
495
|
|
|
* |
496
|
|
|
* @param string $table |
497
|
|
|
* @return array |
498
|
|
|
*/ |
499
|
|
|
public function describe_table($table, $schema='') |
500
|
|
|
{ |
501
|
|
|
if ($schema == '') { |
502
|
|
|
return $this->fetch_all("DESCRIBE $table"); |
503
|
|
|
} else { |
504
|
|
|
return $this->fetch_all("DESCRIBE $schema.$table"); |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Devuelve la ultima sentencia sql ejecutada por el Adaptador |
510
|
|
|
* |
511
|
|
|
* @return string |
512
|
|
|
*/ |
513
|
|
|
public function last_sql_query() |
514
|
|
|
{ |
515
|
|
|
return $this->last_query; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
} |
519
|
|
|
|