Compare commits
11 Commits
Author | SHA1 | Date |
---|---|---|
sheychen | dea2a99329 | |
sheychen | 889b946c0b | |
sheychen | 8b8ae19dd0 | |
sheychen | 3a36263208 | |
sheychen | 4fb305a1c2 | |
sheychen | 365f2ff580 | |
sheychen | 73de596b86 | |
sheychen | d49baac317 | |
sheychen | acd024998b | |
sheychen | 54fd0bd6ff | |
sheychen | 950e745fd9 |
|
@ -8,12 +8,11 @@
|
|||
}
|
||||
],
|
||||
"require": {
|
||||
"krutush/krutush": "dev-master",
|
||||
"sheychen/inutils": "^1.1"
|
||||
"krutush/krutush": "dev-develop"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Krutush\\Database\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ namespace Krutush\Database;
|
|||
|
||||
class Database{
|
||||
private $pdo;
|
||||
private $debug = false;
|
||||
private $requests = [];
|
||||
|
||||
public function __construct(array $settings){
|
||||
$dns = $settings['driver'] .
|
||||
|
@ -12,6 +14,7 @@ class Database{
|
|||
';dbname=' . $settings['schema'] .
|
||||
((isset($settings['charset'])) ? (';charset=' . $settings['charset']) : '');
|
||||
|
||||
$this->debug = isset($settings['debug']) ? $settings['debug'] : false;
|
||||
$this->pdo = new \PDO($dns, $settings['username'], $settings['password'], $settings['options']);
|
||||
}
|
||||
|
||||
|
@ -20,6 +23,9 @@ class Database{
|
|||
}
|
||||
|
||||
public function prepare(string $request){
|
||||
if($this->debug)
|
||||
$this->requests[] = $request;
|
||||
|
||||
return $this->pdo->prepare($request);
|
||||
}
|
||||
|
||||
|
@ -39,5 +45,49 @@ class Database{
|
|||
|
||||
return $select;
|
||||
}
|
||||
//TODO insert, update, delete
|
||||
|
||||
public function insert(array $fields = null){
|
||||
$insert = new Request\Insert($this);
|
||||
if(isset($fields))
|
||||
return $insert->fields($fields);
|
||||
|
||||
return $insert;
|
||||
}
|
||||
|
||||
public function update(array $fields = null){
|
||||
$update = new Request\Update($this);
|
||||
if(isset($fields))
|
||||
return $update->fields($fields);
|
||||
|
||||
return $update;
|
||||
}
|
||||
|
||||
public function create(string $table = null){
|
||||
$create = new Request\Create($this);
|
||||
if(isset($table))
|
||||
return $create->table($table);
|
||||
|
||||
return $create;
|
||||
}
|
||||
|
||||
public function drop(string $table = null){
|
||||
$drop = new Request\Drop($this);
|
||||
if(isset($table))
|
||||
return $drop->table($table);
|
||||
|
||||
return $drop;
|
||||
}
|
||||
|
||||
public function delete(){
|
||||
return new Request\Delete($this);
|
||||
}
|
||||
|
||||
public function getLastInsertId(): int{
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
public function getRequests(): array{
|
||||
return $this->requests;
|
||||
}
|
||||
//TODO update, delete
|
||||
}
|
951
src/Model.php
951
src/Model.php
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Krutush\Database\Request;
|
||||
|
||||
use Krutush\Database\Database;
|
||||
use Krutush\Database\DatabaseException;
|
||||
|
||||
class Create extends Request{
|
||||
protected $table;
|
||||
protected $columns = [];
|
||||
protected $primary = [];
|
||||
protected $unique = [];
|
||||
protected $index = [];
|
||||
protected $foreign = [];
|
||||
|
||||
public function table(string $table): Create{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function column(string $name, string $type, int $lenght = null, bool $not_null = false, string $more = null): Create{ //Really ?
|
||||
$this->columns[] = '`'.$name.'` '.$type.($lenght ? '('.$lenght.')' : '').($not_null ? ' NOT NULL' : '').(isset($more) ? ' '.$more : '');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function primary(string $name): Create{
|
||||
$this->primary[] = '`'.$name.'`';
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unique(string $name): Create{
|
||||
$this->unique[$name] = ['`'.$name.'`'];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function uniques(string $name, array $columns): Create{
|
||||
$this->unique[$name] = [$columns];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function index(string $name): Create{
|
||||
$this->index[$name] = ['`'.$name.'`'];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function indexs(string $name, array $columns): Create{
|
||||
$this->index[$name] = [$columns];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function foreign(string $name, string $table, string $column, string $on_delete = null, string $on_update = null): Create{ //TODO: complex foreign
|
||||
$this->foreign[$name] = compact('name', 'table', 'column', 'on_delete', 'on_update');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sql(){
|
||||
if(!isset($this->table))
|
||||
throw new DatabaseException('Any table set');
|
||||
|
||||
if(empty($this->columns))
|
||||
throw new DatabaseException('Any columns set');
|
||||
|
||||
$uniques = [];
|
||||
foreach ($this->unique as $name => $columns) {
|
||||
$uniques[] = 'CONSTRAINT `UQ_'.ucfirst(strtolower($this->table)).'_'.ucfirst(strtolower($name)).'` UNIQUE ('.implode(', ', $columns).')';
|
||||
}
|
||||
|
||||
$indexs = [];
|
||||
foreach ($this->index as $name => $columns) {
|
||||
$indexs[] = 'INDEX `ID_'.ucfirst(strtolower($this->table)).'_'.ucfirst(strtolower($name)).'` ('.implode(', ', $columns).')';
|
||||
}
|
||||
|
||||
$foreigns = [];
|
||||
foreach ($this->foreign as $name => $options) {
|
||||
$foreigns[] = 'CONSTRAINT `FK_'.ucfirst(strtolower($this->table)).'_'.ucfirst(strtolower($name)).'` FOREIGN KEY (`'.$options['name'].'`) REFERENCES `'.$options['table'].'` (`'.$options['column'].'`)'.
|
||||
(isset($options['on_delete']) ? 'ON DELETE '.$options['on_delete'] : '').
|
||||
(isset($options['on_update']) ? 'ON UPDATE '.$options['on_update'] : '');
|
||||
}
|
||||
|
||||
return 'CREATE TABLE `'.$this->table.'`('."\n".
|
||||
$sql = implode(",\n",
|
||||
array_merge(
|
||||
$this->columns,
|
||||
(empty($this->primary) ? [] : [
|
||||
'CONSTRAINT `PK_'.ucfirst(strtolower(strtok($this->table, ' '))).'` PRIMARY KEY ('.implode(', ', $this->primary).')'
|
||||
]),
|
||||
$indexs,
|
||||
$uniques,
|
||||
$foreigns
|
||||
)
|
||||
)."\n)";
|
||||
}
|
||||
|
||||
public function run(array $values = null){
|
||||
return parent::execute($this->sql(), $values);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace MVC\Database\Request;
|
||||
namespace Krutush\Database\Request;
|
||||
|
||||
class Data extends Request{
|
||||
protected $values;
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Krutush\Database\Request;
|
||||
|
||||
//TODO: Split in traits
|
||||
//TODO: Add INTO
|
||||
//TODO: Add UNION
|
||||
use Krutush\Database\DatabaseException;
|
||||
|
||||
class Delete extends Data{
|
||||
protected $table;
|
||||
protected $where;
|
||||
|
||||
public function from(string $table, bool $add = false): Delete{
|
||||
$this->table = ($add && $this->table ? $this->table.', ' : '').$table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function where(string $where, bool $add = false): Delete{
|
||||
$this->where = $add && $this->where ? '('.$this->where.') AND ('.$where.')' : $where;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sql(){
|
||||
if(!isset($this->table))
|
||||
throw new DatabaseException('Any table set');
|
||||
|
||||
$sql = 'DELETE FROM '.$this->table.
|
||||
($this->where ? ("\n".'WHERE '.$this->where) : '');
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function run(array $values = null){
|
||||
return parent::execute($this->sql(), $values);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Krutush\Database\Request;
|
||||
|
||||
use Krutush\Database\Database;
|
||||
use Krutush\Database\DatabaseException;
|
||||
|
||||
class Drop extends Request{
|
||||
protected $table;
|
||||
|
||||
public function table(string $table): Drop{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sql(){
|
||||
if(!isset($this->table))
|
||||
throw new DatabaseException('Any table set');
|
||||
|
||||
return 'DROP TABLE `'.$this->table.'`';
|
||||
}
|
||||
|
||||
public function run(array $values = null){
|
||||
return parent::execute($this->sql(), $values);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Krutush\Database\Request;
|
||||
|
||||
use Krutush\Database\DatabaseException;
|
||||
|
||||
class Insert extends Data{
|
||||
protected $fields;
|
||||
protected $table;
|
||||
|
||||
public function fields(array $fields = null, bool $add = false): Insert{
|
||||
$this->fields = $add ? array_merge($this->fields, $fields) : $fields;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function into(string $table): Insert{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sql(){
|
||||
if(!isset($this->table))
|
||||
throw new DatabaseException('Any table set');
|
||||
|
||||
return 'INSERT INTO `'.$this->table."`\n".
|
||||
'('.implode(', ', $this->fields).")\n".
|
||||
'VALUES ('. str_repeat('?, ', count($this->fields)-1).(count($this->fields) > 0 ? '?' : '').')';
|
||||
}
|
||||
|
||||
public function run(array $values = null){
|
||||
return parent::execute($this->sql(), $values);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace MVC\Database\Request;
|
||||
namespace Krutush\Database\Request;
|
||||
|
||||
use MVC\Database\Database;
|
||||
use Krutush\Database\Database;
|
||||
|
||||
class Request{
|
||||
class Request{ //TODO: escape and slugify
|
||||
protected $db;
|
||||
|
||||
public function __construct(Database $db){
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace MVC\Database\Request;
|
||||
namespace Krutush\Database\Request;
|
||||
|
||||
//TODO: Split in traits
|
||||
//TODO: Add INTO
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace Krutush\Database\Request;
|
||||
|
||||
use Krutush\Database\DatabaseException;
|
||||
|
||||
class Update extends Data{
|
||||
protected $fields;
|
||||
protected $table;
|
||||
protected $where;
|
||||
|
||||
public function fields(array $fields = null, bool $add = false): Update{
|
||||
$this->fields = $add ? array_merge($this->fields, $fields) : $fields;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function table(string $table): Update{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function where(string $where, bool $add = false): Update{
|
||||
$this->where = $add && $this->where ? '('.$this->where.') AND ('.$where.')' : $where;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sql(){
|
||||
if(!isset($this->table))
|
||||
throw new DatabaseException('Any table set');
|
||||
|
||||
return 'UPDATE `'.$this->table."`\n".
|
||||
'SET '.implode(', ', array_map(function($field){ return $field.' = ?'; }, $this->fields))."\n".
|
||||
(isset($this->where) ? ('WHERE '.$this->where) : '');
|
||||
}
|
||||
|
||||
public function run(array $values = null){
|
||||
return parent::execute($this->sql(), $values);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue