-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQLBuilder.class.php
More file actions
executable file
·162 lines (149 loc) · 3.9 KB
/
SQLBuilder.class.php
File metadata and controls
executable file
·162 lines (149 loc) · 3.9 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/*
* Replace long SQL string with several functions
* Fixed SQL string is hard to migrate to another DB whose syntax may differ.
* Using a high level abstract interface makes code clean and easy to migrate.
* Is is strongly recommended to use prepared statement in order to avoid SQL-injection
*/
class SQLBuilder
{
private $sql = '';
/**/
public function insert($table, array $key_values)
{
$this->sql = "INSERT INTO `$table`";
$keys = array_keys($key_values);
if (count($keys) && $keys[0] !== 0) { //support `INSERT INTO table VALUES (...)
$this->sql .= '(';
foreach ($keys as $key) {
$this->sql .= "`$key`, ";
}
$this->sql = substr($this->sql, 0, strlen($this->sql) - 2);
$this->sql .= ')';
}
$this->sql .= " VALUES(";
foreach ($key_values as $key => $value) {
if ($value === null) {
$this->sql .= "null, ";
} else if ($value === '?') {// support prepared statment
$this->sql .= "?, ";
} else {// FIXME: $value may contains '
$this->sql .= "'$value', ";
}
}
$this->sql = substr($this->sql, 0, strlen($this->sql) - 2);
$this->sql .= ")";
}
/**/
public function select($table, array $selected_rows = array())
{
$this->sql = 'SELECT ';
foreach ($selected_rows as $row) {
if (strpos($row, ' ') || strpos($row, '(')) {//contain functions, not pure row name
$this->sql .= "$row, ";
} else {
$this->sql .= "`$row`, ";
}
}
if (count($selected_rows) === 0) {
$this->sql .= ' * ';
} else {
$this->sql = substr($this->sql, 0, strlen($this->sql) - 2);
}
$this->sql .= " FROM `$table` ";
}
/**/
public function update($table, array $key_values)
{
if ($key_values === null || count($key_values) === 0) {
return;
}
$this->sql = "UPDATE `$table` SET ";
foreach ($key_values as $key => $value) {
if ($value === null) {
$this->sql .= " `$key` = null, ";
} else if ($value === '?') {
$this->sql .= " `$key` = ?, ";
} else {//FIXME: $value may contain '
$this->sql .= " `$key` = '$value', ";
}
}
$this->sql = substr($this->sql, 0, strlen($this->sql) - 2);
}
/**/
public function delete($table)
{
$this->sql = "DELETE FROM `$table` ";
}
/* currently, can only represent AND */
public function where($key_values, $opts = array())
{
$where_clause_cnt = 0;
$keys = array_keys($key_values);
foreach ($keys as $key) {
if (!isset($opts[$key])) {
$opts[$key] = '=';
}
if ($where_clause_cnt == 0) {
$this->sql .= ' WHERE ';
} else {
$this->sql .= ' AND ';
}
if ($key_values[$key] === null) {
if ($opts[$key] === '=') {
$this->sql .= " `$key` is null ";
} else {
$this->sql .= " `$key` is not null ";
}
$where_clause_cnt++;
} else if ($key_values[$key] === '?' || in_array(strtoupper($opts[$key]), array('IN', 'BETWEEN', 'LIKE'))) {
$this->sql .= " `$key` {$opts[$key]} {$key_values[$key]} ";
$where_clause_cnt++;
} else {//FIXME: $key_values[$key] may contain '
$this->sql .= " `$key` {$opts[$key]} '{$key_values[$key]}' ";
$where_clause_cnt++;
}
}
}
/**/
public function group(array $by_arr)
{
if ($by_arr === null || count($by_arr) === 0) {
return;
}
$this->sql .= ' GROUP BY ';
foreach ($by_arr as $by) {
$this->sql .= "`$by`, ";
}
$this->sql = substr($this->sql, 0, strlen($this->sql) - 2);
}
/**/
public function order(array $by_arr)
{
if ($by_arr === null || count($by_arr) === 0) {
return;
}
$this->sql .= ' ORDER BY ';
foreach ($by_arr as $by => $order) {
if (strtoupper($order) !== 'DESC') {
$order = 'ASC';
}
$this->sql .= "`$by` $order, ";
}
$this->sql = substr($this->sql, 0, strlen($this->sql) - 2);
}
/**/
public function limit($offset, $cnt)
{
if ($cnt < 0) {// support LIMIT 0 ,-1 to get all records
$this->sql .= " LIMIT $offset, 18446744073709551615";
} else {
$this->sql .= " LIMIT $offset, $cnt";
}
}
/**/
public function build()
{
return $this->sql;
}
}