Initial commit

This commit is contained in:
Filip Znachor 2024-01-26 19:30:13 +01:00
commit e5247563fe
3 changed files with 105 additions and 0 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 CodeSpace.cz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

10
composer.json Normal file
View file

@ -0,0 +1,10 @@
{
"name": "codespace/database",
"description": "Very simple database wrapper built on PDO",
"license": "MIT",
"autoload": {
"psr-4": {
"CodeSpace\\Database\\": "src/"
}
}
}

74
src/Database.php Normal file
View file

@ -0,0 +1,74 @@
<?php namespace CodeSpace\Database;
use PDO;
use PDOStatement;
class Database {
/**
* PDO instance
*/
public PDO $db;
/**
* Database connection
* @param string $dialect database dialect
* @param string $host database host
* @param string $db database name
* @param string $user database user
* @param string $password user password
*/
public function __construct(string $dialect, string $host, string $db, string $user, string $password) {
$dsn = "$dialect:host=$host;dbname=$db";
$this->db = new PDO($dsn, $user, $password);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
/**
* Query list of items from DB
* @param string $sql SQL query
* @param array $array parameters
* @return array array of results
*/
public function query(string $sql, array $array = []): array {
$statement = $this->db->prepare($sql);
$statement->execute($array);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Run query on the DB
* @param string $sql SQL query
* @param array $array parameters
* @return bool true on success or false on failure
*/
public function run(string $sql, array $array = []): bool {
$statement = $this->db->prepare($sql);
return $statement->execute($array);
}
/**
* Run query on the DB and return raw statement
* @param string $sql SQL query
* @param array $array parameters
* @return PDOStatement|bool PDO statement od false on failure
*/
public function raw(string $sql, array $array = []): PDOStatement | bool {
$statement = $this->db->prepare($sql);
$statement->execute($array);
return $statement;
}
/**
* Get first row of query from DB
* @param string $sql SQL query
* @param array $array parameters
* @return ?array first row or null
*/
public function row(string $sql, array $array = []): ?array {
$resp = self::query($sql, $array);
if(sizeOf($resp) == 0) return null;
return $resp[0];
}
}