safronik / db-wrapper
适用于不同关系数据库驱动程序(如PDO、Mysqli、WPDB、Drupal等)的包装器
0.1.1
2024-07-23 03:17 UTC
Requires
- php: >=8.0
- safronik/code-patterns: ^0.1.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-22 05:56:56 UTC
README
一个PHP库,包含数据库包装器以简化其使用
关于
本包包含数据库包装器,包括不同数据库的驱动程序
- PDO
mysqli(开发中)- WordPress
Joomla(开发中)Drupal(开发中)- ...
存在一个名为 "Extensions" 的命名空间,包含此数据库包装器的扩展。还有一些扩展以简化程序员的编程生活
- 查询构建器
- 对表的操作
- 输入数据的占位符(原生支持,您可以看到它)
- 服务器端预编译扩展(仅适用于PDO)
SQL Schema(开发中)
安装
首选的安装方法是使用Composer。运行以下命令安装包并将其添加到项目composer.json
中的要求
composer require safronik/db-warapper
或直接下载文件或克隆仓库(在这种情况下,您应该担心自动加载器)
使用方法
创建连接
如果您使用PDO(您没有任何现成的连接)
$db = DB::getInstance( new \Safronik\DB\DBConfig([ 'driver' => 'pdo', 'username' => 'root', 'password' => 'root', 'hostname' => 'localhost', // or could be a container name if you are using Docker ]) );
现有PDO连接
global $some_pdo_connection_object; // should be an instanceof \PDO $db = DB::getInstance( new \Safronik\DB\DBConfig([ 'driver' => 'pdo', 'connection' => $some_pdo_connection_object, ]) );
因为默认情况下驱动程序是PDO,所以这也应该起作用
global $some_pdo_connection_object; // should be an instanceof \PDO $db = DB::getInstance( new \Safronik\DB\DBConfig([ 'connection' => $some_pdo_connection_object, ]) );
对于WordPress
$global $wpdb; $db = DB::getInstance( new \Safronik\DB\DBConfig([ 'driver' => 'wpdb', 'connection' => $wpdb, ]) );
原始查询
$rows_affected = $db->query( 'DELETE FROM some_table LIMIT 10' );
$query_result = $db ->query( 'SELECT * FROM some_table' ) // Query already executed at this point ->fetchAll(); // Fetching the result
查询构建器
使用流式(瀑布)接口的构建器
Select
允许的方法
- table
- columns
- join(以下为Join描述)
groupBy(开发中)having(开发中)- orderBy
- limit
- with(以下为CTE描述)
- run
$db ->select('users') ->orderBy('register_date', 'desc') ->limit(10) ->run();
Insert
允许的方法
- columns
- ignore
- values
- onDuplicateKey
- run
$values = [ 'some' => 'thing', 'another' => 'stuff', ] $db ->insert( 'some_entity' ) ->columns( array_keys( $values ) ) ->values( $values ) ->onDuplicateKey( 'update', $values ) ->run();
Update
允许的方法
- set
- where
- and
- or
- run
$db ->update( 'options' ) ->set( [ 'option_value' => $value ] ) ->where([ 'option_name' => $option, 'affiliation' => $group, ]) ->and([ 'something' => 'different']) ->or( ['another' => 'example']) ->run()
Delete
允许的方法
- where
- and
- or
- orderBy
- limit
- run
$db ->update( 'options' ) ->set( [ 'option_value' => $value ] ) ->where([ 'option_name' => $option, 'affiliation' => $group, ]) ->run()
Join(仅作为Select语句的一部分)
- 支持通过第二个参数传递的左连接、右连接和内部连接
- 连接运算符支持 <=> | != | > | < | >= | <= 但并不确定 =D
- 如果没有指定,将选择连接表中的所有列
$db ->select('users') ->join( [ [ 'table_name_or_alias', 'id' ], '=', // <=> | != | > | < | >= | <= [ 'table_name_of_second_table_or_alias_2', 'some_id' ], ], 'left', // right | left | inner ['some_id', 'another_column', 'some_other_column'] // list of columns you want to join ) ->limit(10) ->run();
CTE(公共表表达式)
当您调用with()时,应该传递$db->cte()内部
因此,所有3种方法(cte()
、anchor()
和recursive()
)都应该被调用
cte()
设置公共表表达式的名称。您可以将它视为表名。
anchor()
是根选择表达式
recursive()
递归表达式
任何选择表达式都可以是任何难度的级别,使用连接、排序和其他
$db ->select( 'cte' ) ->with( $db ->cte( 'cte' ) ->anchor( 'SELECT * FROM some_table WHERE id = 1' ) ->recursive( ' SELECT some_table.* FROM some_table, cte WHERE some_table.parent = cte.id' ) ) ) ->run();
您也可以在这些anchor
和recursive
表达式中使用查询构建器
$db ->select( 'cte' ) ->with( $db ->cte( 'cte' ) ->anchor( $db ->select( $block_table ) ->where( [ 'id' => 1 ] ) ) ->recursive( $db ->select( [ $block_table, 'cte' ]) ->columns( '*', $block_table ) ->where( [ [ [ $block_table, 'parent' ], '=', [ 'cte', 'id' ] ] ] ) ) ) ->run();
表操作
Exists
检查表是否存在
$db->isTableExists( 'table_name');
Drop
删除表
$db->dropTable( 'table_name');
Create
有一天我会添加文档
Alter
有一天我会添加文档