Compare commits

...

11 Commits

Author SHA1 Message Date
sheychen dea2a99329 Delete 2018-06-27 19:20:45 +02:00
sheychen 889b946c0b Foreign: manual index 2018-06-25 09:44:15 +02:00
sheychen 8b8ae19dd0 getColumn sql and load ids fix 2018-06-22 10:13:23 +02:00
sheychen 3a36263208 Foreign and comments 2018-06-20 10:50:58 +02:00
sheychen 4fb305a1c2 Foreign, debug, datetime and ids 2018-05-13 13:43:45 +02:00
sheychen 365f2ff580 Create Update 2018-05-02 21:28:48 +02:00
sheychen 73de596b86 Create Drop Insert 2018-05-01 18:57:32 +02:00
sheychen d49baac317 Fix: not null not checked with type 2018-05-01 16:45:41 +02:00
sheychen acd024998b Rework a bit 2018-05-01 14:48:27 +02:00
sheychen 54fd0bd6ff Setup Develop 2018-04-25 19:12:44 +02:00
sheychen 950e745fd9 Move to Krutush and typing 2017-09-10 20:17:56 +02:00
11 changed files with 1115 additions and 134 deletions

View File

@ -8,12 +8,11 @@
}
],
"require": {
"krutush/krutush": "dev-master",
"sheychen/inutils": "^1.1"
"krutush/krutush": "dev-develop"
},
"autoload": {
"psr-4": {
"Krutush\\Database\\": "src/"
}
}
}
}

View File

@ -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
}

File diff suppressed because it is too large Load Diff

97
src/Request/Create.php Normal file
View File

@ -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);
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace MVC\Database\Request;
namespace Krutush\Database\Request;
class Data extends Request{
protected $values;

36
src/Request/Delete.php Normal file
View File

@ -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);
}
}

26
src/Request/Drop.php Normal file
View File

@ -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);
}
}

33
src/Request/Insert.php Normal file
View File

@ -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);
}
}

View File

@ -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){

View File

@ -1,6 +1,6 @@
<?php
namespace MVC\Database\Request;
namespace Krutush\Database\Request;
//TODO: Split in traits
//TODO: Add INTO

39
src/Request/Update.php Normal file
View File

@ -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);
}
}