Passed
Branch master (d51fdb)
by Joao
05:45 queued 02:33
created

DbMysqlFunctions::concat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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