Exemplo n.º 1
0
 public function Get()
 {
     if (!empty($this->queryString)) {
         return $this->queryString;
     }
     $this->type = strtolower($this->type);
     $result = $this->type . ' ';
     if (!empty($this->queryMod)) {
         $result .= $this->queryMod . ' ';
     }
     $table_result = '';
     if (is_array($this->tables)) {
         foreach ($this->tables as $table) {
             $table_result .= $table . ', ';
         }
         $table_result = substr($table_result, 0, strlen($table_result) - 2);
     } else {
         $table_result = $this->tables;
     }
     switch ($this->type) {
         case 'select':
             $field_result = '';
             if (is_array($this->fields)) {
                 foreach ($this->fields as $field) {
                     $field_result .= $field . ', ';
                 }
                 $field_result = substr($field_result, 0, strlen($field_result) - 2);
             } else {
                 $field_result = $this->fields;
             }
             if ($this->calcRows) {
                 $result .= 'SQL_CALC_FOUND_ROWS ';
             }
             $result .= $field_result;
             if (!empty($table_result)) {
                 $result .= ' from ' . $table_result;
             }
             break;
         case 'delete':
             $result .= ' from ' . $table_result;
             break;
         case 'insert':
             $result .= ' into ';
             $result .= $table_result . ' (';
             $field_result = '';
             if (is_array($this->fields)) {
                 foreach ($this->fields as $field) {
                     $field_result .= $field . ', ';
                 }
                 $field_result = substr($field_result, 0, strlen($field_result) - 2);
             } else {
                 $field_result = $this->fields;
             }
             $result .= $field_result . ' ) values ( ';
             if (is_array($this->values)) {
                 for ($i = 0; $i < count($this->values); $i++) {
                     if (strpos($this->values[$i], 'inline:') === false) {
                         $result .= "'" . addslashes($this->values[$i]) . "', ";
                     } else {
                         $this->values[$i] = preg_replace('/^inline:(.*)/', '\\1', $this->values[$i]);
                         $result .= $this->values[$i] . ", ";
                     }
                 }
                 $result = substr($result, 0, strlen($result) - 2);
             }
             $result .= ' )';
             break;
         case 'update':
             $result .= $table_result . ' set ';
             if (is_array($this->fields)) {
                 for ($i = 0; $i < count($this->fields); $i++) {
                     if (strpos($this->values[$i], 'inline:') === false) {
                         //$result .= $this->fields[$i]." = '".addslashes($this->values[$i])."', ";
                         $result .= $this->fields[$i] . " = :" . $this->fields[$i] . ", ";
                     } else {
                         $this->values[$i] = preg_replace('/^inline:(.*)/', '\\1', $this->values[$i]);
                         $result .= $this->fields[$i] . " = " . $this->values[$i] . ", ";
                     }
                 }
                 $result = substr($result, 0, strlen($result) - 2);
             }
             break;
     }
     if (!$this->where->isEmpty()) {
         if ($this->policy) {
             $this->where->policy = $this->policy;
         }
         $result .= ' where ' . $this->where->get();
         $this->_params = array_merge($this->_params, $this->where->getParams());
     }
     if (!empty($this->groupBy)) {
         $result .= ' group by ' . $this->groupBy;
     }
     if (!empty($this->having)) {
         $result .= ' having ' . $this->having;
     }
     if (!empty($this->orderBy)) {
         $result .= ' order by ' . $this->orderBy;
     }
     if (!empty($this->limit)) {
         $result .= ' limit ' . $this->limit;
     }
     $first_union = true;
     foreach ($this->union as $union) {
         if ($first_union && empty($field_result) && empty($table_result)) {
             $result = $union->get();
         } else {
             if ($this->unionAllFlag) {
                 $result .= ' UNION ALL ' . $union->Get();
             } else {
                 $result .= ' union ' . $union->Get();
             }
         }
         $first_union = false;
     }
     return $result;
 }