MySQL 数据库连接类,继承 ginkgo\db\Connector
namespace ginkgo\db\connector;
use ginkgo\db\Connector;
class Mysql extends Connector {
// 继承的属性
public $config = array();
public $obj_builder;
public $obj_pdo;
public $obj_result;
protected static $instance;
protected $isConnect;
protected $mid;
protected $optDebugDump = false;
protected $paramType = array(
'bool' => PDO::PARAM_BOOL,
'int' => PDO::PARAM_INT,
'str' => PDO::PARAM_STR,
);
protected $_table = array();
protected $_tableTemp = array();
protected $_pk = array();
protected $_force = '';
protected $_distinct = false;
protected $_join = '';
protected $_where = '';
protected $_whereOr = array();
protected $_whereAnd = array();
protected $_paginate = array();
protected $_group = '';
protected $_order = '';
protected $_limit = '';
protected $_bind = array();
protected $_fetchSql = false;
// 方法
public insertId() : int
public table( string $table ) : object
public force( string $index ) : object
public join( string $table [, string $on [, string $type ]] ) : object
public where( mixed $where [, string $exp [, string $value [, string $param [, string $type ]]]] ) : object
public whereAnd( mixed $where [, string $exp [, string $value [, string $param [, string $type ]]]] ) : object
public whereOr( mixed $where [, string $exp [, string $value [, string $param [, string $type ]]]] ) : object
public group( mixed $field ) : object
public order( mixed $field, [ string $type = 'ASC' ] ) : object
public limit( [ int $limit = false [, int $length = false ]] ) : object
public find( [ mixed $field ] ) : mixed
public select( [ mixed $field [, bool $all = true ]] ) : mixed
public insert( mixed $field [, string $value [, string $param [, string $type ]]] ) : int
public update( mixed $field [, string $value [, string $param [, string $type ]]] ) : int
public delete() : int
public duplicate( [ mixed $field [, string $table ]] ) : int
public pagination( [ int $perpage = 0 [, mixed $current = 'get' [, string $pageparam = 'page' [, int $pergroup = 0 [, bool $reset = true ]]]]] ) : array
public count( [ mixed $field [, bool $reset = true ]] ) : int
public max( [ mixed $field ] ) : mixed
public min( [ mixed $field ] ) : mixed
public avg( [ mixed $field ] ) : mixed
public sum( [ mixed $field ] ) : mixed
public buildSql() : string
public getFields( [ string $type = '' [, string $table = '' ]] ) : array
public getTableInfo( [ string $type = '' [, string $table = '' ]] ) : mixed
public getTables( [ string $dbName = '' ] ) : array
private aggProcess( string $type, mixed $field [, bool $reset = true ] ) : mixed
private buildSelect( [ mixed $field ] ) : string
private buildInsert( mixed $field [, string $value [, string $param [, string $type ]]] ) : string
private buildUpdate( mixed $field [, string $value [, string $param [, string $type ]]] ) : string
private buildDelete() : string
private buildDuplicate( [ mixed $field [, string $table ]] ) : string
private buildWhere( [ bool $add_where = true ] ) : string
private buildAgg( string $type [, mixed $field ] ) : string
// 继承的方法
public static instance( [ array $config ] ) : object
public config( [ array $config ] )
public connect( [ array $config ] )
public exec( string $sql ) : int
public query( string $sql ) : object
public lastInsertId() : int
public prepare( string $sql [, mixed $bind [, mixed $value [, string $type ]]] ) : object
public execute( [ mixed $bind [, mixed $value [, string $type [, bool $reset = true ]]]] ) : object
public distinct( [ bool $bool = true ] ) : object
public paginate( [ int $perpage = 0 [, mixed $current = 'get' [, string $pageparam = 'page' [, int $pergroup = 0 ]]]] ) : object
public fetchSql( [ bool $bool = true ] ) : object
public getRowCount() : int
public getRow() : mixed
public getResult( [ bool $all = true [, int $type = PDO::FETCH_ASSOC ]] ) : mixed
public setModel( string $model )
public setTable( string $table )
public getTable() : string
public setPk( string $pk )
public getPk() : string
public bind( mixed $bind [, mixed $value [, string $type ]] ) : object
public resetSql()
protected __construct()
protected __clone()
protected fetchBind( string $sql, mixed $bind [, mixed $value [, string $type ]] ) : string
}
- | 权限 | 类型 | 描述 |
---|---|---|---|
继承的属性 | - | - | - |
$config |
public | array | 配置 |
$obj_builder |
public | object | 语句构造器实例 |
$obj_pdo |
public | object | PDO 实例 |
$obj_result |
public | object | PDOStatement 对象 |
$instance |
protected | object static | 本类的实例 |
$isConnect |
protected | bool | 是否连接标记 |
$mid |
protected | string | 模型 ID |
$optDebugDump |
protected | mixed | 调试配置 |
$_table |
protected | string | 数据表名 |
$_tableTemp |
protected | array | 临时数据表名(切换操作的数据表、对多表进行操作) |
$_pk |
protected | string | 主键 0.2.2 新增 |
$_force |
protected | string | 强制使用索引名 |
$_distinct |
protected | mixed | 是否不重复 |
$_join |
protected | string | join 语句 |
$_where |
protected | string | where 条件语句 |
$_whereOr |
protected | array | whereOr 语句数组 |
$_whereAnd |
protected | array | whereAnd 语句数组 |
$_paginate |
protected | array | 分页参数 |
$_group |
protected | string | group 语句 |
$_order |
protected | string | order 语句 |
$_limit |
protected | string | limit 语句 |
$_bind |
protected | array | 绑定参数数组 |
$_fetchSql |
protected | bool | 是否获取 sql 语句 |
$paramType |
protected | array | 默认参数类型 |
方法 | - | - | - |
insertId() | public | 取得插入 ID | |
table() | public | 切换数据表 | |
force() | public | 强制使用索引 | |
join() | public | join 语句处理 | |
where() | public | where 语句处理 | |
whereAnd() | public | whereAnd 语句处理 | |
whereOr() | public | whereOr 语句处理 | |
group() | public | group 语句处理 | |
order() | public | order 语句处理 | |
limit() | public | limit 语句处理 | |
find() | public | 读取一条记录 | |
select() | public | 执行 select 查询 | |
insert() | public | 插入记录 | |
update() | public | update 语句处理 | |
delete() | public | delete 语句处理 | |
duplicate() | public | 克隆数据 | |
pagination() | public | 统计分页 | |
count() | public | 记录数 | |
max() | public | 最大值 | |
min() | public | 最小值 | |
avg() | public | 平均值 | |
sum() | public | 计算和 | |
buildSql() | public | 构建 SQL 语句 | |
getFields() | public | 取得字段,0.2.2 新增 |
|
getTableInfo() | public | 取得表信息,0.2.2 新增 |
|
getTables() | public | 取得数据表列表,0.2.2 新增 |
|
aggProcess() | private | 聚合查询处理 | |
buildSelect() | private | 构建 select 语句 | |
buildInsert() | private | 构建 insert 语句 | |
buildUpdate() | private | 构建 update 语句 | |
buildDelete() | private | 构建 delete 语句 | |
buildDuplicate() | private | 构建克隆语句 | |
buildWhere() | private | 构建 where 语句 | |
buildAgg() | private | 构建聚语句 | |
继承的方法 | - | - | - |
instance() | public | static | 实例化方法 |
config() | public | 配置数据库 | |
connect() | public | 连接数据库 | |
exec() | public | 执行原生 SQL(一般用于 插入、更新 或者 删除) | |
query() | public | 执行原生 SQL(一般用于 查询) | |
lastInsertId() | public | 取得新插入的 ID | |
prepare() | public | 预处理 SQL 语句 | |
execute() | public | 执行预处理 SQL 语句 | |
distinct() | public | 是否查询不重复的记录 | |
paginate() | public | 分页 | |
fetchSql() | public | 是否获取 SQL 语句 | |
getRowCount() | public | 取得影响行数 | |
getRow() | public | 取得当前行数据 | |
getResult() | public | 取得结果 | |
setModel() | public | 设置模型名 | |
setTable() | public | 设置数据表名 | |
getTable() | public | 取得当前数据表名 | |
setPk() | public | 设置主键 0.2.2 新增 |
|
getPk() | public | 取得当前主键名 0.2.2 新增 |
|
bind() | public | 绑定参数 | |
resetSql() | public | 重置 SQL | |
fetchBind() | public | 取得绑定后 SQL(配合 fetchSql() 方法) |
|
__construct() | protected | 同 instance() | |
__clone() | protected | 克隆,无实际功能,仅供限制为单例模式使用 |
insertId()
取得新插入的 IDpublic function insertId() : int
参数
返回
table()
切换数据表public function table( string $table ) : object
参数
table
表名返回
force()
强制使用索引名public function force( string $index ) : object
参数
index
索引名返回
join()
join 语句处理public function join( string $table [, string $on [, string $type ]] ) : object
参数
table
join 的表on
on 条件type
join 类型
可能的值
值 | 类型 | 描述 |
---|---|---|
INNER(默认) | string | 内连接 |
LEFT | string | 左连接 |
RIGHT | string | 右连接 |
FULL | string | 全连接 |
返回
where()
where 语句处理public function where( mixed $where [, string $exp [, string $value [, string $param [, string $type ]]]] ) : object
whereAnd()
whereAnd 语句处理public function whereAnd( mixed $where [, string $exp [, string $value [, string $param [, string $type ]]]] ) : object
whereOr()
whereOr 语句处理public function whereOr( mixed $where [, string $exp [, string $value [, string $param [, string $type ]]]] ) : object
参数
where
字段名
支持两种类型:字符串、数组,为数组时表示批量条件
exp
表达式
当 where
为字符串时为必须,当 where
为数组时自动忽略。
value
条件值
当 where
为字符串时为必须,当 where
为数组时自动忽略。
param
参数名
当 where
为数组时自动忽略。
type
数据类型
为空自动判断,当 where
为数组时自动忽略。
可能的值
值 | 类型 | 描述 |
---|---|---|
str(默认) | string | 字符串 |
int | string | 整数 |
float | string | 浮点数 |
double | string | 数字 |
bool | string | 布尔值 |
返回
group()
group 语句处理public function group( mixed $field ) : object
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
order()
order 语句处理public function order( mixed $field, [ string $type = 'ASC' ] ) : object
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段排序type
排序类型返回
limit()
limit 语句处理public function limit( [ int $limit = false [, int $length = false ]] ) : object
参数
limit
偏离或长度length
长度返回
find()
读取一条记录public function find( [ mixed $field ] ) : mixed
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
select()
执行 select 查询public function select( [ mixed $field [, bool $all = true ]] ) : mixed
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段all
是否全部记录返回
insert()
插入记录public function insert( mixed $field [, string $value [, string $param [, string $type ]]] ) : int
参数
返回
update()
更新记录public function update( mixed $field [, string $value [, string $param [, string $type ]]] ) : int
参数
返回
delete()
删除记录public function delete() : int
参数
返回
duplicate()
克隆数据public function duplicate( [ mixed $field [, string $table ]] ) : int
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段table
目的表名返回
pagination()
统计分页public function pagination( [ int $perpage = 0 [, mixed $current = 'get' [, string $pageparam = 'page' [, int $pergroup = 0 [, bool $reset = true ]]]]] ) : array
参数
perpage
每页记录数:
数值型,为 0 表示采用系统默认设置
current
当前页码:
混合型,默认为 get
可能的值
值 | 描述 |
---|---|
get | 用 get 方法获取页码 |
post | 用 post 方法获取页码 |
整数 | 当前页码 |
pageparam
分页参数名:
字符型
pergroup
每组页数
数值型
reset
是否重置 SQL,如为 true,所有 $_名称
类型的属性都将被重置
返回
count()
记录数public function count( [ mixed $field [, bool $reset = true ]] ) : int
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段reset
是否重置 SQL,如为 true,所有 $_名称
类型的属性都将被重置返回
max()
最大值public function max( [ mixed $field ] ) : mixed
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
min()
最小值public function min( [ mixed $field ] ) : mixed
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
avg()
平均值public function avg( [ mixed $field ] ) : mixed
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
sum()
和public function sum( [ mixed $field ] ) : mixed
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
buildSql()
构建 SQL 语句public function buildSql() : string
参数
返回
getFields()
取得字段0.2.2
新增
public function getFields( [ string $type = '' [, string $table = '' ]] ) : array
参数
type
类型
可能的值
值 | 类型 | 描述 |
---|---|---|
空(默认) | 去掉前缀的表明 | |
full | string | 完整表名 |
table
指定表
返回
getTableInfo()
取得表信息0.2.2
新增
public function getTableInfo( [ string $type = '' [, string $table = '' ]] ) : mixed
参数
type
类型
可能的值
值 | 类型 | 描述 |
---|---|---|
空(默认) | 字段信息 | |
full_columns | string | 完整字段信息 |
index | string | 索引 |
fields | string | 完整字段列表 |
pk | string | 主键 |
table
指定表
返回
getTables()
取得数据表列表0.2.2
新增
public function getTables( [ string $dbName = '' ] ) : array
参数
dbName
指定数据库返回
aggProcess()
绑定处理(配合 fetchSql()
方法)private function aggProcess( string $type, mixed $field [, bool $reset = true ] ) : mixed
参数
type
类型
可能的值
值 | 类型 | 描述 |
---|---|---|
COUNT(默认) | string | 记录数 |
MAX | string | 最大值 |
MIN | string | 最小值 |
AVG | string | 平均值 |
SUM | string | 和 |
field
字段,可以使用数组和字符串,为数组时表示多个字段
reset
是否重置 SQL,如为 true,所有 $_名称
类型的属性都将被重置返回
buildSelect()
构建 select 语句private function buildSelect( [ mixed $field ] ) : string
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段返回
buildInsert()
构建 insert 语句private function buildInsert( mixed $field [, string $value [, string $param [, string $type ]]] ) : string
参数
返回
buildUpdate()
构建 update 语句private function buildUpdate( mixed $field [, string $value [, string $param [, string $type ]]] ) : string
参数
返回
buildDelete()
构建 delete 语句private function buildDelete() : string
参数
返回
buildDuplicate()
构建克隆语句private function buildDuplicate( [ mixed $field [, string $table ]] ) : string
参数
field
字段,可以使用数组和字符串,为数组时表示多个字段table
目的表名返回
buildWhere()
构建 where 语句private function buildWhere( [ bool $add_where = true ] ) : string
参数
add_where
是否添加 WHERE
返回
buildAgg()
构建聚合 SQL 语句private function buildAgg( string $type [, mixed $field ] ) : string
参数
type
类型
可能的值
值 | 类型 | 描述 |
---|---|---|
COUNT(默认) | string | 记录数 |
MAX | string | 最大值 |
MIN | string | 最小值 |
AVG | string | 平均值 |
SUM | string | 和 |
field
字段,可以使用数组和字符串,为数组时表示多个字段
返回
field
字段,可以使用数组和字符串,为数组时表示多个字段value
值
当 field
为字符串时为必须,当 field
为数组时自动忽略。
param
参数
当 field
为数组时自动忽略。
type
参数类型
可能的值
值 | 类型 | 描述 |
---|---|---|
str(默认) | string | 字符串 |
int | string | 整数 |
float | string | 浮点数 |
double | string | 数字 |
bool | string | 布尔值 |