Compare commits
	
		
			3 Commits
		
	
	
		
			2af9b0b8f0
			...
			89f24d8d90
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 89f24d8d90 | |||
| 299b90c85e | |||
| cae38dae95 | 
@ -166,11 +166,46 @@ $sql;
 | 
				
			|||||||
EOT;
 | 
					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;
 | 
					  abstract function _getMigration(CapacitorChannel $channel): _migration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const CHANNELS_TABLE = "_channels";
 | 
					  const CHANNELS_TABLE = "_channels";
 | 
				
			||||||
  const CHANNELS_COLS = [
 | 
					  const CHANNELS_COLS = [
 | 
				
			||||||
    "name" => "varchar primary key",
 | 
					    "name" => "varchar not null primary key",
 | 
				
			||||||
    "table_name" => "varchar",
 | 
					    "table_name" => "varchar",
 | 
				
			||||||
    "class_name" => "varchar",
 | 
					    "class_name" => "varchar",
 | 
				
			||||||
  ];
 | 
					  ];
 | 
				
			||||||
@ -204,6 +239,7 @@ EOT;
 | 
				
			|||||||
  protected function _create(CapacitorChannel $channel): void {
 | 
					  protected function _create(CapacitorChannel $channel): void {
 | 
				
			||||||
    $channel->ensureSetup();
 | 
					    $channel->ensureSetup();
 | 
				
			||||||
    if (!$channel->isCreated()) {
 | 
					    if (!$channel->isCreated()) {
 | 
				
			||||||
 | 
					      $this->_prepareMetadata();
 | 
				
			||||||
      $this->_getMigration($channel)->migrate($this->db());
 | 
					      $this->_getMigration($channel)->migrate($this->db());
 | 
				
			||||||
      $this->_afterCreate($channel);
 | 
					      $this->_afterCreate($channel);
 | 
				
			||||||
      $channel->setCreated();
 | 
					      $channel->setCreated();
 | 
				
			||||||
@ -211,7 +247,9 @@ EOT;
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /** tester si le canal spécifié existe */
 | 
					  /** 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 {
 | 
					  function exists(?string $channel): bool {
 | 
				
			||||||
    return $this->_exists($this->getChannel($channel));
 | 
					    return $this->_exists($this->getChannel($channel));
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@
 | 
				
			|||||||
namespace nulib\db\_private;
 | 
					namespace nulib\db\_private;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use nulib\cl;
 | 
					use nulib\cl;
 | 
				
			||||||
 | 
					use nulib\str;
 | 
				
			||||||
use nulib\ValueException;
 | 
					use nulib\ValueException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class _generic extends _common {
 | 
					class _generic extends _common {
 | 
				
			||||||
@ -13,9 +14,13 @@ class _generic extends _common {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static function parse(array $query, ?array &$bindings=null): string {
 | 
					  static function parse(array $query, ?array &$bindings=null): string {
 | 
				
			||||||
    if (!cl::is_list($query)) {
 | 
					    $sql = "";
 | 
				
			||||||
      throw new ValueException("Seuls les tableaux séquentiels sont supportés");
 | 
					    foreach ($query as $value) {
 | 
				
			||||||
 | 
					      if ($sql && !str::ends_with(" ", $sql) && !str::starts_with(" ", $value)) {
 | 
				
			||||||
 | 
					        $sql .= " ";
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      $sql .= $value;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return self::merge_seq($query);
 | 
					    return $sql;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -83,7 +83,7 @@ class _select extends _common {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    ## from
 | 
					    ## from
 | 
				
			||||||
    $from = $query["from"] ?? null;
 | 
					    $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];
 | 
					      if ($from === null) $from = $ms[1];
 | 
				
			||||||
      $sql[] = "from";
 | 
					      $sql[] = "from";
 | 
				
			||||||
      $sql[] = $from;
 | 
					      $sql[] = $from;
 | 
				
			||||||
 | 
				
			|||||||
@ -23,6 +23,23 @@ class MysqlStorage extends CapacitorStorage {
 | 
				
			|||||||
    "id_" => "integer primary key auto_increment",
 | 
					    "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 {
 | 
					  function _getMigration(CapacitorChannel $channel): _mysqlMigration {
 | 
				
			||||||
    return new _mysqlMigration(cl::merge([
 | 
					    return new _mysqlMigration(cl::merge([
 | 
				
			||||||
      $this->_createSql($channel),
 | 
					      $this->_createSql($channel),
 | 
				
			||||||
@ -35,7 +52,7 @@ class MysqlStorage extends CapacitorStorage {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const CHANNELS_COLS = [
 | 
					  const CHANNELS_COLS = [
 | 
				
			||||||
    "name" => "varchar(255) primary key",
 | 
					    "name" => "varchar(255) not null primary key",
 | 
				
			||||||
    "table_name" => "varchar(64)",
 | 
					    "table_name" => "varchar(64)",
 | 
				
			||||||
    "class_name" => "varchar(255)",
 | 
					    "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 {
 | 
					  function close(): void {
 | 
				
			||||||
    $this->db->close();
 | 
					    $this->db->close();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,23 @@ class PgsqlStorage extends CapacitorStorage {
 | 
				
			|||||||
    "id_" => "serial primary key",
 | 
					    "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 {
 | 
					  function _getMigration(CapacitorChannel $channel): _pgsqlMigration {
 | 
				
			||||||
    return new _pgsqlMigration(cl::merge([
 | 
					    return new _pgsqlMigration(cl::merge([
 | 
				
			||||||
      $this->_createSql($channel),
 | 
					      $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 {
 | 
					  function close(): void {
 | 
				
			||||||
    $this->db->close();
 | 
					    $this->db->close();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -23,6 +23,16 @@ class SqliteStorage extends CapacitorStorage {
 | 
				
			|||||||
    "id_" => "integer primary key autoincrement",
 | 
					    "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 {
 | 
					  function _getMigration(CapacitorChannel $channel): _sqliteMigration {
 | 
				
			||||||
    return new _sqliteMigration(cl::merge([
 | 
					    return new _sqliteMigration(cl::merge([
 | 
				
			||||||
      $this->_createSql($channel),
 | 
					      $this->_createSql($channel),
 | 
				
			||||||
@ -34,16 +44,6 @@ class SqliteStorage extends CapacitorStorage {
 | 
				
			|||||||
    return self::format_sql($channel, $query->getSql());
 | 
					    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 {
 | 
					  function channelExists(string $name): bool {
 | 
				
			||||||
    return null !== $this->db->get([
 | 
					    return null !== $this->db->get([
 | 
				
			||||||
      "select name",
 | 
					      "select name",
 | 
				
			||||||
@ -72,10 +72,6 @@ class SqliteStorage extends CapacitorStorage {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function _exists(CapacitorChannel $channel): bool {
 | 
					 | 
				
			||||||
    return $this->tableExists($channel->getTableName());
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  function close(): void {
 | 
					  function close(): void {
 | 
				
			||||||
    $this->db->close();
 | 
					    $this->db->close();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user