/** * Prepara um SQL para DELETE * * @param boolean $whereAddOnly Prepara o SQL somente com os parametros definidos com where * @author Hugo Ferreira da Silva * @link http://www.hufersil.com.br/lumine * @throws Lumine_Exception * @return string SQL preparado para DELETE */ protected function _deleteSQL($whereAddOnly = false) { $fields = array(); $values = array(); $where = array(); $a = $this->alias(); $this->alias(''); $where_str = ''; if ($whereAddOnly == true) { // para cada condicao em where foreach ($this->_where as $i => $item) { // tiramos espacos em branco $item = trim($item); // se iniciar com OR ou AND if (preg_match('@^\\b(or|and)\\b@i', $item)) { // somente adicionamos na clausula $where_str .= ' ' . $item; // do contrario } else { // o padrao eh AND $where_str .= empty($where_str) ? $item : ' AND ' . $item; } } $where_str = Lumine_Parser::parseSQLValues($this, $where_str); } else { $pks = $this->metadata()->getPrimaryKeys(); foreach ($pks as $id => $def) { $name = $def['name']; $valor = $this->fieldValue($name); if ($this->{$name} !== null) { $where[] = $def['column'] . ' = ' . Lumine_Parser::getParsedValue($this, $valor, $def['type']); } } $where_str = implode(' AND ', $where); } $this->alias($a); if (empty($where_str)) { throw new Lumine_Exception('nao e possivel remover sem definicao de chaves ou argumentos WHERE', Lumine_Exception::ERROR); } $table = $this->metadata()->getTablename(); $schema = $this->_getConfiguration()->getOption('schema_name'); if (!empty($schema)) { $table = $schema . '.' . $table; } $sql = "DELETE FROM " . $table . " "; $sql .= " WHERE " . $where_str; return $sql; }
/** * Monta a SQL que sera executada * * @author Hugo Ferreira da Silva * @link http://www.hufersil.com.br/ * @return string */ public function getSQL() { if (empty($this->_union)) { Lumine_Log::warning('Nenhuma classe incluida para realizar a uniao'); return false; } $sql = array(); foreach ($this->_union as $obj) { $sql[] = "(" . trim($obj->_getSQL(Lumine_Base::SQL_SELECT)) . ")"; } if (!empty($this->_data)) { $strSQL = 'SELECT ' . Lumine_Parser::parseSQLValues($this, implode(', ', $this->_data)) . ' FROM '; } else { $strSQL = ' SELECT * FROM '; } $strSQL .= '(' . implode(PHP_EOL . ' UNION ' . PHP_EOL, $sql) . ') AS LUMINE_UNION'; if (!empty($this->_where)) { $strSQL .= PHP_EOL . " WHERE " . implode(' AND ', $this->_where); } if (!empty($this->_group)) { $strSQL .= PHP_EOL . " GROUP BY " . implode(', ', $this->_group); } if (!empty($this->_having)) { $strSQL .= PHP_EOL . " HAVING " . implode(' AND ', $this->_having); } if (!empty($this->_order)) { $strSQL .= PHP_EOL . " ORDER BY " . implode(', ', $this->_order); } $strSQL .= PHP_EOL . $this->_union[0]->_getConnection()->setLimit($this->_offset, $this->_limit); return $strSQL; }