61 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			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";
 | |
|   const GENSERIAL_DEFINITION = "serial primary key";
 | |
|   const GENTEXT_DEFINITION = "text";
 | |
|   const GENBOOL_DEFINITION = "boolean";
 | |
|   const GENUUID_DEFINITION = "uuid";
 | |
| 
 | |
|   function __construct($pgsql) {
 | |
|     $this->db = Pgsql::with($pgsql);
 | |
|   }
 | |
| 
 | |
|   protected Pgsql $db;
 | |
| 
 | |
|   function db(): Pgsql {
 | |
|     return $this->db;
 | |
|   }
 | |
| 
 | |
|   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();
 | |
|   }
 | |
| }
 |