bumip / json-schema-sql
将 json-schemas 转换为 SQL 创建表语句的辅助工具
dev-master / 1.4.x-dev
2020-09-08 11:18 UTC
Requires
- php: ^7.2
- justinrainbow/json-schema: ^5.0@dev
Requires (Dev)
- phpunit/phpunit: ^9
This package is not auto-updated.
Last update: 2024-10-03 07:00:30 UTC
README
此类将 json-schema 转换为有效的 SQL 表。已与 MySql 和 Sqlite 进行测试。对 @WebMaestroFr 的这个辅助工具表示衷心的感谢。
安装
只需运行
composer require bumip/json-schema-sql dev-master
PHP
正在测试 PHP 版本。删除 tests/database/dbtest.db 以开始测试
// Instanciate PDO $pdo = new \PDO("mysql:dbname=example;host=localhost;port=3306", "user", "password"); // Instanciate JSON_Schema_MySQL $sql_schema = new \Bumip\JsonSchema\JsonSchemaSql($pdo); // Or, generate table from a single .json file $sql_schema->createTableFromFile("path/to/schema.json"); ## Shell will be changed to a proper cli tool in the future. `php -q /path/to/json-schema-mysql/json-schema-mysql.php "mysql:dbname=example;host=localhost;port=3306" "user" "password" "/path/to/json/schema/directory"`
CRUD 类 未测试
提供了一个与数据库架构匹配的 CRUD 类。未测试。
require_once "path/to/json-schema-mysql/json-schema-crud.php"; // Instanciate PDO $pdo = new PDO("mysql:dbname=example;host=localhost;port=3306", "user", "password"); // Instanciate JSON_Schema_MySQL $crud = new Schema_Model($pdo, "path/to/json/schema/directory"); // Get model by id $model = $crud->my_schema($id); // Create model $model = $crud->my_schema->create([ "column_1" => "Value one", "column_2" => "Value two" ]); // Get model rows $models = $crud->my_schema->read([ "column_1" => "First filter", "column_2" => "Second filter" ], [ "order_by" => "date", "order" => "DESC", "limit" => 24, "page" => 0 ]); // Update model $model = $crud->my_schema->update($model->id[ "column_1" => "New value one", "column_2" => "New value two" ]); // Delete model $crud->my_schema->delete($model->id);
请随意操作。欢迎贡献。