diff --git a/src/Database.php b/src/Database.php index cc7d18d..57a2099 100644 --- a/src/Database.php +++ b/src/Database.php @@ -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 } \ No newline at end of file diff --git a/src/Model.php b/src/Model.php index 35a1c7f..688b86d 100644 --- a/src/Model.php +++ b/src/Model.php @@ -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; } diff --git a/src/Request/Create.php b/src/Request/Create.php index ea0beaa..3be9bae 100644 --- a/src/Request/Create.php +++ b/src/Request/Create.php @@ -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 diff --git a/src/Request/Drop.php b/src/Request/Drop.php new file mode 100644 index 0000000..8afe34b --- /dev/null +++ b/src/Request/Drop.php @@ -0,0 +1,26 @@ +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); + } +} \ No newline at end of file diff --git a/src/Request/Insert.php b/src/Request/Insert.php new file mode 100644 index 0000000..d062d7e --- /dev/null +++ b/src/Request/Insert.php @@ -0,0 +1,33 @@ +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); + } +} \ No newline at end of file