Passed
Push — dev ( 4d1626...1e4d6c )
by 世昌
02:25
created

PrepareTrait::prepareQueryMark()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 1
nop 2
dl 0
loc 20
rs 9.7333
c 0
b 0
f 0
1
<?php
2
namespace suda\database\statement;
3
4
use Countable;
5
use IteratorAggregate;
6
use suda\database\Binder;
7
use suda\database\exception\SQLException;
8
9
/**
10
 * Trait PrepareTrait
11
 * @package suda\database\statement
12
 */
13
trait PrepareTrait
14
{
15
    use WherePrepareTrait;
16
    
17
    /**
18
     * 准备选择列
19
     *
20
     * @param string|array $reads
21
     * @param string $table
22
     * @return string
23
     */
24
    protected function prepareReadFields($reads, string $table = ''):string
25
    {
26
        if (is_string($reads)) {
27
            $fields = $reads;
28
        } else {
29
            $field = [];
30
            $prefix = strlen($table) ?"`{$table}`." :'';
31
            foreach ($reads as $want) {
32
                $field[] = $prefix."`$want`";
33
            }
34
            $fields = implode(',', $field);
35
        }
36
        return $fields;
37
    }
38
39
40
    /**
41
     * 准备更新
42
     *
43
     * @param array $data
44
     * @return array
45
     */
46
    protected function prepareUpdateSet(array $data)
47
    {
48
        $binders = [];
49
        $sets = [];
50
        foreach ($data as $name => $value) {
51
            $_name = Binder::index($name);
52
            $binders[] = new Binder($_name, $value, $name);
53
            $sets[] = "`{$name}`=:{$_name}";
54
        }
55
        return [implode(',', $sets), $binders];
56
    }
57
58
    /**
59
     * 合并绑定工具
60
     *
61
     * @param Binder[] $binderArray
62
     * @param array $parameter
63
     * @return Binder[]
64
     */
65
    protected function mergeBinder(array $binderArray, array $parameter)
66
    {
67
        foreach ($parameter as $key => $value) {
68
            if (! ($value instanceof Binder)) {
69
                $value = new Binder($key, $value);
70
            }
71
            if (!in_array($value, $binderArray)) {
72
                $binderArray[] = $value;
73
            }
74
        }
75
        return $binderArray;
76
    }
77
}
78