66 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			66 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",
 | |
|   ];
 | |
| 
 | |
|   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 {
 | |
|     $migrations = cl::merge([
 | |
|       "0init" => [$this->_createSql($channel)],
 | |
|     ], $channel->getMigration());
 | |
|     return new _mysqlMigration($migrations, $channel->getName());
 | |
|   }
 | |
| 
 | |
|   const CHANNELS_COLS = [
 | |
|     "name" => "varchar(255) not null 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 close(): void {
 | |
|     $this->db->close();
 | |
|   }
 | |
| }
 |