Browse Source

达梦操作类优化

master
wanghongjun 3 years ago
parent
commit
2ceb2dd687
  1. 65
      source/application/common/dm/Dm.php

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

@ -15,11 +15,13 @@ class Dm
public $database = 'STADIUM';
public $sql;
public function __construct()
public function __construct($dm = [])
{
# 获取配置数据
$database = config('database');
$dm = $database['connections']['dm'];
if (empty($dm)) {
$database = config('database');
$dm = $database['connections']['dm'];
}
# 模式名称
$this->database = $dm['database'];
# 连接达梦数据库
@ -65,7 +67,7 @@ class Dm
* 更新数据
* @param string $table // 更新数据表名
* @param array $data // 更新数据
* @param string $where // 更新数据条件
* @param string|array $where // 更新数据条件
* @return mixed
*/
public function update($table, $data, $where)
@ -76,7 +78,7 @@ class Dm
}
$tableName = $this->splitTableName($table);
$sql = 'UPDATE '.$tableName." SET " . implode(",", $set) . ' WHERE '.$where;
$sql = 'UPDATE '.$tableName." SET " . implode(",", $set) . ' WHERE '.$this->where($where);
$res = dm_exec($this->conn, $sql);
@ -96,7 +98,7 @@ class Dm
public function delete($table, $where)
{
$tableName = $this->splitTableName($table);
$sql = 'DELETE FROM '.$tableName.' WHERE '.$where;
$sql = 'DELETE FROM '.$tableName.' WHERE '.$this->where($where);
$result = dm_exec($this->conn, $sql);
@ -142,7 +144,7 @@ class Dm
$tableName = $this->splitTableName($table);
$sql = 'SELECT '.$fields.' FROM '.$tableName;
if ($where != null) {
$sql .= ' WHERE '.$where;
$sql .= ' WHERE '.$this->where($where);
}
if ($group != null) {
$sql .= ' GROUP BY '.$group;
@ -183,7 +185,7 @@ class Dm
$tableName = $this->splitTableName($table);
$sql = 'SELECT '.$fields.' FROM '.$tableName;
if ($where != null) {
$sql .= ' WHERE '.$where;
$sql .= ' WHERE '.$this->where($where);
}
if ($order != null) {
$sql .= ' ORDER BY '.$order;
@ -206,6 +208,51 @@ class Dm
return $data;
}
/**
* WHERE 条件组合
* @param $where
* @param $glue
* @return string
*/
protected function where($where,$glue = 'AND')
{
if (!is_array($where)) $where = [$where];
$where_conditions = [];
$allow_operator = array('>', '<', '<>', '!=', '>=', '<=', '+=', '-=');
foreach ($where as $condition) {
if (is_string($condition)) {
$where_conditions[] = $condition;
} elseif (is_array($condition)) {
foreach ($condition as $column_name => $value) {
if ($value === null) {
$where_conditions[] = '"'.$column_name.'"' . ' IS NULL';
} elseif (is_array($value)) {
$comparator = $value[0];
if (in_array($comparator,$allow_operator)) {
# 比较符查询 示例 $condition = ['field' => ['comparator','value']]
$where_conditions[] = '"'.$column_name.'"' . " {$comparator} '" . addslashes($value[1]) . "'";
} else {
# “IN”查询 示例: $condition = ['field' => ['value1','value2']];
$in_values = implode(",", array_map(function($val) {
return "'" . addslashes($val) . "'";
}, $value));
$where_conditions[] = '"'.$column_name.'"' . ' IN (' . $in_values . ')';
}
} elseif (is_string($value) && strpos($value, '%') !== false) {
# “LIKE”查询 示例:$condition = ['field' => '%value%'];
$where_conditions[] = '"'.$column_name.'"' . " LIKE '" . addslashes($value) . "'";
} else {
# 默认“=”查询
$where_conditions[] = '"'.$column_name.'"' . " = '" . addslashes($value) . "'";
}
}
}
}
return implode(" {$glue} ",$where_conditions);
}
/**
* 查询数据总数
* @param string $table // 查询表名
@ -218,7 +265,7 @@ class Dm
$sql = 'SELECT COUNT(*) as count FROM '.$tableName;
if ($where != null) {
$sql .= ' WHERE '.$where;
$sql .= ' WHERE '.$this->where($where);
}
$result = dm_exec($this->conn, $sql);

Loading…
Cancel
Save