Completed
Push — master ( 77f05a...77de2d )
by Restu
02:32
created

GrammarMySql::selectColumn()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 21
rs 8.7624
1
<?php
2
namespace JayaCode\Framework\Core\Database\Query\Grammar;
3
4
use JayaCode\Framework\Core\Database\Query\Query;
5
use Stringy\Stringy;
6
7
/**
8
 * Class GrammarMySql
9
 * @package JayaCode\Framework\Core\Database\Query\Grammar
10
 */
11
class GrammarMySql extends Grammar
12
{
13
14
    /**
15
     * @return string
16
     */
17
    public function build()
18
    {
19
        switch ($this->query->getType()) {
20
            case Query::TYPE_SELECT:
21
                return $this->select();
22
23
            case Query::TYPE_QUERY:
24
                return $this->query->query;
25
        }
26
        return null;
27
    }
28
29
    /**
30
     * @return string
31
     */
32
    private function select()
33
    {
34
        $this->queryString = "SELECT ";
35
36
        $this->queryString .= $this->selectColumn();
37
38
        $this->queryString .= " FROM `{$this->query->table}`";
39
40
        $this->queryString .= $this->where();
41
42
        return $this->queryString;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    private function selectColumn()
49
    {
50
        $columns = $this->query->columns;
51
        if (is_null($columns)) {
52
            $columns = [Query::sql("*")];
53
        }
54
55
        if (is_string($columns)) {
56
            $columns = array($columns);
57
        }
58
59
        foreach ($columns as $key => $val) {
60
            if ($columns[$key] instanceof Query) {
61
                $columns[$key] = $columns[$key]->query;
62
            } else {
63
                $columns[$key] = "`{$val}`";
64
            }
65
        }
66
67
        return implode(', ', $columns);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    private function where()
74
    {
75
        if (count($this->query->where) <= 0) {
76
            return "";
77
        }
78
79
        $q = Stringy::create("");
80
81
        foreach ($this->query->where as $where) {
82
            $type = $where[0];
83
            $arr = $where[1];
84
85
            if ($q->count() > 1) {
86
                $q = $q->append(" {$this->getSeparatorWhereGrammar($type)} ");
0 ignored issues
show
Bug introduced by
The method getSeparatorWhereGrammar() does not seem to exist on object<JayaCode\Framewor...y\Grammar\GrammarMySql>.

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...
87
            }
88
89
            if (is_string($arr)) {
90
                $q = $q->append($arr);
91
            }
92
93
            if (is_array($arr)) {
94
                $q = $q->append($this->buildArrWhere($arr));
95
96
                arr_push($this->params, $arr[2]);
97
            }
98
        }
99
100
        return $q->count()?$q->prepend(" WHERE ")->__toString():"";
101
    }
102
103
    private function buildArrWhere($arr)
104
    {
105
        if (count($arr) != 3) {
106
            throw new \OutOfBoundsException();
107
        }
108
109
        switch ($arr[1]) {
110
            case "BETWEEN":
111
                return "`{$arr[0]}` {$arr[1]} ? AND ?";
112
            default:
113
                return "`{$arr[0]}` {$arr[1]} ?";
114
        }
115
    }
116
}
117