datadict-db2.inc.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. @version v5.20.17 31-Mar-2020
  4. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  5. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  6. Released under both BSD license and Lesser GPL library license.
  7. Whenever there is any discrepancy between the two licenses,
  8. the BSD license will take precedence.
  9. Set tabs to 4 for best viewing.
  10. */
  11. // security - hide paths
  12. if (!defined('ADODB_DIR')) die();
  13. class ADODB2_db2 extends ADODB_DataDict {
  14. var $databaseType = 'db2';
  15. var $seqField = false;
  16. function ActualType($meta)
  17. {
  18. switch($meta) {
  19. case 'C': return 'VARCHAR';
  20. case 'XL': return 'CLOB';
  21. case 'X': return 'VARCHAR(3600)';
  22. case 'C2': return 'VARCHAR'; // up to 32K
  23. case 'X2': return 'VARCHAR(3600)'; // up to 32000, but default page size too small
  24. case 'B': return 'BLOB';
  25. case 'D': return 'DATE';
  26. case 'TS':
  27. case 'T': return 'TIMESTAMP';
  28. case 'L': return 'SMALLINT';
  29. case 'I': return 'INTEGER';
  30. case 'I1': return 'SMALLINT';
  31. case 'I2': return 'SMALLINT';
  32. case 'I4': return 'INTEGER';
  33. case 'I8': return 'BIGINT';
  34. case 'F': return 'DOUBLE';
  35. case 'N': return 'DECIMAL';
  36. default:
  37. return $meta;
  38. }
  39. }
  40. // return string must begin with space
  41. function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
  42. {
  43. $suffix = '';
  44. if ($fautoinc) return ' GENERATED ALWAYS AS IDENTITY'; # as identity start with
  45. if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
  46. if ($fnotnull) $suffix .= ' NOT NULL';
  47. if ($fconstraint) $suffix .= ' '.$fconstraint;
  48. return $suffix;
  49. }
  50. function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
  51. {
  52. if ($this->debug) ADOConnection::outp("AlterColumnSQL not supported");
  53. return array();
  54. }
  55. function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
  56. {
  57. if ($this->debug) ADOConnection::outp("DropColumnSQL not supported");
  58. return array();
  59. }
  60. function ChangeTableSQL($tablename, $flds, $tableoptions = false)
  61. {
  62. /**
  63. Allow basic table changes to DB2 databases
  64. DB2 will fatally reject changes to non character columns
  65. */
  66. $validTypes = array("CHAR","VARC");
  67. $invalidTypes = array("BIGI","BLOB","CLOB","DATE", "DECI","DOUB", "INTE", "REAL","SMAL", "TIME");
  68. // check table exists
  69. $cols = $this->MetaColumns($tablename);
  70. if ( empty($cols)) {
  71. return $this->CreateTableSQL($tablename, $flds, $tableoptions);
  72. }
  73. // already exists, alter table instead
  74. list($lines,$pkey) = $this->_GenFields($flds);
  75. $alter = 'ALTER TABLE ' . $this->TableName($tablename);
  76. $sql = array();
  77. foreach ( $lines as $id => $v ) {
  78. if ( isset($cols[$id]) && is_object($cols[$id]) ) {
  79. /**
  80. If the first field of $v is the fieldname, and
  81. the second is the field type/size, we assume its an
  82. attempt to modify the column size, so check that it is allowed
  83. $v can have an indeterminate number of blanks between the
  84. fields, so account for that too
  85. */
  86. $vargs = explode(' ' , $v);
  87. // assume that $vargs[0] is the field name.
  88. $i=0;
  89. // Find the next non-blank value;
  90. for ($i=1;$i<sizeof($vargs);$i++)
  91. if ($vargs[$i] != '')
  92. break;
  93. // if $vargs[$i] is one of the following, we are trying to change the
  94. // size of the field, if not allowed, simply ignore the request.
  95. if (in_array(substr($vargs[$i],0,4),$invalidTypes))
  96. continue;
  97. // insert the appropriate DB2 syntax
  98. if (in_array(substr($vargs[$i],0,4),$validTypes)) {
  99. array_splice($vargs,$i,0,array('SET','DATA','TYPE'));
  100. }
  101. // Now Look for the NOT NULL statement as this is not allowed in
  102. // the ALTER table statement. If it is in there, remove it
  103. if (in_array('NOT',$vargs) && in_array('NULL',$vargs)) {
  104. for ($i=1;$i<sizeof($vargs);$i++)
  105. if ($vargs[$i] == 'NOT')
  106. break;
  107. array_splice($vargs,$i,2,'');
  108. }
  109. $v = implode(' ',$vargs);
  110. $sql[] = $alter . $this->alterCol . ' ' . $v;
  111. } else {
  112. $sql[] = $alter . $this->addCol . ' ' . $v;
  113. }
  114. }
  115. return $sql;
  116. }
  117. }