-
Notifications
You must be signed in to change notification settings - Fork 269
/
adodb-db2.inc.php
2003 lines (1639 loc) · 44.2 KB
/
adodb-db2.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
/**
* IBM DB2 Native Client driver.
*
* Originally DB2 drivers were dependent on an ODBC driver, and some installations
* may still use that. To use an ODBC driver connection, use the odbc_db2
* ADOdb driver. For Linux, you need the 'ibm_db2' PECL extension for PHP,
* For Windows, you need to locate an appropriate version of the php_ibm_db2.dll,
* as well as the IBM data server client software.
* This is basically a full rewrite of the original driver, for information
* about all the changes, see the update information on the ADOdb website
* for version 5.21.0.
*
* @link http://pecl.php.net/package/ibm_db2 PECL Extension For DB2
*
* This file is part of ADOdb, a Database Abstraction Layer library for PHP.
*
* @package ADOdb
* @link https://adodb.org Project's web site and documentation
* @link https://github.com/ADOdb/ADOdb Source code and issue tracker
*
* The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
* and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
* any later version. This means you can use it in proprietary products.
* See the LICENSE.md file distributed with this source code for details.
* @license BSD-3-Clause
* @license LGPL-2.1-or-later
*
* @copyright 2000-2013 John Lim
* @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
* @author Mark Newnham
*/
// security - hide paths
if (!defined('ADODB_DIR')) die();
define("_ADODB_DB2_LAYER", 2 );
class ADODB_db2 extends ADOConnection {
var $databaseType = "db2";
var $fmtDate = "'Y-m-d'";
var $concat_operator = '||';
var $sysTime = 'CURRENT TIME';
var $sysDate = 'CURRENT DATE';
var $sysTimeStamp = 'CURRENT TIMESTAMP';
var $fmtTimeStamp = "'Y-m-d H:i:s'";
var $replaceQuote = "''"; // string to use to replace quotes
var $dataProvider = "db2";
var $hasAffectedRows = true;
var $binmode = DB2_BINARY;
/*
* setting this to true will make array elements in FETCH_ASSOC
* mode case-sensitive breaking backward-compat
*/
var $useFetchArray = false;
var $_bindInputArray = true;
var $_genIDSQL = "VALUES NEXTVAL FOR %s";
var $_genSeqSQL = "
CREATE SEQUENCE %s START WITH %s
NO MAXVALUE NO CYCLE INCREMENT BY 1 NO CACHE
";
var $_dropSeqSQL = "DROP SEQUENCE %s";
var $_autocommit = true;
var $_lastAffectedRows = 0;
var $hasInsertID = true;
var $hasGenID = true;
/*
* Character used to wrap column and table names for escaping special
* characters in column and table names as well as forcing upper and
* lower case
*/
public $nameQuote = '"';
/*
* Executed after successful connection
*/
public $connectStmt = '';
/*
* Holds information about the stored procedure request
* currently being built
*/
private $storedProcedureParameters = false;
function __construct() {}
protected function _insertID($table = '', $column = '')
{
return ADOConnection::GetOne('VALUES IDENTITY_VAL_LOCAL()');
}
public function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
{
return $this->doDB2Connect($argDSN, $argUsername, $argPassword, $argDatabasename);
}
public function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
{
return $this->doDB2Connect($argDSN, $argUsername, $argPassword, $argDatabasename,true);
}
private function doDB2Connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persistent=false)
{
if (!function_exists('db2_connect')) {
ADOConnection::outp("DB2 extension not installed.");
return null;
}
$connectionParameters = $this->unpackParameters($argDSN,
$argUsername,
$argPassword,
$argDatabasename);
if ($connectionParameters == null)
{
/*
* Error thrown
*/
return null;
}
$argDSN = $connectionParameters['dsn'];
$argUsername = $connectionParameters['uid'];
$argPassword = $connectionParameters['pwd'];
$argDatabasename = $connectionParameters['database'];
$useCataloguedConnection = $connectionParameters['catalogue'];
if ($this->debug){
if ($useCataloguedConnection){
$connectMessage = "Catalogued connection using parameters: ";
$connectMessage .= "DB=$argDatabasename / ";
$connectMessage .= "UID=$argUsername / ";
$connectMessage .= "PWD=$argPassword";
}
else
{
$connectMessage = "Uncatalogued connection using DSN: $argDSN";
}
ADOConnection::outp($connectMessage);
}
/*
* This needs to be set before the connect().
*/
ini_set('ibm_db2.binmode', $this->binmode);
if ($persistent)
$db2Function = 'db2_pconnect';
else
$db2Function = 'db2_connect';
/*
* We need to flatten out the connectionParameters
*/
$db2Options = array();
if ($this->connectionParameters)
{
foreach($this->connectionParameters as $p)
foreach($p as $k=>$v)
$db2Options[$k] = $v;
}
if ($useCataloguedConnection)
$this->_connectionID = $db2Function($argDatabasename,
$argUsername,
$argPassword,
$db2Options);
else
$this->_connectionID = $db2Function($argDSN,
null,
null,
$db2Options);
$this->_errorMsg = @db2_conn_errormsg();
if ($this->_connectionID && $this->connectStmt)
$this->execute($this->connectStmt);
return $this->_connectionID != false;
}
/**
* Validates and preprocesses the passed parameters for consistency
*
* @param string $argDSN Either DSN or database
* @param string $argUsername User name or null
* @param string $argPassword Password or null
* @param string $argDatabasename Either DSN or database
*
* @return mixed array if correct, null if not
*/
private function unpackParameters($argDSN, $argUsername, $argPassword, $argDatabasename)
{
$connectionParameters = array('dsn'=>'',
'uid'=>'',
'pwd'=>'',
'database'=>'',
'catalogue'=>true
);
/*
* Uou can either connect to a catalogued connection
* with a database name e.g. 'SAMPLE'
* or an uncatalogued connection with a DSN like connection
* DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;
*/
if (!$argDSN && !$argDatabasename)
{
$errorMessage = 'Supply either catalogued or uncatalogued connection parameters';
$this->_errorMsg = $errorMessage;
if ($this->debug)
ADOConnection::outp($errorMessage);
return null;
}
$useCataloguedConnection = true;
$schemaName = '';
if ($argDSN && $argDatabasename)
{
/*
* If a catalogued connection if provided,
* as well as user and password
* that will take priority
*/
if ($argUsername && $argPassword && !$this->isDsn($argDatabasename))
{
if ($this->debug){
$errorMessage = 'Warning: Because you provided user,';
$errorMessage.= 'password and database, DSN connection ';
$errorMessage.= 'parameters were discarded';
ADOConnection::outp($errorMessage);
}
$argDSN = '';
}
else if ($this->isDsn($argDSN) && $this->isDsn($argDatabasename))
{
$errorMessage = 'Supply uncatalogued connection parameters ';
$errorMessage.= 'in either the database or DSN arguments, ';
$errorMessage.= 'but not both';
if ($this->debug)
ADOConnection::outp($errorMessage);
return null;
}
}
if (!$this->isDsn($argDSN) && $this->isDsn($argDatabasename))
{
/*
* Switch them around for next test
*/
$temp = $argDSN;
$argDsn = $argDatabasename;
$argDatabasenME = $temp;
}
if ($this->isDsn($argDSN))
{
if (!preg_match('/uid=/i',$argDSN)
|| !preg_match('/pwd=/i',$argDSN))
{
$errorMessage = 'For uncatalogued connections, provide ';
$errorMessage.= 'both UID and PWD in the connection string';
if ($this->debug)
ADOConnection::outp($errorMessage);
return null;
}
if (preg_match('/database=/i',$argDSN))
{
if ($argDatabasename)
{
$argDatabasename = '';
if ($this->debug)
{
$errorMessage = 'Warning: Because you provided ';
$errorMessage.= 'database information in the DSN ';
$errorMessage.= 'parameters, the supplied database ';
$errorMessage.= 'name was discarded';
ADOConnection::outp($errorMessage);
}
}
$useCataloguedConnection = false;
}
elseif ($argDatabasename)
{
$this->database = $argDatabasename;
$argDSN .= ';database=' . $argDatabasename;
$argDatabasename = '';
$useCataloguedConnection = false;
}
else
{
$errorMessage = 'Uncatalogued connection parameters ';
$errorMessage.= 'must contain a database= argument';
if ($this->debug)
ADOConnection::outp($errorMessage);
return null;
}
}
if ($argDSN && !$argDatabasename && $useCataloguedConnection)
{
$argDatabasename = $argDSN;
$argDSN = '';
}
if ($useCataloguedConnection
&& (!$argDatabasename
|| !$argUsername
|| !$argPassword))
{
$errorMessage = 'For catalogued connections, provide ';
$errorMessage.= 'database, username and password';
$this->_errorMsg = $errorMessage;
if ($this->debug)
ADOConnection::outp($errorMessage);
return null;
}
if ($argDatabasename)
$this->database = $argDatabasename;
elseif (!$this->database)
$this->database = $this->getDatabasenameFromDsn($argDSN);
$connectionParameters = array('dsn'=>$argDSN,
'uid'=>$argUsername,
'pwd'=>$argPassword,
'database'=>$argDatabasename,
'catalogue'=>$useCataloguedConnection
);
return $connectionParameters;
}
/**
* Does the provided string look like a DSN
*
* @param string $dsnString
*
* @return bool
*/
private function isDsn($dsnString){
$dsnArray = preg_split('/[;=]+/',$dsnString);
if (count($dsnArray) > 2)
return true;
return false;
}
/**
* Gets the database name from the DSN
*
* @param string $dsnString
*
* @return string
*/
private function getDatabasenameFromDsn($dsnString){
$dsnArray = preg_split('/[;=]+/',$dsnString);
$dbIndex = array_search('database',$dsnArray);
return $dsnArray[$dbIndex + 1];
}
/**
* format and return date string in database timestamp format
*
* @param mixed $ts either a string or a unixtime
* @param bool $isField discarded
*
* @return string
*/
function dbTimeStamp($ts,$isField=false)
{
if (empty($ts) && $ts !== 0) return 'null';
if (is_string($ts)) $ts = ADORecordSet::unixTimeStamp($ts);
return 'TO_DATE('.adodb_date($this->fmtTimeStamp,$ts).",'YYYY-MM-DD HH24:MI:SS')";
}
/**
* Format date column in sql string given an input format that understands Y M D
*
* @param string $fmt
* @param bool $col
*
* @return string
*/
function sqlDate($fmt, $col=false)
{
if (!$col) $col = $this->sysDate;
/* use TO_CHAR() if $fmt is TO_CHAR() allowed fmt */
if ($fmt== 'Y-m-d H:i:s')
return 'TO_CHAR('.$col.", 'YYYY-MM-DD HH24:MI:SS')";
$s = '';
$len = strlen($fmt);
for ($i=0; $i < $len; $i++) {
if ($s) $s .= $this->concat_operator;
$ch = $fmt[$i];
switch($ch) {
case 'Y':
case 'y':
if ($len==1) return "year($col)";
$s .= "char(year($col))";
break;
case 'M':
if ($len==1) return "monthname($col)";
$s .= "substr(monthname($col),1,3)";
break;
case 'm':
if ($len==1) return "month($col)";
$s .= "right(digits(month($col)),2)";
break;
case 'D':
case 'd':
if ($len==1) return "day($col)";
$s .= "right(digits(day($col)),2)";
break;
case 'H':
case 'h':
if ($len==1) return "hour($col)";
if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)";
else $s .= "''";
break;
case 'i':
case 'I':
if ($len==1) return "minute($col)";
if ($col != $this->sysDate)
$s .= "right(digits(minute($col)),2)";
else $s .= "''";
break;
case 'S':
case 's':
if ($len==1) return "second($col)";
if ($col != $this->sysDate)
$s .= "right(digits(second($col)),2)";
else $s .= "''";
break;
default:
if ($ch == '\\') {
$i++;
$ch = substr($fmt,$i,1);
}
$s .= $this->qstr($ch);
}
}
return $s;
}
function serverInfo()
{
$sql = "SELECT service_level, fixpack_num
FROM TABLE(sysproc.env_get_inst_info())
AS INSTANCEINFO";
$row = $this->GetRow($sql);
if ($row) {
$info['version'] = $row[0].':'.$row[1];
$info['fixpack'] = $row[1];
$info['description'] = '';
} else {
return ADOConnection::serverInfo();
}
return $info;
}
function createSequence($seqname='adodbseq',$start=1)
{
if (empty($this->_genSeqSQL))
return false;
$ok = $this->execute(sprintf($this->_genSeqSQL,$seqname,$start));
if (!$ok)
return false;
return true;
}
function dropSequence($seqname='adodbseq')
{
if (empty($this->_dropSeqSQL)) return false;
return $this->execute(sprintf($this->_dropSeqSQL,$seqname));
}
function selectLimit($sql,$nrows=-1,$offset=-1,$inputArr=false,$secs2cache=0)
{
$nrows = (integer) $nrows;
if ($offset <= 0)
{
if ($nrows >= 0)
$sql .= " FETCH FIRST $nrows ROWS ONLY ";
$rs = $this->execute($sql,$inputArr);
}
else
{
if ($offset > 0 && $nrows < 0);
else
{
$nrows += $offset;
$sql .= " FETCH FIRST $nrows ROWS ONLY ";
}
/*
* DB2 has no native support for mid table offset
*/
$rs = ADOConnection::selectLimit($sql,$nrows,$offset,$inputArr);
}
return $rs;
}
function errorMsg()
{
if ($this->_errorMsg !== false)
return $this->_errorMsg;
if (empty($this->_connectionID))
return @db2_conn_errormsg();
return @db2_conn_errormsg($this->_connectionID);
}
function errorNo()
{
if ($this->_errorCode !== false)
return $this->_errorCode;
if (empty($this->_connectionID))
$e = @db2_conn_error();
else
$e = @db2_conn_error($this->_connectionID);
return $e;
}
function beginTrans()
{
if (!$this->hasTransactions)
return false;
if ($this->transOff)
return true;
$this->transCnt += 1;
$this->_autocommit = false;
return db2_autocommit($this->_connectionID,false);
}
function CommitTrans($ok=true)
{
if ($this->transOff)
return true;
if (!$ok)
return $this->RollbackTrans();
if ($this->transCnt)
$this->transCnt -= 1;
$this->_autocommit = true;
$ret = @db2_commit($this->_connectionID);
@db2_autocommit($this->_connectionID,true);
return $ret;
}
function RollbackTrans()
{
if ($this->transOff) return true;
if ($this->transCnt) $this->transCnt -= 1;
$this->_autocommit = true;
$ret = @db2_rollback($this->_connectionID);
@db2_autocommit($this->_connectionID,true);
return $ret;
}
/**
* Return a list of Primary Keys for a specified table
*
* We don't use db2_statistics as the function does not seem to play
* well with mixed case table names
*
* @param string $table
* @param bool $primary (optional) only return primary keys
* @param bool $owner (optional) not used in this driver
*
* @return string[] Array of indexes
*/
public function metaPrimaryKeys($table,$owner=false)
{
$primaryKeys = array();
global $ADODB_FETCH_MODE;
$schema = '';
$this->_findschema($table,$schema);
$table = $this->getTableCasedValue($table);
$savem = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
$this->setFetchMode(ADODB_FETCH_NUM);
$sql = "SELECT *
FROM syscat.indexes
WHERE tabname='$table'";
$rows = $this->getAll($sql);
$this->setFetchMode($savem);
$ADODB_FETCH_MODE = $savem;
if (empty($rows))
return false;
foreach ($rows as $r)
{
if ($r[7] != 'P')
continue;
$cols = explode('+',$r[6]);
foreach ($cols as $colIndex=>$col)
{
if ($colIndex == 0)
continue;
$columnName = $this->getMetaCasedValue($col);
$primaryKeys[] = $columnName;
}
break;
}
return $primaryKeys;
}
/**
* Returns a list of Foreign Keys associated with a specific table.
*
* @param string $table
* @param string $owner discarded
* @param bool $upper discarded
* @param bool $associative discarded
*
* @return string[]|false An array where keys are tables, and values are foreign keys;
* false if no foreign keys could be found.
*/
public function metaForeignKeys($table, $owner = '', $upper = false, $associative = false)
{
global $ADODB_FETCH_MODE;
$schema = '';
$this->_findschema($table,$schema);
$savem = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
$this->setFetchMode(ADODB_FETCH_NUM);
$sql = "SELECT SUBSTR(tabname,1,20) table_name,
SUBSTR(constname,1,20) fk_name,
SUBSTR(REFTABNAME,1,12) parent_table,
SUBSTR(refkeyname,1,20) pk_orig_table,
fk_colnames
FROM syscat.references
WHERE tabname = '$table'";
$results = $this->getAll($sql);
$ADODB_FETCH_MODE = $savem;
$this->setFetchMode($savem);
if (empty($results))
return false;
$foreignKeys = array();
foreach ($results as $r)
{
$parentTable = trim($this->getMetaCasedValue($r[2]));
$keyName = trim($this->getMetaCasedValue($r[1]));
$foreignKeys[$parentTable] = $keyName;
}
return $foreignKeys;
}
/**
* Returns a list of tables
*
* @param string $ttype (optional)
* @param string $schema (optional)
* @param string $mask (optional)
*
* @return array
*/
public function metaTables($ttype=false,$schema=false,$mask=false)
{
global $ADODB_FETCH_MODE;
$savem = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
/*
* Values for TABLE_TYPE
* ---------------------------
* ALIAS, HIERARCHY TABLE, INOPERATIVE VIEW, NICKNAME,
* MATERIALIZED QUERY TABLE, SYSTEM TABLE, TABLE,
* TYPED TABLE, TYPED VIEW, and VIEW
*
* If $ttype passed as '', match 'TABLE' and 'VIEW'
* If $ttype passed as 'T' it is assumed to be 'TABLE'
* if $ttype passed as 'V' it is assumed to be 'VIEW'
*/
$ttype = strtoupper($ttype);
if ($ttype) {
/*
* @todo We could do valid type checking or array type
*/
if ($ttype == 'V')
$ttype = 'VIEW';
if ($ttype == 'T')
$ttype = 'TABLE';
}
if (!$schema)
$schema = '%';
if (!$mask)
$mask = '%';
$qid = @db2_tables($this->_connectionID,NULL,$schema,$mask,$ttype);
$rs = new ADORecordSet_db2($qid);
$ADODB_FETCH_MODE = $savem;
if (!$rs)
return false;
$arr = $rs->getArray();
$rs->Close();
$tableList = array();
/*
* Array items
* ---------------------------------
* 0 TABLE_CAT The catalog that contains the table.
* The value is NULL if this table does not have catalogs.
* 1 TABLE_SCHEM Name of the schema that contains the table.
* 2 TABLE_NAME Name of the table.
* 3 TABLE_TYPE Table type identifier for the table.
* 4 REMARKS Description of the table.
*/
for ($i=0; $i < sizeof($arr); $i++)
{
$tableRow = $arr[$i];
$tableName = $tableRow[2];
$tableType = $tableRow[3];
if (!$tableName)
continue;
if ($ttype == '' && (strcmp($tableType,'TABLE') <> 0 && strcmp($tableType,'VIEW') <> 0))
continue;
/*
* Set metacasing if required
*/
$tableName = $this->getMetaCasedValue($tableName);
/*
* If we requested a schema, we prepend the schema
name to the table name
*/
if (strcmp($schema,'%') <> 0)
$tableName = $schema . '.' . $tableName;
$tableList[] = $tableName;
}
return $tableList;
}
/**
* Return a list of indexes for a specified table
*
* We don't use db2_statistics as the function does not seem to play
* well with mixed case table names
*
* @param string $table
* @param bool $primary (optional) only return primary keys
* @param bool $owner (optional) not used in this driver
*
* @return string[] Array of indexes
*/
public function metaIndexes($table, $primary = false, $owner = false) {
global $ADODB_FETCH_MODE;
/* Array(
* [name_of_index] => Array(
* [unique] => true or false
* [columns] => Array(
* [0] => firstcol
* [1] => nextcol
* [2] => etc........
* )
* )
* )
*/
$indices = array();
$primaryKeyName = '';
$table = $this->getTableCasedValue($table);
$savem = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
$this->setFetchMode(ADODB_FETCH_NUM);
$sql = "SELECT *
FROM syscat.indexes
WHERE tabname='$table'";
$rows = $this->getAll($sql);
$this->setFetchMode($savem);
$ADODB_FETCH_MODE = $savem;
if (empty($rows))
return false;
foreach ($rows as $r)
{
$primaryIndex = $r[7] == 'P'?1:0;
if (!$primary)
/*
* Primary key not requested, ignore that one
*/
if ($r[7] == 'P')
continue;
$indexName = $this->getMetaCasedValue($r[1]);
if (!isset($indices[$indexName]))
{
$unique = ($r[7] == 'U')?1:0;
$indices[$indexName] = array('unique'=>$unique,
'primary'=>$primaryIndex,
'columns'=>array()
);
}
$cols = explode('+',$r[6]);
foreach ($cols as $colIndex=>$col)
{
if ($colIndex == 0)
continue;
$columnName = $this->getMetaCasedValue($col);
$indices[$indexName]['columns'][] = $columnName;
}
}
return $indices;
}
/**
* List procedures or functions in an array.
*
* We interrogate syscat.routines instead of calling the PHP
* function procedures because ADOdb requires the type of procedure
* this is not available in the php function
*
* @param string $procedureNamePattern (optional)
* @param string $catalog (optional)
* @param string $schemaPattern (optional)
* @return array of procedures on current database.
*
*/
public function metaProcedures($procedureNamePattern = null, $catalog = null, $schemaPattern = null) {
global $ADODB_FETCH_MODE;
$metaProcedures = array();
$procedureSQL = '';
$catalogSQL = '';
$schemaSQL = '';
$savem = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
if ($procedureNamePattern)
$procedureSQL = "AND ROUTINENAME LIKE " . strtoupper($this->qstr($procedureNamePattern));
if ($catalog)
$catalogSQL = "AND OWNER=" . strtoupper($this->qstr($catalog));
if ($schemaPattern)
$schemaSQL = "AND ROUTINESCHEMA LIKE {$this->qstr($schemaPattern)}";
$fields = "
ROUTINENAME,
CASE ROUTINETYPE
WHEN 'P' THEN 'PROCEDURE'
WHEN 'F' THEN 'FUNCTION'
ELSE 'METHOD'
END AS ROUTINETYPE_NAME,
ROUTINESCHEMA,
REMARKS";
$SQL = "SELECT $fields
FROM syscat.routines
WHERE OWNER IS NOT NULL
$procedureSQL
$catalogSQL
$schemaSQL
ORDER BY ROUTINENAME
";
$result = $this->execute($SQL);
$ADODB_FETCH_MODE = $savem;
if (!$result)
return false;
while ($r = $result->fetchRow()){
$procedureName = $this->getMetaCasedValue($r[0]);
$schemaName = $this->getMetaCasedValue($r[2]);
$metaProcedures[$procedureName] = array('type'=> $r[1],
'catalog' => '',
'schema' => $schemaName,
'remarks' => $r[3]
);
}
return $metaProcedures;
}
/**
* Lists databases. Because instances are independent, we only know about
* the current database name
*
* @return string[]
*/
public function metaDatabases(){