taeluf / lildb
一个简单的PDO包装器,提供insert、update、delete、select等常见SQL语句的便捷方法
Requires (Dev)
- taeluf/cli: v0.1.x-dev
- taeluf/code-scrawl: v0.8.x-dev
- taeluf/tester: v0.3.x-dev
Suggests
- taeluf/cli: install the dev dependency to use the cli features
This package is auto-updated.
Last update: 2024-09-08 07:02:33 UTC
README
LilDb: 一个简单的PDO数据库包装器
使用包装函数简化常见SQL语句的样板代码。
本文档由 php/code-scrawl 生成。有关README模板文件,请参阅 .docsrc/README.src.md
功能
LilSql
是新的!请参阅 test/run/Sql.php 以获取示例!
LilDb
:为SELECT、CREATE、UPDATE、DELETE等常见SQL语句提供简单接口LilMigrations
:从版本化目录中轻松使用SQL迁移LilSql
:在sql文件中使用@query(some.name)
命名查询,并将它们序列化为键值数组以供后续使用LilOrm
:(Alpha版本)一个将数据数组映射到对象的极简ORM,具有魔法获取器和一些便捷方法
安装
composer require taeluf/lildb v0.1.x-dev
用法
实例化LilDb或LilMigration并调用您需要的函数。有关示例,请参阅 test/run/Tests.php 和 test/run/Migrations.php
示例(lildb + 迁移)
<?php
$db = $this->file('test/input/migrate/db.sqlite');
unlink($db);
$migrations_dir = $this->file('test/input/migrate/');
// init the database
$ldb = \Tlf\LilDb::sqlite($db);
$ldb->create('blog',
['title'=>'varchar(200)']
);
$ldb->insert('blog',['title'=>'one']);
// do the migration
$lm = new \Tlf\LilMigrations($ldb->pdo, $migrations_dir);
// $lm->migration_vars = ['some_var'=> 'some value']; # optionally expose variables to the migration files.
$lm->migrate(0,1);
// test that the table has been altered to have a 'description' field
$this->compare(
$ldb->select('blog')[0],
['title'=>'one',
'description'=>'',
],
);
迁移文件 test/input/migrate/v1/up.sql
ALTER TABLE blog
ADD COLUMN description TEXT;
class Tlf\LilDb
LilDb:一个用于创建、更新、选择和删除等常见SQL功能的小型数据库类
请参阅源代码 /code/LilDb.php
常量
属性
public \PDO $pdo;
一个pdo实例
方法
static public function new(string $user, string $password, string $db, $host='localhost')
使用pdo进行初始化的便捷方法static public function sqlite(string $dbName = ':memory:')
在内存中初始化sqlite数据库的便捷方法static public function mysql($dbName = ':memory:')
在内存中初始化mysql数据库的便捷方法public function __construct(\PDO $pdo)
使用数据库句柄进行初始化public function create(string $tableName, array $colDefinitions, bool $recreateIfExists=false)
如果不存在则创建新表。public function query(string $sql, array $binds=[])
执行Sql语句并返回行public function select(string $tableName, array $whereCols=[])
获取具有给定 $whereCols 的表中的行public function insert(string $table, array $row)
将行插入数据库
将数组值转换为jsonpublic function insertAll(string $table, array $rowSet)
public function update(string $table, array $newRowValues, string $idColumnName='id')
更新现有行。updateWhere()的缩写,其中id列作为where值设置。public function updateWhere(string $table, array $newRowValues, array $whereVals)
public function delete(string $table, array $whereCols)
从表中删除行public function execute(string $sql, array $binds=[])
执行Sql语句并返回PDOStatementpublic function exec(string $sql, array $binds=[])
execute()
的别名public function getPdo()
获取pdo对象public function pdo()
获取pdo对象static public function whereSqlFromCols(array $columns)
将键=>值数组转换为WHERE sql。static public function keysToBinds(array $keyedValues)
将数组['key'=>$val, ':key2'=>$val]
转换为绑定:[':key'=>$val, ':key2'=>$val]
。
class Tlf\LilMigrations
处理 SQL 迁移的最小类。创建一个迁移目录。然后创建类似于 v1
、v2
的目录,并在每个版本目录中创建 up.sql
、down.sql
文件。从 1 迁移到 2 将执行 v2/up.sql
。从 3 降级到 1 将执行 v2/down.sql
和 v1/down.sql
。您还可以创建 v1/up-1.sql
、v1/up-2.sql
等文件,按顺序执行多个文件。
您可以创建 .config/ldb.json
文件,包含相对的 'dir'
和 'db'
设置。
查看源代码:[/code/LilMigrations.php](https://gitlab.com/taeluf/php/lildb/-/blob/HEAD//code/LilMigrations.php)
常量
属性
public \PDO $pdo;
一个pdo实例public string $dir;
迁移脚本的目录。public array $migration_vars = [];
数组<string, mixed>
,包含要extract
并在up.php
和down.php
迁移文件中可用的变量。
方法
public function __construct(\PDO $pdo, string $dir)
在 $dir 中,应有名为 'v1'、'v2'、'v3' 等的目录。
在 v1/v2/v3 目录中,应有包含适用于您所用数据库的有效 SQL 语句的 up.sql 和 down.sql 文件。static public function sqlite(string $dbName = ':memory:')
在内存中初始化sqlite数据库的便捷方法public function migrate(int $old, int $new)
从旧版本迁移到新版本。public function run_migration_version($version, $up_or_down)
class Tlf\LilSql
简化将 SQL 存储在磁盘上的 sql 文件,并将这些 sql 文件转换为命令数组,然后进行序列化。
这是一个理想的构建工具,而不是运行时工具,因此您将 unserialize(file_get_contents(...))
来加载查询数组。
或 @query(some.name, delimiter)
如果您不想在分号处停止。
查看源代码:[code/LilSql.php](https://gitlab.com/taeluf/php/lildb/-/blob/HEAD//code/LilSql.php)
常量
属性
public $queries = [];
要使用/序列化的查询数组(在生产中应序列化并跳过此处理!)
方法
public function load_files(string $dir, string $namespace_prefix='')
将指定目录中的 sql 文件加载到查询中。public function serialize(string $file)
public function parse_sql(string $sql, string $prefix='', &$queries [])
将包含多个查询的 SQL 字符串转换为数组。所有查询都必须有显式的标识符。
class Tlf\LilOrm
最小的 ORM 实现。
查看源代码:[code/LilOrm.php](https://gitlab.com/taeluf/php/lildb/-/blob/HEAD//code/LilOrm.php)
常量
属性
public $_cache = [];
方法
public function __construct($row)
public function one($name, $id, $id_column='id')
获取具有给定 id 的对象。public function many($that, $id_column=null, $this_tablenull)
获取指向此项目的对象数组。