nulib-base/php/src/db/pgsql/PgsqlStorage.php

61 lines
1.5 KiB
PHP

<?php
namespace nulib\db\pgsql;
use nulib\cl;
use nulib\db\CapacitorChannel;
use nulib\db\CapacitorStorage;
class PgsqlStorage extends CapacitorStorage {
const SERDATA_DEFINITION = "text";
const SERSUM_DEFINITION = "varchar(40)";
const SERTS_DEFINITION = "timestamp";
function __construct($pgsql) {
$this->db = Pgsql::with($pgsql);
}
protected Pgsql $db;
function db(): Pgsql {
return $this->db;
}
const PRIMARY_KEY_DEFINITION = [
"id_" => "serial primary key",
];
protected function tableExists(string $tableName): bool {
if (($index = strpos($tableName, ".")) !== false) {
$schemaName = substr($tableName, 0, $index);
$tableName = substr($tableName, $index + 1);
} else {
$schemaName = "public";
}
$found = $this->db->get([
"select tablename from pg_tables",
"where" => [
"schemaname" => $schemaName,
"tablename" => $tableName,
],
]);
return $found !== null;
}
function _getMigration(CapacitorChannel $channel): _pgsqlMigration {
$migrations = cl::merge([
"0init" => [$this->_createSql($channel)],
], $channel->getMigration());
return new _pgsqlMigration($migrations, $channel->getName());
}
protected function _addToChannelsSql(CapacitorChannel $channel): array {
return cl::merge(parent::_addToChannelsSql($channel), [
"suffix" => "on conflict (name) do nothing",
]);
}
function close(): void {
$this->db->close();
}
}