/** * 编译sql语句 * * @return array */ protected function compile() { $fields = $this->fields; $updateFields = array(); $updateValues = array(); foreach ($this->expressionFields as $field => $data) { !empty($data['arguments']) and $updateValues += $data['arguments']; if ($data['expression'] instanceof Select) { $updateValues += $data['expression']->getArguments($this->queryIdentifier); $data['expression'] = ' (' . $data['expression']->getString($this->queryIdentifier) . ')'; } $updateFields[] = $field . '=' . $data['expression']; unset($fields[$field]); } $maxPlaceholder = $placeholder = 0; foreach ($fields as $field => $value) { $placeholder = ':mkUpdatePlaceholder' . $maxPlaceholder++; $updateFields[] = $field . '=' . $placeholder; $updateValues[$placeholder] = $value; } $query = 'UPDATE {:' . $this->table . ':} SET ' . implode(', ', $updateFields); if (count($this->condition)) { $query .= "\nWHERE " . $this->condition->getString($this->queryIdentifier); $updateValues = array_merge($updateValues, $this->condition->getArguments($this->queryIdentifier)); } return array('sql' => $query, 'arguments' => $updateValues); }
/** * 编译查询条件 * * @return array */ protected function compile() { $query = array(); $query['sql'] = 'DELETE FROM {:' . $this->table . ':} '; $query['arguments'] = array(); if (count($this->condition)) { $query['sql'] .= "\nWHERE " . $this->condition->getString($this->queryIdentifier); $query['arguments'] = $this->condition->getArguments($this->queryIdentifier); } return $query; }
/** * 获取组装好的查询语句 * * @param null $queryIdentifier * * @return string */ public function getString($queryIdentifier = NULL) { $qi = $queryIdentifier ? $this->queryIdentifier = $queryIdentifier : $this->queryIdentifier; //!$this->compiled() and $this->compile($this); // SELECT $query = 'SELECT '; $this->distinct and $query .= 'DISTINCT '; // FIELDS and EXPRESSIONS $fields = array(); foreach ($this->tables as $alias => $table) { !empty($table['all_fields']) and $fields[] = $alias . '.*'; } foreach ($this->fields as $field) { $fields[] = (isset($field['table']) ? $field['table'] . '.' : '') . $field['field'] . ' AS ' . $field['alias']; } foreach ($this->expressions as $expression) { $fields[] = $expression['expression'] . ' AS ' . $expression['alias']; } $query .= $fields ? implode(', ', $fields) : '*'; // FROM $query .= "\nFROM "; foreach ($this->tables as $table) { $query .= "\n"; !empty($table['join type']) and $query .= $table['join type'] . ' JOIN '; if (isset($table['table']) and $table['table'] instanceof Select) { $tableString = '(' . $table['table']->getString($qi) . ')'; } else { $tableString = '{:' . $table['table'] . ':}'; } $query .= $tableString . ' as ' . $table['alias']; !empty($table['condition']) and $query .= ' ON ' . $table['condition']; } // WHERE count($this->where) and $query .= "\nWHERE " . $this->where->getString($qi); // GROUP BY $this->group and $query .= "\nGROUP BY " . implode(', ', $this->group); // HAVING count($this->having) and $query .= "\nHAVING " . $this->having->getString($qi); // ORDER BY if ($this->order) { $query .= "\nORDER BY "; $fields = array(); foreach ($this->order as $field => $direction) { $fields[] = $field . ' ' . $direction; } $query .= implode(', ', $fields); } // RANGE if (!empty($this->range)) { $query .= "\nLIMIT " . (int) $this->range['length'] . ' OFFSET ' . (int) $this->range['start']; } // if ($this->forUpdate) { // $query .= ' FOR UPDATE' . ($this->forUpdate === true ? '' : $this->forUpdate); // } return $query; }