These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace ByJG\AnyDataset\Database; |
||
4 | |||
5 | use ByJG\AnyDataset\Repository\DBDataset; |
||
6 | |||
7 | class DBMySQLFunctions extends DBBaseFunctions |
||
8 | { |
||
9 | |||
10 | function concat($s1, $s2 = null) |
||
11 | { |
||
12 | return "concat(" . 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 | View Code Duplication | function limit($sql, $start, $qty) |
|
0 ignored issues
–
show
|
|||
23 | { |
||
24 | if (strpos($sql, ' LIMIT ') === false) { |
||
25 | return $sql . " LIMIT $start, $qty "; |
||
26 | } |
||
27 | |||
28 | return $sql; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Given a SQL returns it with the proper TOP or equivalent method included |
||
33 | * @param string $sql |
||
34 | * @param int $qty |
||
35 | * @return string |
||
36 | */ |
||
37 | function top($sql, $qty) |
||
38 | { |
||
39 | return $this->limit($sql, 0, $qty); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Return if the database provider have a top or similar function |
||
44 | * @return bool |
||
45 | */ |
||
46 | function hasTop() |
||
47 | { |
||
48 | return true; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Return if the database provider have a limit function |
||
53 | * @return bool |
||
54 | */ |
||
55 | function hasLimit() |
||
56 | { |
||
57 | return true; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Format date column in sql string given an input format that understands Y M D |
||
62 | * @param string $fmt |
||
63 | * @param string|bool $col |
||
64 | * @return string |
||
65 | * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
||
66 | */ |
||
67 | function sqlDate($fmt, $col = false) |
||
68 | { |
||
69 | if (!$col) $col = 'now()'; |
||
70 | $s = 'DATE_FORMAT(' . $col . ",'"; |
||
71 | $concat = false; |
||
72 | $len = strlen($fmt); |
||
73 | for ($i = 0; $i < $len; $i++) { |
||
74 | $ch = $fmt[$i]; |
||
75 | switch ($ch) { |
||
76 | case 'Y': |
||
77 | case 'y': |
||
78 | $s .= '%Y'; |
||
79 | break; |
||
80 | case 'Q': |
||
81 | case 'q': |
||
82 | $s .= "'),Quarter($col)"; |
||
83 | |||
84 | if ($len > $i + 1) $s .= ",DATE_FORMAT($col,'"; |
||
85 | else $s .= ",('"; |
||
86 | $concat = true; |
||
87 | break; |
||
88 | case 'M': |
||
89 | $s .= '%b'; |
||
90 | break; |
||
91 | |||
92 | case 'm': |
||
93 | $s .= '%m'; |
||
94 | break; |
||
95 | case 'D': |
||
96 | case 'd': |
||
97 | $s .= '%d'; |
||
98 | break; |
||
99 | |||
100 | case 'H': |
||
101 | $s .= '%H'; |
||
102 | break; |
||
103 | |||
104 | case 'h': |
||
105 | $s .= '%I'; |
||
106 | break; |
||
107 | |||
108 | case 'i': |
||
109 | $s .= '%i'; |
||
110 | break; |
||
111 | |||
112 | case 's': |
||
113 | $s .= '%s'; |
||
114 | break; |
||
115 | |||
116 | case 'a': |
||
117 | case 'A': |
||
118 | $s .= '%p'; |
||
119 | break; |
||
120 | |||
121 | View Code Duplication | default: |
|
122 | |||
123 | if ($ch == '\\') { |
||
124 | $i++; |
||
125 | $ch = substr($fmt, $i, 1); |
||
126 | } |
||
127 | $s .= $ch; |
||
128 | break; |
||
129 | } |
||
130 | } |
||
131 | $s.="')"; |
||
132 | if ($concat) $s = "CONCAT($s)"; |
||
133 | return $s; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Format a string date to a string database readable format. |
||
138 | * |
||
139 | * @param string $date |
||
140 | * @param string $dateFormat |
||
141 | * @return string |
||
142 | */ |
||
143 | function toDate($date, $dateFormat) |
||
144 | { |
||
145 | return parent::toDate($date, $dateFormat); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Format a string database readable format to a string date in a free format. |
||
150 | * |
||
151 | * @param string $date |
||
152 | * @param string $dateFormat |
||
153 | * @return string |
||
154 | */ |
||
155 | function fromDate($date, $dateFormat) |
||
156 | { |
||
157 | return parent::fromDate($date, $dateFormat); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * |
||
162 | * @param DBDataset $dbdataset |
||
163 | * @param string $sql |
||
164 | * @param array $param |
||
165 | * @return int |
||
166 | */ |
||
167 | View Code Duplication | function executeAndGetInsertedId($dbdataset, $sql, $param) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
168 | { |
||
169 | $id = parent::executeAndGetInsertedId($dbdataset, $sql, $param); |
||
170 | $it = $dbdataset->getIterator("select LAST_INSERT_ID() id"); |
||
171 | if ($it->hasNext()) { |
||
172 | $sr = $it->moveNext(); |
||
173 | $id = $sr->getField("id"); |
||
174 | } |
||
175 | |||
176 | return $id; |
||
177 | } |
||
178 | } |
||
179 |
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.