Compare commits
	
		
			3 Commits
		
	
	
		
			2af9b0b8f0
			...
			89f24d8d90
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 89f24d8d90 | |||
| 299b90c85e | |||
| cae38dae95 | 
@ -166,11 +166,46 @@ $sql;
 | 
			
		||||
EOT;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  abstract protected function tableExists(string $tableName): bool;
 | 
			
		||||
 | 
			
		||||
  const METADATA_TABLE = "_metadata";
 | 
			
		||||
  const METADATA_COLS = [
 | 
			
		||||
    "name" => "varchar not null primary key",
 | 
			
		||||
    "value" => "varchar",
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  protected function _prepareMetadata(): void {
 | 
			
		||||
    if (!$this->tableExists(static::METADATA_TABLE)) {
 | 
			
		||||
      $db = $this->db();
 | 
			
		||||
      $db->exec([
 | 
			
		||||
        "drop table if exists",
 | 
			
		||||
        "table" => self::CHANNELS_TABLE,
 | 
			
		||||
      ]);
 | 
			
		||||
      $db->exec([
 | 
			
		||||
        "drop table if exists",
 | 
			
		||||
        "table" => _migration::MIGRATION_TABLE
 | 
			
		||||
      ]);
 | 
			
		||||
      $db->exec([
 | 
			
		||||
        "create table",
 | 
			
		||||
        "table" => static::METADATA_TABLE,
 | 
			
		||||
        "cols" => static::METADATA_COLS,
 | 
			
		||||
      ]);
 | 
			
		||||
      $db->exec([
 | 
			
		||||
        "insert",
 | 
			
		||||
        "into" => static::METADATA_TABLE,
 | 
			
		||||
        "values" => [
 | 
			
		||||
          "name" => "version",
 | 
			
		||||
          "value" => "1",
 | 
			
		||||
        ],
 | 
			
		||||
      ]);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  abstract function _getMigration(CapacitorChannel $channel): _migration;
 | 
			
		||||
 | 
			
		||||
  const CHANNELS_TABLE = "_channels";
 | 
			
		||||
  const CHANNELS_COLS = [
 | 
			
		||||
    "name" => "varchar primary key",
 | 
			
		||||
    "name" => "varchar not null primary key",
 | 
			
		||||
    "table_name" => "varchar",
 | 
			
		||||
    "class_name" => "varchar",
 | 
			
		||||
  ];
 | 
			
		||||
@ -204,6 +239,7 @@ EOT;
 | 
			
		||||
  protected function _create(CapacitorChannel $channel): void {
 | 
			
		||||
    $channel->ensureSetup();
 | 
			
		||||
    if (!$channel->isCreated()) {
 | 
			
		||||
      $this->_prepareMetadata();
 | 
			
		||||
      $this->_getMigration($channel)->migrate($this->db());
 | 
			
		||||
      $this->_afterCreate($channel);
 | 
			
		||||
      $channel->setCreated();
 | 
			
		||||
@ -211,7 +247,9 @@ EOT;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** tester si le canal spécifié existe */
 | 
			
		||||
  abstract function _exists(CapacitorChannel $channel): bool;
 | 
			
		||||
  function _exists(CapacitorChannel $channel): bool {
 | 
			
		||||
    return $this->tableExists($channel->getTableName());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function exists(?string $channel): bool {
 | 
			
		||||
    return $this->_exists($this->getChannel($channel));
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@
 | 
			
		||||
namespace nulib\db\_private;
 | 
			
		||||
 | 
			
		||||
use nulib\cl;
 | 
			
		||||
use nulib\str;
 | 
			
		||||
use nulib\ValueException;
 | 
			
		||||
 | 
			
		||||
class _generic extends _common {
 | 
			
		||||
@ -13,9 +14,13 @@ class _generic extends _common {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static function parse(array $query, ?array &$bindings=null): string {
 | 
			
		||||
    if (!cl::is_list($query)) {
 | 
			
		||||
      throw new ValueException("Seuls les tableaux séquentiels sont supportés");
 | 
			
		||||
    $sql = "";
 | 
			
		||||
    foreach ($query as $value) {
 | 
			
		||||
      if ($sql && !str::ends_with(" ", $sql) && !str::starts_with(" ", $value)) {
 | 
			
		||||
        $sql .= " ";
 | 
			
		||||
      }
 | 
			
		||||
    return self::merge_seq($query);
 | 
			
		||||
      $sql .= $value;
 | 
			
		||||
    }
 | 
			
		||||
    return $sql;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -83,7 +83,7 @@ class _select extends _common {
 | 
			
		||||
 | 
			
		||||
    ## from
 | 
			
		||||
    $from = $query["from"] ?? null;
 | 
			
		||||
    if (self::consume('from\s+([a-z_][a-z0-9_]*)\s*(?=;?\s*$|\bwhere\b)', $tmpsql, $ms)) {
 | 
			
		||||
    if (self::consume('from\s+([a-z_][a-z0-9_.]*)\s*(?=;?\s*$|\bwhere\b)', $tmpsql, $ms)) {
 | 
			
		||||
      if ($from === null) $from = $ms[1];
 | 
			
		||||
      $sql[] = "from";
 | 
			
		||||
      $sql[] = $from;
 | 
			
		||||
 | 
			
		||||
@ -23,6 +23,23 @@ class MysqlStorage extends CapacitorStorage {
 | 
			
		||||
    "id_" => "integer primary key auto_increment",
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  protected function tableExists(string $tableName): bool {
 | 
			
		||||
    $db = $this->db;
 | 
			
		||||
    $found = $db->get([
 | 
			
		||||
      "select table_name from information_schema.tables",
 | 
			
		||||
      "where" => [
 | 
			
		||||
        "table_schema" => $db->getDbname(),
 | 
			
		||||
        "table_name" => $tableName,
 | 
			
		||||
      ],
 | 
			
		||||
    ]);
 | 
			
		||||
    return $found !== null;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const METADATA_COLS = [
 | 
			
		||||
    "name" => "varchar(64) not null primary key",
 | 
			
		||||
    "value" => "varchar(255)",
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  function _getMigration(CapacitorChannel $channel): _mysqlMigration {
 | 
			
		||||
    return new _mysqlMigration(cl::merge([
 | 
			
		||||
      $this->_createSql($channel),
 | 
			
		||||
@ -35,7 +52,7 @@ class MysqlStorage extends CapacitorStorage {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const CHANNELS_COLS = [
 | 
			
		||||
    "name" => "varchar(255) primary key",
 | 
			
		||||
    "name" => "varchar(255) not null primary key",
 | 
			
		||||
    "table_name" => "varchar(64)",
 | 
			
		||||
    "class_name" => "varchar(255)",
 | 
			
		||||
  ];
 | 
			
		||||
@ -46,18 +63,6 @@ class MysqlStorage extends CapacitorStorage {
 | 
			
		||||
    ]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  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();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -24,6 +24,23 @@ class PgsqlStorage extends CapacitorStorage {
 | 
			
		||||
    "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 {
 | 
			
		||||
    return new _pgsqlMigration(cl::merge([
 | 
			
		||||
      $this->_createSql($channel),
 | 
			
		||||
@ -41,23 +58,6 @@ class PgsqlStorage extends CapacitorStorage {
 | 
			
		||||
    ]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function _exists(CapacitorChannel $channel): bool {
 | 
			
		||||
    $tableName = $channel->getTableName();
 | 
			
		||||
    if (($index = strpos($tableName, ".")) !== false) {
 | 
			
		||||
      $schemaName = substr($tableName, 0, $index);
 | 
			
		||||
      $tableName = substr($tableName, $index + 1);
 | 
			
		||||
    } else {
 | 
			
		||||
      $schemaName = "public";
 | 
			
		||||
    }
 | 
			
		||||
    return null !== $this->db->get([
 | 
			
		||||
      "select tablename from pg_tables",
 | 
			
		||||
      "where" => [
 | 
			
		||||
        "schemaname" => $schemaName,
 | 
			
		||||
        "tablename" => $tableName,
 | 
			
		||||
      ],
 | 
			
		||||
    ]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function close(): void {
 | 
			
		||||
    $this->db->close();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -23,6 +23,16 @@ class SqliteStorage extends CapacitorStorage {
 | 
			
		||||
    "id_" => "integer primary key autoincrement",
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  protected function tableExists(string $tableName): bool {
 | 
			
		||||
    $found = $this->db->get([
 | 
			
		||||
      # depuis la version 3.33.0 le nom officiel de la table est sqlite_schema,
 | 
			
		||||
      # mais le nom sqlite_master est toujours valable pour le moment
 | 
			
		||||
      "select name from sqlite_master ",
 | 
			
		||||
      "where" => ["name" => $tableName],
 | 
			
		||||
    ]);
 | 
			
		||||
    return $found !== null;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function _getMigration(CapacitorChannel $channel): _sqliteMigration {
 | 
			
		||||
    return new _sqliteMigration(cl::merge([
 | 
			
		||||
      $this->_createSql($channel),
 | 
			
		||||
@ -34,16 +44,6 @@ class SqliteStorage extends CapacitorStorage {
 | 
			
		||||
    return self::format_sql($channel, $query->getSql());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function tableExists(string $tableName): bool {
 | 
			
		||||
    $name = $this->db->get([
 | 
			
		||||
      # depuis la version 3.33.0 le nom officiel de la table est sqlite_schema,
 | 
			
		||||
      # mais le nom sqlite_master est toujours valable pour le moment
 | 
			
		||||
      "select name from sqlite_master ",
 | 
			
		||||
      "where" => ["name" => $tableName],
 | 
			
		||||
    ]);
 | 
			
		||||
    return $name !== null;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function channelExists(string $name): bool {
 | 
			
		||||
    return null !== $this->db->get([
 | 
			
		||||
      "select name",
 | 
			
		||||
@ -72,10 +72,6 @@ class SqliteStorage extends CapacitorStorage {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function _exists(CapacitorChannel $channel): bool {
 | 
			
		||||
    return $this->tableExists($channel->getTableName());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function close(): void {
 | 
			
		||||
    $this->db->close();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user