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

src/Database/DBDblibFunctions.php (11 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\Exception\NotAvailableException;
6
use ByJG\AnyDataset\Repository\DBDataset;
7
8
class DBDblibFunctions extends DBBaseFunctions
9
{
10
11
    function concat($s1, $s2 = null)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
    {
13
        return implode(func_get_args(), ' + ');
14
    }
15
16
    /**
17
     * Given a SQL returns it with the proper LIMIT or equivalent method included
18
     * @param string $sql
19
     * @param int $start
20
     * @param int $qty
21
     * @return string
22
     * @throws NotAvailableException
23
     */
24
    function limit($sql, $start, $qty)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
    {
26
        throw new NotAvailableException("DBLib does not support LIMIT feature.");
27
    }
28
29
    /**
30
     * Given a SQL returns it with the proper TOP or equivalent method included
31
     * @param string $sql
32
     * @param int $qty
33
     * @return string
34
     */
35
    function top($sql, $qty)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
    {
37
        return preg_replace("/^\\s*(select) /i", "\\1 top $qty ", $sql);
38
    }
39
40
    /**
41
     * Return if the database provider have a top or similar function
42
     * @return bool
43
     */
44
    function hasTop()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
    {
46
        return true;
47
    }
48
49
    /**
50
     * Return if the database provider have a limit function
51
     * @return bool
52
     */
53
    function hasLimit()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
54
    {
55
        return false;
56
    }
57
58
    /**
59
     * Format date column in sql string given an input format that understands Y M D
60
     * @param string $fmt
61
     * @param bool|string $col
62
     * @return string
63
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
64
     */
65
    function sqlDate($fmt, $col = false)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
    {
67
        if (!$col) $col = "getdate()";
68
        $s = '';
69
70
        $len = strlen($fmt);
71
        for ($i = 0; $i < $len; $i++) {
72
            if ($s) $s .= '+';
73
            $ch = $fmt[$i];
74
            switch ($ch) {
75
                case 'Y':
76
                case 'y':
77
                    $s .= "datename(yyyy,$col)";
78
                    break;
79
                case 'M':
80
                    $s .= "convert(char(3),$col,0)";
81
                    break;
82
                case 'm':
83
                    $s .= "replace(str(month($col),2),' ','0')";
84
                    break;
85
                case 'Q':
86
                case 'q':
87
                    $s .= "datename(quarter,$col)";
88
                    break;
89
                case 'D':
90
                case 'd':
91
                    $s .= "replace(str(day($col),2),' ','0')";
92
                    break;
93
                case 'h':
94
                    $s .= "substring(convert(char(14),$col,0),13,2)";
95
                    break;
96
97
                case 'H':
98
                    $s .= "replace(str(datepart(hh,$col),2),' ','0')";
99
                    break;
100
101
                case 'i':
102
                    $s .= "replace(str(datepart(mi,$col),2),' ','0')";
103
                    break;
104
                case 's':
105
                    $s .= "replace(str(datepart(ss,$col),2),' ','0')";
106
                    break;
107
                case 'a':
108
                case 'A':
109
                    $s .= "substring(convert(char(19),$col,0),18,2)";
110
                    break;
111
112 View Code Duplication
                default:
0 ignored issues
show
This code seems to be duplicated across 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...
113
                    if ($ch == '\\') {
114
                        $i++;
115
                        $ch = substr($fmt, $i, 1);
116
                    }
117
                    $s .= $ch;
118
                    break;
119
            }
120
        }
121
        return $s;
122
    }
123
124
    /**
125
     * Format a string date to a string database readable format.
126
     *
127
     * @param string $date
128
     * @param string $dateFormat
129
     * @return string
130
     */
131
    function toDate($date, $dateFormat)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
132
    {
133
        return parent::toDate($date, $dateFormat);
134
    }
135
136
    /**
137
     * Format a string database readable format to a string date in a free format.
138
     *
139
     * @param string $date
140
     * @param string $dateFormat
141
     * @return string
142
     */
143
    function fromDate($date, $dateFormat)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
144
    {
145
        return parent::fromDate($date, $dateFormat);
146
    }
147
148
    /**
149
     *
150
     * @param DBDataset $dbdataset
151
     * @param string $sql
152
     * @param array $param
153
     * @return int
154
     */
155 View Code Duplication
    function executeAndGetInsertedId($dbdataset, $sql, $param)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
156
    {
157
        $id = parent::executeAndGetInsertedId($dbdataset, $sql, $param);
158
        $it = $dbdataset->getIterator("select @@identity id");
159
        if ($it->hasNext()) {
160
            $sr = $it->moveNext();
161
            $id = $sr->getField("id");
162
        }
163
164
        return $id;
165
    }
166
}
167