|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Store\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\DbDriverInterface; |
|
6
|
|
|
use ByJG\AnyDataset\DbFunctionsInterface; |
|
7
|
|
|
use DateTime; |
|
8
|
|
|
|
|
9
|
|
|
abstract class DbBaseFunctions implements DbFunctionsInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
const DMY = "d-m-Y"; |
|
13
|
|
|
const MDY = "m-d-Y"; |
|
14
|
|
|
const YMD = "Y-m-d"; |
|
15
|
|
|
const DMYH = "d-m-Y H:i:s"; |
|
16
|
|
|
const MDYH = "m-d-Y H:i:s"; |
|
17
|
|
|
const YMDH = "Y-m-d H:i:s"; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Given two or more string the system will return the string containing the proper |
|
21
|
|
|
* SQL commands to concatenate these string; |
|
22
|
|
|
* use: |
|
23
|
|
|
* for ($i = 0, $numArgs = func_num_args(); $i < $numArgs ; $i++) |
|
24
|
|
|
* to get all parameters received. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $str1 |
|
27
|
|
|
* @param string|null $str2 |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract public function concat($str1, $str2 = null); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Given a SQL returns it with the proper LIMIT or equivalent method included |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $sql |
|
36
|
|
|
* @param int $start |
|
37
|
|
|
* @param int $qty |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract public function limit($sql, $start, $qty = null); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Given a SQL returns it with the proper TOP or equivalent method included |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $sql |
|
46
|
|
|
* @param int $qty |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
abstract public function top($sql, $qty); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Return if the database provider have a top or similar function |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function hasTop() |
|
57
|
|
|
{ |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return if the database provider have a limit function |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function hasLimit() |
|
67
|
|
|
{ |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Format date column in sql string given an input format that understands Y M D |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $format |
|
75
|
|
|
* @param string|bool $column |
|
76
|
|
|
* @return string |
|
77
|
|
|
* @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
|
78
|
|
|
*/ |
|
79
|
|
|
abstract public function sqlDate($format, $column = null); |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
4 |
|
protected function prepareSqlDate($input, $pattern, $delimitString = "'") |
|
83
|
|
|
{ |
|
84
|
4 |
|
$prepareString = preg_split('/([YyMmQqDdhHisaA])/', $input, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
85
|
|
|
|
|
86
|
4 |
|
foreach ($prepareString as $key => $value) { |
|
87
|
4 |
|
if ('' === $value) { |
|
88
|
4 |
|
unset($prepareString[$key]); |
|
89
|
4 |
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
4 |
|
if (isset($pattern[$value])) { |
|
93
|
4 |
|
$formatted = $pattern[$value]; |
|
94
|
4 |
|
} else { |
|
95
|
4 |
|
$formatted = $delimitString . $value . $delimitString; |
|
96
|
|
|
} |
|
97
|
4 |
|
$prepareString[$key] = $formatted; |
|
98
|
4 |
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
return $prepareString; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Format a string date to a string database readable format. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $date |
|
107
|
|
|
* @param string $dateFormat |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function toDate($date, $dateFormat) |
|
111
|
|
|
{ |
|
112
|
|
|
$dateTime = DateTime::createFromFormat($dateFormat, $date); |
|
113
|
|
|
|
|
114
|
|
|
return $dateTime->format(self::YMDH); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Format a string database readable format to a string date in a free format. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $date |
|
121
|
|
|
* @param string $dateFormat |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public function fromDate($date, $dateFormat) |
|
125
|
|
|
{ |
|
126
|
|
|
$dateTime = DateTime::createFromFormat(self::YMDH, $date); |
|
127
|
|
|
|
|
128
|
|
|
return $dateTime->format($dateFormat); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param DbDriverInterface $dbdataset |
|
133
|
|
|
* @param string $sql |
|
134
|
|
|
* @param array $param |
|
135
|
|
|
* @return int |
|
136
|
|
|
*/ |
|
137
|
4 |
|
public function executeAndGetInsertedId(DbDriverInterface $dbdataset, $sql, $param) |
|
138
|
|
|
{ |
|
139
|
4 |
|
$dbdataset->execute($sql, $param); |
|
140
|
|
|
|
|
141
|
4 |
|
return -1; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected $deliFieldLeft = ''; |
|
145
|
|
|
protected $deliFieldRight = ''; |
|
146
|
|
|
protected $deliTableLeft = ''; |
|
147
|
|
|
protected $deliTableRight = ''; |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param array|string $field |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
*/ |
|
153
|
8 |
|
public function delimiterField($field) |
|
154
|
|
|
{ |
|
155
|
8 |
|
$result = []; |
|
156
|
8 |
|
foreach ((array)$field as $fld) { |
|
157
|
8 |
|
$fldAr = explode('.', $fld); |
|
158
|
8 |
|
$result[] = $this->deliFieldLeft |
|
159
|
8 |
|
. implode($this->deliFieldRight . '.' . $this->deliFieldLeft, $fldAr) |
|
160
|
8 |
|
. $this->deliFieldRight; |
|
161
|
8 |
|
} |
|
162
|
|
|
|
|
163
|
8 |
|
if (is_string($field)) { |
|
164
|
8 |
|
return $result[0]; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
4 |
|
return $result; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function delimiterTable($table) |
|
171
|
|
|
{ |
|
172
|
|
|
$tableAr = explode('.', $table); |
|
173
|
|
|
|
|
174
|
|
|
return $this->deliTableLeft |
|
175
|
|
|
. implode($this->deliTableRight . '.' . $this->deliTableLeft, $tableAr) |
|
176
|
|
|
. $this->deliTableRight; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
public function forUpdate($sql) |
|
180
|
|
|
{ |
|
181
|
1 |
|
if (!preg_match('#\bfor update\b#i', $sql)) { |
|
182
|
1 |
|
$sql = $sql . " FOR UPDATE "; |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
return $sql; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
abstract public function hasForUpdate(); |
|
189
|
|
|
} |
|
190
|
|
|
|