Auth, Login, Home, Venta->Listados->Precios
This commit is contained in:
61
app/common/Implement/Connection.php
Normal file
61
app/common/Implement/Connection.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use PDOException;
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
class Connection implements Define\Connection
|
||||
{
|
||||
public function __construct(protected Define\Database $database) {}
|
||||
|
||||
protected PDO $connection;
|
||||
public function connect(): Define\Connection
|
||||
{
|
||||
if (!isset($this->connection)) {
|
||||
if ($this->database->needsUser()) {
|
||||
$this->connection = new PDO($this->database->getDSN(), $this->database->user, $this->database->password);
|
||||
} else {
|
||||
$this->connection = new PDO($this->database->getDSN());
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function getPDO(): PDO
|
||||
{
|
||||
$this->connect();
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
public function query(string $query): PDOStatement
|
||||
{
|
||||
$this->connect();
|
||||
$statement = $this->connection->query($query);
|
||||
if ($statement === false) {
|
||||
throw new PDOException("Query failed: '{$query}'");
|
||||
}
|
||||
return $statement;
|
||||
}
|
||||
public function prepare(string $query): PDOStatement
|
||||
{
|
||||
$this->connect();
|
||||
$statement = $this->connection->prepare($query);
|
||||
if ($statement === false) {
|
||||
throw new PDOException("Query failed: '{$query}'");
|
||||
}
|
||||
return $statement;
|
||||
}
|
||||
public function execute(string $query, ?array $data = null): PDOStatement
|
||||
{
|
||||
if ($data === null) {
|
||||
return $this->query($query);
|
||||
}
|
||||
$statement = $this->prepare($query);
|
||||
$status = $statement->execute($data);
|
||||
if ($status === false) {
|
||||
throw new PDOException("Query could not be executed: '{$query}'");
|
||||
}
|
||||
return $statement;
|
||||
}
|
||||
}
|
24
app/common/Implement/Database/MySQL.php
Normal file
24
app/common/Implement/Database/MySQL.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Database;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
class MySQL implements Define\Database
|
||||
{
|
||||
public function __construct(public string $host, public string $name, public string $user, public string $password) {}
|
||||
|
||||
public int $port = 3306;
|
||||
|
||||
public function getDSN(): string
|
||||
{
|
||||
$dsn = "mysql:host={$this->host};dbname={$this->name}";
|
||||
if ($this->port !== 3306) {
|
||||
$dsn .= ";port={$this->port}";
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
public function needsUser(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
15
app/common/Implement/Exception/EmptyResult.php
Normal file
15
app/common/Implement/Exception/EmptyResult.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Exception;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class EmptyResult extends Exception
|
||||
{
|
||||
public function __construct(string $query, ?Throwable $previous = null)
|
||||
{
|
||||
$message = "Empty results for {$query}";
|
||||
$code = 700;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user