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

DbSqliteFunctions::fromDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

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