/
MsSql.php
133 lines (94 loc) · 3.33 KB
/
MsSql.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
*
* LiftKit MVC PHP Framework
*
*
*/
namespace LiftKit\Database\Query;
use LiftKit\Database\Query\Exception\UnsupportedFeature;
class MsSql extends Query
{
protected function generateInsertIgnoreQuery ()
{
throw new UnsupportedFeature('The MSSQL driver doesn\'t support INSERT IGNORE queries');
}
protected function generateInsertUpdateQuery ()
{
throw new UnsupportedFeature('The MSSQL driver doesn\'t support INSERT UPDATE queries');
}
public function insertIgnore ()
{
throw new UnsupportedFeature('The MSSQL driver doesn\'t support INSERT IGNORE queries');
}
public function insertUpdate ()
{
throw new UnsupportedFeature('The MSSQL driver doesn\'t support INSERT UPDATE queries');
}
public function leftJoinUsing ($table, $field, $alias = null)
{
throw new UnsupportedFeature('The MSSQL driver doesn\'t support USING conditions');
}
public function rightJoinUsing ($table, $field, $alias = null)
{
throw new UnsupportedFeature('The MSSQL driver doesn\'t support USING conditions');
}
public function innerJoinUsing ($table, $field, $alias = null)
{
throw new UnsupportedFeature('The MSSQL driver doesn\'t support USING conditions');
}
protected function generateSelectQuery ()
{
if ($this->limit) {
$query = $this->database->createQuery()
->composeWith($this);
$query->composeWith($this->createLimitQuery());
} else {
$query = $this;
}
$queryLines[] = "SELECT " . $query->processFields();
if ($this->alias) {
$queryLines[] = "FROM " . $query->filterIdentifier($this->table) . " AS " . $query->filterIdentifier($this->alias);
} else {
$queryLines[] = "FROM " . $query->filterIdentifier($this->table);
}
$queryLines[] = $query->processJoins();
$queryLines[] = $query->processWhere();
$queryLines[] = $query->processGroupBy();
$queryLines[] = $query->processHaving();
$queryLines[] = $query->processOrderBy();
$queryLines = array_filter($queryLines);
return implode(PHP_EOL, $queryLines);
}
protected function processLimit ()
{
return '';
}
protected function createLimitQuery ()
{
if (empty($this->orderBys)) {
$this->orderBy($this->getTable() . '.' . $this->database->primaryKey($this->getTable()));
}
$tableName = $this->alias ?: $this->table;
$subQueryLines[] = "SELECT "
. $this->database->quoteIdentifier($tableName . '.' . $this->database->primaryKey($this->table))
. " AS LK_ROW_ID"
. ", ROW_NUMBER() OVER (" . $this->processOrderBy() . ") AS LK_ROW_NUMBER";
$subQueryLines[] = "FROM " . $this->database->quoteIdentifier($this->table) . " "
. ($this->alias ? "AS " . $this->database->quoteIdentifier($this->alias) : "");
$subQueryLines[] = $this->processJoins();
$subQueryLines[] = $this->processWhere();
$subQueryLines[] = $this->processGroupBy();
$subQueryLines[] = $this->processHaving();
return $this->database->createQuery()
->leftJoinEqual(
$this->database->createRaw("(" . implode(PHP_EOL, $subQueryLines) . ")"),
'LK_ROWS.LK_ROW_ID',
$this->table . '.' . $this->database->primaryKey($this->table),
'LK_ROWS'
)
->from($this->table)
->whereGreaterThanOrEqual('LK_ROWS.LK_ROW_NUMBER', $this->start + 1)
->whereLessThanOrEqual('LK_ROWS.LK_ROW_NUMBER', $this->start + $this->limit);
}
}