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|array $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 '.$this->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 '.$this->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 '.$this->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 '.$this->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; } /** * WHERE 条件组合 * @param $where * @param $glue * @return string */ protected function where($where,$glue = 'AND') { $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 // 查询表名 * @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 '.$this->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, ]; } public function dmPDOFind($table,array $where) { try { $database = config('database'); $dm_config = $database['connections']['dm']; $dsn = "dm:host={$dm_config['hostname']};port={$dm_config['hostport']};dbname={$dm_config['database']}"; $connection = new \PDO($dsn, $dm_config['username'], $dm_config['password']); $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $tableName = $this->splitTableName($table); $fields = array_keys($where); $whereSqlArr = []; foreach ($fields as $field) $whereSqlArr[] = '"'.$field.'"' . ' = ' . '?'; $whereSql = implode(" and ",$whereSqlArr); $sql = "SELECT * FROM {$tableName} WHERE " . $whereSql; // 准备预编译语句 $statement = $connection->prepare($sql); $statement->bindParam(1, $where['user_name']); $statement->bindParam(2, $where['password']); // 执行查询 $statement->execute(); $data = []; while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) { $data = $row; } if (empty($data)) return ['status' => false, 'msg' => '失败']; return ['status' => true, 'msg' => '成功','data' => $data]; } catch (\PDOException $e) { return ['status' => false, 'msg' => $e->getMessage()]; } } }