宝体数据调用接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

156 lines
4.0 KiB

<?php
namespace app\common\dm;
class Dm
{
private $host = 'localhost:5236'; // IP 地址
private $port = '5236'; // 端口号
private $dbname = 'CXK'; // 数据库名
private $username = 'SYSDBA'; // 用户名
private $password = 'SYSDBA'; // 密码
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));
$values = "'" . implode("','", array_values($data)) . "'";
$tableName = $this->splitTableName($table);
$sql = 'INSERT INTO '.$tableName.'('.$fields.') VALUES ('.$values.')';
$res = dm_exec($this->conn, $sql);
dm_free_result($res);
return $res;
}
/**
* 更新数据
* @param string $table // 更新数据表名
* @param array $data // 更新数据
* @param string $where // 更新数据条件
* @return mixed
*/
public function update($table, $data, $where)
{
$set = array();
foreach ($data as $key => $value) {
$set[] = $key . '=' . "'{$value}'";
}
$tableName = $this->splitTableName($table);
$sql = 'UPDATE '.$tableName." SET " . implode(",", $set) . ' WHERE '.$where;
$res = dm_exec($this->conn, $sql);
dm_free_result($res);
return $res;
}
/**
* 删除数据
* @param string $table // 删除数据表名
* @param string $where // 删除数据条件
* @return mixed
*/
public function delete($table, $where)
{
$tableName = $this->splitTableName($table);
$sql = 'DELETE FROM '.$tableName.' WHERE '.$where;
$result = dm_exec($this->conn, $sql);
dm_free_result($result);
return $result;
}
/**
* 清空表
* @param string $table // 清空表名
* @return mixed
*/
public function truncate($table)
{
$tableName = $this->splitTableName($table);
$sql = 'truncate table '.$tableName;
$result = dm_exec($this->conn, $sql);
dm_free_result($result);
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)
{
$tableName = $this->splitTableName($table);
$sql = 'SELECT '.$fields.' FROM '.$tableName;
if ($where != null) {
$sql .= ' WHERE '.$where;
}
if ($order != null) {
$sql .= ' ORDER BY '.$order;
}
if ($limit != null) {
$sql .= ' LIMIT '.$limit;
}
$result = dm_exec($this->conn, $sql);
$data = array();
while ($row = dm_fetch_array($result)) {
$data[] = $row;
}
dm_free_result($result);
return $data;
}
}