-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbpdo.inc.php
executable file
·1609 lines (1345 loc) · 45.1 KB
/
dbpdo.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file is part of the RPCL project
*
* Copyright (c) 2012 Embarcadero Technologies, Inc.
*
* Checkout AUTHORS file for more information on the developers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
/**
* DBMS-independent wrapper to handle a PDO instance.
*
* This class is inherited by DBMS-specific implementations. The factory() static method will provide an instance of the
* right child class for the given Database object.
*
* @see Database
*/
class DBPDO
{
/**
* Actual PDO instance wrapped by this class.
*
* @var PDO
*/
public $pdo;
protected $database;
protected $params;
protected $driveroptions;
protected $data_cache;
/**
* Returns an instance of a DBPDO child class for the DBMS of the target database, or and instance of DBPDO if the
* DBMS is not supported.
*
* @param Database $database
*
* @return DBPDO
*/
public static function factory($database)
{
switch($database->Drivername)
{
//case 'cubrid': return new DBCubrid($database);
//case 'dblib': return new DBDblib($database);
case 'interbase': return new DBInterbase($database);
case 'firebird': return new DBFirebird($database);
case 'ibm': return new DBIbm($database);
case 'informix': return new DBInformix($database);
case 'mysql': return new DBMysql($database);
case 'oci': return new DBOci($database);
//case 'odbc': return new DBOdbc($database);
case 'pgsql': return new DBPgsql($database);
case 'sqlite': return new DBSqlLite($database);
case 'sqlsrv': return new DBSqlSrv($database);
//case '4d': return new DB4d($database);
case '': return new DBMysql($database);
default: return new DBPDO($database);
}
}
/**
* Sets the value of an attribute on the PDO instance.
*
* @link http://www.php.net/manual/en/pdo.setattribute.php
*/
function setAttribute($key, $value)
{
$this->pdo->setAttribute($key, $value);
}
/**
* Gets the value of an attribute on the PDO instance.
*
* @link http://www.php.net/manual/en/pdo.getattribute.php
*/
function getAttribute($key)
{
return $this->pdo->getAttribute($key);
}
/**
* Class constructor.
*
* @param Database $database Database object for which the connection will be established. Database->DriverName
* must define the DBMS to be used.
*/
function __construct($database)
{
$this->database = $database;
}
/**
* Returns the name of the database driver, to be used as prefix for the PDO Data Source Name.
*
* @return string
*/
function DSNDriver()
{
return $this->database->Drivername;
}
/**
* Returns the parameters to be used for the PDO Data Source Name.
*
* @return string
*/
function DSNParams()
{
$params = array();
$dsnparams = array();
// base params
if($this->database->Host != '')
{
if($this->database->HostTranslation && strtolower($this->database->Host) == 'localhost')
{
$params['host'] = '127.0.0.1';
}
else
{
$params['host'] = $this->database->Host;
}
}
if($this->database->Databasename != '')
{
$params['dbname'] = $this->database->Databasename;
}
if($this->database->Port != '')
{
$params['port'] = $this->database->Port;
}
if($this->database->Charset != '')
{
$params['charset'] = $this->database->Charset;
}
// custom extra params
if(is_array($this->database->ConnectionParams))
{
$params = array_merge($params, $this->database->ConnectionParams);
}
foreach($params as $key => $value)
{
$dsnparams[] = "{$key}={$value}";
}
return implode(';', $dsnparams);
}
/**
* Returns the PDO Data Source Name to be used to establish the connection.
*
* @return string
*/
function DSN()
{
return $this->DSNDriver() . ':' . $this->DSNParams();
}
/**
* Runs the given query on the database.
*
* You can optionally provide an array of parameters to be used in the query. Such parameters will replace
* the place holders that you defined in the query string using the param() method.
*
* <code>
* $statement = $dbpdo->execute("SELECT something FROM somewhere WHERE somethingElse = ".$dbpdo->param("number"), array(6));
* </code>
*
* @param string $query SQL query.
* @param array $params Parameters to be used in the SQL query.
*
* @return PDOStatement
*/
function execute($query, $params = array())
{
if(!$this->pdo)
{
DatabaseError(_("Error, PDO object is null, connect before execute "));
}
// prepare first
$stmt = $this->pdo->prepare($query);
if(!$stmt)
{
$output = '';
foreach($this->pdo->errorInfo() as $error) $output .= ', '. $error;
DatabaseError(_("Error preparing PDOStatement " . $output));
}
// bind parameters
for($i = 0; $i < count($params); $i++)
{
$param = $params[$i];
$paramNum = $i+1;
// Binding parameters as array we can bind data type, param type and length
if(is_array($param))
{
$arguments = count($param);
switch($arguments)
{
case 1: $stmt->bindParam($paramNum, $param[0]);
break;
case 2: $stmt->bindParam($paramNum, $param[0], $param[1]);
break;
case 3:
default: $stmt->bindParam($paramNum, $param[0], $param[1], $param[2]);
break;
}
}
else
{
// bind parameter as string
$stmt->bindValue($paramNum, $param);
}
}
// execute the statement
$result = $stmt->execute();
if (!$result)
{
$output = '';
foreach($stmt->errorInfo() as $error) $output .= ', '. $error;
DatabaseError(_("Error executing PDOStatement " . $output));
}
return $stmt;
}
/**
* Inserts a new record in a table.
*
* @param string $table The name of the table to insert data into.
* @param array $data Associative array of field-value pairs with the data to insert.
* @param string $autoincrementField Optional parameter to define the table auto increment field.
*/
function insert($table, $data, $autoincrementField = '')
{
// if has an autoincrement value remove it from the values
if ( ! empty($autoincrementField) && array_key_exists($autoincrementField,$data))
unset($data[$autoincrementField]);
$dataCount = count($data);
if ( ! $dataCount)
DatabaseError(_("data to insert can not be empty"));
$inputs = implode(',', array_fill(0, $dataCount, '?'));
$fields = implode(',', array_keys($data));
$values = array_values($data);
$sql = "insert into $table ($fields) values ($inputs)";
$this->execute($sql, $values);
}
/**
* Updates some data of a record or set of records in a table.
*
* @param string $table The name of the table to update the record from.
* @param array $keys Associative array of field-value pairs with the data that determines which records will be affected by the operation.
* @param array $data Associative array of field-value pairs with the data to set of the affected records.
*/
function update($table, $data, $keys)
{
$dataCount = count($data);
$keysCount = count($keys);
if ( ! $dataCount)
DatabaseError(_("data to update can not be empty"));
if ( ! $keysCount)
DatabaseError(_("keys can not be empty"));
// generating the set values from $data
foreach ($data as $field => $value)
{
$setPairs[] = "$field=?";
}
// generating the where values from $keys
foreach ($keys as $field => $value)
{
$keyPairs[] = "$field=?";
}
// generating the set and where clause
$set = 'set ' . implode(', ', $setPairs);
$where = 'where ' . implode(', ', $keyPairs);
// generating the values to bind
$values = array_merge(array_values($data), array_values($keys));
// the query to update
$sql = "update $table $set $where";
$this->execute($sql, $values);
}
/**
* Returns a PDO statement for the given query string that you can bind params to and execute.
*
* @param string $query SQL query for the statement.
* @param array $driveroptions Driver options to be used in the statement.
*
* @return PDOStatement
*/
function prepare($query, $driveroptions = array())
{
return $this->pdo->prepare($query, $driveroptions);
}
/**
* Place holder for a parameter that can be printed in an SQL query string, to be later safely associated to the
* actual parameter value.
*
* @see execute()
*
* @param string $value Actual value the place holder will be later replaced with.
*
* @return string Place holder to be used for the given value.
*/
function param($value)
{
return "?";
}
/**
* Quotes the target value to be safely used inside a query string as a parameter.
*
* @param mixed $value Value to be quoted for safe inclusion in a query string.
* @param mixed $parameter_type Type of the target value. If the value is not a string, you must provide this
* parameter with the right value. For example: PDO::PARAM_INT. See: http://php.net/manual/en/pdo.constants.php
*
* @return string String with the given value properly quoted.
*/
function quote($value, $parameter_type = PDO::PARAM_STR)
{
return $this->pdo->quote($value, $parameter_type);
}
/**
* Begins a transaction against the data provider.
*
* After calling this method, further operations on the data at the other point of the connection will be held until
* a call to completeTrans(), which will perform those operations altogether.
*
* <code>
* $dbpdo->beginTrans();
* $dbpdo->execute("INSERT INTO somewhere (field) VALUES (".$dbpdo->param("value").")", array("Some value"));
* $dbpdo->execute("INSERT INTO somewhere (field) VALUES (".$dbpdo->param("value").")", array("Another value"));
* $dbpdo->completeTrans(); // The execute() operations are not actually executed until this point.
* </code>
*
* @return bool Returns true on sucecss or false on failure.
*/
function beginTrans()
{
return $this->pdo->beginTransaction();
}
/**
* Finishes a transaction started with beginTrans(), either commiting all the operations on the data provider that
* were requested since the transaction started ($commit = true, default), or rolling them back so none of them are
* actually performed against the data provider ($commit = false).
*
* <code>
* $dbpdo->beginTrans();
* $insertResult1 = $customConnection->execute("INSERT INTO somewhere (field) VALUES (".$dbpdo->param("value").")", array("Some value"));
* $insertResult2 = $customConnection->execute("INSERT INTO somewhere (field) VALUES (".$dbpdo->param("value").")", array("Another value"));
* $commit = $insertResult1 && $insertResult2; // Evaluates to false if any of the operations failed.
* $dbpdo->completeTrans($commit);
* </code>
*
* @param bool $commit Whether the operations in the transaction should be commited (true) or rolled back (false).
*
* @return bool Returns true on sucecss or false on failure.
*/
function completeTrans($commit = true)
{
if($commit)
{
return $this->pdo->commit();
}
else
{
return $this->pdo->rollBack();
}
}
/**
* Establishes the connection to the data provider if it was not established already.
*/
function doConnect()
{
if(!$this->pdo)
{
try
{
// create PDO connection, with (maybe) database specific options (@see PDO constants)
$this->pdo = new PDO(
$this->DSN(),
$this->database->Username,
$this->database->Userpassword,
$this->database->parseOptions($this->database->DatabaseOptions));
// force to set Exception error mode
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
DatabaseError(_("Can't connect, PDO Exception: ") . $e->getMessage());
}
}
}
/**
* Terminates the connection to the data provider if it was established.
*/
function doDisconnect()
{
$this->pdo = null;
}
/**
* Whether or not there is an ongoing transaction.
*
* @return bool
*/
function inTransaction()
{
return PDO::inTransaction();
}
/**
* Returns the identifier of the last inserted row. You can provide the name of a target row to get its identifier
* instead.
*
* @link http://php.net/manual/en/pdo.lastinsertid.php
*
* @param string $name Optional. The name of a row whose identifier will be returned by this method.
*
* @return string
*/
function lastInsertId($name = NULL)
{
return $this->pdo->lastInsertId($name);
}
/**
* Returns the names of the columns in the given table.
*
* @param string $table The name of the target table.
*
* @return array Associative array of key-value pairs where the keys are the column names of the target table, and the
* values are always NULL.
*/
function metaFields($table)
{
throw new Exception('The metaFields() method is not implemented for this driver.');
}
/**
* Returns the names of the columns in the given table that are part of the primary key.
*
* @param string $table The name of the target table.
*
* @return array Associative array of key-value pairs where the keys are the names of the columns that are part of the
* primary key, and the values are always PRIMARY.
*/
function primaryKeys($table)
{
throw new Exception('The primaryKeys() method not implemented for this driver.');
}
/**
* Whether the this DBPDO implementation's statements are scrollable (true) or not (false).
*
* @return bool True if statements are scrollable, false otherwise.
*/
function isEnabledScroll()
{
return isset($this->_databaseoptions[PDO::ATTR_CURSOR])
&& $this->_databaseoptions[PDO::ATTR_CURSOR] == PDO::CURSOR_SCROLL;
}
/**
* Returns the SQL code to apply the given limit to query results.
*
* @param string $sql SQL code to define the limit. Required for DBMS that do not comply with the SQL standard on
* how to define the limit of the results returned by a query. Leave empty otherwise, and use
* the second and third parameters instead.
* @param int $count Maximum limit of results.
* @param int $start Number of records to be skipped from the start (none by default).
*
* @return string SQL code. For example: "LIMIT 0, 20" (20 first results)
*/
function limitSQL($sql, $count, $start = 0)
{
if ($count > -1)
{
if ($start > -1)
$limit = $start . ', ' . $count;
else
$limit = $count;
$limit_sql = 'limit ' . $limit;
}
else
{
$limit_sql = '';
}
return "$sql $limit_sql";
}
/**
* Returns the SQL code to define the sorting method for the query results.
*
* @param string $sql SQL code to define the sorting method. Required for DBMS that do not comply with the SQL
* standard on how to define the sorting method of the results returned by a query. Leave empty
* otherwise, and use the second and third parameters instead.
* @param int $field Name of the field to be used to determine the order of the results. You can provide several
* field names, separated by commas.
* @param int $order SQL keyword to determine the order to be applied regarding the value of the field or fields.
* either ascending (ASC) or descending (DESC).
*
* @return string SQL code. For example: "ORDER BY fieldName DESC" (20 first results)
*/
function orderSQL($sql, $field, $order)
{
return $sql . ((!empty($field)) ? " order by $field $order" : "");
}
/**
* Returns the SQL query to run the target procedure with the given parameters.
*
* @param string $name Name of the procedure.
* @param array $params Parameters to be passed to the procedure.
*
* @return string SQL query to run the procedure.
*/
function procedureSQL($name, $params)
{
throw new Exception('Procedures are not implemented for this driver.');
}
/**
* Executes the given SQL query for a procedure.
*
* @see procedureSQL()
*
* @param string $query SQL query to call a procedure.
* @param array $params Array of parameters to be used on the procedure.
*
* @return bool
*
* @throws DatabaseError Raised when the DBPDO implementation does not support stored procedures.
*/
function executeProc($query, $params = array())
{
return $this->execute($query, $params);
}
/**
* Returns an SQL query string to count the results for the given SQL query.
*
* @param string $sql SQL query whose results are to be counted.
*
* @return string SQL query to get the number of results returned by the given SQL query.
*
* @internal
*/
function countSQL($sql)
{
// TODO: improve this to not be "select *" dependent
return "select count(*) from ($sql) as rpclcountquery";
}
/**
* Provides the number of results returned by the given SQL query.
*
* @param string $sql SQL query whose results are to be counted.
* @param array $params Paramaters to be passed to the query, replacing any place holder. See execute().
*
* @return int Number of results returned by the query.
*/
function count($sql, $params = array())
{
return (int) $this->execute($this->countSQL($sql), $params)->fetchColumn();
}
/**
* Retuns the version code of the DBMS.
*
* @return string Version code.
*/
public function version()
{
if (!isset($this->data_cache['version']))
{
// Not all subdrivers support the getAttribute() method
$this->data_cache['version'] = $this->pdo->getAttribute(PDO::ATTR_SERVER_VERSION);
}
return $this->data_cache['version'];
}
/**
* Returns the given parameters as a string to be passed to a procedure call.
*
* @param array $params Parameters to be converted into a string.
*
* @return string Comma-separated list of parameters. For example: "parameter1, parameter2, parameter3".
*/
protected function _convertParamsToString($params)
{
$inputs = array();
// create the procedure SQL, when a param starts with @ is treated as variable parameter
foreach($params as $param)
{
if(!is_array($param) && substr($param, 0, 1) === '@')
{
$inputs[] = $param;
}
else
{
$inputs[] = '?';
}
}
return implode(',', $inputs);
}
}
/**
* Wrapper to handle a PDO instance using the InterBase driver.
*
* @see Database
*/
class DBInterbase extends DBPDO
{
// Documented in the parent.
function DSNDriver()
{
return 'firebird';
}
// Documented in the parent.
function procedureSQL($name, $params)
{
$inputString = $this->_convertParamsToString($params);
return "select * from $name($inputString)";
}
// Documented in the parent.
function beginTrans()
{
// The driver for Interbase and Firebird DBMS requires that the autocommit PDO attribute is disabled for the
// transaction to start.
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
return parent::beginTrans();
}
// Documented in the parent.
function completeTrans($commit = true)
{
parent::completeTrans($commit);
return $this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
}
// Documented in the parent.
function metaFields($tablename)
{
$sql = 'SELECT RDB$FIELD_NAME AFIELDNAME
FROM RDB$RELATION_FIELDS
WHERE RDB$RELATION_NAME=\''. strtoupper ($tablename). '\'';
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
$indexes = $stmt->fetchAll(PDO::FETCH_ASSOC);
$keys = array();
foreach($indexes as $index)
{
$keys[trim($index['AFIELDNAME'])] = NULL;
}
return $keys;
}
// Documented in the parent.
function primaryKeys($tablename)
{
$sql = 'SELECT S.RDB$FIELD_NAME AFIELDNAME
FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON I.RDB$INDEX_NAME=S.RDB$INDEX_NAME
WHERE I.RDB$RELATION_NAME=\''.$tablename.'\'
ORDER BY I.RDB$INDEX_NAME,S.RDB$FIELD_POSITION';
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
$indexes = $stmt->fetchAll(PDO::FETCH_ASSOC);
// converting indexes to 'field' => 'type'
$keys = array();
foreach($indexes as $index)
{
$keys[trim($index['AFIELDNAME'])] = 'PRIMARY';
}
return $keys;
}
// Documented in the parent.
function limitSQL($sql, $count, $start = 0)
{
if($count > -1) {
// first row to interbase is 1, not 0
$start;
if($start > 0)
{
$from = $start + 1;
$to = $start + $count;
$sql = "$sql ROWS $from TO $to";
}
else
$sql = "$sql ROWS $count ";
}
return $sql;
}
// Documented in the parent.
function countSQL($sql)
{
// TODO: improve this to not be "select *" dependent
return str_ireplace("select *", "select count(*)", $sql);
}
}
/**
* Wrapper to handle a PDO instance using the Firebird driver.
*
* @see Database
*/
class DBFirebird extends DBInterbase
{
// Documented in the parent.
function DSNDriver()
{
return 'firebird';
}
// Documented in the parent.
function limitSQL($sql, $count, $start = 0)
{
$limit = "";
if($count > 0)
$limit .= " FIRST $count ";
if($start > 0)
$limit .= " SKIP $start ";
$sql = str_ireplace("select", "select $limit", $sql);
return $sql;
}
}
/**
* Wrapper to handle a PDO instance using the IBM driver.
*
* @see Database
*/
class DBIbm extends DBPDO
{
// Documented in the parent.
function DSNDriver()
{
return 'ibm';
}
// Documented in the parent.
function procedureSQL($name, $params)
{
$inputString = $this->_convertParamsToString($params);
return "call $name($inputString)";
}
// Documented in the parent.
function DSNParams()
{
$params = array();
$dsnparams = array();
// base params
if($this->database->Host != '')
{
if($this->database->HostTranslation)
{
$params['hostname'] = '127.0.0.1';
}
else
{
$params['hostname'] = $this->database->Host;
}
}
if($this->database->Databasename != '')
{
$params['database'] = $this->database->Databasename;
}
if($this->database->Port != '')
{
$params['port'] = $this->database->Port;
}
// custom extra params
if(is_array($this->database->ConnectionParams))
{
$params = array_merge($params, $this->database->ConnectionParams);
}
foreach($params as $key => $value)
{
$dsnparams[] = "{$key}={$value}";
}
return implode(';', $dsnparams);
}
// Documented in the parent.
function metaFields($tablename)
{
$parts = explode('.', $tablename);
$schema = current($parts);
$table = end($parts);
if(count($parts) == 1)
$conditions = "TABNAME='$table'";
else
$conditions = "TABNAME='$table' AND TABSCHEMA='$schema'";
$sql = "SELECT COLNAME from SYSCAT.COLUMNS where $conditions";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
$table_fields = $stmt->fetchAll(PDO::FETCH_ASSOC);
// converting table_fields from '0' => 'columnName' to 'colunName' => NULL
$keys = array();
foreach ($table_fields as $row)
{
$keys[$row['COLNAME']] = NULL;
}
return $keys;
}
// Documented in the parent.
function primaryKeys($tablename)
{
$parts = explode('.', $tablename);
$schema = current($parts);
$table = end($parts);
if(count($parts) == 1)
$conditions = "a.table_name='$table'";
else
$conditions = "a.table_name='$table' AND a.table_schema='$schema'";
$sql = "SELECT COLUMN_NAME
FROM dba_constraints a,dba_cons_columns b
WHERE $conditions
AND a.constraint_name=b.constraint_name
AND a.constraint_type ='P';";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
$table_fields = $stmt->fetchAll(PDO::FETCH_ASSOC);
$keys = array();
foreach ($table_fields as $row)
{
$keys[$row['COLUMN_NAME']] = 'PRIMARY';
}
return $keys;
}
// Documented in the parent.
function limitSQL($sql, $count, $start = 0)
{
if($count > -1)
{
$start = ($start > -1)? $start : 0;
$sql = " SELECT rpcl2.*
FROM (
SELECT ROW_NUMBER() OVER() AS \"RPCL_DB_ROWNUM\", rpcl1.*
FROM (
" . $sql . "
) rpcl1
) rpcl2
WHERE rpcl2.RPCL_DB_ROWNUM BETWEEN " . ($start+1) . " AND " . ($start+$count);
}
return $sql;
}
}
/**
* Wrapper to handle a PDO instance using the Informix driver.
*
* @see Database
*/
class DBInformix extends DBPDO
{
// Documented in the parent.
function DSNDriver()
{
return 'informix';
}
// Documented in the parent.
function DSNParams()
{
$params = array();
$dsnparams = array();
// base params
if($this->database->Host != '')
{
if($this->database->HostTranslation && strtolower($this->database->Host) == 'localhost')
{
$params['host'] = '127.0.0.1';
}
else
{
$params['host'] = $this->database->Host;
}
}