00001 <?php 00025 require_once 'Zend/Db/Adapter/Pdo/Ibm.php'; 00026 00028 require_once 'Zend/Db/Statement/Pdo/Ibm.php'; 00029 00030 00038 class Zend_Db_Adapter_Pdo_Ibm_Db2 00039 { 00043 protected $_adapter = null; 00044 00053 public function __construct($adapter) 00054 { 00055 $this->_adapter = $adapter; 00056 } 00057 00063 public function listTables() 00064 { 00065 $sql = "SELECT tabname " 00066 . "FROM SYSCAT.TABLES "; 00067 return $this->_adapter->fetchCol($sql); 00068 } 00069 00077 public function describeTable($tableName, $schemaName = null) 00078 { 00079 $sql = "SELECT DISTINCT c.tabschema, c.tabname, c.colname, c.colno, 00080 c.typename, c.default, c.nulls, c.length, c.scale, 00081 c.identity, tc.type AS tabconsttype, k.colseq 00082 FROM syscat.columns c 00083 LEFT JOIN (syscat.keycoluse k JOIN syscat.tabconst tc 00084 ON (k.tabschema = tc.tabschema 00085 AND k.tabname = tc.tabname 00086 AND tc.type = 'P')) 00087 ON (c.tabschema = k.tabschema 00088 AND c.tabname = k.tabname 00089 AND c.colname = k.colname) 00090 WHERE " 00091 . $this->_adapter->quoteInto('UPPER(c.tabname) = UPPER(?)', $tableName); 00092 if ($schemaName) { 00093 $sql .= $this->_adapter->quoteInto(' AND UPPER(c.tabschema) = UPPER(?)', $schemaName); 00094 } 00095 $sql .= " ORDER BY c.colno"; 00096 00097 $desc = array(); 00098 $stmt = $this->_adapter->query($sql); 00099 00103 $result = $stmt->fetchAll(Zend_Db::FETCH_NUM); 00104 00109 $tabschema = 0; 00110 $tabname = 1; 00111 $colname = 2; 00112 $colno = 3; 00113 $typename = 4; 00114 $default = 5; 00115 $nulls = 6; 00116 $length = 7; 00117 $scale = 8; 00118 $identityCol = 9; 00119 $tabconstype = 10; 00120 $colseq = 11; 00121 00122 foreach ($result as $key => $row) { 00123 list ($primary, $primaryPosition, $identity) = array(false, null, false); 00124 if ($row[$tabconstype] == 'P') { 00125 $primary = true; 00126 $primaryPosition = $row[$colseq]; 00127 } 00132 if ($row[$identityCol] == 'Y') { 00133 $identity = true; 00134 } 00135 00136 $desc[$this->_adapter->foldCase($row[$colname])] = array( 00137 'SCHEMA_NAME' => $this->_adapter->foldCase($row[$tabschema]), 00138 'TABLE_NAME' => $this->_adapter->foldCase($row[$tabname]), 00139 'COLUMN_NAME' => $this->_adapter->foldCase($row[$colname]), 00140 'COLUMN_POSITION' => $row[$colno]+1, 00141 'DATA_TYPE' => $row[$typename], 00142 'DEFAULT' => $row[$default], 00143 'NULLABLE' => (bool) ($row[$nulls] == 'Y'), 00144 'LENGTH' => $row[$length], 00145 'SCALE' => $row[$scale], 00146 'PRECISION' => ($row[$typename] == 'DECIMAL' ? $row[$length] : 0), 00147 'UNSIGNED' => false, 00148 'PRIMARY' => $primary, 00149 'PRIMARY_POSITION' => $primaryPosition, 00150 'IDENTITY' => $identity 00151 ); 00152 } 00153 00154 return $desc; 00155 } 00156 00166 public function limit($sql, $count, $offset = 0) 00167 { 00168 $count = intval($count); 00169 if ($count < 0) { 00171 require_once 'Zend/Db/Adapter/Exception.php'; 00172 throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid"); 00173 } else { 00174 $offset = intval($offset); 00175 if ($offset < 0) { 00177 require_once 'Zend/Db/Adapter/Exception.php'; 00178 throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid"); 00179 } 00180 00181 if ($offset == 0 && $count > 0) { 00182 $limit_sql = $sql . " FETCH FIRST $count ROWS ONLY"; 00183 return $limit_sql; 00184 } 00191 $limit_sql = "SELECT z2.* 00192 FROM ( 00193 SELECT ROW_NUMBER() OVER() AS \"ZEND_DB_ROWNUM\", z1.* 00194 FROM ( 00195 " . $sql . " 00196 ) z1 00197 ) z2 00198 WHERE z2.zend_db_rownum BETWEEN " . ($offset+1) . " AND " . ($offset+$count); 00199 } 00200 return $limit_sql; 00201 } 00202 00209 public function lastSequenceId($sequenceName) 00210 { 00211 $sql = 'SELECT PREVVAL FOR '.$this->_adapter->quoteIdentifier($sequenceName).' AS VAL FROM SYSIBM.SYSDUMMY1'; 00212 $value = $this->_adapter->fetchOne($sql); 00213 return $value; 00214 } 00215 00222 public function nextSequenceId($sequenceName) 00223 { 00224 $sql = 'SELECT NEXTVAL FOR '.$this->_adapter->quoteIdentifier($sequenceName).' AS VAL FROM SYSIBM.SYSDUMMY1'; 00225 $value = $this->_adapter->fetchOne($sql); 00226 return $value; 00227 } 00228 }