Completed
Push — master ( d96bec...98160d )
by Joao
03:31
created

src/Database/DBMysqlFunctions.php (14 issues)

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
use ByJG\AnyDataset\Repository\DBDataset;
6
7
class DBMySQLFunctions extends DBBaseFunctions
8
{
9
10
    private $sysTimeStamp = 'NOW()';
11
12 View Code Duplication
    function concat($s1, $s2 = null)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
13
    {
14
        $sql = "concat(";
15
        for ($i = 0, $numArgs = func_num_args(); $i < $numArgs; $i++) {
16
            $var = func_get_arg($i);
17
            $sql .= ($i == 0 ? "" : ",") . $var;
18
        }
19
        $sql .= ")";
20
21
        return $sql;
22
    }
23
24
    /**
25
     * Given a SQL returns it with the proper LIMIT or equivalent method included
26
     * @param string $sql
27
     * @param int $start
28
     * @param int $qty
29
     * @return string
30
     */
31 View Code Duplication
    function limit($sql, $start, $qty)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
32
    {
33
        if (strpos($sql, ' LIMIT ') === false) {
34
            return $sql . " LIMIT $start, $qty ";
35
        } else {
36
            return $sql;
37
        }
38
    }
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
    function top($sql, $qty)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
47
    {
48
        return $this->limit($sql, 0, $qty);
49
    }
50
51
    /**
52
     * Return if the database provider have a top or similar function
53
     * @return bool
54
     */
55
    function hasTop()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57
        return true;
58
    }
59
60
    /**
61
     * Return if the database provider have a limit function
62
     * @return bool
63
     */
64
    function hasLimit()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    {
66
        return true;
67
    }
68
69
    /**
70
     * Format date column in sql string given an input format that understands Y M D
71
     * @param string $fmt
72
     * @param string|bool $col
73
     * @return string
74
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
75
     */
76
    function sqlDate($fmt, $col = false)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
77
    {
78
        if (!$col) $col = $this->sysTimeStamp;
79
        $s = 'DATE_FORMAT(' . $col . ",'";
80
        $concat = false;
81
        $len = strlen($fmt);
82
        for ($i = 0; $i < $len; $i++) {
83
            $ch = $fmt[$i];
84
            switch ($ch) {
85
                case 'Y':
86
                case 'y':
87
                    $s .= '%Y';
88
                    break;
89
                case 'Q':
90
                case 'q':
91
                    $s .= "'),Quarter($col)";
92
93
                    if ($len > $i + 1) $s .= ",DATE_FORMAT($col,'";
94
                    else $s .= ",('";
95
                    $concat = true;
96
                    break;
97
                case 'M':
98
                    $s .= '%b';
99
                    break;
100
101
                case 'm':
102
                    $s .= '%m';
103
                    break;
104
                case 'D':
105
                case 'd':
106
                    $s .= '%d';
107
                    break;
108
109
                case 'H':
110
                    $s .= '%H';
111
                    break;
112
113
                case 'h':
114
                    $s .= '%I';
115
                    break;
116
117
                case 'i':
118
                    $s .= '%i';
119
                    break;
120
121
                case 's':
122
                    $s .= '%s';
123
                    break;
124
125
                case 'a':
126
                case 'A':
127
                    $s .= '%p';
128
                    break;
129
130 View Code Duplication
                default:
0 ignored issues
show
This code seems to be duplicated across 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...
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
131
132
                    if ($ch == '\\') {
133
                        $i++;
134
                        $ch = substr($fmt, $i, 1);
135
                    }
136
                    $s .= $ch;
137
                    break;
138
            }
139
        }
140
        $s.="')";
141
        if ($concat) $s = "CONCAT($s)";
142
        return $s;
143
    }
144
145
    /**
146
     * Format a string date to a string database readable format.
147
     *
148
     * @param string $date
149
     * @param string $dateFormat
150
     * @return string
151
     */
152
    function toDate($date, $dateFormat)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
    {
154
        return parent::toDate($date, $dateFormat);
155
    }
156
157
    /**
158
     * Format a string database readable format to a string date in a free format.
159
     *
160
     * @param string $date
161
     * @param string $dateFormat
162
     * @return string
163
     */
164
    function fromDate($date, $dateFormat)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
165
    {
166
        return parent::fromDate($date, $dateFormat);
167
    }
168
169
    /**
170
     *
171
     * @param DBDataset $dbdataset
172
     * @param string $sql
173
     * @param array $param
174
     * @return int
175
     */
176 View Code Duplication
    function executeAndGetInsertedId($dbdataset, $sql, $param)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
177
    {
178
        $id = parent::executeAndGetInsertedId($dbdataset, $sql, $param);
179
        $it = $dbdataset->getIterator("select LAST_INSERT_ID() id");
180
        if ($it->hasNext()) {
181
            $sr = $it->moveNext();
182
            $id = $sr->getField("id");
183
        }
184
185
        return $id;
186
    }
187
}
188