1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Store\Helpers; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\DbDriverInterface; |
6
|
|
|
use ByJG\AnyDataset\Exception\NotAvailableException; |
7
|
|
|
|
8
|
|
|
class DbDblibFunctions extends DbBaseFunctions |
9
|
|
|
{ |
10
|
|
|
|
11
|
9 |
|
public function __construct() |
12
|
|
|
{ |
13
|
9 |
|
$this->deliFieldLeft = '"'; |
14
|
9 |
|
$this->deliFieldRight = '"'; |
15
|
9 |
|
$this->deliTableLeft = '"'; |
16
|
9 |
|
$this->deliTableRight = '"'; |
17
|
9 |
|
} |
18
|
|
|
|
19
|
1 |
|
public function concat($str1, $str2 = null) |
20
|
|
|
{ |
21
|
1 |
|
return implode(func_get_args(), ' + '); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Given a SQL returns it with the proper LIMIT or equivalent method included |
26
|
|
|
* @param string $sql |
27
|
|
|
* @param int $start |
28
|
|
|
* @param int $qty |
29
|
|
|
* @return string |
30
|
|
|
* @throws NotAvailableException |
31
|
|
|
*/ |
32
|
1 |
|
public function limit($sql, $start, $qty = null) |
33
|
|
|
{ |
34
|
1 |
|
throw new NotAvailableException("DBLib does not support LIMIT feature."); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Given a SQL returns it with the proper TOP or equivalent method included |
39
|
|
|
* @param string $sql |
40
|
|
|
* @param int $qty |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
1 |
|
public function top($sql, $qty) |
44
|
|
|
{ |
45
|
1 |
|
if (stripos($sql, ' TOP ') === false) { |
46
|
1 |
|
return preg_replace("/^\\s*(select) /i", "\\1 top $qty ", $sql); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return preg_replace( |
50
|
1 |
|
'~(\s[Tt][Oo][Pp])\s.*?\d+\s~', |
51
|
1 |
|
'$1 ' . $qty . ' ', |
52
|
1 |
|
$sql |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return if the database provider have a top or similar function |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
1 |
|
public function hasTop() |
61
|
|
|
{ |
62
|
1 |
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return if the database provider have a limit function |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
1 |
|
public function hasLimit() |
70
|
|
|
{ |
71
|
1 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Format date column in sql string given an input format that understands Y M D |
76
|
|
|
|
77
|
|
|
* |
78
|
|
|
*@param string $format |
79
|
|
|
* @param bool|string $column |
80
|
|
|
* @return string |
81
|
|
|
* @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
82
|
|
|
*/ |
83
|
1 |
|
public function sqlDate($format, $column = null) |
84
|
|
|
{ |
85
|
1 |
|
if (is_null($column)) { |
86
|
1 |
|
$column = "getdate()"; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$pattern = [ |
90
|
1 |
|
'Y' => "YYYY", |
91
|
|
|
'y' => "YY", |
92
|
|
|
'M' => "MM", |
93
|
|
|
'm' => "M", |
94
|
|
|
'Q' => "", |
95
|
|
|
'q' => "", |
96
|
|
|
'D' => "dd", |
97
|
|
|
'd' => "dd", |
98
|
|
|
'h' => "H", |
99
|
|
|
'H' => "HH", |
100
|
|
|
'i' => "mm", |
101
|
|
|
's' => "ss", |
102
|
|
|
'a' => "", |
103
|
|
|
'A' => "", |
104
|
|
|
]; |
105
|
|
|
|
106
|
1 |
|
$preparedSql = $this->prepareSqlDate($format, $pattern, ''); |
107
|
|
|
|
108
|
1 |
|
return sprintf( |
109
|
1 |
|
"FORMAT(%s, '%s')", |
110
|
1 |
|
$column, |
111
|
1 |
|
implode('', $preparedSql) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Format a string date to a string database readable format. |
117
|
|
|
* |
118
|
|
|
* @param string $date |
119
|
|
|
* @param string $dateFormat |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function toDate($date, $dateFormat) |
123
|
|
|
{ |
124
|
|
|
return parent::toDate($date, $dateFormat); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Format a string database readable format to a string date in a free format. |
129
|
|
|
* |
130
|
|
|
* @param string $date |
131
|
|
|
* @param string $dateFormat |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function fromDate($date, $dateFormat) |
135
|
|
|
{ |
136
|
|
|
return parent::fromDate($date, $dateFormat); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* |
141
|
|
|
* @param DbDriverInterface $dbdataset |
142
|
|
|
* @param string $sql |
143
|
|
|
* @param array $param |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function executeAndGetInsertedId(DbDriverInterface $dbdataset, $sql, $param) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$insertedId = parent::executeAndGetInsertedId($dbdataset, $sql, $param); |
149
|
|
|
$iterator = $dbdataset->getIterator("select @@identity id"); |
150
|
|
|
if ($iterator->hasNext()) { |
151
|
|
|
$singleRow = $iterator->moveNext(); |
152
|
|
|
$insertedId = $singleRow->get("id"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $insertedId; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $sql |
160
|
|
|
* @return string|void |
161
|
|
|
* @throws \ByJG\AnyDataset\Exception\NotAvailableException |
162
|
|
|
*/ |
163
|
1 |
|
public function forUpdate($sql) |
164
|
|
|
{ |
165
|
1 |
|
throw new NotAvailableException('FOR UPDATE not available for Mssql/Dblib'); |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
public function hasForUpdate() |
169
|
|
|
{ |
170
|
1 |
|
return false; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.