public function prepare(DataSourceHandler $handler, array $sqls) { $indent = str_pad('', Statement::$INDENT_NESTED); $block = NULL; foreach ($sqls as $sql) { $block .= "\n{$indent}" . StringHelper::indent($sql, Statement::$INDENT_NESTED, FALSE) . ';'; } return "BEGIN{$block}\nEND;"; }
public function prepare(DataSourceHandler $handler, array $sqls) { $indent = str_pad('', SelectStatementPrint::INDENT); $block = NULL; foreach ($sqls as $sql) { $block .= "\n$indent" . StringHelper::indent($sql, SelectStatementPrint::INDENT, FALSE) . ';'; } return "BEGIN$block\nEND;"; }
public function apply(DataSourceHandler $handler, &$sql, $startWith, $limit) { $firstRecordNumber = isset($startWith) && $startWith > 0 ? $startWith + 1 : NULL; $sql = 'SELECT n.*' . (isset($firstRecordNumber) ? ', rownum AS original_rownum' : '') . ' FROM (' . "\n" . StringHelper::indent($sql, Statement::$INDENT_SELECT_SECTION_ELEMENT, TRUE) . ') n'; $lastRecordNumber = isset($limit) ? (isset($firstRecordNumber) ? $firstRecordNumber : 1) + $limit - 1 : NULL; if (isset($lastRecordNumber)) { $sql .= "\n WHERE rownum <= " . $lastRecordNumber; } if (isset($firstRecordNumber)) { $sql = "SELECT * FROM (\n" . StringHelper::indent($sql, Statement::$INDENT_SELECT_SECTION_ELEMENT, TRUE) . ")\n WHERE original_rownum >= " . $firstRecordNumber; } }
public static function assemble($isSubqueryRequired, array $columnNames = NULL, AssembledSections $assembledSections = NULL, $indent = 0, $indentBlockStart = TRUE) { if (isset($assembledSections->select)) { $sql = "SELECT $assembledSections->select" . "\n FROM $assembledSections->from" . (isset($assembledSections->where) ? "\n WHERE $assembledSections->where" : '') . (isset($assembledSections->groupBy) ? "\n GROUP BY $assembledSections->groupBy" : '') . (isset($assembledSections->having) ? "\nHAVING $assembledSections->having" : ''); } else { if (isset($assembledSections->where) || isset($assembledSections->groupBy) || isset($assembledSections->having)) { throw new UnsupportedOperationException(t("Additional sections could not be added to assembled SQL statement")); } $sql = $assembledSections->from; } if ($isSubqueryRequired) { $assembledSubquerySections = new AssembledSections( self::$TABLE_ALIAS__SOURCE . '.' . (isset($columnNames) ? implode(', ' . self::$TABLE_ALIAS__SOURCE . '.', $columnNames) : '*'), '(' . StringHelper::indent($sql, SelectStatementPrint::INDENT__SUBQUERY, FALSE) . ') ' . self::$TABLE_ALIAS__SOURCE, NULL, NULL, NULL); $sql = self::assemble(FALSE, NULL, $assembledSubquerySections, $indent, $indentBlockStart); } else { $sql = StringHelper::indent($sql, $indent, $indentBlockStart); } return $sql; }
protected function countRecords(DataControllerCallContext $callcontext, DataSourceMetaData $datasource, array $statements) { $countIdentifier = 'record_count'; $TABLE_ALIAS__COUNT = 'c'; $count = count($statements); $requestedColumnNames = array(); // No columns are requested $sql = ''; for ($i = 0; $i < $count; $i++) { $statement = $statements[$i]; list($isSubqueryRequired, $assembledSections) = $statement->prepareSections($requestedColumnNames); if ($i > 0) { $sql .= "\n UNION\n"; } $sql .= $isSubqueryRequired ? "SELECT COUNT(*) AS {$countIdentifier}\n FROM (" . Statement::assemble(FALSE, NULL, $assembledSections, Statement::$INDENT_SUBQUERY, FALSE) . ') ' . $TABLE_ALIAS__COUNT : Statement::assemble(FALSE, NULL, new AssembledSections("COUNT(*) AS {$countIdentifier}", $assembledSections->from, $assembledSections->where, $assembledSections->groupBy, $assembledSections->having)); } if ($count > 1) { $tableAlias = $TABLE_ALIAS__COUNT . '_sum'; $sql = "SELECT SUM({$tableAlias}.{$countIdentifier}) AS {$countIdentifier}\n FROM (" . StringHelper::indent($sql, Statement::$INDENT_SUBQUERY, TRUE) . ") {$tableAlias}"; } LogHelper::log_info(new StatementLogMessage('*.count', $sql)); $records = $this->executeQuery($callcontext, $datasource, $sql, new PassthroughResultFormatter()); return $records[0][$countIdentifier]; }