Browse Source

达梦连接类备注和优化

master
wanghongjun 3 years ago
parent
commit
59c668521d
  1. 70
      source/application/common/dm/Dm.php

70
source/application/common/dm/Dm.php

@ -12,23 +12,44 @@ class Dm
private $charset = 'GBK'; // 字符集
public $conn;
public $database = 'STADIUM';
public function __construct()
{
# 获取配置数据
$database = config('database');
$dm = $database['connections']['dm'];
# 模式名称
$this->database = $dm['database'];
# 连接达梦数据库
$this->conn = dm_connect($dm['hostname'].':'.$dm['hostport'], $dm['username'], $dm['password']);
if (!$this->conn) {
die('连接数据库失败!');
}
}
/**
* 拼接达梦查询数据表名
* @param string $tableName // 表名
* @return string
*/
protected function splitTableName($tableName) {
if (strpos($tableName,$this->database) !== false) return $tableName;
return '"' . $this->database . '"."' . $tableName .'"';
}
/**
* 创建数据
* @param string $table // 创建数据表名
* @param array $data // 创建数据
* @return mixed
*/
public function insert($table, $data)
{
#$fields = implode(',', array_keys($data));
$fields = implode(',', array_keys($data));
$values = "'" . implode("','", array_values($data)) . "'";
$sql = 'INSERT INTO '.$table.' VALUES ('.$values.')';
$tableName = $this->splitTableName($table);
$sql = 'INSERT INTO '.$tableName.'('.$fields.') VALUES ('.$values.')';
$res = dm_exec($this->conn, $sql);
@ -37,7 +58,13 @@ class Dm
return $res;
}
// 更新数据
/**
* 更新数据
* @param string $table // 更新数据表名
* @param array $data // 更新数据
* @param string $where // 更新数据条件
* @return mixed
*/
public function update($table, $data, $where)
{
$set = array();
@ -45,7 +72,8 @@ class Dm
$set[] = $key . '=' . "'{$value}'";
}
$sql = 'UPDATE '.$table." SET " . implode(",", $set) . ' WHERE '.$where;
$tableName = $this->splitTableName($table);
$sql = 'UPDATE '.$tableName." SET " . implode(",", $set) . ' WHERE '.$where;
$res = dm_exec($this->conn, $sql);
@ -54,10 +82,16 @@ class Dm
return $res;
}
// 删除数据
/**
* 删除数据
* @param string $table // 删除数据表名
* @param string $where // 删除数据条件
* @return mixed
*/
public function delete($table, $where)
{
$sql = 'DELETE FROM '.$table.' WHERE '.$where;
$tableName = $this->splitTableName($table);
$sql = 'DELETE FROM '.$tableName.' WHERE '.$where;
$result = dm_exec($this->conn, $sql);
@ -66,11 +100,16 @@ class Dm
return $result;
}
// 清空表
/**
* 清空表
* @param string $table // 清空表名
* @return mixed
*/
public function truncate($table)
{
$sql = 'truncate table '.$table;
$tableName = $this->splitTableName($table);
$sql = 'truncate table '.$tableName;
$result = dm_exec($this->conn, $sql);
@ -79,10 +118,19 @@ class Dm
return $result;
}
// 查询数据
/**
* 查询多条数据
* @param string $table // 查询表名
* @param string $where // 查询条件
* @param string $fields // 查询字段
* @param string $order // 查询排序
* @param string $limit // 查询条数
* @return array // 返回数组
*/
public function select($table, $where = null, $fields = '*', $order = null, $limit = null)
{
$sql = 'SELECT '.$fields.' FROM '.$table;
$tableName = $this->splitTableName($table);
$sql = 'SELECT '.$fields.' FROM '.$tableName;
if ($where != null) {
$sql .= ' WHERE '.$where;
}

Loading…
Cancel
Save