Create Drop Insert
This commit is contained in:
parent
d49baac317
commit
73de596b86
|
@ -40,6 +40,14 @@ class Database{
|
||||||
return $select;
|
return $select;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function insert(array $fields = null){
|
||||||
|
$insert = new Request\Insert($this);
|
||||||
|
if(isset($fields))
|
||||||
|
return $insert->fields($fields);
|
||||||
|
|
||||||
|
return $insert;
|
||||||
|
}
|
||||||
|
|
||||||
public function create(string $table = null){
|
public function create(string $table = null){
|
||||||
$create = new Request\Create($this);
|
$create = new Request\Create($this);
|
||||||
if(isset($table))
|
if(isset($table))
|
||||||
|
@ -47,5 +55,13 @@ class Database{
|
||||||
|
|
||||||
return $create;
|
return $create;
|
||||||
}
|
}
|
||||||
//TODO insert, update, delete
|
|
||||||
|
public function drop(string $table = null){
|
||||||
|
$drop = new Request\Drop($this);
|
||||||
|
if(isset($table))
|
||||||
|
return $drop->table($table);
|
||||||
|
|
||||||
|
return $drop;
|
||||||
|
}
|
||||||
|
//TODO update, delete
|
||||||
}
|
}
|
|
@ -137,30 +137,40 @@ class Model{
|
||||||
return $columns;
|
return $columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getValues(){
|
||||||
|
$values = [];
|
||||||
|
foreach ($this->fields as $field => $data) {
|
||||||
|
$values[] = $data['value'];
|
||||||
|
}
|
||||||
|
return $values;
|
||||||
|
}
|
||||||
|
|
||||||
protected static function convertField($data, $field){
|
protected static function convertField($data, $field){
|
||||||
$options = static::getOptions($field);
|
$options = static::getOptions($field);
|
||||||
if(is_null($data) && isset($options['not_null']) && $options['not_null'] == true)
|
if(is_null($data)){
|
||||||
throw new DatabaseException('Can\'t set null to NOT NULL field : '.$field);
|
if(isset($options['not_null']) && $options['not_null'] == true)
|
||||||
|
throw new DatabaseException('Can\'t set null to NOT NULL field : '.$field);
|
||||||
if(isset($options['type'])){
|
}else{
|
||||||
switch(strtolower($options['type'])){
|
if(isset($options['type'])){
|
||||||
case 'int':
|
switch(strtolower($options['type'])){
|
||||||
$data = intval($data); //MAYBE: E_NOTICE on strange types
|
case 'int':
|
||||||
break;
|
$data = intval($data); //MAYBE: E_NOTICE on strange types
|
||||||
case 'char':
|
break;
|
||||||
case 'varchar':
|
case 'char':
|
||||||
case 'text':
|
case 'varchar':
|
||||||
$data = strval($data); //MAYBE: E_NOTICE on strange types
|
case 'text':
|
||||||
if(isset($options['lenght']) && strlen($data) > $options['lenght'])
|
$data = strval($data); //MAYBE: E_NOTICE on strange types
|
||||||
throw new DatabaseException('data is to long in field : '.$field);
|
if(isset($options['lenght']) && strlen($data) > $options['lenght'])
|
||||||
break;
|
throw new DatabaseException('data is to long in field : '.$field);
|
||||||
default:
|
break;
|
||||||
throw new DatabaseException('unknown type in field : '.$field);
|
default:
|
||||||
break;
|
throw new DatabaseException('unknown type in field : '.$field);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -182,6 +192,18 @@ class Model{
|
||||||
return static::prepare($req);
|
return static::prepare($req);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function insert(): Request\Insert{
|
||||||
|
$req = Connection::get(static::DATABASE)
|
||||||
|
->insert(static::getColumns())
|
||||||
|
->into(static::TABLE);
|
||||||
|
|
||||||
|
return $req;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function runInsert(){
|
||||||
|
static::insert()->run($this->getValues());
|
||||||
|
}
|
||||||
|
|
||||||
public static function create(): Request\Create{
|
public static function create(): Request\Create{
|
||||||
$req = Connection::get(static::DATABASE)
|
$req = Connection::get(static::DATABASE)
|
||||||
->create(static::TABLE);
|
->create(static::TABLE);
|
||||||
|
@ -193,12 +215,18 @@ class Model{
|
||||||
isset($options['lenght']) ? $options['lenght'] : null,
|
isset($options['lenght']) ? $options['lenght'] : null,
|
||||||
isset($options['not_null']) && $options['not_null'],
|
isset($options['not_null']) && $options['not_null'],
|
||||||
isset($options['primary']) && $options['primary'],
|
isset($options['primary']) && $options['primary'],
|
||||||
|
isset($options['unique']) && $options['unique'],
|
||||||
isset($options['custom']) ? $options['custom'] : null);
|
isset($options['custom']) ? $options['custom'] : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $req;
|
return $req;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function drop(){
|
||||||
|
return Connection::get(static::DATABASE)
|
||||||
|
->drop(static::TABLE);
|
||||||
|
}
|
||||||
|
|
||||||
/* Do advanced customuzation here */
|
/* Do advanced customuzation here */
|
||||||
protected static function prepare($req){ return $req; }
|
protected static function prepare($req){ return $req; }
|
||||||
|
|
||||||
|
|
|
@ -9,16 +9,20 @@ class Create extends Request{
|
||||||
protected $table;
|
protected $table;
|
||||||
protected $columns = [];
|
protected $columns = [];
|
||||||
protected $primary = [];
|
protected $primary = [];
|
||||||
|
protected $unique = [];
|
||||||
|
|
||||||
public function table(string $table): Create{
|
public function table(string $table): Create{
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function column(string $name, string $type, int $lenght = null, bool $not_null = false, bool $primary = false, string $more = null): Create{
|
public function column(string $name, string $type, int $lenght = null, bool $not_null = false, bool $primary = false, bool $unique = false, string $more = null): Create{
|
||||||
$this->columns[] = '`'.$name.'` '.$type.($lenght ? '('.$lenght.')' : '').($not_null ? ' NOT NULL' : '').(isset($more) ? ' '.$more : '');
|
$this->columns[] = '`'.$name.'` '.$type.($lenght ? '('.$lenght.')' : '').($not_null ? ' NOT NULL' : '').(isset($more) ? ' '.$more : '');
|
||||||
if($primary)
|
if($primary)
|
||||||
$this->primary[] = '`'.$name.'`';
|
$this->primary[] = '`'.$name.'`';
|
||||||
|
|
||||||
|
if($unique)
|
||||||
|
$this->unique[$name] = [$name];
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,11 +33,20 @@ class Create extends Request{
|
||||||
if(empty($this->columns))
|
if(empty($this->columns))
|
||||||
throw new DatabaseException('Any columns set');
|
throw new DatabaseException('Any columns set');
|
||||||
|
|
||||||
return 'CREATE `'.$this->table.'`('."\n".
|
$uniques = [];
|
||||||
|
foreach ($this->unique as $name => $columns) {
|
||||||
|
$uniques[] = 'CONSTRAINT UC_'.ucfirst(strtolower($name)).' UNIQUE ('.implode(', ', $columns).')';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'CREATE TABLE `'.$this->table.'`('."\n".
|
||||||
$sql = implode(",\n",
|
$sql = implode(",\n",
|
||||||
array_merge($this->columns, (empty($this->primary) ? [] : [
|
array_merge(
|
||||||
'CONSTRAINT PK_'.ucfirst(strtolower(strtok($this->table, ' '))).' PRIMARY KEY ('.implode(', ', $this->primary).')'
|
$this->columns,
|
||||||
]))
|
(empty($this->primary) ? [] : [
|
||||||
|
'CONSTRAINT PK_'.ucfirst(strtolower(strtok($this->table, ' '))).' PRIMARY KEY ('.implode(', ', $this->primary).')'
|
||||||
|
]),
|
||||||
|
$uniques
|
||||||
|
)
|
||||||
)."\n)";
|
)."\n)";
|
||||||
|
|
||||||
//TODO: foreign keys
|
//TODO: foreign keys
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue