3 Commits
1.0.0 ... 1.0.4

Author SHA1 Message Date
Gregory Letellier
35188860ce gestion du charset en mysql 2023-11-14 16:41:09 +01:00
Gregory Letellier
c8d6e911a6 meilleure gestion des noms de tables sur les modèles hérités 2023-11-14 15:55:08 +01:00
Gregory Letellier
03d26d9167 fix bug in array host reading 2023-11-14 15:33:19 +01:00
2 changed files with 23 additions and 4 deletions

View File

@@ -18,6 +18,7 @@ class Connection
private string $database; private string $database;
private ?string $port; private ?string $port;
private ?string $host; private ?string $host;
private ?string $charset;
private string $error; private string $error;
private function __construct($name, $config) private function __construct($name, $config)
@@ -40,10 +41,11 @@ class Connection
$key = (strtolower(trim($this->name))); $key = (strtolower(trim($this->name)));
$this->type = isset($config[$key]["TYPE"]) ? $config[$key]["TYPE"] : null; $this->type = isset($config[$key]["TYPE"]) ? $config[$key]["TYPE"] : null;
$this->user = isset($config[$key]["USER"]) ? $config[$key]["USER"] : null; $this->user = isset($config[$key]["USER"]) ? $config[$key]["USER"] : null;
$this->host = isset($config[$key]["HOST"]) ? $config[$config[$key]["HOST"]] : null; $this->host = isset($config[$key]["HOST"]) ? $config[$key]["HOST"] : null;
$this->password = isset($config[$key]["PASSWORD"]) ? $config[$key]["PASSWORD"] : null; $this->password = isset($config[$key]["PASSWORD"]) ? $config[$key]["PASSWORD"] : null;
$this->database = isset($config[$key]["DATABASE"]) ? $config[$key]["DATABASE"] : null; $this->database = isset($config[$key]["DATABASE"]) ? $config[$key]["DATABASE"] : null;
$this->port = isset($config[$key]["PORT"]) ? $config[$key]["PORT"] : null; $this->port = isset($config[$key]["PORT"]) ? $config[$key]["PORT"] : null;
$this->charset = isset($config[$key]["CHARSET"]) ? $config[$key]["CHARSET"] : null;
} catch (\Throwable $th) { } catch (\Throwable $th) {
$this->error = $th->getMessage(); $this->error = $th->getMessage();
} }
@@ -70,6 +72,7 @@ class Connection
$this->password = isset($_ENV[$this->getKey("PASSWORD", $key)]) ? $_ENV[$this->getKey("PASSWORD", $key)] : null; $this->password = isset($_ENV[$this->getKey("PASSWORD", $key)]) ? $_ENV[$this->getKey("PASSWORD", $key)] : null;
$this->database = isset($_ENV[$this->getKey("DATABASE", $key)]) ? $_ENV[$this->getKey("DATABASE", $key)] : null; $this->database = isset($_ENV[$this->getKey("DATABASE", $key)]) ? $_ENV[$this->getKey("DATABASE", $key)] : null;
$this->port = isset($_ENV[$this->getKey("PORT", $key)]) ? $_ENV[$this->getKey("PORT", $key)] : null; $this->port = isset($_ENV[$this->getKey("PORT", $key)]) ? $_ENV[$this->getKey("PORT", $key)] : null;
$this->charset = isset($_ENV[$this->getKey("CHARSET", $key)]) ? $_ENV[$this->getKey("CHARSET", $key)] : null;
} catch (\Throwable $th) { } catch (\Throwable $th) {
$this->error = $th->getMessage(); $this->error = $th->getMessage();
} }
@@ -117,10 +120,19 @@ class Connection
return $stmt; return $stmt;
} }
private function getMySQLOptions()
{
$ret = "";
if ($this->charset) {
$ret .= ";charset=" . $this->charset;
}
return $ret;
}
private function getConnString() private function getConnString()
{ {
$this->connstring = match ($this->type) { $this->connstring = match ($this->type) {
"mysql" => "mysql:host=" . $this->host . ":" . $this->port . ";dbname=" . $this->database, "mysql" => "mysql:host=" . $this->host . ":" . $this->port . ";dbname=" . $this->database . $this->getMySQLOptions() ,
"sqlite" => "sqlite:" . $this->database, "sqlite" => "sqlite:" . $this->database,
}; };
} }

View File

@@ -8,7 +8,7 @@ class Model implements JsonSerializable
{ {
protected mixed $values; protected mixed $values;
protected array $updated; protected array $updated;
protected string $table; protected string $table = "";
protected string $connection = ""; protected string $connection = "";
protected string $pk; protected string $pk;
private bool $new; private bool $new;
@@ -19,8 +19,15 @@ class Model implements JsonSerializable
$this->values = array(); $this->values = array();
$this->updated = array(); $this->updated = array();
$this->pk = "id"; $this->pk = "id";
$this->initTable();
}
private function initTable(): void
{
if ($this->table == "") {
$this->table = $this->getDefaultTableName(); $this->table = $this->getDefaultTableName();
} }
}
public function getTable(): string public function getTable(): string
{ {