nulib/php/src/db/mysql/MysqlStorage.php

65 lines
1.5 KiB
PHP

<?php
namespace nulib\db\mysql;
use nulib\cl;
use nulib\db\CapacitorChannel;
use nulib\db\CapacitorStorage;
/**
* Class MysqlStorage
*/
class MysqlStorage extends CapacitorStorage {
function __construct($mysql) {
$this->db = Mysql::with($mysql);
}
protected Mysql $db;
function db(): Mysql {
return $this->db;
}
const PRIMARY_KEY_DEFINITION = [
"id_" => "integer primary key auto_increment",
];
function _getMigration(CapacitorChannel $channel): _mysqlMigration {
return new _mysqlMigration(cl::merge([
$this->_createSql($channel),
], $channel->getMigration()), $channel->getName());
}
function _getCreateSql(CapacitorChannel $channel): string {
$query = new _mysqlQuery($this->_createSql($channel));
return self::format_sql($channel, $query->getSql());
}
const CHANNELS_COLS = [
"name" => "varchar(255) primary key",
"table_name" => "varchar(64)",
"class_name" => "varchar(255)",
];
protected function _addToChannelsSql(CapacitorChannel $channel): array {
return cl::merge(parent::_addToChannelsSql($channel), [
"suffix" => "on duplicate key update name = name",
]);
}
function _exists(CapacitorChannel $channel): bool {
$mysql = $this->db;
$tableName = $mysql->get([
"select table_name from information_schema.tables",
"where" => [
"table_schema" => $mysql->getDbname(),
"table_name" => $channel->getTableName(),
],
]);
return $tableName !== null;
}
function close(): void {
$this->db->close();
}
}