- First draft of current WIP
This commit is contained in:
commit
b73d046e0a
47 files changed
+2797
No files matched your search
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Adapter;
|
||||
|
||||
use Ulmus\Common\PdoObject;
|
||||
|
||||
class MySQL implements AdapterInterface {
|
||||
|
||||
public string $hostname;
|
||||
public string $database;
|
||||
public string $username;
|
||||
public string $password;
|
||||
public string $charset;
|
||||
public int $port;
|
||||
|
||||
public function __construct(
|
||||
?string $hostname = null,
|
||||
?string $database = null,
|
||||
?string $username = null,
|
||||
?string $password = null,
|
||||
?int $port = null,
|
||||
?string $charset = null
|
||||
) {
|
||||
if ($hostname) {
|
||||
$this->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 {
|
||||
$obj = new PdoObject($this->buildDataSourceName(), $this->username, $this->password);
|
||||
}
|
||||
catch(PDOException $ex){
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function buildDataSourceName() : string
|
||||
{
|
||||
$parts[] = "host={$this->hostname}";
|
||||
$parts[] = "dbname={$this->database}";
|
||||
$parts[] = "port={$this->port}";
|
||||
|
||||
if ( $this->socket ?? false ) {
|
||||
$parts[] = "socket={$this->socket}";
|
||||
}
|
||||
|
||||
if ( $this->charset ?? false ) {
|
||||
$parts[] = "charset={$this->charset}";
|
||||
}
|
||||
|
||||
return "mysql:" . implode(';', $parts);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user