{"id":15187610,"url":"https://github.com/nikolas/db_type","last_synced_at":"2025-10-02T02:31:30.720Z","repository":{"id":4395032,"uuid":"5532112","full_name":"nikolas/db_type","owner":"nikolas","description":"DB_Pgsql_Type is a library framework to parse+assemble built-in PostgreSQL types (multi-dimensional arrays, row, hstore, timestamp etc.) in PHP","archived":false,"fork":true,"pushed_at":"2012-11-12T21:30:49.000Z","size":162,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-25T09:44:26.155Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"nehxby/db_type","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nikolas.png","metadata":{"files":{"readme":"README.txt","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-23T20:40:09.000Z","updated_at":"2023-09-08T16:34:55.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/nikolas/db_type","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nikolas/db_type","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikolas%2Fdb_type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikolas%2Fdb_type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikolas%2Fdb_type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikolas%2Fdb_type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nikolas","download_url":"https://codeload.github.com/nikolas/db_type/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikolas%2Fdb_type/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":277714339,"owners_count":25864437,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-30T02:00:09.208Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-27T18:40:38.997Z","updated_at":"2025-10-02T02:31:30.459Z","avatar_url":"https://github.com/nikolas.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"DB_Type: conversion of complex PostgreSQL types (ARRAY, ROW, HSTORE) to PHP and vice versa\nVersion: 2.x\n(C) Dmitry Koterov, http://en.dklab.ru/lib/DB_Type/\nLicense: GPL\n\nABSTRACT\n--------\n\nDB_Type is a simple framework to convert complex database types (e.g.\nPostgreSQL arrays, hstores, rows) into their PHP equivalents and back.\nYou may work with complex typed values (e.g. PostgreSQL's 2-dimensional \narray of a composite type) as simple as it is a PHP 2d array.\n\nSuported types:\n\n* SQL'92 standard tyles (string, numeric, date, timestamp etc.)\n* PostgreSQL array with elements of any type (including multi-dimensional).\n* PostgreSQL composite type and ROWTYPE (including multi-dimensional).\n* PostgreSQL hstore (including stringified complex elements).\n\n\nWHAT IS THIS LIBRARY FOR?\n-------------------------\n\nMany databases (e.g. PostgreSQL) supports complex types as a column value.\nE.g. you may define a column which holds a 2d-array of strings:\n\n  CREATE TABLE something(\n    id INTEGER,\n    matrix TEXT[][]\n  );\n  INSERT INTO something(id, matrix) VALUES(\n    1, ARRAY[ARRAY['one','two'], ARRAY['three \"3\"','four']]\n  );\n\nIf you try to fetch such value in PHP:\n\n  $rs = $pdo-\u003equery(\"SELECT matrix FROM something WHERE id=1\");\n  echo $rs-\u003efetchColumn();\n  \nyou will get a \"stringified\" representation of such value:\n\n  {{one,two},{\"three \\\"3\\\"\",four}}\n  \nDB_Type allows you to convert such \"stringified\" values into their PHP\nrepresentations and vice versa. It takes care about escaping of special\ncharacters (quotes, empty strings, NULLs etc.) which methods are different\nfor different complex types.\n\n\nSYNOPSIS\n--------\n\n// Create an \"array of strings\" parser.\n$parser = new DB_Type_Pgsql_Array(new DB_Type_String());\n// Will return array(\"one\", \"two\")\n$array = $parser-\u003einput('{one,two}');\n\n\n// Create an \"array of array of strings\" parser.\n$parser = new DB_Type_Pgsql_Array(\n  new DB_Type_Pgsql_Array(\n    new DB_Type_String()\n  )\n);\n// Will return array(array(\"one\", \"two\"), array('three \"3\"', \"four\"))\n$array = $parser-\u003einput('{{one,two},{\"three \\\"3\\\"\",four}}');\n// Convert value back to stringified format to pass to PostgreSQL:\necho $parser-\u003eoutput($array);\n\n\n-- Assume we have a row type:\nCREATE TYPE inventory_item AS (\n    name            text,\n    supplier_id     integer,\n    price           numeric\n);\nCREATE TABLE on_hand (\n    item      inventory_item,\n    count     integer\n);\nINSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000);\n// Create this row parser.\n$parser = new DB_Type_Pgsql_Row(array(\n  'name'        =\u003e new DB_Type_String(),\n  'supplier_id' =\u003e new DB_Type_Int(),\n  'price'       =\u003e new DB_Type_Numeric(),\n));\n// Will return array(\"name\" =\u003e 'fuzzy dice', 'supplier_id' =\u003e 42, 'price' =\u003e 1.99)\n$row = $parser-\u003einput('(\"fuzzy dice\",42,1.99)');\n// Build the stringified representation back:\necho $parser-\u003eoutput($row);\n\n\n// Create a parser for \"array of rows of (string, int, numerid)\".\n$parser = new DB_Type_Pgsql_Array(\n  new DB_Type_Pgsql_Row(array(\n    'name'        =\u003e new DB_Type_String(),\n    'supplier_id' =\u003e new DB_Type_Int(),\n    'price'       =\u003e new DB_Type_Numeric(),\n  )\n);\n\n\n// Create a hstore parser.\n$parser = new DB_Type_Pgsql_Hstore(new DB_Type_String());\n// Will return array(\"aaa\" =\u003e 'bq', 'b' =\u003e null, '' =\u003e 1)\n$hash = $parser-\u003einput('aaa=\u003ebq, b=\u003eNULL, \"\"=\u003e1');\n// You may also create \"hstore of arrays\" or even \"hstore of\n// arrays of composite types\"\n\n\n// Timestamp parser.\n$parser = new DB_Pgsql_Type_Timestamp();\n// Will return 1204450462.\necho $parser-\u003einput(\"2008-03-02 12:34:22\");\n// Will build the timestamp back.\necho $parser-\u003eoutput(1204450462);\n\n\n// Other simple types:\n// - DB_Type_Date\n// - DB_Type_Time\n// - DB_Type_String\n// - DB_Type_Numeric\n// - DB_Type_Int\n// - DB_Type_Pgsql_Boolean (\"t\" and \"f\" PostgreSQL's values)\n\n\nTYPE MODIFIERS (WRAPPERS)\n-------------------------\n\n// Create a parser/builder \"array of strings in which all strings\n// are space-trimmed on insertion automatically\"\n$parser = new DB_Type_Pgsql_Array(\n  new DB_Type_Wrapper_Trim(new DB_Type_String())\n);\n\n\n// Create a parser/builder \"array of strings in which all strings are\n// space-trimmed, and empty strings are converted to NULLs\".\n$parser = new DB_Type_Pgsql_Array(\n  new DB_Type_Wrapper_EmptyNull\n    new DB_Type_Wrapper_Trim(\n      new DB_Type_String()\n    )\n  )\n);\n\n\n// Create a date with month-truncating on insertion.\n$parser = new DB_Type_Date(DB_Type_Date::TRUNC_MONTH);\n// Create a time with minutes-truncating on insertion.\n$parser = new DB_Type_Time(DB_Type_Time::TRUNC_MINUTE);\n// Create a timestamp with hour-truncating on insertion.\n$parser = new DB_Type_Timestamp(DB_Type_Timestamp::TRUNC_HOUR);\n\n\n// Create a parser/builder which always inserts a constant.\n$parser = new DB_Type_Constant(\"value\");\n// Will print \"value\"\nechp $parser-\u003eoutput('123');\n\n\nVALIDATORS AND LIMITERS\n-----------------------\n\n// A parser/builder \"array of strings within 10 to 20 characters\".\ntry {\n  $parser = new DB_Type_Pgsql_Array(\n    new DB_Type_String(10, 20)\n  );\n} catch (DB_Type_Exception_Common $e) {\n  ...\n}\n\n\n// The same as above, but with separated validator wrapper.\n$parser = new DB_Type_Pgsql_Array(\n  new DB_Type_Wrapper_Length(new DB_Type_String(), 10, 20)\n);\n\n\n// Also validation exceptions are thrown when you try to\n// convert a wrongly-formated string to complex types or\n// wrong type values to strings.\n\n\nHOW TO CONVERT FROM 1.X VERSION?\n--------------------------------\n\nThe 2.x version of this library has namespace DB_Type, not\nDB_Pgsql_Type as 1.x version.\n\nIf you use the previous version of this library, DB_Pgsql_Type,\nplease use utils/MIGRATE_100_TO_200.pl script to search for\nall DB_Pgsql_* class names and replace them to DB_Type_* names.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikolas%2Fdb_type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikolas%2Fdb_type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikolas%2Fdb_type/lists"}