|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\Database\Query\Translator; |
|
3
|
|
|
|
|
4
|
|
|
use Darya\Database\Query\AbstractSqlTranslator; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Darya's SQL Server query translator. |
|
8
|
|
|
* |
|
9
|
|
|
* TODO: Offset! |
|
10
|
|
|
* |
|
11
|
|
|
* @author Chris Andrew <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class SqlServer extends AbstractSqlTranslator { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Resolve the given value as an identifier. |
|
17
|
|
|
* |
|
18
|
|
|
* @param mixed $identifier |
|
19
|
|
|
* @return mixed |
|
20
|
|
|
*/ |
|
21
|
|
|
public function resolveIdentifier($identifier) { |
|
22
|
|
|
return $identifier; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Prepare a LIMIT clause using the given limit and offset. |
|
27
|
|
|
* |
|
28
|
|
|
* @param int $limit [optional] |
|
29
|
|
|
* @param int $offset [optional] |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function prepareLimit($limit = 0, $offset = 0) { |
|
33
|
|
|
if (!static::limitIsUseful($limit, $offset)) { |
|
34
|
|
|
return null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$limit = (int) $limit; |
|
38
|
|
|
$offset = (int) $offset; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
return "TOP $limit"; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Prepare a SELECT statement using the given columns, table, clauses and |
|
45
|
|
|
* options. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $table |
|
48
|
|
|
* @param array|string $columns |
|
49
|
|
|
* @param string $joins [optional] |
|
50
|
|
|
* @param string $where [optional] |
|
51
|
|
|
* @param string $order [optional] |
|
52
|
|
|
* @param string $limit [optional] |
|
53
|
|
|
* @param bool $distinct [optional] |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
View Code Duplication |
protected function prepareSelect($table, $columns, $joins = null, $where = null, $order = null, $limit = null, $distinct = false) { |
|
|
|
|
|
|
57
|
|
|
$table = $this->identifier($table); |
|
58
|
|
|
|
|
59
|
|
|
$distinct = $distinct ? 'DISTINCT' : ''; |
|
60
|
|
|
|
|
61
|
|
|
return static::concatenate(array('SELECT', $distinct, $limit, $columns, 'FROM', $table, $joins, $where, $order)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Prepare an UPDATE statement with the given table, data and clauses. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $table |
|
68
|
|
|
* @param array $data |
|
69
|
|
|
* @param string $where [optional] |
|
70
|
|
|
* @param string $limit [optional] |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
View Code Duplication |
protected function prepareUpdate($table, $data, $where = null, $limit = null) { |
|
|
|
|
|
|
74
|
|
|
$table = $this->identifier($table); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($data as $key => $value) { |
|
77
|
|
|
$column = $this->identifier($key); |
|
78
|
|
|
$value = $this->value($value); |
|
79
|
|
|
$data[$key] = "$column = $value"; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$values = implode(', ', $data); |
|
83
|
|
|
|
|
84
|
|
|
return static::concatenate(array('UPDATE', $limit, $table, 'SET', $values, $where)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Prepare a DELETE statement with the given table and clauses. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $table |
|
91
|
|
|
* @param string $where [optional] |
|
92
|
|
|
* @param string $limit [optional] |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
protected function prepareDelete($table, $where = null, $limit = null) { |
|
|
|
|
|
|
96
|
|
|
$table = $this->identifier($table); |
|
97
|
|
|
|
|
98
|
|
|
if ($table == '*' || !$table || !$where) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return static::concatenate(array('DELETE', $limit, 'FROM', $table, $where)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.