From f65d3aa417c06deb43ba90735c5c1f3f0814d84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E9=87=91=E5=8B=87?= Date: Sat, 1 Apr 2023 14:22:24 +0000 Subject: [PATCH] * Install for dmdb. --- lib/base/dao/dao.class.php | 148 ++++++++++-- lib/dao/dm.class.php | 355 +++++++++++++++++++++++++++++ lib/dao/mysql.class.php | 22 ++ lib/dbh/dbh.class.php | 270 +++++++++++++++++++--- module/install/lang/de.php | 1 - module/install/lang/en.php | 1 - module/install/lang/fr.php | 1 - module/install/lang/vi.php | 1 - module/install/lang/zh-cn.php | 1 - module/install/model.php | 9 +- module/install/view/step3.html.php | 1 + 11 files changed, 751 insertions(+), 59 deletions(-) create mode 100644 lib/dao/dm.class.php create mode 100644 lib/dao/mysql.class.php diff --git a/lib/base/dao/dao.class.php b/lib/base/dao/dao.class.php index c585eb2a4b..159611b9fc 100644 --- a/lib/base/dao/dao.class.php +++ b/lib/base/dao/dao.class.php @@ -316,6 +316,18 @@ class baseDAO $this->dbh->commit(); } + /** + * Desc table, show fields. + * + * @param string $tableName + * @access public + * @return array + */ + public function descTable($tableName) + { + return $this->query("DESC $tableName")->fetchAll(); + } + /** * select方法,调用sql::select()。 * The select method, call sql::select(). @@ -372,7 +384,7 @@ class baseDAO **/ try { - $row = $this->dbh->query($sql)->fetch(PDO::FETCH_OBJ); + $row = $this->dbh->rawQuery($sql)->fetch(PDO::FETCH_OBJ); } catch (PDOException $e) { @@ -552,7 +564,7 @@ class baseDAO public function explain($sql = '') { $sql = empty($sql) ? $this->processSQL() : $sql; - $result = $this->dbh->query('explain ' . $sql)->fetch(); + $result = $this->dbh->rawQuery('explain ' . $sql)->fetch(); a($result); } @@ -567,6 +579,26 @@ class baseDAO { $sql = $this->sqlobj->get(); + /* INSERT INTO table VALUES(...) */ + if($this->method == 'insert' and !empty($this->sqlobj->data)) + { + $skipFields = $this->sqlobj->skipFields; + $fields = '('; + $values = 'VALUES('; + foreach($this->sqlobj->data as $field => $value) + { + if(strpos($skipFields, ",$field,") !== false) continue; + $fields .= "`{$field}`,"; + if(is_string($value)) $value = $this->sqlobj->quote($value); + $values .= $value . ','; + } + $fields = substr($fields, 0, -1); + $values = substr($values, 0, -1); + $fields .= ')'; + $values .= ')'; + $sql .= $fields . ' ' . $values; + } + /** * 如果是magic模式,处理表和字段。 * If the mode is magic, process the $fields and $table. @@ -670,7 +702,7 @@ class baseDAO if($sql) { - $sql = trim($sql); + $sql = $this->dbh->formatSQL($sql); $sqlMethod = strtolower(substr($sql, 0, strpos($sql, ' '))); $this->setMethod($sqlMethod); $this->sqlobj = new sql(); @@ -688,11 +720,11 @@ class baseDAO if($this->slaveDBH and $method == 'select') { - return $this->slaveDBH->query($sql); + return $this->slaveDBH->rawQuery($sql); } else { - return $this->dbh->query($sql); + return $this->dbh->rawQuery($sql); } } catch (PDOException $e) @@ -1101,7 +1133,7 @@ class baseDAO if($funcName == 'unique') { $args = func_get_args(); - $sql = "SELECT COUNT(*) AS count FROM $this->table WHERE `$fieldName` = " . $this->sqlobj->quote($value); + $sql = "SELECT COUNT(*) AS `count` FROM $this->table WHERE `$fieldName` = " . $this->sqlobj->quote($value); if($condition) $sql .= ' AND ' . $condition; try { @@ -1339,7 +1371,7 @@ class baseDAO { $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); $sql = "DESC $this->table"; - $rawFields = $this->dbh->query($sql)->fetchAll(); + $rawFields = $this->dbh->rawQuery($sql)->fetchAll(); $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); } catch (PDOException $e) @@ -1347,6 +1379,7 @@ class baseDAO $this->sqlError($e); } + $fields = array(); foreach($rawFields as $rawField) { $firstPOS = strpos($rawField->type, '('); @@ -1447,7 +1480,7 @@ class baseSQL public $dbh; /** - * 更新或插入日期。 + * 更新或插入的数据。 * The data to update or insert. * * @var mix @@ -1455,6 +1488,32 @@ class baseSQL */ public $data; + /** + * 不需要拼接SQL的字段 + * skipFields + * + * @var mixed + * @access public + */ + public $skipFields; + + /** + * SQL 方法, insert, update, delete ... + * SQL method, insert, update, delete ... + * + * @var mixed + * @access public + */ + public $method; + + /** + * setField + * + * @var mixed + * @access public + */ + public $setField; + /** * 是否是第一次设置。 * Is the first time to call set. @@ -1528,6 +1587,19 @@ class baseSQL return new sql($table); } + /** + * 设置SQL的方法。 + * Set SQL method. + * + * @param string $method + * @access public + * @return void + */ + public function setMethod($method = '') + { + $this->method = $method; + } + /** * select语句。 * The sql is select. @@ -1539,6 +1611,7 @@ class baseSQL public static function select($field = '*') { $sqlobj = self::factory(); + $sqlobj->setMethod('select'); $sqlobj->sql = "SELECT $field "; return $sqlobj; } @@ -1554,6 +1627,7 @@ class baseSQL public static function update($table) { $sqlobj = self::factory(); + $sqlobj->setMethod('update'); $sqlobj->sql = "UPDATE $table SET "; return $sqlobj; } @@ -1569,7 +1643,8 @@ class baseSQL public static function insert($table) { $sqlobj = self::factory(); - $sqlobj->sql = "INSERT INTO $table SET "; + $sqlobj->setMethod('insert'); + $sqlobj->sql = "INSERT INTO $table "; return $sqlobj; } @@ -1584,6 +1659,7 @@ class baseSQL public static function replace($table) { $sqlobj = self::factory(); + $sqlobj->setMethod('replace'); $sqlobj->sql = "REPLACE $table SET "; return $sqlobj; } @@ -1598,6 +1674,7 @@ class baseSQL public static function delete() { $sqlobj = self::factory(); + $sqlobj->setMethod('delete'); $sqlobj->sql = "DELETE "; return $sqlobj; } @@ -1616,15 +1693,18 @@ class baseSQL $data = (object) $data; if($skipFields) $skipFields = ',' . str_replace(' ', '', $skipFields) . ','; - foreach($data as $field => $value) + if($this->method != 'insert') { - if(!preg_match('|^\w+$|', $field)) + foreach($data as $field => $value) { - unset($data->$field); - continue; + if(!preg_match('|^\w+$|', $field)) + { + unset($data->$field); + continue; + } + if(strpos($skipFields, ",$field,") !== false) continue; + $this->sql .= "`$field` = " . $this->quote($value) . ','; } - if(strpos($skipFields, ",$field,") !== false) continue; - $this->sql .= "`$field` = " . $this->quote($value) . ','; } $this->data = $data; @@ -1676,15 +1756,23 @@ class baseSQL { if($this->inCondition and !$this->conditionIsTrue) return $this; - /* Add ` to avoid keywords of mysql. */ - if(strpos($set, '=') ===false) + if($this->method == 'update') { - $set = str_replace(',', '', $set); - $set = '`' . str_replace('`', '', $set) . '`'; + /* Add ` to avoid keywords of mysql. */ + if(strpos($set, '=') ===false) + { + $set = str_replace(',', '', $set); + } + + $this->sql .= $this->isFirstSet ? " $set" : ", $set"; + if($this->isFirstSet) $this->isFirstSet = false; + } + elseif($this->method == 'insert') + { + $this->setField = $value; + $this->data->$value = ''; } - $this->sql .= $this->isFirstSet ? " $set" : ", $set"; - if($this->isFirstSet) $this->isFirstSet = false; return $this; } @@ -1796,7 +1884,7 @@ class baseSQL } else { - $condition = $arg1; + $condition = ctype_alnum($arg1) ? '`' . $arg1 . '`' : $arg1; } if(!$this->inMark) $this->sql .= ' ' . DAO::WHERE ." $condition "; @@ -1815,6 +1903,8 @@ class baseSQL public function andWhere($condition, $addMark = false) { if($this->inCondition and !$this->conditionIsTrue) return $this; + if(ctype_alnum($condition)) $condition = '`' . $condition . '`'; + $mark = $addMark ? '(' : ''; $this->sql .= " AND {$mark} $condition "; return $this; @@ -1831,6 +1921,8 @@ class baseSQL public function orWhere($condition) { if($this->inCondition and !$this->conditionIsTrue) return $this; + if(ctype_alnum($condition)) $condition = '`' . $condition . '`'; + $this->sql .= " OR $condition "; return $this; } @@ -1846,7 +1938,17 @@ class baseSQL public function eq($value) { if($this->inCondition and !$this->conditionIsTrue) return $this; - $this->sql .= " = " . $this->quote($value); + + if($this->method == 'insert') + { + $field = $this->setField; + $this->data->$field = $value; + } + else + { + $this->sql .= " = " . $this->quote($value); + } + return $this; } diff --git a/lib/dao/dm.class.php b/lib/dao/dm.class.php new file mode 100644 index 0000000000..2b7f618090 --- /dev/null +++ b/lib/dao/dm.class.php @@ -0,0 +1,355 @@ +table}'"; + $rawFields = $this->dbh->rawQuery($sql)->fetchAll(); + + $fields = array(); + foreach($rawFields as $rawField) + { + $field = new stdClass(); + $field->Field = $rawField->field; + $fields[] = $field; + } + return $fields; + } + + /** + * select方法,调用sql::select()。 + * The select method, call sql::select(). + * + * @param string $fields + * @access public + * @return static|sql|baseDAO the dao object self. + */ + public function select($fields = '*') + { + /* Split by ','. */ + $fieldList = preg_split("/,(?![^(]+\))/", $fields); + foreach($fieldList as $key => $field) + { + $field = trim($field); + $pos = strrpos($field, ' '); + if($pos) + { + $originField = substr($field, 0, $pos); + $alias = trim(substr($field, $pos)); + + $fieldList[$key] = $this->formatField($originField, $alias); + } + else + { + $fieldList[$key] = $this->formatField($field, ''); + } + } + + return parent::select(implode(',', $fieldList)); + } + + private function formatField($originField, $alias) + { + /* Format originField. */ + $replace = array( + 'GROUP_CONCAT' => 'WM_CONCAT', + ); + + if(strcasecmp($originField, 'distinct') !== 0) + { + $tableField = explode('.', $originField); + if(count($tableField) == 2 and ctype_alnum($tableField[1]) and $tableField[1] != '*') + { + $originField = $tableField[0] . '."' . $tableField[1] . '"'; + } + elseif(count($tableField) == 1 and ctype_alnum($tableField[0]) and $tableField[0] != '*') + { + $originField = '"' . $tableField[0] . '"'; + } + $originField = str_ireplace(array_keys($replace), array_values($replace), $originField); + } + + /* Format alias. */ + if($alias and ctype_alnum($alias)) $alias = '"' . $alias . '"'; + + return $originField . ' ' . $alias; + } + + /** + * 创建WHERE部分。 + * Create the where part. + * + * @param string $arg1 the field name + * @param string $arg2 the operator + * @param string $arg3 the value + * @access public + * @return static|sql the sql object. + */ + public function where($arg1, $arg2 = null, $arg3 = null) + { + $arg1 = $this->formatWhere($arg1); + return parent::where($arg1, $arg2, $arg3); + } + + /** + * 创建AND部分。 + * Create the AND part. + * + * @param string $condition + * @access public + * @return static|sql the sql object. + */ + public function andWhere($condition = '', $addMark = false) + { + $condition = $this->formatWhere($condition); + return parent::andWhere($condition, $addMark); + } + + /** + * 创建OR部分。 + * Create the OR part. + * + * @param bool $condition + * @access public + * @return static|sql the sql object. + */ + public function orWhere($condition) + { + $condition = $this->formatWhere($condition); + return parent::orWhere($condition); + } + + private function formatWhere($condition) + { + $condition = trim($condition); + if($condition == '1') return '1 = 1'; + + $pos = strrpos($condition, ' '); + if($pos) + { + $originField = substr($condition, 0, $pos); + return $this->formatField($originField, '') . substr($condition, $pos); + } + else + { + return $this->formatField($condition, ''); + } + } + + public function getPKColumns() + { + $sql = "SELECT A.OWNER, A.TABLE_NAME, WM_CONCAT(B.COLUMN_NAME) PK_COLUMNS FROM ALL_CONSTRAINTS A, ALL_CONS_COLUMNS B where A.CONSTRAINT_type = 'P' AND A.OWNER = '{$this->config->db->user}' AND A.TABLE_NAME = '{$this->table}' AND B.OWNER = A.OWNER AND A.TABLE_NAME = B.TABLE_NAME GROUP BY A.OWNER, A.TABLE_NAME;"; + + $content = $this->dbh->query($sql)->fetch(); + return empty($content) ? false : $content->PK_COLUMNS; + } + + /** + * 执行SQL。query()会返回stmt对象,该方法只返回更改或删除的记录数。 + * Execute the sql. It's different with query(), which return the stmt object. But this not. + * + * @param string $sql + * @access public + * @return int the modified or deleted records. 更改或删除的记录数。 + */ + public function exec($sql = '') + { + if($sql) + { + $this->sqlobj = new sql(); + $this->sqlobj->sql = $sql; + } + else + { + $sql = $this->processSQL(); + $this->sqlobj->sql = $sql; + } + + if($this->method == 'replace' && !empty($this->sqlobj->data)) + { + $insertSql = "INSERT INTO {$this->table} "; + $fields = '('; + $values = 'VALUES('; + foreach($this->sqlobj->data as $field => $value) + { + $fields .= "`{$field}`,"; + if(is_string($value)) $value = $this->sqlobj->quote($value); + $values .= $value . ','; + } + $fields = substr($fields, 0, -1); + $values = substr($values, 0, -1); + $fields .= ')'; + $values .= ')'; + $insertSql .= $fields . ' ' . $values; + + $updateSql = str_replace('REPLACE', 'UPDATE', $sql); + $pk = $this->getPKColumns(); + if(!empty($pk) && isset($this->sqlobj->data->{$pk})) $updateSql .= " where {$pk} = '{$this->sqlobj->data->{$pk}}'"; + + $deleteSql = "DELETE FROM {$this->table} WHERE "; + $ingore = array(); + $ingore['`zt_config`'] = array('value'); + foreach($this->sqlobj->data as $field => $value) + { + if(isset($ingore[$this->table]) and in_array($field, $ingore[$this->table])) continue; + $deleteSql .= "`{$field}` = "; + $deleteSql .= is_string($value) ? "'{$value}'" : $value; + $deleteSql .= ' AND '; + } + $deleteSql = rtrim($deleteSql, 'AND '); + + $sql = <<table) unset(dao::$cache[$this->table]); + $this->reset(); + return $this->dbh->exec($sql); + } + catch (PDOException $e) + { + $this->sqlError($e); + } + } + + /** + * 获取一个记录。 + * Fetch one record. + * + * @param string $field 如果已经设置获取的字段,则只返回这个字段的值,否则返回这个记录。 + * if the field is set, only return the value of this field, else return this record + * @access public + * @return object|mixed + */ + public function fetch($field = '') + { + return parent::fetch($field); + } + + /** + * 获取所有记录。 + * Fetch all records. + * + * @param string $keyField 返回以该字段做键的记录 + * the key field, thus the return records is keyed by this field + * @access public + * @return array the records + */ + public function fetchAll($keyField = '') + { + return parent::fetchAll($keyField); + } + + /** + * 获取表的字段类型。 + * Get the defination of fields of the table. + * + * @access public + * @return array + */ + public function getFieldsType() + { + try + { + $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); + $sql = "select * from all_tab_columns where table_name='{$this->table}'"; + $rawFields = $this->dbh->rawQuery($sql)->fetchAll(); + $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); + } + catch (PDOException $e) + { + $this->sqlError($e); + } + + $fields = array(); + foreach($rawFields as $rawField) + { + $firstPOS = strpos($rawField->data_type, '('); + $type = substr($rawField->data_type, 0, $firstPOS > 0 ? $firstPOS : strlen($rawField->type)); + $type = str_replace(array('big', 'small', 'medium', 'tiny', 'var'), '', $type); + $field = array(); + + if($type == 'VARCHAR' or $type == 'CHAR') + { + $length = $rawField->data_length; + $field['rule'] = 'length'; + $field['options']['max'] = $length; + $field['options']['min'] = 0; + } + elseif($type == 'INTEGER') + { + $field['rule'] = 'int'; + } + elseif($type == 'FLOAT' or $type == 'DOUBLE') + { + $field['rule'] = 'float'; + } + elseif($type == 'DATE') + { + $field['rule'] = 'date'; + } + elseif($type == 'DATETIME') + { + $field['rule'] = 'datetime'; + } + else + { + $field['rule'] = 'skip'; + } + $fields[$rawField->field] = $field; + } + return $fields; + } +} diff --git a/lib/dao/mysql.class.php b/lib/dao/mysql.class.php new file mode 100644 index 0000000000..261a4957bf --- /dev/null +++ b/lib/dao/mysql.class.php @@ -0,0 +1,22 @@ +driver}:host={$config->host}:{$config->port}"; + $dsn = "{$config->driver}:host={$config->host}:{$config->port};dbname={$config->name}"; + $pdo = new PDO($dsn, $config->user, $config->password); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); @@ -54,23 +56,15 @@ class dbh $pdo->exec("SET NAMES {$config->encoding}"); if(isset($this->config->strictMode) and $this->config->strictMode == false) $pdo->exec("SET @@sql_mode= ''"); } + else if($setSchema) + { + $pdo->exec("SET SCHEMA {$config->name}"); + } - $this->pdo = $pdo; + $this->pdo = $pdo; $this->config = $config; } - /** - * Query sql. - * - * @param string $sql - * @access public - * @return PDOStatement|false - */ - public function query($sql) - { - return $this->pdo->query($sql); - } - /** * Execute sql. * @@ -80,9 +74,50 @@ class dbh */ public function exec($sql) { + $sql = $this->formatSQL($sql); + if(!$sql) return true; + return $this->pdo->exec($sql); } + /** + * Query sql. + * + * @param string $sql + * @access public + * @return PDOStatement|false + */ + public function query($sql) + { + $sql = $this->formatSQL($sql); + return $this->pdo->query($sql); + } + + /** + * Query raw sql. + * + * @param string $sql + * @access public + * @return PDOStatement|false + */ + public function rawQuery($sql) + { + return $this->pdo->query($sql); + } + + /** + * Set attribute. + * + * @param int $attribute + * @param mixed $value + * @access public + * @return bool + */ + public function setAttribute($attribute, $value) + { + return $this->pdo->setAttribute($attribute, $value); + } + /** * Check db exits or not. * @@ -97,13 +132,12 @@ class dbh $sql = "SHOW DATABASES like '{$this->config->name}'"; break; case 'dm': - $sql = "select * from dba_objects where object_type='SCH' and owner='{$this->config->name}';"; + $sql = "SELECT * FROM dba_objects WHERE object_type='SCH' AND owner='{$this->config->name}'"; break; default: $sql = ''; } - - return $this->query($sql)->fetch(); + return $this->rawQuery($sql)->fetch(); } /** @@ -123,13 +157,13 @@ class dbh $sql = "SHOW TABLES FROM {$this->config->name} like {$tableName}"; break; case 'dm': - $sql = "select * from all_tables where owner='{$this->config->name}' and table_name={$tableName};"; + $sql = "SELECT * FROM all_tables WHERE owner='{$this->config->name}' AND table_name={$tableName}"; break; default: $sql = ''; } - return $this->query($sql)->fetch(); + return $this->rawQuery($sql)->fetch(); } /** @@ -141,19 +175,28 @@ class dbh */ public function createDB($version) { - switch($this->config->driver == 'mysql') + switch($this->config->driver) { case 'mysql': $sql = "CREATE DATABASE `{$this->config->name}`"; if($version > 4.1) $sql .= " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"; - return $this->query($sql); + return $this->rawQuery($sql); case 'dm': - $createTableSpace = "CREATE TABLESPACE {$this->config->name} DATAFILE '{$this->config->name}.DBF' size 150 AUTOEXTEND ON"; - $createUser = "CREATE USER {$this->config->name} IDENTIFIED by {$this->config->password} DEFAULT TABLESPACE {$this->config->name} DEFAULT INDEX TABLESPACE {$this->config->name};"; + $tableSpace = strtoupper($this->config->name); + $res = $this->rawQuery("SELECT * FROM dba_data_files WHERE TABLESPACE_NAME = '$tableSpace'")->fetchAll(); - $this->query($createTableSpace); - return $this->query($createUser); + if(empty($res)) + { + $createTableSpace = "CREATE TABLESPACE $tableSpace DATAFILE '{$this->config->name}.DBF' size 150 AUTOEXTEND ON"; + $createUser = "CREATE USER {$this->config->name} IDENTIFIED by {$this->config->password} DEFAULT TABLESPACE {$this->config->name} DEFAULT INDEX TABLESPACE {$this->config->name}"; + + $this->rawQuery($createTableSpace); + $this->rawQuery($createUser); + } + + $createSchema = "CREATE SCHEMA {$this->config->name} AUTHORIZATION {$this->config->name}"; + return $this->rawQuery($createSchema); default: return false; @@ -169,7 +212,7 @@ class dbh */ public function useDB($dbName) { - switch($this->config->driver == 'mysql') + switch($this->config->driver) { case 'mysql': return $this->exec("USE {$this->config->name}"); @@ -181,4 +224,177 @@ class dbh return false; } } + + /** + * Format sql. + * + * @param string $sql + * @access public + * @return string + */ + public function formatSQL($sql) + { + switch($this->config->driver) + { + case 'dm': + return $this->formatDmSQL($sql); + return $sql; + + default: + return $sql; + } + } + + /** + * Format dm sql. + * + * @param string $sql + * @access public + * @return string + */ + public function formatDmSQL($sql) + { + $sql = trim($sql); + $actionPos = strpos($sql, ' '); + $action = strtoupper(substr($sql, 0, $actionPos)); + $setPos = 0; + switch($action) + { + case 'SELECT': + return $this->formatField($sql); + case 'REPLACE': + $sql = str_replace('REPLACE', 'INSERT', $sql); + $action = 'INSERT'; + case 'INSERT': + case 'UPDATE': + $setPos = stripos($sql, ' VALUES'); + $sql = str_replace('0000-00-00', '1970-01-01', $sql); + $sql = str_replace('00:00:00', '00:00:01', $sql); + if(strpos($sql, "\\'") !== FALSE) $sql = str_replace("\\'", "''''", $sql); + if(strpos($sql, '\"') !== FALSE) $sql = str_replace('\"', '"', $sql); + if(strpos($sql, '\\\\') !== FALSE) $sql = str_replace('\\\\', '\\', $sql); + break; + case 'CREATE': + if(stripos($sql, 'CREATE OR REPLACE VIEW ') === 0) return ''; + if(stripos($sql, 'CREATE VIEW') === 0) return ''; + if(stripos($sql, 'CREATE FUNCTION') === 0) return ''; + case 'ALTER': + $sql = $this->formatField($sql); + $sql = $this->formatAttr($sql); + return $sql; + case 'SET': + if(stripos($sql, 'SET SCHEMA') === 0) return $sql; + case 'USE': + return ''; + case 'DROP': + return $this->formatField($sql); + } + + if($setPos <= 0) return $sql; + + $fields = substr($sql, 0, $setPos); + $fields = $this->formatField($fields); + + $sql = $fields . substr($sql, $setPos); + + /* DMDB must set IDENTITY_INSERT 'on' to insert id field. */ + if($action == 'INSERT' and stripos($fields, '"id"') !== FALSE) + { + $tableBegin = strpos($sql, '"' . $this->config->prefix); + $tableEnd = strpos($sql, '"', $tableBegin + 1); + $tableName = '' . $this->config->name . '."' . substr($sql, $tableBegin + 1, $tableEnd - $tableBegin - 1) . '"'; + return "SET IDENTITY_INSERT $tableName ON;" . $sql; + } + + return $sql; + } + + /** + * Format field. + * + * @param string $sql + * @access public + * @return string + */ + public function formatField($sql) + { + switch($this->config->driver) + { + case 'dm': + $sql = str_replace('`', '"', $sql); + return $sql; + + default: + return $sql; + } + } + + /** + * Format attribute of field. + * + * @param string $sql + * @access public + * @return string + */ + public function formatAttr($sql) + { + switch($this->config->driver) + { + case 'dm': + $pos = stripos($sql, ' ENGINE'); + if($pos > 0) $sql = substr($sql, 0, $pos); + + $sql = preg_replace('/\(\ *\d+\ *\)/', '', $sql); + + $replace = array( + " AUTO_INCREMENT" => ' IDENTITY(1, 1)', + " int " => ' integer ', + " mediumint " => ' integer ', + " smallint " => ' integer ', + " tinyint " => ' integer ', + " varchar " => ' varchar(255) ', + " char " => ' varchar(255) ', + " mediumtext " => ' text ', + " mediumtext," => ' text,', + " longtext " => ' text ', + "COLLATE 'utf8_general_ci'" => ' ', + " unsigned " => ' ', + " zerofill " => ' ', + "0000-00-00" => '1970-01-01', + ); + + $sql = preg_replace('/ enum[\_0-9a-z\,\'\"\( ]+\)+/i', ' varchar(255) ', $sql); + $sql = str_ireplace(array_keys($replace), array_values($replace), $sql); + $sql = preg_replace('/\,\s+key[\_\"0-9a-z ]+\(+[\,\_\"0-9a-z ]+\)+/i', '', $sql); + $sql = preg_replace('/\,\s*(unique|fulltext)*\s+key[\_\"0-9a-z ]+\(+[\,\_\"0-9a-z ]+\)+/i', '', $sql); + $sql = preg_replace('/ float\s*\(+[\,\_\"0-9a-z ]+\)+/i', ' float', $sql); + } + + return $sql; + } + + /** + * Quote. + * + * @param string $string + * @param int $parameter_type + * @access public + * @return string + */ + public function quote($string, $parameter_type = PDO::PARAM_STR) + { + return $this->pdo->quote($string, $parameter_type); + } + + /** + * Get last insert id. + * + * @param string $name + * @access public + * @return string|false + */ + public function lastInsertId($name = null) + { + return $this->pdo->lastInsertId($name); + } } diff --git a/module/install/lang/de.php b/module/install/lang/de.php index 3525d2244e..af424f3c21 100644 --- a/module/install/lang/de.php +++ b/module/install/lang/de.php @@ -136,7 +136,6 @@ $lang->install->working = 'Work Mode'; $lang->install->dbDriverList = array(); $lang->install->dbDriverList['mysql'] = 'MySQL'; -$lang->install->dbDriverList['pgsql'] = 'PostgreSQL'; $lang->install->dbDriverList['dm'] = 'DM8'; $lang->install->requestTypes['GET'] = 'GET'; diff --git a/module/install/lang/en.php b/module/install/lang/en.php index a1a856e980..3b080ad321 100644 --- a/module/install/lang/en.php +++ b/module/install/lang/en.php @@ -136,7 +136,6 @@ $lang->install->working = 'Operation Mode'; $lang->install->dbDriverList = array(); $lang->install->dbDriverList['mysql'] = 'MySQL'; -$lang->install->dbDriverList['pgsql'] = 'PostgreSQL'; $lang->install->dbDriverList['dm'] = 'DM8'; $lang->install->requestTypes['GET'] = 'GET'; diff --git a/module/install/lang/fr.php b/module/install/lang/fr.php index 75b3aa5a5b..bc14b41339 100644 --- a/module/install/lang/fr.php +++ b/module/install/lang/fr.php @@ -136,7 +136,6 @@ $lang->install->working = 'Operation Mode'; $lang->install->dbDriverList = array(); $lang->install->dbDriverList['mysql'] = 'MySQL'; -$lang->install->dbDriverList['pgsql'] = 'PostgreSQL'; $lang->install->dbDriverList['dm'] = 'DM8'; $lang->install->requestTypes['GET'] = 'GET'; diff --git a/module/install/lang/vi.php b/module/install/lang/vi.php index e23baf560c..35bb2fc9e6 100644 --- a/module/install/lang/vi.php +++ b/module/install/lang/vi.php @@ -106,7 +106,6 @@ $lang->install->working = 'Operation chế độ'; $lang->install->dbDriverList = array(); $lang->install->dbDriverList['mysql'] = 'MySQL'; -$lang->install->dbDriverList['pgsql'] = 'PostgreSQL'; $lang->install->dbDriverList['dm'] = 'DM8'; $lang->install->requestTypes['GET'] = 'GET'; diff --git a/module/install/lang/zh-cn.php b/module/install/lang/zh-cn.php index 6783e0f298..32515e84cc 100644 --- a/module/install/lang/zh-cn.php +++ b/module/install/lang/zh-cn.php @@ -136,7 +136,6 @@ $lang->install->working = '工作方式'; $lang->install->dbDriverList = array(); $lang->install->dbDriverList['mysql'] = 'MySQL'; -$lang->install->dbDriverList['pgsql'] = 'PostgreSQL'; $lang->install->dbDriverList['dm'] = '达梦'; $lang->install->requestTypes['GET'] = '普通方式'; diff --git a/module/install/model.php b/module/install/model.php index 434856c906..0660545460 100644 --- a/module/install/model.php +++ b/module/install/model.php @@ -353,7 +353,7 @@ class installModel extends model { try { - return new dbh($this->config->db); + return new dbh($this->config->db, false); } catch (PDOException $exception) { @@ -369,7 +369,7 @@ class installModel extends model */ public function getDatabaseVersion() { - if($this->config->db->driver != 'mysql') return 0; + if($this->config->db->driver != 'mysql') return 8; $sql = "SELECT VERSION() AS version"; $result = $this->dbh->query($sql)->fetch(); @@ -423,7 +423,8 @@ class installModel extends model $table = str_replace('`zt_', $this->config->db->name . '.`zt_', $table); $table = str_replace('`ztv_', $this->config->db->name . '.`ztv_', $table); $table = str_replace('zt_', $this->config->db->prefix, $table); - if(!$this->dbh->query($table)) return false; + + $this->dbh->exec($table); } } catch (PDOException $exception) @@ -479,7 +480,7 @@ class installModel extends model $admin->password = md5($this->post->password); $admin->gender = 'f'; $admin->visions = 'rnd,lite'; - $this->dao->replace(TABLE_USER)->data($admin)->exec(); + $this->dao->insert(TABLE_USER)->data($admin)->exec(); } } diff --git a/module/install/view/step3.html.php b/module/install/view/step3.html.php index 8414fa692e..64c142ccf1 100644 --- a/module/install/view/step3.html.php +++ b/module/install/view/step3.html.php @@ -19,6 +19,7 @@ if(!isset($error)) \$config->debug = false; \$config->requestType = '$requestType'; \$config->timezone = '$timezone'; +\$config->db->driver = '$dbDriver'; \$config->db->host = '$dbHost'; \$config->db->port = '$dbPort'; \$config->db->name = '$dbName';