SetTrait::setWithArrayData()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits\Clause;
16
17
use Phossa2\Query\Interfaces\ClauseInterface;
18
use Phossa2\Query\Interfaces\Clause\SetInterface;
19
use Phossa2\Query\Misc\Template;
20
use Phossa2\Query\Interfaces\Statement\UpdateStatementInterface;
21
22
/**
23
 * SetTrait
24
 *
25
 * Implementation of SetInterface
26
 *
27
 * @package Phossa2\Query
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     SetInterface
30
 * @version 2.0.0
31
 * @since   2.0.0 added
32
 */
33
trait SetTrait
34
{
35
    use AbstractTrait;
36
37
    /**
38
     * data storage
39
     *
40
     * @var    array
41
     * @access protected
42
     */
43
    protected $set_data = [];
44
45
    /**
46
     * data row number
47
     *
48
     * @var    int
49
     * @access protected
50
     */
51
    protected $set_row = 0;
52
53
    /**
54
     * storage for col names
55
     *
56
     * @var    array
57
     * @access protected
58
     */
59
    protected $set_col = [];
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function set($col, $value = ClauseInterface::NO_VALUE)
65
    {
66
        if (empty($col)) { // nothing
67
            return $this;
68
        }
69
        if (is_array($col)) { // array provided
70
            return $this->setWithArrayData($col);
71
        }
72
        if (!isset($this->set_col[$col])) { // save col names
73
            $this->set_col[$col] = true;
74
        }
75
        $this->set_data[$this->set_row][$col] = $value;
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function setRaw(
83
        /*# string */ $col,
84
        $value = ClauseInterface::NO_VALUE,
85
        array $params = []
86
    ) {
87
        if (ClauseInterface::NO_VALUE !== $value) {
88
            $value = $this->positionedParam($value, $params);
89
        }
90
        return $this->set((string) $col, $value);
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function setTpl(
97
        /*# string */ $col,
98
        /*# string */ $template,
99
        $field,
100
        array $params = []
101
    ) {
102
        $template = $this->positionedParam($template, $params);
103
        return $this->set($col, new Template($template, $field));
104
    }
105
106
    /**
107
     * Batch SET
108
     *
109
     * @param  array $data
110
     * @return $this
111
     * @access protected
112
     */
113
    protected function setWithArrayData(array $data)
114
    {
115
        if (isset($data[0])) { // multiple rows
116
            foreach ($data as $row) {
117
                $this->set($row);
118
            }
119
        } else { // multiple values
120
            foreach ($data as $col => $val) {
121
                $this->set($col, $val);
122
            }
123
            $this->set_row++;
124
        }
125
        return $this;
126
    }
127
128
    /**
129
     * Build SET
130
     *
131
     * @param  string $prefix
132
     * @param  array $settings
133
     * @return string
134
     * @access protected
135
     */
136
    protected function buildSet(
137
        /*# string */ $prefix,
138
        array $settings
139
    )/*# : array */ {
140
        if ($this instanceof UpdateStatementInterface) {
141
            return $this->buildUpdateSet($prefix, $settings);
142
        } else {
143
            return $this->buildInsertSet($prefix, $settings);
144
        }
145
    }
146
147
    /**
148
     * Build SET for INSERT
149
     *
150
     * @param  string $prefix
151
     * @param  array $settings
152
     * @return string
153
     * @access protected
154
     */
155
    protected function buildInsertSet(
156
        /*# string */ $prefix,
157
        array $settings
158
    )/*# : string */ {
159
        if (empty($this->set_data)) {
160
            return '';
161
        }
162
163
        $cols = [];
164
        foreach (array_keys($this->set_col) as $col) {
165
            $cols[] = $this->quote($col, $settings);
166
        }
167
        return $settings['seperator'] . '(' . join(', ', $cols) . ')';
168
    }
169
170
    /**
171
     * Build SET ... = ..., ... = ...
172
     *
173
     *
174
     * @param  string $prefix
175
     * @param  array $settings
176
     * @return string
177
     * @access protected
178
     */
179
    protected function buildUpdateSet(
180
        /*# string */ $prefix,
181
        array $settings
182
    )/*# : string */ {
183
        $result = [];
184
        if (isset($this->set_data[0])) {
185
            foreach ($this->set_data[0] as $col => $val) {
186
                if (ClauseInterface::NO_VALUE === $val) {
187
                    $result[] = $col;
188
                } else {
189
                    $result[] = $this->quote($col, $settings) . ' = ' .
190
                        $this->processValue($val, $settings);
191
                }
192
            }
193
        }
194
        return $this->joinClause($prefix, ',', $result, $settings);
195
    }
196
197
    /**
198
     * Build VALUES ( ... )
199
     *
200
     * @param  string $prefix
201
     * @param  array $settings
202
     * @return string
203
     * @access protected
204
     */
205
    protected function buildValues(
206
        /*# string */ $prefix,
207
        array $settings
208
    )/*# : string */ {
209
        $rows = [];
210
        $cols = array_keys($this->set_col);
211
        foreach ($this->set_data as $row) {
212
            $values = [];
213
            foreach ($cols as $col) {
214
                $values[] = isset($row[$col]) ?
215
                    $this->processValue($row[$col], $settings) :
216
                    $this->nullOrDefault($settings);
217
            }
218
            $rows[] = '(' . join(', ', $values) . ')';
219
        }
220
        return $this->joinClause($prefix, ',', $rows, $settings);
221
    }
222
223
    /**
224
     * Get NULL or DEFAULT for values base on the settings
225
     *
226
     * @param  array $settings
227
     * @return string
228
     * @access protected
229
     */
230
    protected function nullOrDefault(array $settings)/*# : string */
231
    {
232
        return $settings['useNullAsDefault'] ? 'NULL' : 'DEFAULT';
233
    }
234
}
235