datadict-db2.inc.php 3.8 KB

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