SqlStatementBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
namespace DAL;
3
4
class SqlStatementBuilder
5
{
6
7
    public $bindings;
8
    public $queries;
9
10 24
    public function __construct()
11
    {
12 24
        $this->bindings = [];
13 24
        $this->queries  = [];
14 24
    }
15
16 5
    public function create($table, $fields)
17
    {
18 5
        $query = 'insert into ' . $table . '(';
19
20
        $setValues = $this->compileFields($fields, function () {
21 5
            return '?';
22 5
        });
23
24 5
        $segmentFields = implode(', ', array_keys($fields));
25 5
        $segmentValues = implode(', ', $setValues);
26
27 5
        $this->queries[] = $query . $segmentFields . ') values(' . $segmentValues . ')';
28 5
    }
29
30 3
    public function update($table, array $fields, Condition $criteria = null, $limit = null)
31
    {
32 3
        $limitPart = '';
33 3
        if (null !== $limit) {
34
            $limitPart = ' limit ' . $limit;
35
        }
36
37 3
        $query = 'update ' . $table . ' set ';
38
39 3
        $setValues = $this->compileFields($fields, function ($field) {
40 3
            return $field . ' = ?';
41 3
        });
42
43 3
        $this->queries[] = $query . implode(', ', $setValues)
44 3
                . $this->getWhereClause($criteria)
45 3
                . $limitPart;
46 3
    }
47
48 3
    public function delete($table, Condition $criteria = null)
49
    {
50 3
        $this->queries[] = 'delete from ' . $table . $this->getWhereClause($criteria);
51 3
    }
52
53 13
    public function select(array $columns, $table, Condition $criteria = null, $limit = null)
54
    {
55 13
        $limitPart = '';
56 13
        if (null !== $limit) {
57 4
            $limitPart = ' limit ' . $limit;
58 4
        }
59
60 13
        $query = 'select ' . implode(', ', $columns)
61 13
                . ' from ' . $table
62 13
                . $this->getWhereClause($criteria)
63 13
                . $limitPart;
64
65 13
        $this->queries[] = $query;
66 13
    }
67
68
    /**
69
     * @param array $fields
70
     * @param callable $iterationCallback
71
     */
72 8
    private function compileFields(array $fields, $iterationCallback)
73
    {
74 8
        $index    = 0;
75 8
        $compiled = [];
76 8
        foreach ($fields as $field => $value) {
77 8
            ++$index;
78 8
            $this->bindings[] = $value;
79
80 8
            $compiled[] = call_user_func($iterationCallback, $field);
81 8
        }
82 8
        return $compiled;
83
    }
84
85 19
    private function getWhereClause(Condition $criteria = null)
86
    {
87 19
        if (null !== $criteria) {
88 17
            return ' where ' . $this->compileCondition($criteria);
89
        }
90 2
        return null;
91
    }
92
93
    /**
94
     * @param string $value
95
     * @param boolean $shouldEscape
96
     *
97
     * @return string
98
     */
99 17
    private function processValue($value, $shouldEscape)
100
    {
101 17
        if ($value instanceof Condition) {
102
            $value = '(' . $this->compileCondition($value) . ')';
103 17
        } elseif ($shouldEscape) {
104 17
            $this->bindings[] = $value;
105
106 17
            $value = '?';
107 17
        }
108 17
        return $value;
109
    }
110
111 17
    private function compileCondition(Condition $criteria = null)
112
    {
113 17
        if (null === $criteria) {
114
            return null;
115
        }
116 17
        $operator = $criteria->getOperator();
117 17
        if ('regex' === $operator) {
118
            $operator = 'regexp';
119
        }
120
121
        $segments = [
122 17
            $this->processValue($criteria->getLeft(), $criteria->shouldEscapeLeft()),
123 17
            $operator,
124 17
            $this->processValue($criteria->getRight(), $criteria->shouldEscapeRight())
125 17
        ];
126
127 17
        return implode(' ', $segments);
128
    }
129
}
130