esd/postgresql-plugin

postgresql-plugin

0.1.3 2019-06-16 15:52 UTC

This package is auto-updated.

Last update: 2024-09-27 12:33:30 UTC


README

插件安装

composer require esd/postgresql-plugin

插件用法

PostgreSQL插件的用法,力求与MySQL用法一样。如果你已经熟悉使用 esd/mysql-plugin,可以忽略以下内容。

1. 启用插件

src/Application.php

<?php
namespace app;
use ESD\Go\GoApplication;
use ESD\Plugins\Postgresql\PostgresqlPlugin;

class Application
{
    /**
     * @throws \DI\DependencyException
     * @throws \DI\NotFoundException
     * @throws \ESD\Core\Exception
     * @throws \ESD\Core\Plugins\Config\ConfigException
     * @throws \ReflectionException
     */
    public static function main()
    {
        $application = new GoApplication();
        $application->addPlug(new PostgresqlPlugin());

        $application->run();
    }
}

2. 使用插件

有两种方法在对象中使用。

方法1

使用 use trait,然后通过 $this->postgresql() 获取数据库连接。

use GetPostgresql;
public function test()
{
    $this->postgresql()->query("select * from pg_stat_activity");
    return $res;
}

如果需要将 postgresql 配置为 test,可按如下方法修改。

public function test()
{
    $this->postgresql('test')->query("select * from pg_stat_activity");
    return $res;
}

方法2

在对象中注入 postgresql 对象

use DI\Annotation\Inject;

/**
 * @Inject()
 * @var \ESD\Plugins\Postgresql\PostgresDb
 */
protected $postgresql;

public function test(){
	$res = $this->postgresql->query("select * from pg_stat_activity");
	return $res;
}

通过注入的方法,目前还不支持切换 postgresql 连接配置。

PostgreSQL 对象的使用方法

数据表例子:

CREATE TABLE p_customer (
"user_name" varchar(200),
"contact" jsonb
)

插入查询

$res = $this->postgresql()->insert("p_customer", [
            "user_name" => "奥里",
            "contact" => json_encode([
                "QQ" => "123456",
                "Wechat" => "黑暗森林"
            ], JSON_UNESCAPED_UNICODE)
        ]);

选择查询

$res = $this->postgresql()
	->where("contact->>'phone'", '15838381234')
	->get("p_customer");

更新查询

$res = $this->postgresql()
	->where("contact->>'phone'", '15838381234')
	->update("p_customer", [
			"user_name" => "钱三强"
		]);

删除查询

$res = $this->postgresql()
	->where("contact->>'phone'", '15838381234')
	->delete("p_customer");

排序方法

$res = $this->postgresql()
	->orderByd("contact->>'QQ'", "ASC")
	->get("p_customer");

分组方法

$res = $this->postgresql()
	->groupBy("contact->>'QQ'")
	->get("p_customer");

连接方法

$res = $this->postgresql()
	->join("p_customer_qq cq", "c.contact->>'QQ' = cq.qq_number", "LEFT")
	->where("c.contact->>'phone'", '15838381234')
	->get("p_customer c", NULL, "c.*, cq.qq_avator");

存在方法

一个方便的函数,如果调用此方法之前的 "where" 方法存在至少一个满足条件的元素,则返回 TRUE。

$res = $this->postgresql()
	->where("contact->>'phone'", '15838381234')
	->has("p_customer");

辅助方法

获取最后执行的 SQL 查询

$sql = $this->postgresql()->getLastQuery();

检查表是否存在

$exist = $this->postgresql()->tableExists("p_customer");

事务辅助

$db = $this->postgresql();

$db->startTransaction();
...
if (!$db->insert ('myTable', $insertData)) {
    //Error while saving, cancel new record
    $db->rollback();
} else {
    //OK
    $db->commit();
}

错误辅助

$error = $this->postgresql()->getLastError();

分页

$db = $this->postgresql();
$page = 1;
// set page limit to 2 results per page. 20 by default
$db->pageLimit = 2;
$res = $db->paginate("p_customer", $page);

loadData、XML、loadXML、insertMulti、subQuery 等MySQL插件的功能,暂时没有实现

运行原始 SQL 查询

$res = $this->postgresql()
    ->query('SELECT * from p_customer where contact->>'phone' = ?', Array('15838381234'));