'INTEGER', 'number' => 'INTEGER', 'varchar' => 'VARHAR', 'char' => 'CHAR', 'text' => 'TEXT', 'bigtext' => 'TEXT', 'date' => 'VARCHAR(14)', 'float' => 'REAL', ); /** * @brief constructor **/ function DBSqlite3_pdo() { $this->_setDBInfo(); $this->_connect(); } /** * @brief create an instance of this class */ function create() { return new DBSqlite3_pdo; } /** * @brief Return if installable **/ function isSupported() { return class_exists('PDO'); } /** * @brief DB settings and connect/close **/ function _setDBInfo() { $db_info = Context::getDBInfo(); $this->database = $db_info->db_database; $this->prefix = $db_info->db_table_prefix; if(!substr($this->prefix,-1)!='_') $this->prefix .= '_'; } /** * @brief DB Connection **/ function _connect() { // override if db information not exists if(!$this->database) return; // Attempt to access the database file try { // PDO is only supported with PHP5, // so it is allowed to use try~catch statment in this class. $this->handler = new PDO('sqlite:'.$this->database); } catch (PDOException $e) { $this->setError(-1, 'Connection failed: '.$e->getMessage()); $this->is_connected = false; return; } // Check connections $this->is_connected = true; $this->password = md5($this->password); } /** * @brief disconnect to DB **/ function close() { if(!$this->is_connected) return; $this->commit(); } /** * @brief Begin a transaction **/ function begin() { if(!$this->is_connected || $this->transaction_started) return; if($this->handler->beginTransaction()) $this->transaction_started = true; } /** * @brief Rollback **/ function rollback() { if(!$this->is_connected || !$this->transaction_started) return; $this->handler->rollBack(); $this->transaction_started = false; } /** * @brief Commit **/ function commit($force = false) { if(!$force && (!$this->is_connected || !$this->transaction_started)) return; $this->handler->commit(); $this->transaction_started = false; } /** * @brief Add or change quotes to the query string variables **/ function addQuotes($string) { if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string)); if(!is_numeric($string)) $string = str_replace("'","''",$string); return $string; } /** * @brief : Prepare a query statement **/ function _prepare($query) { if(!$this->is_connected) return; // notify to start a query execution $this->actStart($query); $this->stmt = $this->handler->prepare($query); if($this->handler->errorCode() != '00000') { $this->setError($this->handler->errorCode(), print_r($this->handler->errorInfo(),true)); $this->actFinish(); } $this->bind_idx = 0; $this->bind_vars = array(); } /** * @brief : Binding params in stmt **/ function _bind($val) { if(!$this->is_connected || !$this->stmt) return; $this->bind_idx ++; $this->bind_vars[] = $val; $this->stmt->bindParam($this->bind_idx, $val); } /** * @brief : execute the prepared statement **/ function _execute() { if(!$this->is_connected || !$this->stmt) return; $this->stmt->execute(); if($this->stmt->errorCode() === '00000') { $output = null; while($tmp = $this->stmt->fetch(PDO::FETCH_ASSOC)) { unset($obj); foreach($tmp as $key => $val) { $pos = strpos($key, '.'); if($pos) $key = substr($key, $pos+1); $obj->{$key} = str_replace("''","'",$val); } $output[] = $obj; } } else { $this->setError($this->stmt->errorCode(),print_r($this->stmt->errorInfo(),true)); } $this->stmt = null; $this->actFinish(); if(is_array($output) && count($output)==1) return $output[0]; return $output; } /** * @brief Return the sequence value incremented by 1 **/ function getNextSequence() { $query = sprintf("insert into %ssequence (seq) values (NULL)", $this->prefix); $this->_prepare($query); $result = $this->_execute(); $sequence = $this->handler->lastInsertId(); if($sequence % 10000 == 0) { $query = sprintf("delete from %ssequence where seq < %d", $this->prefix, $sequence); $this->_prepare($query); $result = $this->_execute(); } return $sequence; } /** * @brief return if the table already exists **/ function isTableExists($target_name) { $query = sprintf('pragma table_info(%s%s)', $this->prefix, $target_name); $this->_prepare($query); if(!$this->_execute()) return false; return true; } /** * @brief Add a column to a table **/ function addColumn($table_name, $column_name, $type='number', $size='', $default = '', $notnull=false) { $type = $this->column_type[$type]; if(strtoupper($type)=='INTEGER') $size = ''; $query = sprintf("alter table %s%s add %s ", $this->prefix, $table_name, $column_name); if($size) $query .= sprintf(" %s(%s) ", $type, $size); else $query .= sprintf(" %s ", $type); if($default) $query .= sprintf(" default '%s' ", $default); if($notnull) $query .= " not null "; $this->_prepare($query); return $this->_execute(); } /** * @brief Remove a column from a table **/ function dropColumn($table_name, $column_name) { $query = sprintf("alter table %s%s drop column %s ", $this->prefix, $table_name, $column_name); $this->_query($query); } /** * @brief Return column information of a table **/ function isColumnExists($table_name, $column_name) { $query = sprintf("pragma table_info(%s%s)", $this->prefix, $table_name); $this->_prepare($query); $output = $this->_execute(); if($output) { $column_name = strtolower($column_name); foreach($output as $key => $val) { $name = strtolower($val->name); if($column_name == $name) return true; } } return false; } /** * @brief Add an index to a table * $target_columns = array(col1, col2) * $is_unique? unique : none **/ function addIndex($table_name, $index_name, $target_columns, $is_unique = false) { if(!is_array($target_columns)) $target_columns = array($target_columns); $key_name = sprintf('%s%s_%s', $this->prefix, $table_name, $index_name); $query = sprintf('CREATE %s INDEX %s ON %s%s (%s)', $is_unique?'UNIQUE':'', $key_name, $this->prefix, $table_name, implode(',',$target_columns)); $this->_prepare($query); $this->_execute(); } /** * @brief Drop an index from a table **/ function dropIndex($table_name, $index_name, $is_unique = false) { $key_name = sprintf('%s%s_%s', $this->prefix, $table_name, $index_name); $query = sprintf("DROP INDEX %s", $this->prefix, $table_name, $key_name); $this->_query($query); } /** * @brief Return index information of a table **/ function isIndexExists($table_name, $index_name) { $key_name = sprintf('%s%s_%s', $this->prefix, $table_name, $index_name); $query = sprintf("pragma index_info(%s)", $key_name); $this->_prepare($query); $output = $this->_execute(); if(!$output) return false; return true; } /** * @brief create a table from xml file **/ function createTableByXml($xml_doc) { return $this->_createTable($xml_doc); } /** * @brief create a table from xml file **/ function createTableByXmlFile($file_name) { if(!file_exists($file_name)) return; // read xml file $buff = FileHandler::readFile($file_name); return $this->_createTable($buff); } /** * @brief generate a query to create a table using the schema xml * * type : number, varchar, text, char, date, \n * opt : notnull, default, size\n * index : primary key, index, unique\n **/ function _createTable($xml_doc) { // xml parsing $oXml = new XmlParser(); $xml_obj = $oXml->parse($xml_doc); // Create a table schema $table_name = $xml_obj->table->attrs->name; if($this->isTableExists($table_name)) return; $table_name = $this->prefix.$table_name; if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column; else $columns = $xml_obj->table->column; foreach($columns as $column) { $name = $column->attrs->name; $type = $column->attrs->type; if(strtoupper($this->column_type[$type])=='INTEGER') $size = ''; else $size = $column->attrs->size; $notnull = $column->attrs->notnull; $primary_key = $column->attrs->primary_key; $index = $column->attrs->index; $unique = $column->attrs->unique; $default = $column->attrs->default; $auto_increment = $column->attrs->auto_increment; if($auto_increment) { $column_schema[] = sprintf('%s %s PRIMARY KEY %s', $name, $this->column_type[$type], $auto_increment?'AUTOINCREMENT':'' ); } else { $column_schema[] = sprintf('%s %s%s %s %s %s', $name, $this->column_type[$type], $size?'('.$size.')':'', $notnull?'NOT NULL':'', $primary_key?'PRIMARY KEY':'', isset($default)?"DEFAULT '".$default."'":'' ); } if($unique) $unique_list[$unique][] = $name; else if($index) $index_list[$index][] = $name; } $schema = sprintf('CREATE TABLE %s (%s%s) ;', $table_name," ", implode($column_schema,", ")); $this->_prepare($schema); $this->_execute(); if($this->isError()) return; if(count($unique_list)) { foreach($unique_list as $key => $val) { $query = sprintf('CREATE UNIQUE INDEX %s_%s ON %s (%s)', $this->addQuotes($table_name), $key, $this->addQuotes($table_name), implode(',',$val)); $this->_prepare($query); $this->_execute(); if($this->isError()) $this->rollback(); } } if(count($index_list)) { foreach($index_list as $key => $val) { $query = sprintf('CREATE INDEX %s_%s ON %s (%s)', $this->addQuotes($table_name), $key, $this->addQuotes($table_name), implode(',',$val)); $this->_prepare($query); $this->_execute(); if($this->isError()) $this->rollback(); } } } /** * @brief insertAct **/ function _executeInsertAct($output) { $this->_prepare($query); $val_count = count($val_list); for($i=0;$i<$val_count;$i++) $this->_bind($val_list[$i]); return $this->_execute(); } /** * @brief updateAct **/ function _executeUpdateAct($output) { $this->_prepare($query); return $this->_execute(); } /** * @brief deleteAct **/ function _executeDeleteAct($output) { $this->_prepare($query); return $this->_execute(); } /** * @brief selectAct * * To fetch a list of the page conveniently when selecting, \n * navigation method supported **/ function _executeSelectAct($output) { $buff = new Object(); $buff->data = $data; return $buff; } } return new DBSqlite3_pdo; ?>