Initial commit
This commit is contained in:
71
app/Core/OldQueryBuilder.php
Normal file
71
app/Core/OldQueryBuilder.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace App\Core;
|
||||
|
||||
use PDO;
|
||||
|
||||
class OldQueryBuilder {
|
||||
protected $pdo;
|
||||
protected $table;
|
||||
protected $where = [];
|
||||
protected $params = [];
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
public function table($table) {
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function where($column, $value) {
|
||||
$this->where[] = "{$column} = :{$column}";
|
||||
$this->params[$column] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get() {
|
||||
$sql = "SELECT * FROM {$this->table}";
|
||||
if (!empty($this->where)) {
|
||||
$sql .= " WHERE " . implode(' AND ', $this->where);
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($this->params);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
public function insert(array $data) {
|
||||
$columns = implode(', ', array_keys($data));
|
||||
$placeholders = ':' . implode(', :', array_keys($data));
|
||||
|
||||
$sql = "INSERT INTO {$this->table} ({$columns}) VALUES ({$placeholders})";
|
||||
return $this->pdo->prepare($sql)->execute($data);
|
||||
}
|
||||
|
||||
public function update(array $data) {
|
||||
$fields = "";
|
||||
foreach ($data as $key => $value) {
|
||||
$fields .= "{$key} = :{$key}, ";
|
||||
}
|
||||
$fields = rtrim($fields, ', ');
|
||||
|
||||
$sql = "UPDATE {$this->table} SET {$fields}";
|
||||
if (!empty($this->where)) {
|
||||
$sql .= " WHERE " . implode(' AND ', $this->where);
|
||||
}
|
||||
|
||||
// Merge update data with where parameters
|
||||
return $this->pdo->prepare($sql)->execute(array_merge($data, $this->params));
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
$sql = "DELETE FROM {$this->table}";
|
||||
if (!empty($this->where)) {
|
||||
$sql .= " WHERE " . implode(' AND ', $this->where);
|
||||
}
|
||||
|
||||
return $this->pdo->prepare($sql)->execute($this->params);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user