From e5247563fe35f4209be176556aacc87e93d4fca7 Mon Sep 17 00:00:00 2001 From: Filip Znachor Date: Fri, 26 Jan 2024 19:30:13 +0100 Subject: [PATCH] Initial commit --- LICENSE | 21 ++++++++++++++ composer.json | 10 +++++++ src/Database.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 src/Database.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e318484 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..deb6d80 --- /dev/null +++ b/composer.json @@ -0,0 +1,10 @@ +{ + "name": "codespace/database", + "description": "Very simple database wrapper built on PDO", + "license": "MIT", + "autoload": { + "psr-4": { + "CodeSpace\\Database\\": "src/" + } + } +} diff --git a/src/Database.php b/src/Database.php new file mode 100644 index 0000000..dbf58b0 --- /dev/null +++ b/src/Database.php @@ -0,0 +1,74 @@ +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]; + } + +}