Добавлен класс DatabaseConfig
This commit is contained in:
23
app/app.php
23
app/app.php
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace App;
|
||||
|
||||
use App\Library\DatabaseConfig;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
@ -10,27 +11,9 @@ use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
|
||||
use App\Library\ContingentParser;
|
||||
use App\Library\Database;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
|
||||
$dbconfig = [
|
||||
"host" => "10.90.1.201",
|
||||
"database1" => "opendata",
|
||||
"database2" => "niimko",
|
||||
"user" => "niimko_user",
|
||||
"password" => "MOhA17FeboXE"
|
||||
];
|
||||
|
||||
$dbOpendata = new Database(
|
||||
"mysql:host={$dbconfig['host']};dbname={$dbconfig['database1']}",
|
||||
$dbconfig['user'],
|
||||
$dbconfig['password']
|
||||
);
|
||||
|
||||
$dbNiimko = new Database(
|
||||
"mysql:host={$dbconfig['host']};dbname={$dbconfig['database2']}",
|
||||
$dbconfig['user'],
|
||||
$dbconfig['password']
|
||||
);
|
||||
$dbOpendata = new Database(new DatabaseConfig('opendata'));
|
||||
$dbNiimko = new Database(new DatabaseConfig('niimko'));
|
||||
|
||||
$builder = new GenericBuilder();
|
||||
|
||||
|
@ -1,15 +1,19 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
|
||||
use App\Library\DatabaseConfig;
|
||||
use PDOException;
|
||||
use PDO;
|
||||
class Database extends PDO
|
||||
class Database
|
||||
{
|
||||
public function __construct(string $dsn, string $username, string $password)
|
||||
private PDO $pdo;
|
||||
public function __construct(DatabaseConfig $config)
|
||||
{
|
||||
try {
|
||||
$dsn .= ";charset=utf8";
|
||||
parent::__construct(
|
||||
$dsn = $config->getDsn();
|
||||
$username = $config->getUsername();
|
||||
$password = $config->getPassword();
|
||||
$this->pdo = new PDO(
|
||||
$dsn,
|
||||
$username,
|
||||
$password,
|
||||
@ -26,12 +30,13 @@ class Database extends PDO
|
||||
echo "Подключение прервано!\n";
|
||||
}
|
||||
|
||||
public function executeQuery(string $sql, array $params) : array|bool
|
||||
public function executeQuery(string $sql, array $params) : array|null
|
||||
{
|
||||
try {
|
||||
$stmt = $this->prepare($sql);
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$params = array_values($params);
|
||||
for ($i = 0; $i < count($params); $i++) {
|
||||
$stmt->bindParam(":v".$i+1, $params[$i]);
|
||||
$stmt->bindParam(":v".$i++, $params[$i]);
|
||||
}
|
||||
$stmt->execute();
|
||||
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
49
app/library/DatabaseConfig.php
Normal file
49
app/library/DatabaseConfig.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
|
||||
class DatabaseConfig
|
||||
{
|
||||
private string $driver;
|
||||
private string $host;
|
||||
private string $dbname;
|
||||
private string $port;
|
||||
private string $charset;
|
||||
private string $username;
|
||||
private string $password;
|
||||
|
||||
public function __construct(string $db)
|
||||
{
|
||||
$envVars = parse_ini_file('.env', true);
|
||||
$db = strtoupper($db);
|
||||
foreach ($envVars as $dbname => $dbconfig) {
|
||||
if ($dbname == $db) {
|
||||
$config = $dbconfig;
|
||||
}
|
||||
}
|
||||
$this->driver = $config['DB_DRIVER'];
|
||||
$this->host = $config['DB_HOST'];
|
||||
$this->dbname = $config['DB_NAME'];
|
||||
$this->port = $config['DB_PORT'];
|
||||
$this->charset = $config["DB_CHARSET"];
|
||||
$this->username = $config['DB_USERNAME'];
|
||||
$this->password = $config['DB_PASSWORD'];
|
||||
}
|
||||
|
||||
public function getDsn() : string
|
||||
{
|
||||
return $this->driver.":host=".$this->host
|
||||
.";dbname=".$this->dbname
|
||||
.";charset=".$this->charset
|
||||
.";port=".$this->port;
|
||||
}
|
||||
|
||||
public function getUsername() : string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function getPassword() : string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user