nikolas/db_type
Composer 安装命令:
composer require nikolas/db_type
包简介
Conversion of complex PostgreSQL types (ARRAY, ROW, HSTORE) to PHP and vice versa
关键字:
README 文档
README
DB_Type: conversion of complex PostgreSQL types (ARRAY, ROW, HSTORE) to PHP and vice versa Version: 2.x (C) Dmitry Koterov, http://en.dklab.ru/lib/DB_Type/ License: GPL ABSTRACT -------- DB_Type is a simple framework to convert complex database types (e.g. PostgreSQL arrays, hstores, rows) into their PHP equivalents and back. You may work with complex typed values (e.g. PostgreSQL's 2-dimensional array of a composite type) as simple as it is a PHP 2d array. Suported types: * SQL'92 standard tyles (string, numeric, date, timestamp etc.) * PostgreSQL array with elements of any type (including multi-dimensional). * PostgreSQL composite type and ROWTYPE (including multi-dimensional). * PostgreSQL hstore (including stringified complex elements). WHAT IS THIS LIBRARY FOR? ------------------------- Many databases (e.g. PostgreSQL) supports complex types as a column value. E.g. you may define a column which holds a 2d-array of strings: CREATE TABLE something( id INTEGER, matrix TEXT[][] ); INSERT INTO something(id, matrix) VALUES( 1, ARRAY[ARRAY['one','two'], ARRAY['three "3"','four']] ); If you try to fetch such value in PHP: $rs = $pdo->query("SELECT matrix FROM something WHERE id=1"); echo $rs->fetchColumn(); you will get a "stringified" representation of such value: {{one,two},{"three \"3\"",four}} DB_Type allows you to convert such "stringified" values into their PHP representations and vice versa. It takes care about escaping of special characters (quotes, empty strings, NULLs etc.) which methods are different for different complex types. SYNOPSIS -------- // Create an "array of strings" parser. $parser = new DB_Type_Pgsql_Array(new DB_Type_String()); // Will return array("one", "two") $array = $parser->input('{one,two}'); // Create an "array of array of strings" parser. $parser = new DB_Type_Pgsql_Array( new DB_Type_Pgsql_Array( new DB_Type_String() ) ); // Will return array(array("one", "two"), array('three "3"', "four")) $array = $parser->input('{{one,two},{"three \"3\"",four}}'); // Convert value back to stringified format to pass to PostgreSQL: echo $parser->output($array); -- Assume we have a row type: CREATE TYPE inventory_item AS ( name text, supplier_id integer, price numeric ); CREATE TABLE on_hand ( item inventory_item, count integer ); INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000); // Create this row parser. $parser = new DB_Type_Pgsql_Row(array( 'name' => new DB_Type_String(), 'supplier_id' => new DB_Type_Int(), 'price' => new DB_Type_Numeric(), )); // Will return array("name" => 'fuzzy dice', 'supplier_id' => 42, 'price' => 1.99) $row = $parser->input('("fuzzy dice",42,1.99)'); // Build the stringified representation back: echo $parser->output($row); // Create a parser for "array of rows of (string, int, numerid)". $parser = new DB_Type_Pgsql_Array( new DB_Type_Pgsql_Row(array( 'name' => new DB_Type_String(), 'supplier_id' => new DB_Type_Int(), 'price' => new DB_Type_Numeric(), ) ); // Create a hstore parser. $parser = new DB_Type_Pgsql_Hstore(new DB_Type_String()); // Will return array("aaa" => 'bq', 'b' => null, '' => 1) $hash = $parser->input('aaa=>bq, b=>NULL, ""=>1'); // You may also create "hstore of arrays" or even "hstore of // arrays of composite types" // Timestamp parser. $parser = new DB_Pgsql_Type_Timestamp(); // Will return 1204450462. echo $parser->input("2008-03-02 12:34:22"); // Will build the timestamp back. echo $parser->output(1204450462); // Other simple types: // - DB_Type_Date // - DB_Type_Time // - DB_Type_String // - DB_Type_Numeric // - DB_Type_Int // - DB_Type_Pgsql_Boolean ("t" and "f" PostgreSQL's values) TYPE MODIFIERS (WRAPPERS) ------------------------- // Create a parser/builder "array of strings in which all strings // are space-trimmed on insertion automatically" $parser = new DB_Type_Pgsql_Array( new DB_Type_Wrapper_Trim(new DB_Type_String()) ); // Create a parser/builder "array of strings in which all strings are // space-trimmed, and empty strings are converted to NULLs". $parser = new DB_Type_Pgsql_Array( new DB_Type_Wrapper_EmptyNull new DB_Type_Wrapper_Trim( new DB_Type_String() ) ) ); // Create a date with month-truncating on insertion. $parser = new DB_Type_Date(DB_Type_Date::TRUNC_MONTH); // Create a time with minutes-truncating on insertion. $parser = new DB_Type_Time(DB_Type_Time::TRUNC_MINUTE); // Create a timestamp with hour-truncating on insertion. $parser = new DB_Type_Timestamp(DB_Type_Timestamp::TRUNC_HOUR); // Create a parser/builder which always inserts a constant. $parser = new DB_Type_Constant("value"); // Will print "value" echp $parser->output('123'); VALIDATORS AND LIMITERS ----------------------- // A parser/builder "array of strings within 10 to 20 characters". try { $parser = new DB_Type_Pgsql_Array( new DB_Type_String(10, 20) ); } catch (DB_Type_Exception_Common $e) { ... } // The same as above, but with separated validator wrapper. $parser = new DB_Type_Pgsql_Array( new DB_Type_Wrapper_Length(new DB_Type_String(), 10, 20) ); // Also validation exceptions are thrown when you try to // convert a wrongly-formated string to complex types or // wrong type values to strings. HOW TO CONVERT FROM 1.X VERSION? -------------------------------- The 2.x version of this library has namespace DB_Type, not DB_Pgsql_Type as 1.x version. If you use the previous version of this library, DB_Pgsql_Type, please use utils/MIGRATE_100_TO_200.pl script to search for all DB_Pgsql_* class names and replace them to DB_Type_* names.
nikolas/db_type 适用场景与选型建议
nikolas/db_type 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.36k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2012 年 11 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「postgresql」 「plpgsql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nikolas/db_type 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nikolas/db_type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nikolas/db_type 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Sql dumps containing databases for the linna apps.
Manage PostgreSQL schemas in Laravel applications
VV database abstraction layer with query builder and DB structure models
VV DB Driver over PDO extension
统计信息
- 总下载量: 9.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2012-11-12