/** * * @param string $aliasTable * @param string $field * @param string $command * @return string */ public function generateColumn($aliasTable, $field, $command = '') { $aliasCommand = ''; if ($command) { $aliasCommand = strtoupper($command . '-'); } $aliasOrig = $aliasCommand . "{$aliasTable}-{$field}"; $aliasName = $this->_table->getAdapter()->foldCase($aliasOrig); $aliasAux = $aliasOrig; /** * O Banco não trabalha com Alias possuindo mais de 30 dígitos */ if (strlen($aliasOrig) > self::LIMIT_ALIAS_CHAR) { $increment = count($this->_replaceAlias) + 1; $aliasAux = substr($aliasOrig, 0, self::LIMIT_ALIAS_CHAR - 2 - strlen($aliasCommand)) . $increment; $this->_replaceAlias[$this->_table->getAdapter()->foldCase($aliasAux)] = $aliasName; } /** * Instancia os mappers a serem utilizados após execução da consulta */ $this->_mappers->add($aliasName, $this->getTable($aliasTable)->getMapperName(), $field); return " {$command}({$aliasTable}.{$field}) AS \"{$aliasAux}\", "; }
/** * Retornar as colunas de mapeamento para execução do recordset * * @return \ZendT_Db_Column_Mapper * @throws ZendT_Exception_Error */ public function getColumnsMapper() { if (count($this->_columns) == 0) { throw new ZendT_Exception_Error('Favor adicionar as colunas!'); } $columnsMapper = new ZendT_Db_Column_Mapper(); $_order = $this->_getOrder(); foreach ($_order as $aliasColumn) { if (!isset($this->_columns[$aliasColumn])) { continue; } $aliasTable = $this->_columns[$aliasColumn]['aliasTable']; $columnName = $this->_columns[$aliasColumn]['columnName']; if (isset($this->_columns[$aliasColumn]['expression'])) { $columnsMapper->add($aliasColumn, $this->_columns[$aliasColumn]['mapperName'], $this->_columns[$aliasColumn]['columnName'], $this->_columns[$aliasColumn]['operation'], $this->_columns[$aliasColumn]['expression']); } else { $columnsMapper->add($columnName . '_' . $aliasTable, $this->_columns[$aliasColumn]['mapperName'], $this->_columns[$aliasColumn]['columnName'], $this->_columns[$aliasColumn]['operation']); $columnsMapper->add($aliasColumn, $this->_columns[$aliasColumn]['mapperName'], $this->_columns[$aliasColumn]['columnName'], $this->_columns[$aliasColumn]['operation']); } } return $columnsMapper; }
/** * Monta o recorset para a visão * * @param ZendT_Db_Where $where * @param array $postData * @return ZendT_Grid_Data */ public function getDataGrid($where, $postData, $retrieve = false, $found = false) { /** * Através dos dados postados adquire as informações * para paginar os dados e ordená-los */ $pager = new ZendT_Grid_Paginator($postData); /** * Monta a base do SQL */ if ($found) { $cmdSelect = " SELECT 1 as found "; } else { $cmdSelect = " SELECT " . $this->getColumns($retrieve)->getColumnsSelect($retrieve, '*', $this->getModel()->getAdapter()); } $cmdFrom = " FROM " . $this->_getSqlBase(); /** * Trata a entrada do Where para ser um Grupo de Where */ if ($where instanceof ZendT_Db_Where_Group) { $whereGroup = $where; } else { if ($where instanceof ZendT_Db_Where) { $whereGroup = new ZendT_Db_Where_Group(); $whereGroup->addWhere($where); } else { $whereGroup = new ZendT_Db_Where_Group(); } } /** * Avalia se existe algum Where específico do MapperView * colocando o mesmo dentro do objeto que agrupa os wheres */ $_whereMapperView = $this->_getWhere($postData, $where); if ($_whereMapperView) { $whereGroup->addWhere($_whereMapperView); } /** * Monta o comando Where */ $binds = $whereGroup->getBinds(); $cmdWhere = " WHERE " . $whereGroup->getSqlWhere(); if ($found) { $cmdWhere .= " AND rownum = 1 "; } /** * Define a ordenação */ $orderBy = $this->_getOrderBy(); if ($orderBy) { $cmdOrderBy = " ORDER BY " . $orderBy; } else { $orderBy = str_replace("expression.", "", $pager->getOrderBy()); $cmdOrderBy = " ORDER BY " . $orderBy; } /** * Pega o número de registro para paginação */ if (!isset($postData['count'])) { $postData['count'] = true; } if (!isset($postData['page'])) { if (isset($postData['noPage'])) { $postData['page'] = false; } else { $postData['page'] = true; } } $numRows = 0; if ($postData['count']) { $sql = $this->getModel()->getAdapter()->sqlCount($cmdFrom, $cmdWhere, $pager->getLimitCount(), $pager->getLimitOffset()); $this->_prepareSql($sql, $binds, 'count'); $numRows = $this->getModel()->getAdapter()->fetchOne($sql, $binds); } /** * Configura o range de dados que será buscado */ if ($postData['page']) { $sql = $this->getModel()->getAdapter()->sqlLimit($cmdSelect, $cmdFrom, $cmdWhere, $cmdOrderBy, $pager->getLimitCount(), $pager->getLimitOffset()); $this->_prepareSql($sql, $binds, 'limit'); } else { $sql = $cmdSelect . $cmdFrom . $cmdWhere . $cmdOrderBy; $this->_prepareSql($sql, $binds, 'full'); } /** * Pega os dados */ $stmt = $this->getModel()->getAdapter()->query($sql, $binds); /** * Retorna os dados através do objeto Data * para facilar a codificação na action */ if ($found) { $mapper = new ZendT_Db_Column_Mapper(); $mapper->add('found', new ZendT_Type_Number()); $data = new ZendT_Grid_Data($stmt, $mapper, true); } else { $data = new ZendT_Grid_Data($stmt, $this->getColumns($retrieve)->getColumnsMapper(), true); } $replaceAlias = $this->getColumns($retrieve)->getReplaceAlias(); if (count($replaceAlias) > 0) { foreach ($replaceAlias as $alias => $newAlias) { $data->addReplaceAlias($newAlias, $alias); } } $data->setNumRows($numRows); $data->setNumPage($pager->getNumPage()); return $data; }