Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Schema {
- public $name;
- public $engine;
- public $charset = 'utf8';
- public $collation;
- protected $fields = [];
- protected $current_field;
- function __construct($name){
- $this->name = $name;
- }
- // type
- function int(string $name, int $len = NULL){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'INT';
- if ($len != NULL)
- $this->fields[$this->current_field]['len'] = $len;
- return $this;
- }
- function integer(string $name, int $len = NULL){
- $this->int($name, $len);
- return $this;
- }
- function serial(string $name, int $len = NULL){
- //$this->bigint($name, $len)->unsigned()->auto()->unique();
- $this->fields[$this->current_field]['type'] = 'SERIAL';
- return $this;
- }
- function bigint(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'BIGINT';
- return $this;
- }
- function mediumint(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'MEDIUMINT';
- return $this;
- }
- function smallint(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'SMALLINT';
- return $this;
- }
- function tinyint(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'TINYINT';
- return $this;
- }
- function boolean(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'BOOLEAN';
- return $this;
- }
- function bool(string $name){
- $this->boolean($name);
- return $this;
- }
- function bit(string $name, int $len){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'BIT';
- $this->fields[$this->current_field]['len'] = $len;
- return $this;
- }
- function decimal(string $name, int $len = 15, int $len_dec = 4){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'DECIMAL';
- $this->fields[$this->current_field]['len'] = [$len, $len_dec];
- return $this;
- }
- function float(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'FLOAT';
- return $this;
- }
- function double(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'DOUBLE';
- return $this;
- }
- function real(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'REAL';
- return $this;
- }
- function char(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'CHAR';
- return $this;
- }
- function varchar(string $name, int $len = 60){
- if ($len > 65535)
- throw new \InvalidArgumentException("Max length is 65535");
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'VARCHAR';
- $this->fields[$this->current_field]['len'] = $len;
- return $this;
- }
- function text(string $name, int $len = NULL){
- if ($len > 65535)
- throw new \InvalidArgumentException("Max length is 65535");
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'TEXT';
- if ($len != NULL)
- $this->fields[$this->current_field]['len'] = $len;
- return $this;
- }
- function tinytext(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'TINYTEXT';
- return $this;
- }
- function mediumtext(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'MEDIUMTEXT';
- return $this;
- }
- function longtext(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'LONGTEXT';
- return $this;
- }
- function varbinary(string $name, int $len = 60){
- if ($len > 65535)
- throw new \InvalidArgumentException("Max length is 65535");
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'VARBINARY';
- $this->fields[$this->current_field]['len'] = $len;
- return $this;
- }
- function blob(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'BLOB';
- return $this;
- }
- function binary(string $name, int $len){
- if ($len > 255)
- throw new \InvalidArgumentException("Max length is 65535");
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'BINARY';
- $this->fields[$this->current_field]['len'] = $len;
- return $this;
- }
- function tinyblob(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'TINYBLOB';
- return $this;
- }
- function mediumblob(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'MEDIUMBLOB';
- return $this;
- }
- function longblob(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'LONGBLOB';
- return $this;
- }
- function json(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'JSON';
- return $this;
- }
- function set(string $name, array $values){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'SET';
- $this->fields[$this->current_field]['array'] = $values;
- return $this;
- }
- function enum(string $name, array $values){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'ENUM';
- $this->fields[$this->current_field]['array'] = $values;
- return $this;
- }
- function time(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'TIME';
- return $this;
- }
- function year(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'YEAR';
- return $this;
- }
- function date(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'DATE';
- return $this;
- }
- function datetime(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'DATETIME';
- return $this;
- }
- function timestamp(string $name){
- $this->current_field = $name;
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'TIMESTAMP';
- return $this;
- }
- function softDeletes(){
- $this->current_field = 'deleted_at';
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'DATETIME';
- return $this;
- }
- function datetimes(){
- $this->current_field = 'created_at';
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'DATETIME';
- $this->current_field = 'updated_at';
- $this->fields[$this->current_field] = [];
- $this->fields[$this->current_field]['type'] = 'DATETIME';
- return $this;
- }
- // modifiers
- function auto(){
- $this->fields[$this->current_field]['auto'] = true;
- return $this;
- }
- function unsigned(){
- $this->fields[$this->current_field]['unsigned'] = true;
- return $this;
- }
- function nullable(bool $value = true){
- $this->fields[$this->current_field]['nullable'] = $value;
- return $this;
- }
- function comment($string){
- $this->fields[$this->current_field]['comment'] = $string;
- return $this;
- }
- function default($val = NULL){
- if ($val == NULL)
- $val = 'NULL';
- $this->fields[$this->current_field]['default'] = $val;
- return $this;
- }
- function current(){
- $this->default('current_timestamp()');
- return $this;
- }
- function after(string $field){
- $this->fields[$this->current_field]['after'] = $field;
- return $this;
- }
- // Place the column "first" in the table (MySQL)
- // ALTER TABLE `aaa` ADD `inicio` INT NOT NULL FIRST;
- function first(){
- if (isset($this->fields[$this->current_field]['after']))
- unset($this->fields[$this->current_field]['after']);
- foreach ($this->fields as $k => $field){
- if (isset($this->fields[$k]['first']))
- unset($this->fields[$k]['first']);
- }
- $this->fields[$this->current_field]['first'] = true;
- return $this;
- }
- // debe crear un índice
- function unique(){
- $this->fields[$this->current_field]['unique'] = true;
- return $this;
- }
- // reflexion
- function getSchema(){
- return $this->fields;
- }
- }
- /*
- https://www.w3resource.com/mysql/mysql-data-types.php
- https://manuales.guebs.com/mysql-5.0/spatial-extensions.html
- */
- $table = new Schema('test');
- $table->name = 'users';
- $table->engine = 'InnoDB';
- $table->charset = 'utf8';
- $table->collation = 'utf8_general_ci';
- $table->integer('id')->auto()->unsigned();
- $table->varchar('firstname');
- $table->varchar('lastname')->nullable();
- $table->varchar('password', 128);
- $table->char('password');
- $table->varbinary('texto_vb', 300);
- // BLOB and TEXT columns cannot have DEFAULT values.
- $table->text('texto');
- $table->tinytext('texto_tiny');
- $table->mediumtext('texto_md');
- $table->longtext('texto_long');
- $table->blob('codigo');
- $table->tinyblob('blob_tiny');
- $table->mediumblob('blob_md');
- $table->longblob('blob_long');
- $table->binary('bb', 255);
- $table->json('json_str');
- $table->serial('id');
- $table->int('karma')->default(100);
- $table->bigint('big_num');
- $table->bigint('ubig')->unsigned();
- $table->mediumint('medium');
- $table->smallint('small');
- $table->tinyint('tiny');
- $table->decimal('saldo');
- $table->float('flotante');
- $table->double('doble_p');
- $table->real('num_real');
- $table->bit('some_bits', 3);
- $table->boolean('active')->default(1);
- $table->boolean('paused')->default(true);
- $table->set('flavors', ['strawberry', 'vanilla']);
- $table->enum('role', ['admin', 'normal']);
- /*
- The major difference between DATETIME and TIMESTAMP is that TIMESTAMP values are converted from the current time zone to UTC while storing, and converted back from UTC to the current time zone when retrieved. The datetime data type value is unchanged.
- */
- $table->time('hora');
- $table->year('birth_year');
- $table->date('fecha')->first();
- $table->datetime('vencimiento')->nullable()->after('num_real');
- $table->timestamp('ts')->current()->comment('some comment')->first(); // solo un first
- $table->softDeletes(); // agrega DATETIME deleted_at
- $table->datetimes(); // agrega DATETIME(s) no-nullables created_at y deleted_at
- echo '<pre>';
- var_dump($table->getSchema());
- echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement