-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSQLServer.php
90 lines (74 loc) · 2.68 KB
/
SQLServer.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
<?php
namespace koolreport\querybuilder;
class SQLServer extends SQL
{
protected $identifierQuotes = array('"', '"'); //For table name and column name
protected function buildProcedureQuery($options = [])
{
$sql = "";
foreach ($this->query->procedures as $proc) {
$statement = "EXEC " . $proc[0] . " @params ;";
$params = [];
foreach ($proc[1] as $value) {
if (gettype($value) === "string") {
array_push($params, $this->coverValue($this->escapeString($value)));
} else {
array_push($params, $value);
}
}
$statement = str_replace("@params", implode(",", $params), $statement);
$sql .= $statement;
}
return $sql;
}
protected function buildSelectQuery($options = [])
{
$sql = "SELECT ";
if ($this->query->distinct) {
$sql .= "DISTINCT ";
}
if (count($this->query->columns) > 0) {
$sql .= $this->getSelect($this->query->columns);
} else {
$sql .= "*";
}
if (count($this->query->tables) > 0) {
$sql .= " FROM " . $this->getFrom($this->query->tables);
} else {
throw new \Exception("No table available in SQL Query");
}
if (count($this->query->joins) > 0) {
$sql .= $this->getJoin($this->query->joins);
}
if (count($this->query->conditions) > 0) {
$sql .= " WHERE " . $this->getWhere($this->query->conditions);
}
if (count($this->query->groups) > 0) {
$sql .= " GROUP BY " . $this->getGroupBy($this->query->groups);
}
if ($this->query->having) {
$sql .= " HAVING " . $this->getHaving($this->query->having);
}
/*
SQL Server requires ORDER BY and OFFET ROWS if using FETCH ROWS
*/
if (isset($this->query->limit)) {
if (empty($this->query->orders)) $this->query->orders = [['[{raw}]', 1]];
if (empty($this->query->offset)) $this->query->offset = 0;
}
if (count($this->query->orders) > 0) {
$sql .= " ORDER BY " . $this->getOrderBy($this->query->orders);
}
if ($this->query->offset !== null) {
$sql .= " OFFSET " . $this->query->offset . " ROWS";
}
if ($this->query->limit !== null) {
$sql .= " FETCH NEXT " . $this->query->limit . " ROWS ONLY";
}
if (count($this->query->unions) > 0) {
$sql .= $this->getUnions($this->query->unions);
}
// echo "sql: $sql<br>";
return $sql;
}
}