Completed
Pull Request — master (#1)
by Joao
03:38
created

DbMysqlFunctions::sqlDate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 23

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
dl 31
loc 31
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 2
1
<?php
2
3
namespace ByJG\AnyDataset\Database\Expressions;
4
5
use ByJG\AnyDataset\Repository\DBDataset;
6
7 View Code Duplication
class DbMysqlFunctions extends DbBaseFunctions
0 ignored issues
show
Duplication introduced by
This class 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...
8
{
9
10
    public function concat($str1, $str2 = 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
    public function limit($sql, $start, $qty = null)
23
    {
24
        if (is_null($qty)) {
25
            $qty = 50;
26
        }
27
28
        if (stripos($sql, ' LIMIT ') === false) {
29
            $sql = $sql . " LIMIT x, y";
30
        }
31
32
        return preg_replace(
33
            '~(\s[Ll][Ii][Mm][Ii][Tt])\s.*?,\s*.*~',
34
            '$1 ' . $start .', ' .$qty,
35
            $sql
36
        );
37
    }
38
39
    /**
40
     * Given a SQL returns it with the proper TOP or equivalent method included
41
     * @param string $sql
42
     * @param int $qty
43
     * @return string
44
     */
45
    public function top($sql, $qty)
46
    {
47
        return $this->limit($sql, 0, $qty);
48
    }
49
50
    /**
51
     * Return if the database provider have a top or similar function
52
     * @return bool
53
     */
54
    public function hasTop()
55
    {
56
        return true;
57
    }
58
59
    /**
60
     * Return if the database provider have a limit function
61
     * @return bool
62
     */
63
    public function hasLimit()
64
    {
65
        return true;
66
    }
67
68
    /**
69
     * Format date column in sql string given an input format that understands Y M D
70
     *
71
     * @param string $format
72
     * @param string|null $column
73
     * @return string
74
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
75
     */
76
    public function sqlDate($format, $column = null)
77
    {
78
        if (is_null($column)) {
79
            $column = 'now()';
80
        }
81
82
        $pattern = [
83
            'Y' => "%Y",
84
            'y' => "%y",
85
            'M' => "%b",
86
            'm' => "%m",
87
            'Q' => "",
88
            'q' => "",
89
            'D' => "%d",
90
            'd' => "%d",
91
            'h' => "%I",
92
            'H' => "%H",
93
            'i' => "%i",
94
            's' => "%s",
95
            'a' => "%p",
96
            'A' => "%p",
97
        ];
98
99
        $preparedSql = $this->prepareSqlDate($format, $pattern, '');
100
101
        return sprintf(
102
            "DATE_FORMAT(%s,'%s')",
103
            $column,
104
            implode('', $preparedSql)
105
        );
106
    }
107
108
    /**
109
     * Format a string date to a string database readable format.
110
     *
111
     * @param string $date
112
     * @param string $dateFormat
113
     * @return string
114
     */
115
    public function toDate($date, $dateFormat)
116
    {
117
        return parent::toDate($date, $dateFormat);
118
    }
119
120
    /**
121
     * Format a string database readable format to a string date in a free format.
122
     *
123
     * @param string $date
124
     * @param string $dateFormat
125
     * @return string
126
     */
127
    public function fromDate($date, $dateFormat)
128
    {
129
        return parent::fromDate($date, $dateFormat);
130
    }
131
132
    /**
133
     *
134
     * @param DBDataset $dbdataset
135
     * @param string $sql
136
     * @param array $param
137
     * @return int
138
     */
139
    public function executeAndGetInsertedId($dbdataset, $sql, $param)
140
    {
141
        $returnedId = parent::executeAndGetInsertedId($dbdataset, $sql, $param);
142
        $iterator = $dbdataset->getIterator("select LAST_INSERT_ID() id");
143
        if ($iterator->hasNext()) {
144
            $singleRow = $iterator->moveNext();
145
            $returnedId = $singleRow->getField("id");
146
        }
147
148
        return $returnedId;
149
    }
150
}
151