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