Skip to content

Latest commit

 

History

History
1303 lines (798 loc) · 40.6 KB

odbc.rst

File metadata and controls

1303 lines (798 loc) · 40.6 KB

ODBC

ODBC方式PostgreSQL数据库模型类
属性
命名空间 fize\db\realization\pgsql\mode
类名 Odbc
父类 fize\db\realization\pgsql\Db
方法:
方法名 说明
__construct() 构造
__destruct() 析构时关闭ODBC
lastInsertId() 返回最后插入行的ID或序列值
limit() 设置LIMIT,支持链式调用
prototype() 返回当前使用的数据库对象原型,用于原生操作
query() 执行一个SQL语句并返回相应结果
startTrans() 开始事务
commit() 执行事务
rollback() 回滚事务
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() 批量插入记录

方法

__construct()

构造

public function __construct (

    string $host,

    string $user,

    string $pwd,

    string $dbname,

    string|int $port = "",

    string $driver = null

)
参数:

名称

说明

host

服务器地址

user

用户名

pwd

用户密码

dbname

数据库名

port

端口号,选填,PostgreSQL默认是5432

driver

指定ODBC驱动名称。

__destruct()

析构时关闭ODBC

public function __destruct ()

lastInsertId()

返回最后插入行的ID或序列值

public function lastInsertId (

    string $name = null

) : int|string
参数:

名称

说明

name

应该返回ID的那个序列对象的名称,该参数在PostgreSQL中必须指定

limit()

设置LIMIT,支持链式调用

public function limit (

    int $rows,

    int $offset = null

) : $this
参数:

名称

说明

rows

要返回的记录数

offset

要设置的偏移量

prototype()

返回当前使用的数据库对象原型,用于原生操作

public function prototype () : \Driver

query()

执行一个SQL语句并返回相应结果

public function query (

    string $sql,

    array $params = [],

    callable $callback = null

) : array|int
参数:

名称

说明

sql

SQL语句,支持原生的ODBC问号预处理

params

可选的绑定参数

callback

如果定义该记录集回调函数则不返回数组而直接进行循环回调

返回值:

SELECT语句返回数组,其余返回受影响行数。

startTrans()

开始事务

public function startTrans ()

commit()

执行事务

public function commit ()

rollback()

回滚事务

public function rollback ()

getLastSql()

获取最后组装的SQL

public function getLastSql (

    bool $real = false

) : string
参数:

名称

说明

real

是否返回最终SQL语句而非预处理语句

仅供日志使用的SQL语句,由于本身存在SQL危险请不要真正用于执行

distinct()

指定distinct查询

public function distinct (

    bool $distinct = true

) : $this
参数:

名称

说明

distinct

为true时表示distinct

field()

指定要查询的字段,支持链式调用

public function field (

    array|string $fields

) : $this
参数:

名称

说明

fields

要查询的字段组成的数组或者字符串,如果需要指定别名,则使用:别名=>实际名称

prefix()

设置表前缀

public function prefix (

    string $prefix

) : $this
参数:

名称

说明

prefix

前缀

table()

指定当前要操作的表

public function table (

    string $name,

    string $prefix = null

) : $this
参数:

名称

说明

name

表名

prefix

表前缀,默认为使用当前前缀

alias()

对当前表设置别名

public function alias (

    string $alias

) : $this
参数:

名称

说明

alias

别名

group()

GROUP语句

public function group (

    mixed $fields

) : $this
参数:

名称

说明

fields

要GROUP的字段字符串或则数组

order()

设置排序条件

public function order (

    array|string $field_order

) : $this
参数:

名称

说明

field_order

字符串原样,如果是数组(推荐),则形如字段=>排序

where()

设置WHERE语句

public function where (

    \Query|array|string $statements,

    array $parse = []

) : $this
参数:

名称

说明

statements

“Query对象”或者“查询数组”或者“WHERE子语句”,其中“WHERE子语句”支持原生的PDO问号预处理占位符;

parse

如果$statements是SQL预处理语句,则可以传递本参数用于预处理替换参数数组

通常情况下,我们使用简洁方式来更简便地定义条件,对于复杂条件无法满足的,可以使用查询器或者直接使用预处理语句

having()

HAVING语句

public function having (

    \Query|array|string $statements,

    array $parse = []

) : $this
参数:

名称

说明

statements

“QueryMysql对象”或者“查询数组”或者“WHERE子语句”,其中“WHERE子语句”支持原生的PDO问号预处理占位符;

parse

如果$statements是SQL预处理语句,则可以传递本参数用于预处理替换参数数组

通常情况下,我们使用简洁方式来更简便地定义条件,对于复杂条件无法满足的,可以使用查询器或者直接使用预处理语句

join()

JOIN条件,可以使用所有JOIN变种

public function join (

    string|array $table,

    string $type = "JOIN",

    string $on = null,

    string $using = null

) : $this
参数:

名称

说明

table

表名,是数组时是形如别名=>表名,且只能有一个元素,否则无效

type

JOIN形式,默认为JOIN

on

ON条件,建议ON条件单独开来

using

USING字段

innerJoin()

INNER JOIN条件

public function innerJoin (

    string|array $table,

    string $on = null

) : $this
参数:

名称

说明

table

表名,是数组时是形如别名=>表名,且只能有一个元素,否则无效

on

ON条件,建议ON条件单独开来

leftJoin()

LEFT JOIN条件

public function leftJoin (

    string|array $table,

    string $on = null

) : $this
参数:

名称

说明

table

表名,是数组时是形如别名=>表名,且只能有一个元素,否则无效

on

ON条件,建议ON条件单独开来

rightJoin()

RIGHT JOIN条件

public function rightJoin (

    string|array $table,

    string $on = null

) : $this
参数:

名称

说明

table

表名,是数组时是形如别名=>表名,且只能有一个元素,否则无效

on

ON条件,建议ON条件单独开来

union()

UNION语句

public function union (

    string $sql,

    string $union_type = "UNION"

) : $this
参数:

名称

说明

sql

要UNION的SQL语句

union_type

类型,可选值UNION、UNION ALL、UNION DISTINCT,默认UNION

unionAll()

UNION ALL语句

public function unionAll (

    string $sql

) : $this
参数:

名称

说明

sql

要UNION ALL的SQL语句

unionDistinct()

UNION DISTINCT语句

public function unionDistinct (

    string $sql

) : $this
参数:

名称

说明

sql

要UNION DISTINCT的SQL语句

insert()

插入记录

public function insert (

    array $data

) : int
参数:

名称

说明

data

数据

返回值:

返回受影响行数

insertGetId()

插入记录,并返回最后插入行的ID或序列值

public function insertGetId (

    array $data,

    string $name = null

) : int|string
参数:

名称

说明

data

数据

name

序列名

fetch()

遍历当前结果集

public function fetch (

    callable $func

)
参数:

名称

说明

func

遍历函数

由于少了一层循环和转化,fetch方法比select性能上略有提升,但不方便外部调用,特别是MVC等架构

delete()

删除记录

public function delete () : int
返回值:返回受影响记录条数

update()

更新记录

public function update (

    array $data

) : int
参数:

名称

说明

data

要设置的数据

返回值:

返回受影响记录条数

select()

执行查询,返回结果记录列表

public function select (

    bool $cache = true

) : array
参数:

名称

说明

cache

是否使用搜索缓存,默认true

findOrNull()

执行查询,获取单条记录

public function findOrNull (

    bool $cache = false

) : array
参数:

名称

说明

cache

是否使用搜索缓存,默认false

返回值:

如果无记录则返回null

find()

执行查询,获取单条记录,如果未找到则抛出错误

public function find (

    bool $cache = false

) : array
参数:

名称

说明

cache

是否使用搜索缓存,默认false

value()

得到某个字段的值

public function value (

    string $field,

    mixed $default = null,

    bool $force = false

) : mixed
参数:

名称

说明

field

字段名

default

默认值

force

强制转为数字类型

返回值:

如果$force为true时则返回数字类型

column()

得到某个列的数组

public function column (

    string $field

) : array
参数:

名称

说明

field

字段名

page()

使用模拟的LIMIT语句进行简易分页,支持链式调用

public function page (

    int $index,

    int $prepg = 10

) : $this
参数:

名称

说明

index

页码

prepg

每页记录数量

count()

COUNT查询

public function count (

    string $field = "*"

) : int
参数:

名称

说明

field

字段名

sum()

SUM查询

public function sum (

    string $field

) : int
参数:

名称

说明

field

字段名

min()

MIN查询

public function min (

    string $field,

    bool $force = true

) : mixed
参数:

名称

说明

field

字段名

force

强制转为数字类型

返回值:

如果$force为true时真返回数字类型

max()

MAX查询

public function max (

    string $field,

    bool $force = true

) : mixed
参数:

名称

说明

field

字段名

force

强制转为数字类型

返回值:

如果$force为true时真返回数字类型

avg()

AVG查询

public function avg (

    string $field

) : mixed
参数:

名称

说明

field

字段名

setValue()

设置数据

public function setValue (

    mixed $field,

    mixed $value

) : int
参数:

名称

说明

field

字段名

value

字段值,数组为原样语句写入,其余为值写入

返回值:

返回受影响记录条数

setInc()

字段值增长

public function setInc (

    string $field,

    int $step = 1

) : int
参数:

名称

说明

field

字段名

step

增长值,默认为1

返回值:

返回受影响记录条数

setDec()

字段值减少

public function setDec (

    string $field,

    int $step = 1

) : int
参数:

名称

说明

field

字段名

step

增长值,默认为1

返回值:

返回受影响记录条数

paginate()

完整分页,执行该方法可以获取到分页记录、完整记录数、总页数,可用于分页输出

public function paginate (

    int $page,

    int $size = 10

) : array
参数:

名称

说明

page

页码

size

每页记录数量,默认每页10个

返回值:

[记录个数, 总页数、记录数组]

insertAll()

批量插入记录

public function insertAll (

    array $data_sets,

    array $fields = null

) : int
参数:

名称

说明

data_sets

数据集

fields

可选参数$fields用于指定要插入的字段名数组,这样参数$data_set的元素数组就可以不需要指定键名,方便输入

返回值:

返回插入成功的记录数