hostname = $hostname; } if ($database) { $this->database = $database; } if ($port) { $this->port = $port; } if ($username) { $this->username = $username; } if ($password) { $this->password = $password; } if ($charset) { $this->charset = $charset; } } public function connect() : PdoObject { try { $pdo = new PdoObject($this->buildDataSourceName(), $this->username, $this->password); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); $pdo->setAttribute(\PDO::ATTR_AUTOCOMMIT, false); $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); $pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); } catch(PDOException $ex){ throw $ex; } finally { $this->password = str_repeat('*', random_int(8,16)); } return $pdo; } public function buildDataSourceName() : string { if ( false !== ($this->socket ?? false) ) { $parts[] = "unix_socket={$this->socket}"; } else { $parts[] = "host={$this->hostname}"; if ( false !== ( $this->port ?? false ) ) { $parts[] = "port={$this->port}"; } } $parts[] = "dbname={$this->database}"; if ( $this->charset ?? false ) { $parts[] = "charset={$this->charset}"; } return static::DSN_PREFIX . ":" . implode(';', $parts); } public function setup(array $configuration) : void { $connection = array_change_key_case($configuration, \CASE_LOWER); # Either Unix Socket is used, or Host / Port if ( null === ( $this->socket = $connection['socket'] ?? null ) ) { if ( "" === ( $this->hostname = $connection['host'] ?? "" ) ) { throw new AdapterConfigurationException("Your `host` name is missing from your configuration array"); } if ( false !== ( $configuration['port'] ?? false ) ) { $this->port = $configuration['port']; } } if ( false === ( $this->database = $connection['database'] ?? false ) ) { throw new AdapterConfigurationException("Your `database` name is missing from your configuration array"); } elseif ( false === ( $this->username = $connection['username'] ?? false ) ) { throw new AdapterConfigurationException("Your `username` is missing from your configuration array"); } elseif ( false === ( $this->password = $connection['password'] ?? false ) ) { throw new AdapterConfigurationException("Your `password` is missing from your configuration array"); } if ( false !== ( $configuration['charset'] ?? false ) ) { $this->charset = $configuration['charset']; } } public function escapeIdentifier(string $segment, int $type) : string { switch($type) { case static::IDENTIFIER_DATABASE: case static::IDENTIFIER_TABLE: case static::IDENTIFIER_FIELD: return "`" . trim(str_replace("`", "``", $segment), '`') . "`"; case static::IDENTIFIER_VALUE: return "\"$segment\""; } } public function writableValue(mixed $value) : mixed { switch (true) { case $value instanceof \UnitEnum: return Ulmus::convertEnum($value); case is_object($value): return Ulmus::convertObject($value); case is_array($value): return json_encode($value); case is_bool($value): return (int) $value; } return $value; } public function defaultEngine(): ? string { return "InnoDB"; } }