sealandia-framework/framework/database.php

146 lines
3.8 KiB
PHP
Raw Normal View History

2024-08-14 16:41:14 +02:00
<?php
2024-08-16 09:08:07 +02:00
namespace Framework;
2024-08-14 16:41:14 +02:00
use PDO;
Class Database{
private $pdo;
private $type;
private $table;
private $columns;
private $where;
private $limit;
private $query;
2024-08-15 08:54:44 +02:00
/**
* Constructor method to initialize the PDO instance
*/
2024-08-14 16:41:14 +02:00
public function __construct(){
$this->pdo = new PDO('mysql:host=' . $db_config['host'] . ';dbname=' . $db_config['database'] . ';charset=utf8mb4', $db_config['username'], $db_config['password']);
}
2024-08-15 08:54:44 +02:00
/**
* Method to start a SELECT query and specify columns
*/
2024-08-14 16:41:14 +02:00
public function select($columns = "*"){
$this->type = "SELECT";
$this->columns = $columns;
}
2024-08-15 08:54:44 +02:00
/**
* Method to specify the table to query
*/
2024-08-14 16:41:14 +02:00
public function from($table){
$this->table = $table;
}
2024-08-15 08:54:44 +02:00
/**
* Method to add a WHERE clause to the query
*/
public function where($key, $value){
2024-08-14 16:41:14 +02:00
$this->where = array($key, $value);
}
2024-08-15 08:54:44 +02:00
/**
* Method to add additional conditions to the WHERE clause
*/
public function andWhere($key, $value){
2024-08-14 16:41:14 +02:00
if(!$this->where) $this->where = array();
$this->where[$key] = $value;
}
2024-08-15 08:54:44 +02:00
/**
* Method to add a LIMIT clause to the query
*/
2024-08-14 16:41:14 +02:00
public function limit($number){
$this->limit = $number;
}
2024-08-15 08:54:44 +02:00
/**
* Method to execute the query and return all results
*/
2024-08-14 16:41:14 +02:00
public function all(){
$this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table;
2024-08-15 08:54:44 +02:00
/**
* Check if a WHERE clause is set, if so, then append it to the query.
* Then construct WHERE conditions using placeholders for prepared statements and
* join the WHERE conditions with AND and append them to the query
*/
2024-08-14 16:41:14 +02:00
if(!empty($this->where)){
$whereClauses = [];
foreach($this->where as $condition){
$whereClauses[] = $condition[0] . ' = :"' . $condition[0];
}
$this->query .= ' WHERE ' . implode(' AND ', $whereClauses);
}
if(!empty($this->limit)){
$this->query .= ' LIMIT ' . $this->limit;
}
$results = $this->pdo->prepare($Query);
2024-08-15 08:54:44 +02:00
/**
* Bind the values from the WHERE clause to the prepared statement
*/
2024-08-14 16:41:14 +02:00
foreach($this->where as $condition){
$results->bindValue(':' . $condition[0], $condition[1]);
}
$results->execute();
return $results->fetchAll();
}
2024-08-15 08:54:44 +02:00
/**
* Method to execute the query and return a single result
*/
2024-08-14 16:41:14 +02:00
public function one(){
$this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table;
2024-08-15 08:54:44 +02:00
/**
* Check if a WHERE clause is set, if so, then append it to the query.
* Then construct WHERE conditions using placeholders for prepared statements and
* join the WHERE conditions with AND and append them to the query
*/
2024-08-14 16:41:14 +02:00
if(!empty($this->where)){
$whereClauses = [];
foreach($this->where as $condition){
$whereClauses[] = $condition[0] . ' = :"' . $condition[0];
}
$this->query .= ' WHERE ' . implode(' AND ', $whereClauses);
}
2024-08-15 08:54:44 +02:00
$this->query .= " LIMIT 1";
2024-08-14 16:41:14 +02:00
$results = $this->pdo->prepare($Query);
2024-08-15 08:54:44 +02:00
/**
* Bind the values from the WHERE clause to the prepared statement
*/
2024-08-14 16:41:14 +02:00
foreach($this->where as $condition){
$results->bindValue(':' . $condition[0], $condition[1]);
}
$results->execute();
return $results->fetch();
}
}
// Database::select(id, name)->from('tablename')->all()
// Database::select(id, name)->from('tablename')->limit(100)->all()
// Database::select()->from('tablename')->where('id', 3)->one()
?>