dzadsod/include/class/tsql.class.php

420 lines
13 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
defined('IN_MET') or exit('No permission');
# 数据结构处理
class tsql
{
private $sql = [];
private $m_name = M_NAME;
private $special = [];
private $ufield = [];
private $query;
private $multi_table = false; //采用分组才会采用子查询计算总数
public function __construct()
{
global $_M;
self::table_unset();
}
# 清空
public function table_unset()
{
global $_M;
unset($this->sql, $this->special, $this->ufield);
return $this;
}
# 数据库
# $sign false 不添加应用文件名前缀
public function table($tname, $sign = true)
{
global $_M;
self::table_unset();
$this->sql['table'] = self::full_table_name($tname, $sign);
return $this;
}
# 多表联合查询数据库,制作表的联合方式,通过多次调用来实现
# $tname 包含缩写数据名和 缩写名
# $join 数据库连接方式
# $sign false 不添加应用文件名前缀
#
public function tables($tname = [], $join = '', $sign = true)
{
global $_M;
list($name, $ab) = $tname;
$table = self::full_table_name($name, $sign);
$this->sql['table'] .= $table . " AS {$ab} {$join} ";
return $this;
}
# 需要特殊处理的值例如counts+1 等类似的 或者执行函数
# 传入的为特殊处理的值,并 非字段名
public function special($_special)
{
global $_M;
$this->special = is_array($_special) ? $_special : [$_special];
return $this;
}
# 需要删除的字段,仅支持约束条件下
# 目前仅用于adup()时的 unique() 方法
public function ufield($_field = '')
{
global $_M;
$this->ufield = is_array($_field) ? $_field : [$_field];
return $this;
}
# 查询字段
# 查询或者多条插入信息
public function qfield($_field = '*')
{
global $_M;
if (is_array($_field)) $_field = arrayto_string($_field, ',');
$this->sql['field'] = $_field;
return $this;
}
# 插入更新字段
# $sign 单条插入或者更新设置其他值查询或者多条插入信息时使用qfield()
public function field($_field = '')
{
global $_M;
if (is_array($_field)) $_field = self::field_arr($_field);
$this->sql['field'] = $_field;
return $this;
}
# 分组查询 $_having 同where 一样
public function group($_group = '', $_having = '')
{
global $_M;
if (is_array($_group)) $_group = arrayto_string($_group, ',');
if (!empty($_group)) $this->sql['group'] = " GROUP BY " . $_where;
if ($_group) {
$this->multi_table = true;
if (is_array($_having)) $_having = self::where_arr($_having);
if (!empty($_having)) $this->sql['group'] .= " HAVING " . $_having;
}
return $this;
}
# 查询条件
public function where($_where = '1=1')
{
global $_M;
if (is_array($_where)) $_where = self::where_arr($_where);
if (!empty($_where)) $this->sql['where'] = " WHERE " . $_where;
return $this;
}
# 排序条件
public function order($_order = 'id DESC')
{
global $_M;
$this->sql['order'] = " ORDER BY " . $_order;
return $this;
}
# 单独值,用于 多条信息插入,或者约束插入更新
public function values($_values = '')
{
global $_M;
if (is_array($_values)) $_values = self::values_arr($_values);
$this->sql['values'] = $_values;
return $this;
}
# 约束条件下的UPDATE
public function unique($_unique = '')
{
global $_M;
if (is_array($_unique)) {
$_unique = self::field_del($_unique);
$_unique = self::unique_arr($_unique);
}
$this->sql['unique'] = " ON DUPLICATE KEY UPDATE " . $_unique;
return $this;
}
# 查询条数限制
public function limit($_limit = '30', $_start = '0')
{
global $_M;
$this->sql['limit'] = " LIMIT {$_start}," . $_limit;
return $this;
}
# 删除
public function del($_where)
{
global $_M;
if (empty($this->sql['table'])) return false;
if (!empty($_where) && empty($this->sql['where'])) self::where($_where);
$this->query = "DELETE FROM {$this->sql['table']} {$this->sql['where']} ";
unset($this->sql);
DB::query($this->query);
return $this;
}
# 单条修改
public function upd()
{
global $_M;
if (empty($this->sql['table']) || empty($this->sql['field'])) return false;
$this->query = "UPDATE {$this->sql['table']} SET {$this->sql['field']} {$this->sql['where']} ";
unset($this->sql);
DB::query($this->query);
return $this;
}
# 单条新增
public function add($_field)
{
global $_M;
if (!empty($_field) && empty($this->sql['field'])) self::field($_field);
if (empty($this->sql['table']) || empty($this->sql['field'])) return false;
$this->query = "INSERT INTO {$this->sql['table']} SET {$this->sql['field']} ";
unset($this->sql);
DB::query($this->query);
return $this;
}
# 约束字段新增更新unique 值不存在即可实现多条信息插入
# field 字段需要查询类型的 qfield()
# $data 一维二维都可以key = field
# $sign false 并且也没有单独调用unique()是多条信息插入, true 为是约束更新插入
public function adup($data = [], $sign = true)
{
global $_M;
if (empty($this->sql['table'])) return false;
if (is_array($data)) {
$arr = array_level($data) == 1 ? $data : reset($data);
$qfield = array_keys($arr);
if (empty($this->sql['field'])) self::qfield($qfield);
if (empty($this->sql['values'])) self::values($data);
if ($sign == true && empty($this->sql['unique'])) {
$unique = [];
$arr = self::field_del($arr);
foreach ($arr as $key => $val) {
if (in_array($val, $this->special, true)) {
$unique[ $key ] = $val;
} else {
$unique[] = $key;
}
}
self::unique($unique);
}
}
$this->query = "INSERT INTO {$this->sql['table']} ({$this->sql['field']}) VALUES {$this->sql['values']} {$this->sql['unique']} ";
unset($this->sql);
DB::query($this->query);
return $this;
}
# 查询单条
public function one()
{
global $_M;
if (empty($this->sql['table'])) return false;
if (empty($this->sql['field'])) $this->sql['field'] = '*';
$this->query = "SELECT {$this->sql['field']} FROM {$this->sql['table']} {$this->sql['where']} {$this->sql['group']} {$this->sql['order']} {$this->sql['limit']} ";
unset($this->sql);
return DB::get_one($this->query);
}
# 查询更多条数
public function all($key = 'id')
{
global $_M;
if (empty($this->sql['table'])) return false;
if (empty($this->sql['field'])) $this->sql['field'] = '*';
$this->query = "SELECT {$this->sql['field']} FROM {$this->sql['table']} {$this->sql['where']} {$this->sql['group']} {$this->sql['order']} {$this->sql['limit']} ";
unset($this->sql);
$res = [];
$result = DB::query($this->query);
while ($val = DB::fetch_array($result)) {
$res[ $val[ $key ] ] = $val;
}
return $res;
}
# 获取表结构
public function show()
{
global $_M;
if (empty($this->sql['table'])) return false;
$this->query = "SHOW FULL COLUMNS FROM .{$this->sql['table']}";
unset($this->sql);
$res = [];
$rescolumns = DB::query($this->query);
while ($row = DB::fetch_array($rescolumns)) {
$res[] = $row;
}
return $res;
}
# 条数统计
# field 采用查询方法 qfield
# $multi_table 若是采用分组了计算方式要调整为 子查询计算总条数
public function count($_field = '*')
{
global $_M;
if (empty($this->sql['table'])) return 0;
if (empty($this->sql['field'])) $this->sql['field'] = "COUNT({$_field})";
$this->query = " SELECT {$this->sql['field']} FROM {$this->sql['table']} {$this->sql['where']} ";
if ($this->multi_table) $this->query = " SELECT count(*) FROM ($this->query) num ";
$result = DB::query($this->query);
$fetch_row = DB::fetch_row($result);
return $fetch_row[0];
}
# 获取刚才插入的信息ID
public function id()
{
global $_M;
return DB::insert_id();
}
# 错误信息(对查询是无效的)
public function error()
{
global $_M;
return DB::error();
}
# 返回完整数据库名
public function full_table_name($tname, $sign = true)
{
global $_M;
return $sign == true ? $_M['table'][ $this->m_name . '_' . $tname ] : $_M['table'][ $tname ];
}
# 字段删除方法
public function field_del($array = [])
{
global $_M;
foreach ($this->ufield as $val) {
unset($array[ $val ]);
}
return $array;
}
# where条件的转换
# 仅是值等于的处理,例如某个字段判断为不等于或者其他的方法,切勿使用数组传递
public function where_arr($arr)
{
global $_M;
$and = [];
foreach ($arr as $key => $val) {
// 函数的使用 数字键名采用函数模式
if (is_numeric($key)) {
$and[] = $val;
continue;
}
// 仅是值等于的处理
if (is_array($val)) {
$or = [];
foreach ($val as $ky => $kv) {
$or[] = " {$key} = '{$kv}' ";
// 函数的使用,非数字则采用函数模式
if (!is_numeric($ky)) {
$or[] = $val;
continue;
}
}
$orstr = arrayto_string($or, ' OR ');
$and[] = " ({$orstr}) ";
} else {
$and[] = " {$key} = '{$val}' ";
}
}
return arrayto_string($and, ' AND ');
}
# 入库字段转换
public function field_arr($arr)
{
global $_M;
$str = [];
foreach ($arr as $k => $v) {
if (strstr($v, "'")) $v = str_replace("'", "\'", $v);
if (in_array($v, $this->special, true)) {
$str[] = " {$k} = {$v} ";
} else {
$str[] = " {$k} = '{$v}' ";
}
}
return arrayto_string($str, ',');
}
# 单独值的字符串转换
public function values_arr($arr)
{
global $_M;
$str = [];
foreach ($arr as $val) {
if (is_array($val)) {
$str[] = self::values_arr($val);
} else {
if (in_array($val, $this->special, true)) {
$str[] = " $val ";
} else {
$str[] = " '$val' ";
}
}
}
$arrtostr = arrayto_string($str, ',');
return array_level($arr) == 1 ? "({$arrtostr})" : $arrtostr;
}
# 约束条件新增的语句更新值的规则(最后一段)
# 数组key值为字段名非数字 说明是要单独处理的值
public function unique_arr($arr)
{
global $_M;
$str = [];
foreach ($arr as $k => $v) {
if (is_numeric($k)) {
$str[] = " $v = VALUES($v) ";
} else {
$str[] = " $k = $v ";
}
}
return arrayto_string($str, ',');
}
# 通过数据库整理出该数据库的字段一般是净化form提交的字段避免一个一个组装
public function tfield($form, $tablename = '')
{
global $_M;
if (!empty($tablename)) {
if (is_array($tablename)) $tablename = [$tablename, true];
list($name, $sign) = array_values($tablename);
self::table($name, $sign);
}
$karr = DB::get_all("DESC {$this->sql['table']}");
foreach ($karr as $v) {
$arr[ $v['Field'] ] = $v['Field'];
}
#根据数据库字段提炼
return array_intersect_key($form, $arr);
}
# 查看刚才执行过的sql语句方便核查sql语句
public function query()
{
global $_M;
return $this->query;
}
}
?>