{"id":34629316,"url":"https://github.com/esd-projects/postgresql-plugin","last_synced_at":"2026-06-01T07:31:44.358Z","repository":{"id":56979211,"uuid":"191869913","full_name":"esd-projects/postgresql-plugin","owner":"esd-projects","description":"esd postgresql plugin","archived":false,"fork":false,"pushed_at":"2019-08-27T00:59:56.000Z","size":28,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-27T14:56:29.609Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esd-projects.png","metadata":{"files":{"readme":"README.md","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":"2019-06-14T03:26:29.000Z","updated_at":"2019-08-27T00:59:57.000Z","dependencies_parsed_at":"2022-08-21T11:50:38.148Z","dependency_job_id":null,"html_url":"https://github.com/esd-projects/postgresql-plugin","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/esd-projects/postgresql-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fpostgresql-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fpostgresql-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fpostgresql-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fpostgresql-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esd-projects","download_url":"https://codeload.github.com/esd-projects/postgresql-plugin/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fpostgresql-plugin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33765378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-01T02:00:06.963Z","response_time":115,"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":"2025-12-24T16:33:40.244Z","updated_at":"2026-06-01T07:31:44.353Z","avatar_url":"https://github.com/esd-projects.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# postgresql-plugin\n\n## 插件安装\n\n```\ncomposer require esd/postgresql-plugin\n```\n\n\n\n## 插件用法\n\nPostgreSQL插件的用法，力求与MySQL用法一样。如果你已熟用过 esd/mysql-plugin，可以忽略以下内容。\n\n\n\n## 1. 启用插件\n\nsrc/Application.php\n\n```php\n\u003c?php\nnamespace app;\nuse ESD\\Go\\GoApplication;\nuse ESD\\Plugins\\Postgresql\\PostgresqlPlugin;\n\nclass Application\n{\n    /**\n     * @throws \\DI\\DependencyException\n     * @throws \\DI\\NotFoundException\n     * @throws \\ESD\\Core\\Exception\n     * @throws \\ESD\\Core\\Plugins\\Config\\ConfigException\n     * @throws \\ReflectionException\n     */\n    public static function main()\n    {\n        $application = new GoApplication();\n        $application-\u003eaddPlug(new PostgresqlPlugin());\n\n        $application-\u003erun();\n    }\n}\n\n```\n\n\n\n## 2. 使用插件\n\n有两种方法在对象中使用。\n\n### 方法1\n\n使用 use trait，然后通过 $this-\u003epostgresql() 获取数据库连接。\n\n```php\nuse GetPostgresql;\n```\n\n```php\npublic function test()\n{\n    $this-\u003epostgresql()-\u003equery(\"select * from pg_stat_activity\");\n    return $res;\n}\n```\n\n如果需要切换 postgresql配置为test，可按照如下方法修改。\n\n```php\npublic function test()\n{\n    $this-\u003epostgresql('test')-\u003equery(\"select * from pg_stat_activity\");\n    return $res;\n}\n```\n\n\n\n### 方法2\n\n在对象中进行注入postgresql对象\n\n```php\nuse DI\\Annotation\\Inject;\n\n/**\n * @Inject()\n * @var \\ESD\\Plugins\\Postgresql\\PostgresDb\n */\nprotected $postgresql;\n\npublic function test(){\n\t$res = $this-\u003epostgresql-\u003equery(\"select * from pg_stat_activity\");\n\treturn $res;\n}\n```\n\n**通过注入的方法，目前还不支持切换 postgresql 连接配置。**\n\n\n\n## PostgreSQL 对象的使用方法\n\n数据表例子：\n\n```sql\nCREATE TABLE p_customer (\n\"user_name\" varchar(200),\n\"contact\" jsonb\n)\n```\n\n| user_name | contact                                                      |\n| --------- | ------------------------------------------------------------ |\n| 张三      | {\"QQ\": 120012, \"tel\": 6768456, \"phone\": 15838381234, \"Wechat\": \"风的季节\"} |\n| 李四      | {\"tel\": 8661235545}                                          |\n\n\n\n### Insert Query\n\n```php\n$res = $this-\u003epostgresql()-\u003einsert(\"p_customer\", [\n            \"user_name\" =\u003e \"奥里\",\n            \"contact\" =\u003e json_encode([\n                \"QQ\" =\u003e \"123456\",\n                \"Wechat\" =\u003e \"黑暗森林\"\n            ], JSON_UNESCAPED_UNICODE)\n        ]);\n```\n\n\n\n### Select Query \n\n```php\n$res = $this-\u003epostgresql()\n\t-\u003ewhere(\"contact-\u003e\u003e'phone'\", '15838381234')\n\t-\u003eget(\"p_customer\");\n```\n\n\n\n### Update Query\n\n```sql\n$res = $this-\u003epostgresql()\n\t-\u003ewhere(\"contact-\u003e\u003e'phone'\", '15838381234')\n\t-\u003eupdate(\"p_customer\", [\n\t\t\t\"user_name\" =\u003e \"钱三强\"\n\t\t]);\n```\n\n\n\n### Delete Query\n\n```php\n$res = $this-\u003epostgresql()\n\t-\u003ewhere(\"contact-\u003e\u003e'phone'\", '15838381234')\n\t-\u003edelete(\"p_customer\");\n```\n\n\n\n### Ordering method\n\n```php\n$res = $this-\u003epostgresql()\n\t-\u003eorderByd(\"contact-\u003e\u003e'QQ'\", \"ASC\")\n\t-\u003eget(\"p_customer\");\n```\n\n\n\n### Grouping method\n\n```php\n$res = $this-\u003epostgresql()\n\t-\u003egroupBy(\"contact-\u003e\u003e'QQ'\")\n\t-\u003eget(\"p_customer\");\n```\n\n\n\n### JOIN method\n\n```php\n$res = $this-\u003epostgresql()\n\t-\u003ejoin(\"p_customer_qq cq\", \"c.contact-\u003e\u003e'QQ' = cq.qq_number\", \"LEFT\")\n\t-\u003ewhere(\"c.contact-\u003e\u003e'phone'\", '15838381234')\n\t-\u003eget(\"p_customer c\", NULL, \"c.*, cq.qq_avator\");\n```\n\n\n\n### Has method\n\nA convenient function that returns TRUE if exists at least an element that satisfy the where condition specified calling the \"where\" method before this one.\n\n```php\n$res = $this-\u003epostgresql()\n\t-\u003ewhere(\"contact-\u003e\u003e'phone'\", '15838381234')\n\t-\u003ehas(\"p_customer\");\n```\n\n\n\n### Helper methods\n\nGet last executed SQL query\n\n```php\n$sql = $this-\u003epostgresql()-\u003egetLastQuery();\n```\n\n\n\n### Check if table exists\n\n```php\n$exist = $this-\u003epostgresql()-\u003etableExists(\"p_customer\");\n```\n\n\n\n### Transaction helpers\n\n```php\n$db = $this-\u003epostgresql();\n\n$db-\u003estartTransaction();\n...\nif (!$db-\u003einsert ('myTable', $insertData)) {\n    //Error while saving, cancel new record\n    $db-\u003erollback();\n} else {\n    //OK\n    $db-\u003ecommit();\n}\n```\n\n\n\n### Error helpers\n\n```php\n$error = $this-\u003epostgresql()-\u003egetLastError();\n```\n\n\n\n### Pagination\n\n```php\n$db = $this-\u003epostgresql();\n$page = 1;\n// set page limit to 2 results per page. 20 by default\n$db-\u003epageLimit = 2;\n$res = $db-\u003epaginate(\"p_customer\", $page);\n```\n\n\n\n**loadData、XML、loadXML、insertMulti、subQuery 等MySQL插件的功能，暂时没有实现**。\n\n\n\n### Running raw SQL queries\n\n```php\n$res = $this-\u003epostgresql()\n    -\u003equery('SELECT * from p_customer where contact-\u003e\u003e'phone' = ?', Array('15838381234'));\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesd-projects%2Fpostgresql-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesd-projects%2Fpostgresql-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesd-projects%2Fpostgresql-plugin/lists"}