MySQL 数据库 SQL 语句构造器
namespace ginkgo\db\builder;
use ginkgo\db\Builder;
class Mysql extends Builder {
// 属性
private $exp = array(
'EQ' => '=',
'NEQ' => '<>',
'GT' => '>',
'EGT' => '>=',
'LT' => '<',
'ELT' => '<=',
'LIKE' => 'LIKE',
'NOTLIKE' => 'NOT LIKE',
'NOT LIKE' => 'NOT LIKE',
'IN' => 'IN',
'NOTIN' => 'NOT IN',
'NOT IN' => 'NOT IN',
'BETWEEN' => 'BETWEEN',
'NOTBETWEEN' => 'NOT BETWEEN',
'NOT BETWEEN' => 'NOT BETWEEN',
'EXP' => 'EXP',
);
private $compopr = array(
'=', '<>', '>', '>=', '<', '<='
);
private $logic = array(
'AND', 'OR'
);
private $order = array(
'DESC', 'ASC'
);
private $join = array(
'INNER', 'LEFT', 'RIGHT', 'FULL'
);
// 继承的属性
public $config = array();
protected static $instance;
private $configThis = array(
'type' => 'mysql',
'host' => '',
'name' => '',
'user' => '',
'pass' => '',
'charset' => 'utf8',
'prefix' => 'ginkgo_',
'debug' => false,
'port' => 3306,
);
// 方法
public field( mixed $field ) : string
public insert( mixed $field [, string $value [, string $param [, string $type ]]] ) : array
public update( mixed $field [, string $value [, string $param [, string $type [, string $from = 'update' ]]]] ) : array
public table( string $table ) : string
public force( string $index ) : string
public join( mixed $join [, string $on [, string $type ]] ) : string
public where( mixed $where [, string $exp [, string $value [, string $param [, string $type ]]]] ) : array
public group( mixed $field ) : string
public order( mixed $field [, string $type ] ) : string
public limit( [ int $limit = false [, int $length = false ]] ) : string
public addChar( string $value ) : string
public fieldProcess( string $field ) : string
protected __construct()
protected __clone()
private inArrayProcess( string $name [, string $type = 'order' ] ) : string
private expProcess( string $name ) : string
private updateProcess( mixed $field [, string $param [, string $value [, string $from = 'update' ]]] ) : string
private joinProcess( string $join [, string $on [, string $type = 'INNER' ]] ) : string
private onProcess( [ string $fidle_1 [, string $compopr [, string $fidle_2 ]]] ) : string
private whereProcess( string $field [, string $exp = '=' [, string $param [, string $value ]]] ) : string
private whereProcessSub( string $field [, string $exp = '=' [, string $param [, string $value ]]] ) : string
private orderProcess( string $field [, string $type = 'ASC' ] ) : string
private bindProcess( string $bind, string $value [, string $from [, string $param [, string $type [, string $exp [, array $return ]]]]] ) : array
private isSpecBind( string $value, string $exp ) : bool
private paramChar( string $param [, bool $is_sql = true [, string $from ]] ) : string
// 继承的方法
public instance( [ array $config ] ) : object
public config( array $config )
}
- | 权限 | 类型 | 描述 |
---|---|---|---|
属性 | - | - | - |
$exp |
private | array | SQL 操作符 |
$compopr |
private | array | 比较运算符 |
$logic |
private | array | 逻辑运算符 |
$order |
private | array | 排序方式 |
$join |
private | array | join 方式 |
继承的属性 | - | - | - |
$config |
public | array | 配置 |
$instance |
protected | object static | 本类的实例 |
$configThis |
private | array | 默认配置 |
方法 | - | - | - |
field() | public | 处理字段 | |
insert() | public | 构建 insert 语句 | |
update() | public | 构建 update 语句 | |
table() | public | 处理表名 | |
force() | public | 处理强制索引 | |
join() | public | 构建 join 命令 | |
where() | public | 构建 where 语句 | |
group() | public | 构建 group 命令 | |
order() | public | 构建 order 语句 | |
limit() | public | 构建 limit 语句 | |
addChar() | public | 添加 SQL 语句名称界定符 | |
fieldProcess() | public | 字段名处理 | |
inArrayProcess() | private | 命令合法性处理 | |
expProcess() | private | 运算符合法性处理 | |
updateProcess() | private | 处理 update 语句 | |
joinProcess() | private | 处理 join 语句 | |
onProcess() | private | join 语句的 on 命令处理 | |
whereProcess() | private | 处理 where 语句 | |
whereProcessSub() | private | 处理 where 子语句 | |
orderProcess() | private | 排序语句处理 | |
bindProcess() | private | 绑定处理 | |
isSpecBind() | private | 是否特殊绑定 | |
paramChar() | private | 绑定参数名处理 | |
继承的方法 | - | - | - |
instance() | public | static | 实例化 |
config() | public | 设定配置 | |
__construct() | protected | 同 instance() | |
__clone() | protected | 克隆,无实际功能,仅供限制为单例模式使用 |
$exp
SQL 操作符private $exp;
结构
名称 | 类型 | 值 | 描述 |
---|---|---|---|
EQ | string | = | 等于 |
NEQ | string | <> | 不等于 |
GT | string | > | 大于 |
EGT | string | >= | 大于等于 |
LT | string | < | 小于 |
ELT | string | <= | 小于等于 |
LIKE | string | LIKE | 模糊匹配 |
NOTLIKE | string | NOT LIKE | 模糊不匹配 |
NOT LIKE | string | NOT LIKE | 模糊不匹配 |
IN | string | IN | 在范围内 |
NOTIN | string | NOT IN | 不在范围内 |
NOT IN | string | NOT IN | 不在范围内 |
BETWEEN | string | BETWEEN | 在两者之间 |
NOTBETWEEN | string | NOT BETWEEN | 不在两者之间 |
NOT BETWEEN | string | NOT BETWEEN | 不在两者之间 |
EXP | string | EXP | 原生表达式 |
$compopr
比较运算符private $compopr;
结构
名称 | 类型 | 值 | 描述 |
---|---|---|---|
0 | string | = | 等于 |
1 | string | <> | 不等于 |
2 | string | > | 大于 |
3 | string | >= | 大于等于 |
4 | string | < | 小于 |
5 | string | <= | 小于等于 |
$logic
逻辑运算符private $logic;
结构
名称 | 类型 | 值 | 描述 |
---|---|---|---|
0 | string | AND | 且 |
1 | string | OR | 或 |
$order
排序方式private $order;
结构
名称 | 类型 | 值 | 描述 |
---|---|---|---|
0 | string | DESC | 倒序 |
1 | string | ASC | 顺序 |
$join
join 方式private $join;
结构
名称 | 类型 | 值 | 描述 |
---|---|---|---|
0 | string | INNER | 内连接 |
1 | string | LEFT | 左连接 |
2 | string | RIGHT | 右连接 |
3 | string | FULL | 全连接 |
field()
处理字段public function field( mixed $field ) : string
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
insert()
构建 insert 语句public function insert( mixed $field [, string $value [, string $param [, string $type ]]] ) : array
参数
返回
处理后的参数,结构
值 | 描述 |
---|---|
insert | insert 语句 |
bind | 绑定参数 |
update()
构建 update 语句public function update( mixed $field [, string $value [, string $param [, string $type [, string $from = 'update' ]]]] ) : array
参数
返回
处理后的参数,结构
值 | 描述 |
---|---|
update | update 语句 |
bind | 绑定参数 |
table()
处理表名public function table( string $table ) : string
参数
table
表名返回
force()
处理强制索引名public function force( string $index ) : string
参数
index
索引名返回
join()
构建 join 语句public function join( mixed $join [, string $on [, string $type ]] ) : string
参数
返回
where()
构建 where 语句public function where( mixed $where [, string $exp [, string $value [, string $param [, string $type ]]]] ) : array
参数
where
where 条件exp
运算符value
值param
参数type
参数类型
可能的值
值 | 类型 | 描述 |
---|---|---|
str(默认) | string | 字符串 |
int | string | 整数 |
float | string | 浮点数 |
double | string | 数字 |
bool | string | 布尔值 |
返回
处理后的参数,结构
值 | 描述 |
---|---|
where | where 语句 |
bind | 绑定参数 |
group()
构建 group 命令public function group( mixed $field ) : string
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
order()
构建 order 语句public function order( mixed $field [, string $type ] ) : string
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段type
类型
可能的值
| 值 | 类型 | 描述 | | ASC(默认) | string | ASC | 顺序 | | DESC | string | DESC | 倒序 |
返回
limit()
构建 limit 语句public function limit( [ int $limit = false [, int $length = false ]] ) : string
参数
limit
偏离或长度length
长度返回
addChar()
添加 SQL 名称界定符public function addChar( string $value ) : string
参数
value
名称返回
fieldProcess()
字段名处理public function fieldProcess( string $field ) : string
参数
field
字段名返回
inArrayProcess()
命令合法性处理,到属性规定的数组中过滤一遍private function inArrayProcess( string $name [, string $type = 'order' ] ) : string
参数
name
命令type
类型
可能的值
值 | 描述 |
---|---|
order(默认) | 排序方式 |
logic | 逻辑运算符 |
join | join 方式 |
compopr | 比较运算符 |
返回
expProcess()
运算符合法性处理private function expProcess( string $name ) : string
参数
name
运算符返回
updateProcess()
处理 update 语句private function updateProcess( mixed $field [, string $param [, string $value [, string $from = 'update' ]]] ) : string
参数
返回
joinProcess()
处理 join 语句private function joinProcess( string $join [, string $on [, string $type = 'INNER' ]] ) : string
参数
返回
onProcess()
join 语句的 on 命令处理private function onProcess( [ string $fidle_1 [, string $compopr [, string $fidle_2 ]]] ) : string
参数
fidle_1
字段 1compopr
运算符fidle_2
字段 2返回
whereProcess()
处理 where 语句private function whereProcess( string $field [, string $exp = '=' [, string $param [, string $value ]]] ) : string
参数
field
字段exp
运算符param
参数value
值返回
whereProcessSub()
处理 where 子语句private function whereProcessSub( string $field [, string $exp = '=' [, string $param [, string $value ]]] ) : string
参数
field
字段exp
运算符param
参数value
值返回
orderProcess()
排序语句处理private function orderProcess( string $field [, string $type = 'ASC' ] ) : string
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段type
类型
可能的值
| 值 | 类型 | 描述 | | ASC(默认) | string | ASC | 顺序 | | DESC | string | DESC | 倒序 |
返回
bindProcess()
绑定处理private function bindProcess( string $bind, string $value [, string $from [, string $param [, string $type [, string $exp [, array $return ]]]]] ) : array
参数
返回
isSpecBind()
是否特殊绑定private function isSpecBind( string $value, string $exp ) : bool
参数
value
值exp
运算符返回
paramChar()
绑定参数名处理private function paramChar( string $param [, bool $is_sql = true [, string $from ]] ) : string
参数
param
参数is_sql
是否为 SQL 命令from
来源返回
field
字段,可以使用数组和字符串,为数组时表示多个字段value
值
当 field
为字符串时为必须,当 field
为数组时自动忽略。
param
参数
当 field
为数组时自动忽略。
type
参数类型
可能的值
值 | 类型 | 描述 |
---|---|---|
str(默认) | string | 字符串 |
int | string | 整数 |
float | string | 浮点数 |
double | string | 数字 |
bool | string | 布尔值 |
join
join 表名on
on 条件type
join 类型
可能的值
值 | 类型 | 描述 |
---|---|---|
INNER(默认) | string | 内连接 |
LEFT | string | 左连接 |
RIGHT | string | 右连接 |
FULL | string | 全连接 |