2017-06-10 15:19:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Krutush\Database;
|
|
|
|
|
|
|
|
class Database{
|
|
|
|
private $pdo;
|
2018-05-13 11:43:45 +00:00
|
|
|
private $debug = false;
|
|
|
|
private $requests = [];
|
2017-06-10 15:19:03 +00:00
|
|
|
|
|
|
|
public function __construct(array $settings){
|
|
|
|
$dns = $settings['driver'] .
|
|
|
|
':host=' . $settings['host'] .
|
|
|
|
((isset($settings['port'])) ? (';port=' . $settings['port']) : '') .
|
|
|
|
';dbname=' . $settings['schema'] .
|
|
|
|
((isset($settings['charset'])) ? (';charset=' . $settings['charset']) : '');
|
|
|
|
|
2018-05-13 11:43:45 +00:00
|
|
|
$this->debug = isset($settings['debug']) ? $settings['debug'] : false;
|
2017-06-10 15:19:03 +00:00
|
|
|
$this->pdo = new \PDO($dns, $settings['username'], $settings['password'], $settings['options']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function exec(string $request){
|
|
|
|
return $this->pdo->exec($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function prepare(string $request){
|
2018-05-13 11:43:45 +00:00
|
|
|
if($this->debug)
|
|
|
|
$this->requests[] = $request;
|
|
|
|
|
2017-06-10 15:19:03 +00:00
|
|
|
return $this->pdo->prepare($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(string $request, array $values = null, $row = false){
|
|
|
|
$req = $this->prepare($request);
|
|
|
|
$req->execute($values);
|
|
|
|
if($row == false)
|
|
|
|
return $req->fetchAll();
|
|
|
|
|
|
|
|
return $req;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function select(array $fields = null){
|
|
|
|
$select = new Request\Select($this);
|
|
|
|
if(isset($fields))
|
|
|
|
return $select->fields($fields);
|
|
|
|
|
|
|
|
return $select;
|
|
|
|
}
|
2018-05-01 12:48:27 +00:00
|
|
|
|
2018-05-01 16:57:32 +00:00
|
|
|
public function insert(array $fields = null){
|
|
|
|
$insert = new Request\Insert($this);
|
|
|
|
if(isset($fields))
|
|
|
|
return $insert->fields($fields);
|
|
|
|
|
|
|
|
return $insert;
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:28:48 +00:00
|
|
|
public function update(array $fields = null){
|
|
|
|
$update = new Request\Update($this);
|
|
|
|
if(isset($fields))
|
|
|
|
return $update->fields($fields);
|
|
|
|
|
|
|
|
return $update;
|
|
|
|
}
|
|
|
|
|
2018-05-01 12:48:27 +00:00
|
|
|
public function create(string $table = null){
|
|
|
|
$create = new Request\Create($this);
|
|
|
|
if(isset($table))
|
|
|
|
return $create->table($table);
|
|
|
|
|
|
|
|
return $create;
|
|
|
|
}
|
2018-05-01 16:57:32 +00:00
|
|
|
|
|
|
|
public function drop(string $table = null){
|
|
|
|
$drop = new Request\Drop($this);
|
|
|
|
if(isset($table))
|
|
|
|
return $drop->table($table);
|
|
|
|
|
|
|
|
return $drop;
|
|
|
|
}
|
2018-05-02 19:28:48 +00:00
|
|
|
|
2018-06-27 17:20:45 +00:00
|
|
|
public function delete(){
|
|
|
|
return new Request\Delete($this);
|
|
|
|
}
|
|
|
|
|
2018-05-13 11:43:45 +00:00
|
|
|
public function getLastInsertId(): int{
|
2018-05-02 19:28:48 +00:00
|
|
|
return $this->pdo->lastInsertId();
|
|
|
|
}
|
2018-05-13 11:43:45 +00:00
|
|
|
|
|
|
|
public function getRequests(): array{
|
|
|
|
return $this->requests;
|
|
|
|
}
|
2018-05-01 16:57:32 +00:00
|
|
|
//TODO update, delete
|
2017-06-10 15:19:03 +00:00
|
|
|
}
|