Advertisement
linuxyamigos

Untitled

Dec 29th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.72 KB | None | 0 0
  1. <?php
  2.  
  3. class Schema {
  4.     public $name;
  5.     public $engine;
  6.     public $charset = 'utf8';
  7.     public $collation;
  8.    
  9.     protected $fields = [];
  10.     protected $current_field;
  11.    
  12.     function __construct($name){
  13.         $this->name = $name;
  14.     }  
  15.    
  16.     // type
  17.    
  18.     function int(string $name, int $len = NULL){
  19.         $this->current_field = $name;
  20.         $this->fields[$this->current_field] = [];
  21.         $this->fields[$this->current_field]['type'] = 'INT';
  22.        
  23.         if ($len != NULL)
  24.             $this->fields[$this->current_field]['len'] = $len;
  25.        
  26.         return $this;      
  27.     }  
  28.    
  29.     function integer(string $name, int $len = NULL){
  30.         $this->int($name, $len);
  31.         return $this;      
  32.     }  
  33.    
  34.     function serial(string $name, int $len = NULL){    
  35.         //$this->bigint($name, $len)->unsigned()->auto()->unique();
  36.         $this->fields[$this->current_field]['type'] = 'SERIAL';
  37.         return $this;      
  38.     }  
  39.    
  40.     function bigint(string $name){
  41.         $this->current_field = $name;
  42.         $this->fields[$this->current_field] = [];
  43.         $this->fields[$this->current_field]['type'] = 'BIGINT';
  44.         return $this;      
  45.     }  
  46.    
  47.     function mediumint(string $name){
  48.         $this->current_field = $name;
  49.         $this->fields[$this->current_field] = [];
  50.         $this->fields[$this->current_field]['type'] = 'MEDIUMINT';
  51.         return $this;      
  52.     }  
  53.    
  54.     function smallint(string $name){
  55.         $this->current_field = $name;
  56.         $this->fields[$this->current_field] = [];
  57.         $this->fields[$this->current_field]['type'] = 'SMALLINT';
  58.         return $this;      
  59.     }  
  60.    
  61.     function tinyint(string $name){
  62.         $this->current_field = $name;
  63.         $this->fields[$this->current_field] = [];
  64.         $this->fields[$this->current_field]['type'] = 'TINYINT';
  65.         return $this;      
  66.     }  
  67.    
  68.     function boolean(string $name){
  69.         $this->current_field = $name;
  70.         $this->fields[$this->current_field] = [];
  71.         $this->fields[$this->current_field]['type'] = 'BOOLEAN';
  72.         return $this;      
  73.     }  
  74.    
  75.     function bool(string $name){
  76.         $this->boolean($name);
  77.         return $this;      
  78.     }
  79.    
  80.     function bit(string $name, int $len){
  81.         $this->current_field = $name;
  82.         $this->fields[$this->current_field] = [];
  83.         $this->fields[$this->current_field]['type'] = 'BIT';
  84.         $this->fields[$this->current_field]['len'] = $len;     
  85.         return $this;      
  86.     }
  87.    
  88.     function decimal(string $name, int $len = 15, int $len_dec = 4){
  89.         $this->current_field = $name;
  90.         $this->fields[$this->current_field] = [];
  91.         $this->fields[$this->current_field]['type'] = 'DECIMAL';
  92.         $this->fields[$this->current_field]['len'] = [$len, $len_dec];     
  93.         return $this;      
  94.     }  
  95.    
  96.     function float(string $name){
  97.         $this->current_field = $name;
  98.         $this->fields[$this->current_field] = [];
  99.         $this->fields[$this->current_field]['type'] = 'FLOAT';
  100.         return $this;      
  101.     }  
  102.    
  103.     function double(string $name){
  104.         $this->current_field = $name;
  105.         $this->fields[$this->current_field] = [];
  106.         $this->fields[$this->current_field]['type'] = 'DOUBLE';
  107.         return $this;      
  108.     }  
  109.    
  110.     function real(string $name){
  111.         $this->current_field = $name;
  112.         $this->fields[$this->current_field] = [];
  113.         $this->fields[$this->current_field]['type'] = 'REAL';
  114.         return $this;      
  115.     }  
  116.    
  117.     function char(string $name){       
  118.         $this->current_field = $name;
  119.         $this->fields[$this->current_field] = [];
  120.         $this->fields[$this->current_field]['type'] = 'CHAR';
  121.         return $this;      
  122.     }  
  123.    
  124.     function varchar(string $name, int $len = 60){
  125.         if ($len > 65535)
  126.             throw new \InvalidArgumentException("Max length is 65535");
  127.        
  128.         $this->current_field = $name;
  129.         $this->fields[$this->current_field] = [];
  130.         $this->fields[$this->current_field]['type'] = 'VARCHAR';
  131.         $this->fields[$this->current_field]['len'] = $len;
  132.         return $this;      
  133.     }  
  134.    
  135.     function text(string $name, int $len = NULL){
  136.         if ($len > 65535)
  137.             throw new \InvalidArgumentException("Max length is 65535");
  138.        
  139.         $this->current_field = $name;
  140.         $this->fields[$this->current_field] = [];
  141.         $this->fields[$this->current_field]['type'] = 'TEXT';
  142.        
  143.         if ($len != NULL)
  144.             $this->fields[$this->current_field]['len'] = $len;
  145.        
  146.         return $this;      
  147.     }  
  148.    
  149.     function tinytext(string $name){       
  150.         $this->current_field = $name;
  151.         $this->fields[$this->current_field] = [];
  152.         $this->fields[$this->current_field]['type'] = 'TINYTEXT';
  153.         return $this;      
  154.     }
  155.    
  156.     function mediumtext(string $name){     
  157.         $this->current_field = $name;
  158.         $this->fields[$this->current_field] = [];
  159.         $this->fields[$this->current_field]['type'] = 'MEDIUMTEXT';
  160.         return $this;      
  161.     }
  162.    
  163.     function longtext(string $name){       
  164.         $this->current_field = $name;
  165.         $this->fields[$this->current_field] = [];
  166.         $this->fields[$this->current_field]['type'] = 'LONGTEXT';
  167.         return $this;      
  168.     }
  169.    
  170.     function varbinary(string $name, int $len = 60){
  171.         if ($len > 65535)
  172.             throw new \InvalidArgumentException("Max length is 65535");
  173.        
  174.         $this->current_field = $name;
  175.         $this->fields[$this->current_field] = [];
  176.         $this->fields[$this->current_field]['type'] = 'VARBINARY';
  177.         $this->fields[$this->current_field]['len'] = $len;
  178.         return $this;      
  179.     }
  180.    
  181.     function blob(string $name){
  182.         $this->current_field = $name;
  183.         $this->fields[$this->current_field] = [];
  184.         $this->fields[$this->current_field]['type'] = 'BLOB';
  185.         return $this;      
  186.     }  
  187.    
  188.     function binary(string $name, int $len){
  189.         if ($len > 255)
  190.             throw new \InvalidArgumentException("Max length is 65535");
  191.        
  192.         $this->current_field = $name;
  193.         $this->fields[$this->current_field] = [];
  194.         $this->fields[$this->current_field]['type'] = 'BINARY';
  195.         $this->fields[$this->current_field]['len'] = $len;
  196.         return $this;      
  197.     }
  198.    
  199.     function tinyblob(string $name){       
  200.         $this->current_field = $name;
  201.         $this->fields[$this->current_field] = [];
  202.         $this->fields[$this->current_field]['type'] = 'TINYBLOB';
  203.         return $this;      
  204.     }
  205.    
  206.     function mediumblob(string $name){     
  207.         $this->current_field = $name;
  208.         $this->fields[$this->current_field] = [];
  209.         $this->fields[$this->current_field]['type'] = 'MEDIUMBLOB';
  210.         return $this;      
  211.     }
  212.    
  213.     function longblob(string $name){       
  214.         $this->current_field = $name;
  215.         $this->fields[$this->current_field] = [];
  216.         $this->fields[$this->current_field]['type'] = 'LONGBLOB';
  217.         return $this;      
  218.     }
  219.    
  220.     function json(string $name){       
  221.         $this->current_field = $name;
  222.         $this->fields[$this->current_field] = [];
  223.         $this->fields[$this->current_field]['type'] = 'JSON';
  224.         return $this;      
  225.     }
  226.    
  227.     function set(string $name, array $values){     
  228.         $this->current_field = $name;
  229.         $this->fields[$this->current_field] = [];
  230.         $this->fields[$this->current_field]['type'] = 'SET';
  231.         $this->fields[$this->current_field]['array'] = $values;
  232.         return $this;      
  233.     }
  234.    
  235.     function enum(string $name, array $values){    
  236.         $this->current_field = $name;
  237.         $this->fields[$this->current_field] = [];
  238.         $this->fields[$this->current_field]['type'] = 'ENUM';
  239.         $this->fields[$this->current_field]['array'] = $values;
  240.         return $this;      
  241.     }
  242.    
  243.     function time(string $name){       
  244.         $this->current_field = $name;
  245.         $this->fields[$this->current_field] = [];
  246.         $this->fields[$this->current_field]['type'] = 'TIME';
  247.         return $this;      
  248.     }
  249.    
  250.     function year(string $name){       
  251.         $this->current_field = $name;
  252.         $this->fields[$this->current_field] = [];
  253.         $this->fields[$this->current_field]['type'] = 'YEAR';
  254.         return $this;      
  255.     }
  256.    
  257.     function date(string $name){       
  258.         $this->current_field = $name;
  259.         $this->fields[$this->current_field] = [];
  260.         $this->fields[$this->current_field]['type'] = 'DATE';
  261.         return $this;      
  262.     }
  263.    
  264.     function datetime(string $name){       
  265.         $this->current_field = $name;
  266.         $this->fields[$this->current_field] = [];
  267.         $this->fields[$this->current_field]['type'] = 'DATETIME';
  268.         return $this;      
  269.     }
  270.    
  271.     function timestamp(string $name){      
  272.         $this->current_field = $name;
  273.         $this->fields[$this->current_field] = [];
  274.         $this->fields[$this->current_field]['type'] = 'TIMESTAMP';
  275.         return $this;      
  276.     }
  277.    
  278.     function softDeletes(){    
  279.         $this->current_field = 'deleted_at';
  280.         $this->fields[$this->current_field] = [];
  281.         $this->fields[$this->current_field]['type'] = 'DATETIME';
  282.         return $this;      
  283.     }
  284.    
  285.     function datetimes(){      
  286.         $this->current_field = 'created_at';
  287.         $this->fields[$this->current_field] = [];
  288.         $this->fields[$this->current_field]['type'] = 'DATETIME';
  289.         $this->current_field = 'updated_at';
  290.         $this->fields[$this->current_field] = [];
  291.         $this->fields[$this->current_field]['type'] = 'DATETIME';
  292.         return $this;      
  293.     }
  294.    
  295.     // modifiers
  296.    
  297.     function auto(){
  298.         $this->fields[$this->current_field]['auto'] =  true;
  299.         return $this;
  300.     }
  301.    
  302.     function unsigned(){
  303.         $this->fields[$this->current_field]['unsigned'] =  true;
  304.         return $this;
  305.     }
  306.    
  307.     function nullable(bool $value =  true){
  308.         $this->fields[$this->current_field]['nullable'] =  $value;
  309.         return $this;
  310.     }
  311.    
  312.     function comment($string){
  313.         $this->fields[$this->current_field]['comment'] =  $string;
  314.         return $this;
  315.     }
  316.    
  317.     function default($val = NULL){
  318.         if ($val == NULL)
  319.             $val = 'NULL';
  320.        
  321.         $this->fields[$this->current_field]['default'] =  $val;
  322.         return $this;
  323.     }
  324.    
  325.     function current(){
  326.         $this->default('current_timestamp()'); 
  327.         return $this;
  328.     }
  329.    
  330.     function after(string $field){
  331.         $this->fields[$this->current_field]['after'] =  $field;
  332.         return $this;
  333.     }
  334.    
  335.     // Place the column "first" in the table (MySQL)
  336.     // ALTER TABLE `aaa` ADD `inicio` INT NOT NULL FIRST;
  337.     function first(){
  338.         if (isset($this->fields[$this->current_field]['after']))
  339.             unset($this->fields[$this->current_field]['after']);
  340.        
  341.         foreach ($this->fields as $k => $field){
  342.             if (isset($this->fields[$k]['first']))
  343.                 unset($this->fields[$k]['first']);
  344.         }  
  345.        
  346.         $this->fields[$this->current_field]['first'] =  true;
  347.         return $this;
  348.     }
  349.    
  350.     // debe crear un índice
  351.     function unique(){
  352.         $this->fields[$this->current_field]['unique'] =  true;
  353.         return $this;
  354.     }
  355.    
  356.     // reflexion
  357.    
  358.     function getSchema(){
  359.         return $this->fields;
  360.     }
  361. }
  362.  
  363. /*
  364.  
  365. https://www.w3resource.com/mysql/mysql-data-types.php
  366. https://manuales.guebs.com/mysql-5.0/spatial-extensions.html
  367.  
  368. */
  369.  
  370. $table = new Schema('test');
  371.  
  372. $table->name      = 'users';
  373. $table->engine    = 'InnoDB';
  374. $table->charset   = 'utf8';
  375. $table->collation = 'utf8_general_ci';
  376.  
  377. $table->integer('id')->auto()->unsigned();
  378. $table->varchar('firstname');
  379. $table->varchar('lastname')->nullable();
  380. $table->varchar('password', 128);
  381. $table->char('password');
  382. $table->varbinary('texto_vb', 300);
  383.  
  384. // BLOB and TEXT columns cannot have DEFAULT values.
  385. $table->text('texto');
  386. $table->tinytext('texto_tiny');
  387. $table->mediumtext('texto_md');
  388. $table->longtext('texto_long');
  389. $table->blob('codigo');
  390. $table->tinyblob('blob_tiny');
  391. $table->mediumblob('blob_md');
  392. $table->longblob('blob_long');
  393. $table->binary('bb', 255);
  394. $table->json('json_str');
  395.  
  396. $table->serial('id');
  397. $table->int('karma')->default(100);
  398. $table->bigint('big_num');
  399. $table->bigint('ubig')->unsigned();
  400. $table->mediumint('medium');
  401. $table->smallint('small');
  402. $table->tinyint('tiny');
  403. $table->decimal('saldo');
  404. $table->float('flotante');
  405. $table->double('doble_p');
  406. $table->real('num_real');
  407.  
  408. $table->bit('some_bits', 3);
  409. $table->boolean('active')->default(1);
  410. $table->boolean('paused')->default(true);
  411.  
  412. $table->set('flavors', ['strawberry', 'vanilla']);
  413. $table->enum('role', ['admin', 'normal']);
  414.  
  415.  
  416. /*
  417.     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.
  418. */
  419.  
  420. $table->time('hora');
  421. $table->year('birth_year');
  422. $table->date('fecha')->first();
  423. $table->datetime('vencimiento')->nullable()->after('num_real');
  424. $table->timestamp('ts')->current()->comment('some comment')->first(); // solo un first
  425.  
  426.  
  427. $table->softDeletes(); // agrega DATETIME deleted_at
  428. $table->datetimes();  // agrega DATETIME(s) no-nullables created_at y deleted_at
  429.  
  430.  
  431. echo '<pre>';
  432. var_dump($table->getSchema());
  433. echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement