Completed
Pull Request — master (#1)
by Joao
04:01 queued 01:28
created

DbDblibFunctions   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 5
dl 0
loc 142
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A concat() 0 4 1
A limit() 0 4 1
A top() 0 12 2
A hasTop() 0 4 1
A hasLimit() 0 4 1
B sqlDate() 0 31 2
A toDate() 0 4 1
A fromDate() 0 4 1
A executeAndGetInsertedId() 0 11 2
1
<?php
2
3
namespace ByJG\AnyDataset\Database\Expressions;
4
5
use ByJG\AnyDataset\Exception\NotAvailableException;
6
use ByJG\AnyDataset\Repository\DBDataset;
7
8
class DbDblibFunctions extends DbBaseFunctions
9
{
10
11
    public function concat($s1, $s2 = null)
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
    public function limit($sql, $start, $qty = null)
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
    public function top($sql, $qty)
36
    {
37
        if (stripos($sql, ' TOP ') === false) {
38
            return  preg_replace("/^\\s*(select) /i", "\\1 top $qty ", $sql);
39
        }
40
41
        return preg_replace(
42
            '~(\s[Tt][Oo][Pp])\s.*?\d+\s~',
43
            '$1 ' . $qty . ' ',
44
            $sql
45
        );
46
    }
47
48
    /**
49
     * Return if the database provider have a top or similar function
50
     * @return bool
51
     */
52
    public function hasTop()
53
    {
54
        return true;
55
    }
56
57
    /**
58
     * Return if the database provider have a limit function
59
     * @return bool
60
     */
61
    public function hasLimit()
62
    {
63
        return false;
64
    }
65
66
    /**
67
     * Format date column in sql string given an input format that understands Y M D
68
69
*
70
*@param string $format
71
     * @param bool|string $column
72
     * @return string
73
     * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao")
74
     */
75
    public function sqlDate($format, $column = null)
76
    {
77
        if (is_null($column)) {
78
            $column = "getdate()";
79
        }
80
81
        $pattern = [
82
            'Y' => "YYYY",
83
            'y' => "YY",
84
            'M' => "MM",
85
            'm' => "M",
86
            'Q' => "",
87
            'q' => "",
88
            'D' => "dd",
89
            'd' => "dd",
90
            'h' => "H",
91
            'H' => "HH",
92
            'i' => "mm",
93
            's' => "ss",
94
            'a' => "",
95
            'A' => "",
96
        ];
97
98
        $preparedSql = $this->prepareSqlDate($format, $pattern, '');
99
100
        return sprintf(
101
            "FORMAT(%s, '%s')",
102
            $column,
103
            implode('', $preparedSql)
104
        );
105
    }
106
107
    /**
108
     * Format a string date to a string database readable format.
109
     *
110
     * @param string $date
111
     * @param string $dateFormat
112
     * @return string
113
     */
114
    public function toDate($date, $dateFormat)
115
    {
116
        return parent::toDate($date, $dateFormat);
117
    }
118
119
    /**
120
     * Format a string database readable format to a string date in a free format.
121
     *
122
     * @param string $date
123
     * @param string $dateFormat
124
     * @return string
125
     */
126
    public function fromDate($date, $dateFormat)
127
    {
128
        return parent::fromDate($date, $dateFormat);
129
    }
130
131
    /**
132
     *
133
     * @param DBDataset $dbdataset
134
     * @param string $sql
135
     * @param array $param
136
     * @return int
137
     */
138
    public function executeAndGetInsertedId($dbdataset, $sql, $param)
139
    {
140
        $insertedId = parent::executeAndGetInsertedId($dbdataset, $sql, $param);
141
        $iterator = $dbdataset->getIterator("select @@identity id");
142
        if ($iterator->hasNext()) {
143
            $singleRow = $iterator->moveNext();
144
            $insertedId = $singleRow->getField("id");
145
        }
146
147
        return $insertedId;
148
    }
149
}
150