体育管项目脚本
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.

275 lines
6.9 KiB

<?php
namespace task\module\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 $sql;
public function __construct()
{
# 获取配置数据
$database = config('database');
$dm = $database['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);
$this->sql = $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);
$this->sql = $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);
$this->sql = $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);
$this->sql = $sql;
dm_free_result($result);
return $result;
}
/**
* 查询多条数据
* @param string $table // 查询表名
* @param string $where // 查询条件
* @param string $fields // 查询字段
* @param string $order // 查询排序
* @param string $limit // 查询条数
* @param string $group // 查询分组
* @return array // 返回数组
*/
public function select($table, $where = null, $fields = '*', $order = null, $limit = null, $group = null)
{
$tableName = $this->splitTableName($table);
$sql = 'SELECT '.$fields.' FROM '.$tableName;
if ($where != null) {
$sql .= ' WHERE '.$where;
}
if ($group != null) {
$sql .= ' GROUP BY '.$group;
}
if ($order != null) {
$sql .= ' ORDER BY '.$order;
}
if ($limit != null) {
$sql .= ' LIMIT '.$limit;
}
$result = dm_exec($this->conn, $sql);
$this->sql = $sql;
$data = array();
while ($row = dm_fetch_array($result)) {
$data[] = $row;
}
dm_free_result($result);
return $data;
}
/**
* 查询单条数据
* @param string $table // 查询表名
* @param string $where // 查询条件
* @param string $fields // 查询字段
* @param string $order // 查询排序
* @return array // 返回数组
*/
public function find($table, $where = null, $fields = '*', $order = null)
{
$tableName = $this->splitTableName($table);
$sql = 'SELECT '.$fields.' FROM '.$tableName;
if ($where != null) {
$sql .= ' WHERE '.$where;
}
if ($order != null) {
$sql .= ' ORDER BY '.$order;
}
$sql .= ' LIMIT 1';
$result = dm_exec($this->conn, $sql);
$this->sql = $sql;
$data = array();
while ($row = dm_fetch_array($result)) {
$data = $row;
}
dm_free_result($result);
return $data;
}
/**
* 查询数据总数
* @param string $table // 查询表名
* @param string $where // 查询条件
* @return array // 返回数组
*/
public function count($table, $where = null)
{
$tableName = $this->splitTableName($table);
$sql = 'SELECT COUNT(*) as count FROM '.$tableName;
if ($where != null) {
$sql .= ' WHERE '.$where;
}
$result = dm_exec($this->conn, $sql);
$this->sql = $sql;
$count = 0;
while ($row = dm_fetch_array($result)) {
$count = $row['COUNT'];
}
dm_free_result($result);
return $count;
}
/**
* 获取上次执行数据Sql
* @return mixed
*/
public function getLastSql()
{
return $this->sql;
}
/**
* @param $table
* @param $where
* @param $fields
* @param $order
* @param $limit
* @param $page
* @return array
*/
public function getPaginate($table, $where = null, $fields = '*', $order = null, $limit = 10, $page = 1)
{
# 查询条数
$pageLimit = ($page - 1) * $limit;
# 总数
$total = $this->count($table);
# 数据
$list = $this->select($table,$where,$fields,$order,$pageLimit.','.$limit);
# 计算总页数
$totalPages = ceil($total / $limit);
# 组合分页按钮
$pageData = $totalPages >= 2 ? get_paginate($totalPages,$page,'/?s=/admin/pass.pass/index') : '';
return [
'list' => $list ?: [],
'pageData' => $pageData,
];
}
}