1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Database\Expressions; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Database\SqlHelper; |
6
|
|
|
|
7
|
|
|
class DbPgsqlFunctions extends DbBaseFunctions |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public function concat($str1, $str2 = null) |
11
|
|
|
{ |
12
|
|
|
return implode(func_get_args(), ' || '); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Given a SQL returns it with the proper LIMIT or equivalent method included |
17
|
|
|
* @param string $sql |
18
|
|
|
* @param int $start |
19
|
|
|
* @param int $qty |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public function limit($sql, $start, $qty = null) |
23
|
|
|
{ |
24
|
|
|
if (is_null($qty)) { |
25
|
|
|
$qty = 50; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (stripos($sql, ' LIMIT ') === false) { |
29
|
|
|
$sql = $sql . " LIMIT x OFFSET y"; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return preg_replace( |
33
|
|
|
'~(\s[Ll][Ii][Mm][Ii][Tt])\s.*?\s([Oo][Ff][Ff][Ss][Ee][Tt])\s.*~', |
34
|
|
|
'$1 ' . $qty .' $2 ' .$start, |
35
|
|
|
$sql |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Given a SQL returns it with the proper TOP or equivalent method included |
41
|
|
|
* @param string $sql |
42
|
|
|
* @param int $qty |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function top($sql, $qty) |
46
|
|
|
{ |
47
|
|
|
return $this->limit($sql, 0, $qty); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return if the database provider have a top or similar function |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function hasTop() |
55
|
|
|
{ |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return if the database provider have a limit function |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function hasLimit() |
64
|
|
|
{ |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Format date column in sql string given an input format that understands Y M D |
70
|
|
|
* |
71
|
|
|
* @param string $format |
72
|
|
|
* @param string|null $column |
73
|
|
|
* @return string |
74
|
|
|
* @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
75
|
|
|
*/ |
76
|
|
|
public function sqlDate($format, $column = null) |
77
|
|
|
{ |
78
|
|
|
if (is_null($column)) { |
79
|
|
|
$column = 'current_timestamp'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$pattern = [ |
83
|
|
|
'Y' => "YYYY", |
84
|
|
|
'y' => "YY", |
85
|
|
|
'M' => "Mon", |
86
|
|
|
'm' => "MM", |
87
|
|
|
'Q' => "Q", |
88
|
|
|
'q' => "Q", |
89
|
|
|
'D' => "DD", |
90
|
|
|
'd' => "DD", |
91
|
|
|
'h' => "HH", |
92
|
|
|
'H' => "HH24", |
93
|
|
|
'i' => "MI", |
94
|
|
|
's' => "SS", |
95
|
|
|
'a' => "AM", |
96
|
|
|
'A' => "AM", |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
return sprintf( |
100
|
|
|
"TO_CHAR(%s,'%s')", |
101
|
|
|
$column, |
102
|
|
|
implode('', $this->prepareSqlDate($format, $pattern, '')) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Format a string date to a string database readable format. |
108
|
|
|
* |
109
|
|
|
* @param string $date |
110
|
|
|
* @param string $dateFormat |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function toDate($date, $dateFormat) |
114
|
|
|
{ |
115
|
|
|
return parent::toDate($date, $dateFormat); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Format a string database readable format to a string date in a free format. |
120
|
|
|
* |
121
|
|
|
* @param string $date |
122
|
|
|
* @param string $dateFormat |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function fromDate($date, $dateFormat) |
126
|
|
|
{ |
127
|
|
|
return parent::fromDate($date, $dateFormat); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function executeAndGetInsertedId($dbdataset, $sql, $param, $sequence = null) |
131
|
|
|
{ |
132
|
|
|
$idInserted = parent::executeAndGetInsertedId($dbdataset, $sql, $param); |
133
|
|
|
$iterator = $dbdataset->getIterator( |
134
|
|
|
SqlHelper::createSafeSQL( |
135
|
|
|
"select currval(':sequence') id", |
136
|
|
|
array(':sequence' => $sequence) |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
if ($iterator->hasNext()) { |
141
|
|
|
$singleRow = $iterator->moveNext(); |
142
|
|
|
$idInserted = $singleRow->getField("id"); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $idInserted; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|