niirrty / niirrty.db
我的数据库库。支持MySQL、PgSQL和SQLite
0.6.2
2024-02-19 12:53 UTC
Requires
- php: >=8.1
- ext-json: *
- ext-mbstring: *
- ext-pdo: *
- niirrty/niirrty.core: ^0.6
- niirrty/niirrty.io: ^0.6
- niirrty/niirrty.io.vfs: ^0.6
- symfony/yaml: ~v3.4
This package is auto-updated.
Last update: 2024-09-19 14:08:17 UTC
README
Niirrty数据库库
查看示例了解用法。
示例
首先,您必须定义配置。
实际上可以通过三种不同的方式完成
- 通过PHP配置文件
- 通过YAML配置文件
- 通过PHP代码
1. 通过PHP配置文件
1.1. 配置文件
在这种情况下,您需要定义一个包含如下内容的PHP文件
<?php return [ 'type' => 'pgsql', 'host' => '127.0.0.1', #'port' => 5432, 'db' => 'db-name', 'charset' => 'UTF8', 'user' => 'db-username', 'password' => 'db-password' ];
- type: 定义驱动类型('pgsql' | 'mysql' | 'sqlite')
- host: 数据库主机或IP地址
- port: 数据库端口
- db: 应选择的数据库名称
- charset: 数据库连接字符集
- user: 数据库登录用户名
- password: 数据库登录密码
1.2. 使用方法
如何使用PHP配置文件
<?php include '../vendor/autoload.php'; use Niirrty\DB\Driver\Factory as DriverFactory; use Niirrty\DB\Connection as DbConnection; try { // Init the driver from above defined php config file $driver = DriverFactory::FromConfigFile( __DIR__ . '/driver-config.php' ); // Open a database connection with the defined driver $conn = new DbConnection( $driver ); // The example table is a simple users table // Check if the table is empty $sql = 'SELECT COUNT(*) FROM users'; $cnt = (int) $conn->fetchScalar( $sql, [], '0' ); if ( $cnt < 1 ) { // OK the table is empty => insert the first user $sql = "INSERT INTO `users` ( u_name, u_display_name, u_mail, u_pass, u_created, u_last_login, u_deleted ) VALUES( ?, ?, ?, ?, DEFAULT, NULL, NULL )"; $conn->exec( $sql, [ 'Administrator', 'John Who (Admin)', 'j.who@example.com', \hash( 'sha512', 'secret' ) ] ); } // We have a first user => select all but only use the first found record :-D $sql = ' SELECT u_id, u_name, u_display_name, u_mail, u_pass, u_created, u_last_login, u_deleted FROM users ORDER BY u_display_name'; $record = $conn->fetchRecord( $sql ); var_dump( $record ); } catch ( \Throwable $ex ) { echo $ex; }
2. 通过YAML文件格式
2.1. 配置文件
创建一个包含以下内容的文件,并保存为.yml
文件扩展名
type: pgsql host: 127.0.0.1 #port: 5432 db: db-name charset: utf8 user: db-username password: db-password
- type: 定义驱动类型('pgsql' | 'mysql' | 'sqlite')
- host: 数据库主机或IP地址
- port: 数据库端口
- db: 应选择的数据库名称
- charset: 数据库连接字符集
- user: 数据库登录用户名
- password: 数据库登录密码
2.2. 使用方法
<?php include '../vendor/autoload.php'; use Niirrty\DB\Driver\Factory as DriverFactory; use Niirrty\DB\Connection as DbConnection; try { // Init the driver from above defined php config file $driver = DriverFactory::FromConfigFile( __DIR__ . '/driver-config.php' ); // Open a database connection with the defined driver $conn = new DbConnection( $driver ); // Working with the connection… } catch ( \Throwable $ex ) { echo $ex; }
3. 通过纯PHP代码
3.1. PostgreSQL示例
<?php include dirname( __DIR__ ) . '/vendor/autoload.php'; use \Niirrty\DB\Driver\PgSQL as PgSQLDriver; use Niirrty\DB\Connection as DbConnection; try { $driver = ( new PgSQLDriver() ) ->setHost( '127.0.0.1' ) ->setDbName( 'db-name' ) ->setAuthUserName( 'db-username' ) ->setAuthPassword( 'db-password' ) ->setCharset( 'UTF8' ); $conn = new DbConnection( $driver ); // Working with the connection… } catch ( \Throwable $ex ) { echo $ex; }
3.2. MySQL示例
<?php include dirname( __DIR__ ) . '/vendor/autoload.php'; use \Niirrty\DB\Driver\MySQL as MySQLDriver; use Niirrty\DB\Connection as DbConnection; try { $driver = ( new MySQLDriver() ) ->setHost( '127.0.0.1' ) ->setDbName( 'db-name' ) ->setAuthUserName( 'db-username' ) ->setAuthPassword( 'db-password' ) ->setCharset( 'UTF8' ); $conn = new DbConnection( $driver ); // Working with the connection… } catch ( \Throwable $ex ) { echo $ex; }
3.2. SQLite示例
<?php include dirname( __DIR__ ) . '/vendor/autoload.php'; use \Niirrty\DB\Driver\SQLite as SQLiteDriver; use Niirrty\DB\Connection as DbConnection; try { $driver = ( new SQLiteDriver() ) ->setDb( __DIR__ . '/example.sqlite3' ); $conn = new DbConnection( $driver ); // Working with the connection… } catch ( \Throwable $ex ) { echo $ex; }