|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Database\Expressions; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Repository\DBDataset; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
|
|
8
|
|
|
abstract class DbBaseFunctions implements DbFunctionsInterface |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
const DMY = "d-m-Y"; |
|
12
|
|
|
const MDY = "m-d-Y"; |
|
13
|
|
|
const YMD = "Y-m-d"; |
|
14
|
|
|
const DMYH = "d-m-Y H:i:s"; |
|
15
|
|
|
const MDYH = "m-d-Y H:i:s"; |
|
16
|
|
|
const YMDH = "Y-m-d H:i:s"; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Given two or more string the system will return the string containing the proper |
|
20
|
|
|
* SQL commands to concatenate these string; |
|
21
|
|
|
* use: |
|
22
|
|
|
* for ($i = 0, $numArgs = func_num_args(); $i < $numArgs ; $i++) |
|
23
|
|
|
* to get all parameters received. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $str1 |
|
26
|
|
|
* @param string|null $str2 |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract public function concat($str1, $str2 = null); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Given a SQL returns it with the proper LIMIT or equivalent method included |
|
33
|
|
|
* @param string $sql |
|
34
|
|
|
* @param int $start |
|
35
|
|
|
* @param int $qty |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract public function limit($sql, $start, $qty = null); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Given a SQL returns it with the proper TOP or equivalent method included |
|
42
|
|
|
* @param string $sql |
|
43
|
|
|
* @param int $qty |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract public function top($sql, $qty); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Return if the database provider have a top or similar function |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
public function hasTop() |
|
53
|
|
|
{ |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Return if the database provider have a limit function |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
public function hasLimit() |
|
62
|
|
|
{ |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Format date column in sql string given an input format that understands Y M D |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $format |
|
70
|
|
|
* @param string|bool $column |
|
71
|
|
|
* @return string |
|
72
|
|
|
* @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
|
73
|
|
|
*/ |
|
74
|
|
|
abstract public function sqlDate($format, $column = null); |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
protected function prepareSqlDate($input, $pattern, $delimitString = "'") |
|
78
|
|
|
{ |
|
79
|
|
|
$prepareString = preg_split('/([YyMmQqDdhHisaA])/', $input, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($prepareString as $key => $value) { |
|
82
|
|
|
if ('' === $value) { |
|
83
|
|
|
unset($prepareString[$key]); |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (isset($pattern[$value])) { |
|
88
|
|
|
$formatted = $pattern[$value]; |
|
89
|
|
|
} else { |
|
90
|
|
|
$formatted = $delimitString . $value . $delimitString; |
|
91
|
|
|
} |
|
92
|
|
|
$prepareString[$key] = $formatted; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $prepareString; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Format a string date to a string database readable format. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $date |
|
102
|
|
|
* @param string $dateFormat |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function toDate($date, $dateFormat) |
|
106
|
|
|
{ |
|
107
|
|
|
$dateTime = DateTime::createFromFormat($dateFormat, $date); |
|
108
|
|
|
return $dateTime->format(self::YMDH); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Format a string database readable format to a string date in a free format. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $date |
|
115
|
|
|
* @param string $dateFormat |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function fromDate($date, $dateFormat) |
|
119
|
|
|
{ |
|
120
|
|
|
$dateTime = DateTime::createFromFormat(self::YMDH, $date); |
|
121
|
|
|
return $dateTime->format($dateFormat); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* |
|
126
|
|
|
* @param DBDataset $dbdataset |
|
127
|
|
|
* @param string $sql |
|
128
|
|
|
* @param array $param |
|
129
|
|
|
* @return int |
|
130
|
|
|
*/ |
|
131
|
|
|
public function executeAndGetInsertedId($dbdataset, $sql, $param) |
|
132
|
|
|
{ |
|
133
|
|
|
$dbdataset->execSQL($sql, $param); |
|
134
|
|
|
return -1; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|