Passed
Push — master ( 5379a4...a2888f )
by Joao
04:49
created

src/Database/DBPgsqlFunctions.php (1 issue)

Upgrade to new PHP Analysis Engine

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
class DBPGSqlFunctions extends DBBaseFunctions
6
{
7
8 View Code Duplication
    function concat($s1, $s2 = null)
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...
9
    {
10
        $sql = "";
11
        for ($i = 0, $numArgs = func_num_args(); $i < $numArgs; $i++) {
12
            $var = func_get_arg($i);
13
            $sql .= ($i == 0 ? "" : " || ") . $var;
14
        }
15
16
        return $sql;
17
    }
18
19
    /**
20
     * Given a SQL returns it with the proper LIMIT or equivalent method included
21
     * @param string $sql
22
     * @param int $start
23
     * @param int $qty
24
     * @return string
25
     */
26 View Code Duplication
    function limit($sql, $start, $qty)
27
    {
28
        if (strpos($sql, ' LIMIT ') === false) {
29
            return $sql . " LIMIT $qty OFFSET $start ";
30
        } else {
31
            return $sql;
32
        }
33
    }
34
35
    /**
36
     * Given a SQL returns it with the proper TOP or equivalent method included
37
     * @param string $sql
38
     * @param int $qty
39
     * @return string
40
     */
41
    function top($sql, $qty)
42
    {
43
        return $this->limit($sql, 0, $qty);
44
    }
45
46
    /**
47
     * Return if the database provider have a top or similar function
48
     * @return bool
49
     */
50
    function hasTop()
51
    {
52
        return true;
53
    }
54
55
    /**
56
     * Return if the database provider have a limit function
57
     * @return bool
58
     */
59
    function hasLimit()
60
    {
61
        return true;
62
    }
63
64
    /**
65
     * Format date column in sql string given an input format that understands Y M D
66
     * @param string $fmt
67
     * @param string|bool $col
68
     * @return string
69
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
70
     */
71
    function sqlDate($fmt, $col = false)
72
    {
73
        if (!$col) $col = time();
74
        $s = 'TO_CHAR(' . $col . ",'";
75
76
        $len = strlen($fmt);
77
        for ($i = 0; $i < $len; $i++) {
78
            $ch = $fmt[$i];
79
            switch ($ch) {
80
                case 'Y':
81
                case 'y':
82
                    $s .= 'YYYY';
83
                    break;
84
                case 'Q':
85
                case 'q':
86
                    $s .= 'Q';
87
                    break;
88
89
                case 'M':
90
                    $s .= 'Mon';
91
                    break;
92
93
                case 'm':
94
                    $s .= 'MM';
95
                    break;
96
                case 'D':
97
                case 'd':
98
                    $s .= 'DD';
99
                    break;
100
101
                case 'H':
102
                    $s.= 'HH24';
103
                    break;
104
105
                case 'h':
106
                    $s .= 'HH';
107
                    break;
108
109
                case 'i':
110
                    $s .= 'MI';
111
                    break;
112
113
                case 's':
114
                    $s .= 'SS';
115
                    break;
116
117
                case 'a':
118
                case 'A':
119
                    $s .= 'AM';
120
                    break;
121
122
                default:
123
                    // handle escape characters...
124
                    if ($ch == '\\') {
125
                        $i++;
126
                        $ch = substr($fmt, $i, 1);
127
                    }
128
                    if (strpos('-/.:;, ', $ch) !== false) {
129
                        $s .= $ch;
130
                    } else {
131
                        $s .= '"' . $ch . '"';
132
                    }
133
            }
134
        }
135
        return $s . "')";
136
    }
137
138
    /**
139
     * Format a string date to a string database readable format.
140
     *
141
     * @param string $date
142
     * @param string $dateFormat
143
     * @return string
144
     */
145
    function toDate($date, $dateFormat)
146
    {
147
        return parent::toDate($date, $dateFormat);
148
    }
149
150
    /**
151
     * Format a string database readable format to a string date in a free format.
152
     *
153
     * @param string $date
154
     * @param string $dateFormat
155
     * @return string
156
     */
157
    function fromDate($date, $dateFormat)
158
    {
159
        return parent::fromDate($date, $dateFormat);
160
    }
161
162
    function executeAndGetInsertedId($dbdataset, $sql, $param, $sequence = null)
163
    {
164
        $id = parent::executeAndGetInsertedId($dbdataset, $sql, $param);
165
        $it = $dbdataset->getIterator(SQLHelper::createSafeSQL("select currval(':sequence') id",
166
                array(':sequence' => $sequence)));
167
        if ($it->hasNext()) {
168
            $sr = $it->moveNext();
169
            $id = $sr->getField("id");
170
        }
171
172
        return $id;
173
    }
174
}
175