Completed
Push — master ( 1857e6...d96bec )
by Joao
03:02
created

src/Database/DBMysqlFunctions.php (2 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
    function concat($s1, $s2 = null)
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)
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)
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 unknown_type
54
     */
55
    function hasTop()
56
    {
57
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type declared by the interface ByJG\AnyDataset\Database...ctionsInterface::hasTop of type ByJG\AnyDataset\Database\unknown_type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
58
    }
59
60
    /**
61
     * Return if the database provider have a limit function
62
     * @return bool
63
     */
64
    function hasLimit()
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 $col
73
     * @return string
74
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
75
     */
76
    function sqlDate($fmt, $col = false)
77
    {
78
        if (!$col) $col = $this->sysTimeStamp;
0 ignored issues
show
Bug Best Practice introduced by
The expression $col of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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:
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)
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)
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)
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