Passed
Push — master ( 5379a4...a2888f )
by Joao
04:49
created

src/Database/DBDblibFunctions.php (1 issue)

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\Exception\NotAvailableException;
6
use ByJG\AnyDataset\Repository\DBDataset;
7
8
class DBDblibFunctions extends DBBaseFunctions
9
{
10
11 View Code Duplication
    function concat($s1, $s2 = null)
12
    {
13
        $sql = "";
14
        for ($i = 0, $numArgs = func_num_args(); $i < $numArgs; $i++) {
15
            $var = func_get_arg($i);
16
            $sql .= ($i == 0 ? "" : "+") . $var;
17
        }
18
19
        return $sql;
20
    }
21
22
    /**
23
     * Given a SQL returns it with the proper LIMIT or equivalent method included
24
     * @param string $sql
25
     * @param int $start
26
     * @param int $qty
27
     * @return string
28
     * @throws NotAvailableException
29
     */
30
    function limit($sql, $start, $qty)
31
    {
32
        throw new NotAvailableException("DBLib does not support LIMIT feature.");
33
    }
34
35
    /**
36
     * Given a SQL returns it with the proper TOP or equivalent method included
37
     * @param string $sql
38
     * @param int $qty
39
     * @return string
40
     */
41
    function top($sql, $qty)
42
    {
43
        return preg_replace("/^\s*(select) /i", "\\1 top $qty ", $sql);
44
    }
45
46
    /**
47
     * Return if the database provider have a top or similar function
48
     * @return bool
49
     */
50
    function hasTop()
51
    {
52
        return true;
53
    }
54
55
    /**
56
     * Return if the database provider have a limit function
57
     * @return bool
58
     */
59
    function hasLimit()
60
    {
61
        return false;
62
    }
63
64
    /**
65
     * Format date column in sql string given an input format that understands Y M D
66
     * @param string $fmt
67
     * @param bool|string $col
68
     * @return string
69
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
70
     */
71
    function sqlDate($fmt, $col = false)
72
    {
73
        if (!$col) $col = "getdate()";
74
        $s = '';
75
76
        $len = strlen($fmt);
77
        for ($i = 0; $i < $len; $i++) {
78
            if ($s) $s .= '+';
79
            $ch = $fmt[$i];
80
            switch ($ch) {
81
                case 'Y':
82
                case 'y':
83
                    $s .= "datename(yyyy,$col)";
84
                    break;
85
                case 'M':
86
                    $s .= "convert(char(3),$col,0)";
87
                    break;
88
                case 'm':
89
                    $s .= "replace(str(month($col),2),' ','0')";
90
                    break;
91
                case 'Q':
92
                case 'q':
93
                    $s .= "datename(quarter,$col)";
94
                    break;
95
                case 'D':
96
                case 'd':
97
                    $s .= "replace(str(day($col),2),' ','0')";
98
                    break;
99
                case 'h':
100
                    $s .= "substring(convert(char(14),$col,0),13,2)";
101
                    break;
102
103
                case 'H':
104
                    $s .= "replace(str(datepart(hh,$col),2),' ','0')";
105
                    break;
106
107
                case 'i':
108
                    $s .= "replace(str(datepart(mi,$col),2),' ','0')";
109
                    break;
110
                case 's':
111
                    $s .= "replace(str(datepart(ss,$col),2),' ','0')";
112
                    break;
113
                case 'a':
114
                case 'A':
115
                    $s .= "substring(convert(char(19),$col,0),18,2)";
116
                    break;
117
118 View Code Duplication
                default:
119
                    if ($ch == '\\') {
120
                        $i++;
121
                        $ch = substr($fmt, $i, 1);
122
                    }
123
                    $s .= $this->qstr($ch);
0 ignored issues
show
The method qstr() does not seem to exist on object<ByJG\AnyDataset\Database\DBDblibFunctions>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
                    break;
125
            }
126
        }
127
        return $s;
128
    }
129
130
    /**
131
     * Format a string date to a string database readable format.
132
     *
133
     * @param string $date
134
     * @param string $dateFormat
135
     * @return string
136
     */
137
    function toDate($date, $dateFormat)
138
    {
139
        return parent::toDate($date, $dateFormat);
140
    }
141
142
    /**
143
     * Format a string database readable format to a string date in a free format.
144
     *
145
     * @param string $date
146
     * @param string $dateFormat
147
     * @return string
148
     */
149
    function fromDate($date, $dateFormat)
150
    {
151
        return parent::fromDate($date, $dateFormat);
152
    }
153
154
    /**
155
     *
156
     * @param DBDataset $dbdataset
157
     * @param string $sql
158
     * @param array $param
159
     * @return int
160
     */
161 View Code Duplication
    function executeAndGetInsertedId($dbdataset, $sql, $param)
162
    {
163
        $id = parent::executeAndGetInsertedId($dbdataset, $sql, $param);
164
        $it = $dbdataset->getIterator("select @@identity id");
165
        if ($it->hasNext()) {
166
            $sr = $it->moveNext();
167
            $id = $sr->getField("id");
168
        }
169
170
        return $id;
171
    }
172
}
173