rtshome/pgbabylon
Composer 安装命令:
composer require rtshome/pgbabylon
包简介
PDO class extension with helpers for special PostgreSQL types
README 文档
README
PgBabylon is a user-land extension to PHP PDO and PDOStatement native classes that helps to deal with PostgreSQL types like JSON, Arrays, etc.
It provides conversion between PHP types and PostgreSQL types and simplifies usage of SQL clauses like IN.
// Example: insert a PHP array in a PostgreSQL JSON field $arr = [ "name" => "Mark", "age" => 39, "address" => "White street, 23", "town" => "London" ]; $s = $pdo->prepare("INSERT INTO my_table(json) VALUES (:json)"); $s->execute([ ":json" => \PgBabylon\DataTypes\JSON($arr) ]);
Getting Started
PgBabylon can be installed using composer:
composer require rtshome/pgbabylon
PgBabylon currently supports the following PostgreSQL datatypes:
- Date (PHP DateTime)
- Timestamp (PHP DateTime)
- JSON (PHP Array)
- text, int and float single dimension arrays (PHP Array)
These types can also be used with the IN operator (see example below).
-- Sample table CREATE TABLE person( id SERIAL NOT NULL PRIMARY KEY, data JSON NOT NULL, insertion_date DATE NOT NULL );
<?php use PgBabylon\PDO; use PgBabylon\DataTypes; use PgBabylon\Operators; $pdo = new PDO("pgsql:dbname=testdb;host=127.0.0.1;user=myuser;pass=mypasswd"); $s = $pdo->prepare("INSERT INTO person(data, insertion_date) VALUES (:person, :ins_date) RETURNING *"); $person = [ "name" => "Mark", "age" => 39, "address" => "White street, 23", "town" => "London" ]; $s->execute([ ':person' => DataTypes\JSON($person), ':ins_date' => DataTypes\Date(new DateTime()) ]); // PgBabylon\PDOStatement::setColumnTypes() is the method that makes PgBabylon to recognize and convert from Pgsql types $s->setColumnTypes([ 'data' => PDO::PARAM_JSON, 'insertion_date' => PDO::PARAM_DATE ]); $r = $s->fetch(PDO::FETCH_ASSOC); var_dump($r); /* var_dump output array(3) { 'id' => int(1) 'data' => array(4) { 'name' => string(4) "Mark" 'age' => int(39) 'address' => string(16) "White street, 23" 'town' => string(6) "London" } 'insertion_date' => class DateTime#4 (3) { public $date => string(26) "2015-10-04 00:00:00.000000" public $timezone_type => int(3) public $timezone => string(11) "Europe/Rome" } } */ $s = $pdo->prepare("SELECT data FROM person WHERE id IN :person_ids"); $s->execute([ ':person_ids' => Operators\IN([1,2]) ]); $s->setColumnTypes([ 'data' => PDO::PARAM_JSON ]); var_dump($r); /* var_dump output array(1) { 'data' => array(4) { 'name' => string(4) "Mark" 'age' => int(39) 'address' => string(16) "White street, 23" 'town' => string(6) "London" } } */
Compatibility with original PDO class
PgBabylon\PDO is fully backward compatible with the original PDO class.
You can switch from original PDO class to PgBabylon\PDO adding a use statement:
<?php // PDO usage with original class /* Connect to a PgSQL database using driver invocation */ $dsn = 'pgsql:dbname=testdb;host=127.0.0.1;user=postgres;pass=mypasswd'; $dbh = new PDO($dsn); $dbh->prepare("SELECT id FROM person WHERE id=:id"); $r = $dbh->execute([':id' => 1]); ?>
<?php // Switch to PgBabylon\PDO! use PgBabylon\PDO; /* Connect to a PgSQL database using driver invocation */ $dsn = 'pgsql:dbname=testdb;host=127.0.0.1;user=postgres;pass=mypasswd'; $dbh = new PDO($dsn); $dbh->prepare("SELECT id FROM person WHERE id=:id"); $r = $dbh->execute([':id' => 1]); ?>
rtshome/pgbabylon 适用场景与选型建议
rtshome/pgbabylon 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.53k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「json」 「postgresql」 「pdo」 「pgsql」 「postgres」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rtshome/pgbabylon 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rtshome/pgbabylon 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rtshome/pgbabylon 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
ext-json wrapper with sane defaults
统计信息
- 总下载量: 2.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD
- 更新时间: 2015-10-04