From 605744e22e4c9c5a7eb02bd7b5af0f2f0da59824 Mon Sep 17 00:00:00 2001 From: Aman Rawat Date: Sat, 2 Nov 2024 21:10:24 +0530 Subject: [PATCH] Implement QueryBuilder for database operations --- examples/database-connection/README.md | 23 +++++ examples/database-connection/composer.json | 18 ++++ examples/database-connection/index.php | 29 ++++++ src/Helpers/DatabaseHelpler.php | 103 +++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 examples/database-connection/README.md create mode 100644 examples/database-connection/composer.json create mode 100644 examples/database-connection/index.php create mode 100644 src/Helpers/DatabaseHelpler.php diff --git a/examples/database-connection/README.md b/examples/database-connection/README.md new file mode 100644 index 0000000..1cb3b34 --- /dev/null +++ b/examples/database-connection/README.md @@ -0,0 +1,23 @@ +# Basic Auth Example + +This example demonstrates how to Connect Database in Dumbo. + +## Running the Example + +1. Install dependencies: + + ```bash + composer install + ``` + +2. Start the server: + + ```bash + composer start + ``` + +3. Access the protected route: + + ```bash + curl -u user:password http://localhost:8000 + ``` diff --git a/examples/database-connection/composer.json b/examples/database-connection/composer.json new file mode 100644 index 0000000..977521e --- /dev/null +++ b/examples/database-connection/composer.json @@ -0,0 +1,18 @@ +{ + "require": { + "notrab/dumbo": "@dev" + }, + "repositories": [ + { + "type": "path", + "url": "../../" + } + ], + "scripts": { + "start": [ + "Composer\\Config::disableProcessTimeout", + "php -S localhost:8000 -t ." + ] + } + } + \ No newline at end of file diff --git a/examples/database-connection/index.php b/examples/database-connection/index.php new file mode 100644 index 0000000..f8858fa --- /dev/null +++ b/examples/database-connection/index.php @@ -0,0 +1,29 @@ +use(function ($container) { + $dbHelper = new DatabaseHelper('localhost', 'your_db', 'username', 'password'); + $container->set('db', $dbHelper); +}); + +$app->get('/', function ($container) { + $db = $container->get('db'); + $queryBuilder = $db->table('your_table'); + + try { + $results = $queryBuilder->select('*')->get(); + foreach ($results as $result) { + print_r($result); + } + } catch (Exception $e) { + error_log('Database error: ' . $e->getMessage()); + } +}); +$app->run(); diff --git a/src/Helpers/DatabaseHelpler.php b/src/Helpers/DatabaseHelpler.php new file mode 100644 index 0000000..f168692 --- /dev/null +++ b/src/Helpers/DatabaseHelpler.php @@ -0,0 +1,103 @@ +pdo = new PDO($dsn, $user, $password); + $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } catch (PDOException $e) { + error_log('Database connection error: ' . $e->getMessage()); + throw new Exception('Database connection error'); + } + } + + public function table($table) + { + return new QueryBuilder($this->pdo, $table); + } +} + +class QueryBuilder +{ + private $pdo; + private $table; + private $select = '*'; + private $where = []; + private $params = []; + + public function __construct(PDO $pdo, $table) + { + $this->pdo = $pdo; + $this->table = $table; + } + + public function select($columns = '*') + { + $this->select = $columns; + return $this; + } + + public function where($column, $operator, $value) + { + $this->where[] = "$column $operator :$column"; + $this->params[$column] = $value; + return $this; + } + + public function get() + { + $whereClause = count($this->where) ? 'WHERE ' . implode(' AND ', $this->where) : ''; + $sql = "SELECT {$this->select} FROM {$this->table} $whereClause"; + $stmt = $this->pdo->prepare($sql); + $stmt->execute($this->params); + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } + + public function insert($data) + { + $fields = implode(", ", array_keys($data)); + $placeholders = ":" . implode(", :", array_keys($data)); + $sql = "INSERT INTO {$this->table} ($fields) VALUES ($placeholders)"; + $stmt = $this->pdo->prepare($sql); + $stmt->execute($data); + return $this->pdo->lastInsertId(); + } + + public function update($data) + { + $set = ""; + foreach ($data as $key => $value) { + $set .= "$key = :$key, "; + } + $set = rtrim($set, ", "); + $sql = "UPDATE {$this->table} SET $set"; + if ($this->where) { + $sql .= ' WHERE ' . implode(' AND ', $this->where); + } + $stmt = $this->pdo->prepare($sql); + $stmt->execute(array_merge($data, $this->params)); + return $this; + } + + public function delete() + { + $sql = "DELETE FROM {$this->table}"; + if ($this->where) { + $sql .= ' WHERE ' . implode(' AND ', $this->where); + } + $stmt = $this->pdo->prepare($sql); + $stmt->execute($this->params); + return $this; + } +}