db
1<?php
2
3namespace Upside\Db\SQL\Compiler;
4
5use Upside\Db\SQL\Compiler;
6use Upside\Db\SQL\SQLStatement;
7
8class DB2 extends Compiler
9{
10/**
11* Returns the SQL for a select statement
12*/
13public function select(SQLStatement $select): string
14{
15$limit = $select->get_limit();
16
17if ($limit <= 0) {
18return parent::select($select);
19}
20
21$order = \trim($this->handle_orderings($select->get_order()));
22
23if (empty($order)) {
24$order = 'ORDER BY (SELECT 0)';
25}
26
27$sql = $select->get_distinct() ? 'SELECT DISTINCT ' : 'SELECT ';
28$sql .= $this->handle_columns($select->get_columns());
29$sql .= ', ROW_NUMBER() OVER (' . $order . ') AS opis_rownum';
30$sql .= ' FROM ';
31$sql .= $this->handle_tables($select->get_tables());
32$sql .= $this->handle_joins($select->get_joins());
33$sql .= $this->handle_wheres($select->get_wheres());
34$sql .= $this->handle_groupings($select->get_group_by());
35$sql .= $this->handle_havings($select->get_having());
36
37$offset = $select->get_offset();
38
39if ($offset < 0) {
40$offset = 0;
41}
42
43$limit += $offset;
44$offset++;
45
46return 'SELECT * FROM (' . $sql . ') AS m1 WHERE opis_rownum BETWEEN ' . $offset . ' AND ' . $limit; // TODO: opis_rownum
47}
48}
49