Completed
Push — master ( 4a04e3...83688f )
by Changwan
03:47
created

InsertQuery::toSql()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 8
nop 0
dl 0
loc 25
ccs 17
cts 17
cp 1
crap 5
rs 8.439
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database\Query;
3
4
use Traversable;
5
use Wandu\Database\Contracts\ExpressionInterface;
6
use Wandu\Database\Contracts\QueryInterface;
7
use Wandu\Database\Support\Helper;
8
9
class InsertQuery implements QueryInterface
10
{
11
    /** @var string */
12
    protected $table;
13
    
14
    /** @var array */
15
    protected $values = [];
16
    
17
    /**
18
     * @param string $table
19
     * @param array|\Traversable $values
20
     */
21 7
    public function __construct($table, $values = [])
22
    {
23 7
        $this->table = $table;
24 7
        if (is_array($values)) {
25 7
            $this->values = $values;
26
        } elseif ($values instanceof Traversable) {
27
            $this->values = iterator_to_array($values);
28
        } else {
29
            $this->values = [];
30
        }
31 7
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 7
    public function toSql()
37
    {
38 7
        $valuesList = $this->values;
39 7
        if (array_values($valuesList) !== $valuesList) {
40 3
            $valuesList = [$valuesList];
41
        }
42 7
        $fields = array_keys($valuesList[0]); // fields
43
44 7
        $sqlPartValuesList = [];
45 7
        foreach ($valuesList as $values) {
46 7
            $sqlPartValues = [];
47 7
            foreach ($fields as $field) {
48 7
                $value = $values[$field] ?? null;
49 7
                if ($value instanceof ExpressionInterface) {
50 1
                    $sqlPartValues[] = $value->toSql();
51
                } else {
52 7
                    $sqlPartValues[] = '?';
53
                }
54
            }
55 7
            $sqlPartValuesList[] = implode(", ", $sqlPartValues);
56
        }
57 7
        $sql = "INSERT INTO `{$this->table}`(`". implode("`, `", array_values($fields)) . "`)"
58 7
            ." VALUES " . Helper::arrayImplode(", ", $sqlPartValuesList, '(', ')');
59 7
        return $sql;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 7
    public function getBindings()
66
    {
67 7
        $valuesList = $this->values;
68 7
        if (array_values($valuesList) !== $valuesList) {
69 3
            $valuesList = [$valuesList];
70
        }
71 7
        $fields = array_keys($valuesList[0]);
72 7
        $bindings = [];
73 7
        foreach ($valuesList as $values) {
74 7
            foreach ($fields as $field) {
75 7
                $value = $values[$field] ?? null;
76 7
                if ($value instanceof ExpressionInterface) {
77 1
                    $bindings = array_merge($bindings, $value->getBindings());
78
                } else {
79 7
                    $bindings[] = $value;
80
                }
81
            }
82
        }
83 7
        return $bindings;
84
    }
85
}
86