数据库¶
Sqlite的ORM模型
| 属性 | 值 |
|---|---|
| 命名空间 | fize\db\realization\sqlite |
| 类名 | Db |
| 修饰符 | abstract |
| 父类 | fize\db\definition\Db |
| 方法: |
|---|
| 方法名 | 说明 |
|---|---|
| limit() | 设置LIMIT,支持链式调用 |
| build() | 根据当前条件构建SQL语句 |
| __destruct() | 析构函数 |
| prototype() | 返回操作对象 |
| query() | 执行一个SQL语句并返回相应结果 |
| startTrans() | 开始事务 |
| commit() | 提交事务 |
| rollback() | 回滚事务 |
| lastInsertId() | 返回最后插入行的ID或序列值 |
| getLastSql() | 获取最后组装的SQL |
| distinct() | 指定distinct查询 |
| field() | 指定要查询的字段,支持链式调用 |
| prefix() | 设置表前缀 |
| table() | 指定当前要操作的表 |
| alias() | 对当前表设置别名 |
| group() | GROUP语句 |
| order() | 设置排序条件 |
| where() | 设置WHERE语句 |
| having() | HAVING语句 |
| join() | JOIN条件,可以使用所有JOIN变种 |
| innerJoin() | INNER JOIN条件 |
| leftJoin() | LEFT JOIN条件 |
| rightJoin() | RIGHT JOIN条件 |
| union() | UNION语句 |
| unionAll() | UNION ALL语句 |
| unionDistinct() | UNION DISTINCT语句 |
| insert() | 插入记录 |
| insertGetId() | 插入记录,并返回最后插入行的ID或序列值 |
| fetch() | 遍历当前结果集 |
| delete() | 删除记录 |
| update() | 更新记录 |
| select() | 执行查询,返回结果记录列表 |
| findOrNull() | 执行查询,获取单条记录 |
| find() | 执行查询,获取单条记录,如果未找到则抛出错误 |
| value() | 得到某个字段的值 |
| column() | 得到某个列的数组 |
| page() | 使用模拟的LIMIT语句进行简易分页,支持链式调用 |
| count() | COUNT查询 |
| sum() | SUM查询 |
| min() | MIN查询 |
| max() | MAX查询 |
| avg() | AVG查询 |
| setValue() | 设置数据 |
| setInc() | 字段值增长 |
| setDec() | 字段值减少 |
| paginate() | 完整分页,执行该方法可以获取到分页记录、完整记录数、总页数,可用于分页输出 |
| insertAll() | 批量插入记录 |
方法¶
limit()¶
设置LIMIT,支持链式调用
public function limit (
int $rows,
int $offset = null
) : $this
| 参数: |
|
|---|
build()¶
根据当前条件构建SQL语句
public function build (
string $action,
array $data = [],
bool $clear = true
) : string
| 参数: |
|
||||||||
|---|---|---|---|---|---|---|---|---|---|
| 返回值: | 最后组装的SQL语句 |
query()¶
执行一个SQL语句并返回相应结果
abstract public function query (
string $sql,
array $params = [],
callable $callback = null
) : array|int
| 参数: |
|
||||||||
|---|---|---|---|---|---|---|---|---|---|
| 返回值: | SELECT语句返回数组,其余返回受影响行数。 |
lastInsertId()¶
返回最后插入行的ID或序列值
abstract public function lastInsertId (
string $name = null
) : int|string
| 参数: |
|
|---|
getLastSql()¶
获取最后组装的SQL
public function getLastSql (
bool $real = false
) : string
| 参数: |
|
|---|
仅供日志使用的SQL语句,由于本身存在SQL危险请不要真正用于执行
distinct()¶
指定distinct查询
public function distinct (
bool $distinct = true
) : $this
| 参数: |
|
|---|
field()¶
指定要查询的字段,支持链式调用
public function field (
array|string $fields
) : $this
| 参数: |
|
|---|
table()¶
指定当前要操作的表
public function table (
string $name,
string $prefix = null
) : $this
| 参数: |
|
|---|
order()¶
设置排序条件
public function order (
array|string $field_order
) : $this
| 参数: |
|
|---|
where()¶
设置WHERE语句
public function where (
\fize\db\realization\sqlite\Query|array|string $statements,
array $parse = []
) : $this
| 参数: |
|
|---|
通常情况下,我们使用简洁方式来更简便地定义条件,对于复杂条件无法满足的,可以使用查询器或者直接使用预处理语句
having()¶
HAVING语句
public function having (
\fize\db\realization\sqlite\Query|array|string $statements,
array $parse = []
) : $this
| 参数: |
|
|---|
通常情况下,我们使用简洁方式来更简便地定义条件,对于复杂条件无法满足的,可以使用查询器或者直接使用预处理语句
join()¶
JOIN条件,可以使用所有JOIN变种
public function join (
string|array $table,
string $type = "JOIN",
string $on = null,
string $using = null
) : $this
| 参数: |
|
|---|
innerJoin()¶
INNER JOIN条件
public function innerJoin (
string|array $table,
string $on = null
) : $this
| 参数: |
|
|---|
leftJoin()¶
LEFT JOIN条件
public function leftJoin (
string|array $table,
string $on = null
) : $this
| 参数: |
|
|---|
rightJoin()¶
RIGHT JOIN条件
public function rightJoin (
string|array $table,
string $on = null
) : $this
| 参数: |
|
|---|
union()¶
UNION语句
public function union (
string $sql,
string $union_type = "UNION"
) : $this
| 参数: |
|
|---|
unionAll()¶
UNION ALL语句
public function unionAll (
string $sql
) : $this
| 参数: |
|
|---|
unionDistinct()¶
UNION DISTINCT语句
public function unionDistinct (
string $sql
) : $this
| 参数: |
|
|---|
insertGetId()¶
插入记录,并返回最后插入行的ID或序列值
public function insertGetId (
array $data,
string $name = null
) : int|string
| 参数: |
|
|---|
fetch()¶
遍历当前结果集
public function fetch (
callable $func
)
| 参数: |
|
|---|
由于少了一层循环和转化,fetch方法比select性能上略有提升,但不方便外部调用,特别是MVC等架构
select()¶
执行查询,返回结果记录列表
public function select (
bool $cache = true
) : array
| 参数: |
|
|---|
findOrNull()¶
执行查询,获取单条记录
public function findOrNull (
bool $cache = false
) : array
| 参数: |
|
||||
|---|---|---|---|---|---|
| 返回值: | 如果无记录则返回null |
find()¶
执行查询,获取单条记录,如果未找到则抛出错误
public function find (
bool $cache = false
) : array
| 参数: |
|
|---|
value()¶
得到某个字段的值
public function value (
string $field,
mixed $default = null,
bool $force = false
) : mixed
| 参数: |
|
||||||||
|---|---|---|---|---|---|---|---|---|---|
| 返回值: | 如果$force为true时则返回数字类型 |
page()¶
使用模拟的LIMIT语句进行简易分页,支持链式调用
public function page (
int $index,
int $prepg = 10
) : $this
| 参数: |
|
|---|
min()¶
MIN查询
public function min (
string $field,
bool $force = true
) : mixed
| 参数: |
|
||||||
|---|---|---|---|---|---|---|---|
| 返回值: | 如果$force为true时真返回数字类型 |
max()¶
MAX查询
public function max (
string $field,
bool $force = true
) : mixed
| 参数: |
|
||||||
|---|---|---|---|---|---|---|---|
| 返回值: | 如果$force为true时真返回数字类型 |
setValue()¶
设置数据
public function setValue (
mixed $field,
mixed $value
) : int
| 参数: |
|
||||||
|---|---|---|---|---|---|---|---|
| 返回值: | 返回受影响记录条数 |
setInc()¶
字段值增长
public function setInc (
string $field,
int $step = 1
) : int
| 参数: |
|
||||||
|---|---|---|---|---|---|---|---|
| 返回值: | 返回受影响记录条数 |
setDec()¶
字段值减少
public function setDec (
string $field,
int $step = 1
) : int
| 参数: |
|
||||||
|---|---|---|---|---|---|---|---|
| 返回值: | 返回受影响记录条数 |
paginate()¶
完整分页,执行该方法可以获取到分页记录、完整记录数、总页数,可用于分页输出
public function paginate (
int $page,
int $size = 10
) : array
| 参数: |
|
||||||
|---|---|---|---|---|---|---|---|
| 返回值: | [记录个数, 总页数、记录数组] |
insertAll()¶
批量插入记录
public function insertAll (
array $data_sets,
array $fields = null
) : int
| 参数: |
|
||||||
|---|---|---|---|---|---|---|---|
| 返回值: | 返回插入成功的记录数 |