* @package Mammut\DB\Adapter\DB2 */ class DB2 extends \Mammut\DB\DB { const _VERSION_ = '1.1.0.0'; protected $dbi = false; public function __construct($cstring, $user, $password, array $options = array(), iCache $cache = NULL) { if(!extension_loaded('ibm_db2')) throw new \Mammut\Exception\ExtensionException('ibm_db2 extension for db2 missing'); $this->dialect = new \Mammut\DB\Sql\Dialect\Db2(); if(preg_match('#([a-zA-Z0-9\.-]+)#', $cstring, $match)) { $this->dbi = array_search(self::OPT_PERSISTENT, $options) !== false ? db2_pconnect($cstring, $user, $password) : db2_connect($cstring, $user, $password); } elseif(preg_match('#([a-zA-Z0-9\.-]+)(:[0-9]+){0,1}/([a-zA-Z0-9_\.-]+)#', $cstring, $match)) { $dbscm = explode(".", $match[3]); $port = empty($match[2]) ? 50000 : $match[2]; $con_st = "DATABASE={$dbscm[0]};HOSTNAME={$match[1]};PORT={$port};PROTOCOL=TCPIP;UID={$user};PWD={$password};"; $this->dbi = array_search(self::OPT_PERSISTENT, $options) !== false ? db2_pconnect($con_st, NULL, NULL) : db2_connect($con_st, NULL, NULL); } else throw new InvalidArgumentException('$cstring is invalid: ' . $cstring); if($st = db2_conn_errormsg()) throw new DBException($st); } public function setDatabase($name) { throw new ImplementationException('not implemented'); } public function getDatabase() { $result = db2_exec($this->dbi, "SELECT current_server AS s FROM sysibm.sysdummy1"); $val = db2_fetch_object($result)->s; db2_free_stmt($result); $this->qcount++; return $val; } public function getSchema() { $result = db2_exec($this->dbi, "SELECT current_schema AS s FROM sysibm.sysdummy1"); $val = db2_fetch_object($result)->s; db2_free_stmt($result); $this->qcount++; return $val; } public function query($query, $limit = -1, $skip = 0) { if(self::$LOGGER) self::$LOGGER->log(LOG_DEBUG, get_class($this) . ': sending new query: ' . $query); db2_exec($this->dbi, $query); } public function prepareStatement($query, $limit = -1, $skip = 0) { throw new ImplementationException('not implemented'); } public function getArray($sql) { throw new ImplementationException('not implemented'); } public function getRow($sql) { throw new ImplementationException('not implemented'); } public function getObject($sql, $class = false, $param = array()) { throw new ImplementationException('not implemented'); } public function getInsertId() { throw new ImplementationException('not implemented'); } public function getAffectedRowCount() { throw new ImplementationException('not implemented'); } public function createTable(TableInfo $info) { $sql = DB2Util::createTableSQL($this, $info, $this->getSchema()); echo $sql; $result = db2_exec($this->dbi, $sql); $this->qcount++; echo db2_stmt_errormsg($result); if(!$result) throw new SQLException(db2_stmt_errormsg($result), $sql); $this->tables = false; } public function createView(\Mammut\DB\Model\ViewInfo $info) { $sql = $this->dialect->getDdl()->getCreateViewSQL($info); $this->query($sql); } public function table($table) { if($table instanceof \Mammut\DB\Model\TableInfo) $table = $table->getName(); if(array_search($table, $this->tableList()) === false) throw new DBException('table "' . $table . '" dosen\'t exist'); return new Table($this, $table, $addPrefix ? $this->getPrefix() : false); } public function tableList() { $result = false; $query = "SELECT * FROM syscat.tables WHERE ownertype='U'"; $result = db2_exec($this->dbi, $query); $this->qcount++; if($st = db2_stmt_errormsg($result)) throw new SQLException($st, $query); if($result) { $tables = array(); while($next = db2_fetch_row($result)) { $tables[] = $next[0]; } db2_free_stmt($result); return $tables; } else return false; } public function getTableInfo($tablename) { throw new ImplementationException('not implemented'); } public function dropTable($table) { $scm = $this->getSchema(); if($table instanceof Table) $table = $table->getName(); elseif($table instanceof TableInfo) $table = $table->getName(); pg_query($this->db, "DROP TABLE " . $this->escapeTableName($scm) . '.' . $this->escapeTableName($table)); $this->qcount++; $this->tables = false; } public function dateCol2uts($date) { throw new ImplementationException('not implemented'); } public function uts2dateCol($uts) { throw new ImplementationException('not implemented'); } public function datetimeCol2uts($datetime) { throw new ImplementationException('not implemented'); } public function uts2datetimeCol($uts) { throw new ImplementationException('not implemented'); } }