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