* Install for dmdb.

This commit is contained in:
朱金勇
2023-04-01 14:22:24 +00:00
parent 0c70fda291
commit f65d3aa417
11 changed files with 751 additions and 59 deletions
+125 -23
View File
@@ -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;
}
+355
View File
@@ -0,0 +1,355 @@
<?php
/**
* ZenTaoPHP的dao和sql类。
* The dao and sql class file of ZenTaoPHP framework.
*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
/**
* Dameng类。
* Dameng driver.
*
* @package framework
*/
class dm extends dao
{
/**
* 类MySQL的DESC语法。
* Desc table, show fields.
*
* @param string $tableName
* @access public
* @return array
*/
public function descTable($tableName)
{
$sql = "select * from all_tab_columns where table_name='{$this->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 = <<<EOT
DECLARE
E_IDENTITY_INSERT EXCEPTION;
E_INSTALL_DUP_VAL_ON_INDEX EXCEPTION;
E_UPDATE_DUP_VAL_ON_INDEX EXCEPTION;
PRAGMA EXCEPTION_INIT (E_IDENTITY_INSERT, -2723);
PRAGMA EXCEPTION_INIT (E_INSTALL_DUP_VAL_ON_INDEX, -6602);
PRAGMA EXCEPTION_INIT (E_UPDATE_DUP_VAL_ON_INDEX, -6610);
BEGIN
BEGIN
$insertSql;
EXCEPTION
WHEN DUP_VAL_ON_INDEX OR E_IDENTITY_INSERT THEN
$updateSql;
WHEN E_INSTALL_DUP_VAL_ON_INDEX THEN
$updateSql;
END;
EXCEPTION
WHEN E_UPDATE_DUP_VAL_ON_INDEX THEN
$deleteSql;
$insertSql;
END;
EOT;
self::$querys[] = $sql;
}
$sql = str_replace('`', '"', $sql);
try
{
if($this->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;
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
/**
* ZenTaoPHP的dao和sql类。
* The dao and sql class file of ZenTaoPHP framework.
*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
/**
* MySQL类。
* MySQL driver.
*
* @package framework
*/
class mysql extends dao
{
}
+243 -27
View File
@@ -39,12 +39,14 @@ class dbh
* Constructor
*
* @param object $config
* @param bool $setSchema
* @access public
* @return void
*/
public function __construct($config)
public function __construct($config, $setSchema = true)
{
$dsn = "{$config->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);
}
}
-1
View File
@@ -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';
-1
View File
@@ -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';
-1
View File
@@ -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';
-1
View File
@@ -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';
-1
View File
@@ -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'] = '普通方式';
+5 -4
View File
@@ -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();
}
}
+1
View File
@@ -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';