Esempio n. 1
0
 /**
  * Add a SQL code for a JOIN|LEFT JOIN|RIGHT JOIN
  *
  * @param string $ACT type of JOIN
  * @param string $table name of the secondary table
  * @return object clone of $this
  */
 public function _join(string $ACT, $table, $table_fun = null, $last = null, $on = true)
 {
     $t = $this;
     if ($table_fun == null) {
         $last = $table_fun;
     }
     $s_last = $last == null ? $t->builder->getLastJoinTable() : $last;
     if (is_array($table)) {
         foreach ($table as $tab_n => $tab_val) {
             $t = $t->_join($ACT, is_int($tab_n) ? $tab_val : $tab_n, is_int($tab_n) ? null : $tab_val, $s_last);
         }
         return $t;
     }
     if (is_object($table_fun) && $table_fun instanceof Closure) {
         $n = static::__construct($t->getBuilderTable());
         $n->builder->prepare = $t->builder->prepare;
         $n->builder->orOn = $t->builder->orOn;
         $n->builder->andOn = $t->builder->andOn;
         $n = $table_fun($n);
         $t->builder->orOn = $n->builder->orOn;
         $t->builder->andOn = $n->builder->andOn;
         $t->builder->prepare = $n->builder->prepare;
         if ($last == null) {
             $last = $table;
         }
     } else {
         list($table_g, $table_alias) = DB::SQL()::GET_ALIAS($table);
         if (!Schema::hasTable($table_g)) {
             throw new \Exception("Schema: {$table} doesn't exists");
         }
     }
     if (empty($t->builder->orOn) && empty($t->builder->andOn)) {
         list($table_g, $table_alias) = DB::SQL()::GET_ALIAS($table);
         if ($on) {
             list($s_last_g, $s_last_alias) = DB::SQL()::GET_ALIAS($s_last);
             $k1 = Schema::getTable($table_g)->getForeignKeyTo($s_last_g);
             $k2 = Schema::getTable($s_last_g)->getForeignKeyTo($table_g);
             if ($k1 !== null) {
                 $k = $k1;
             } else {
                 if ($k2 !== null) {
                     $k = $k2;
                 } else {
                     throw new \Exception("<br>\nCannot relate {$s_last} with {$table}: Error with foreign key\n<br>");
                 }
             }
             $c1 = ($k1 == null ? $table_alias : $s_last_alias) . "." . $k->getForeignColumn();
             $c2 = ($k1 !== null ? $table_alias : $s_last_alias) . "." . $k->getName();
             $t = $t->on($c1, '=', $c2);
             $last = $k1 != null ? $table : $s_last;
         } else {
             $last = $table_g;
         }
     }
     $t->builder->setLastJoinTable($last);
     $t->builder->join[] = DB::SQL()::JOIN($ACT, $table, $t->SQL_ON_EXP());
     $t->builder->andOn = [];
     $t->builder->orOn = [];
     return $t;
 }
Esempio n. 2
0
 /**
  * Drop the column
  *
  * @return result query
  */
 public function dropColumn($column)
 {
     if (($table = Schema::getTable($this->getTable())) == null) {
         return;
     }
     $c = $table->getColumn($column);
     if ($c == null) {
         return;
     }
     if ($c->getForeign()) {
         $this->query(DB::SQL()::DROP_FOREIGN_KEY($this->getTable(), $c->getConstraint()));
     }
     if ($c->getPrimary()) {
         $a = Schema::getTable($this->getTable());
         foreach (Schema::getAllForeignKeyToColumn($this->getTable(), $c->getName()) as $k) {
             $this->query(DB::SQL()::DROP_FOREIGN_KEY($this->getTable(), $k->getConstraint()));
             self::$tables[$k->getTable()]->getColumn($k->getName())->resetForeign($k);
         }
         $this->query(DB::SQL()::MODIFY_COLUMN_RESET($this->getTable(), $c->getName()));
         $this->query(DB::SQL()::DROP_PRIMARY_KEY($this->getTable()));
     } else {
         if ($c->hasIndex()) {
             $this->query(DB::SQL()::DROP_INDEX_KEY($this->getTable(), $c->getIndex()));
         }
     }
     if ($table->countColumns() == 1) {
         unset(Schema::$tables[$this->getTable()]);
         unset(self::$tables[$this->getTable()]);
         return $this->drop();
     } else {
         Schema::$tables[$this->getTable()]->dropColumn($c->getName());
         self::$tables[$this->getTable()]->dropColumn($c->getName());
         return $this->query(DB::SQL()::DROP_COLUMN($this->getTable(), $c->getName()));
     }
 }
Esempio n. 3
0
 /**
  * Drop all undeclared columns
  */
 public static function dropMissing()
 {
     Schema::dropMissing();
 }