Builder::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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;
16
17
use Phossa2\Query\Misc\Raw;
18
use Phossa2\Query\Dialect\Mysql;
19
use Phossa2\Query\Misc\Expression;
20
use Phossa2\Shared\Base\ObjectAbstract;
21
use Phossa2\Query\Traits\SettingsAwareTrait;
22
use Phossa2\Query\Traits\DialectAwareTrait;
23
use Phossa2\Query\Traits\ParameterAwareTrait;
24
use Phossa2\Query\Interfaces\BuilderInterface;
25
use Phossa2\Query\Interfaces\DialectInterface;
26
use Phossa2\Query\Interfaces\StatementInterface;
27
use Phossa2\Query\Interfaces\Clause\ColInterface;
28
use Phossa2\Query\Interfaces\Statement\UnionStatementInterface;
29
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface;
30
use Phossa2\Query\Interfaces\Statement\InsertStatementInterface;
31
use Phossa2\Query\Interfaces\Statement\UpdateStatementInterface;
32
use Phossa2\Query\Interfaces\Statement\DeleteStatementInterface;
33
34
/**
35
 * Builder
36
 *
37
 * @package Phossa2\Query
38
 * @author  Hong Zhang <[email protected]>
39
 * @see     ObjectAbstract
40
 * @see     BuilderInterface
41
 * @see     StatementInterface
42
 * @see     SelectStatementInterface
43
 * @see     InsertStatementInterface
44
 * @see     UnionStatementInterface
45
 * @see     UpdateStatementInterface
46
 * @see     DeleteStatementInterface
47
 * @version 2.0.0
48
 * @since   2.0.0 added
49
 */
50
class Builder extends ObjectAbstract implements BuilderInterface
51
{
52
    use DialectAwareTrait, SettingsAwareTrait, ParameterAwareTrait;
53
54
    /**
55
     * tables
56
     *
57
     * @var    array
58
     * @access protected
59
     */
60
    protected $tables = [];
61
62
    /**
63
     * Constructor
64
     *
65
     * ```php
66
     * // builder with default table `users` and Mysql dialect
67
     * $users = new Builder('users', new Mysql())
68
     *
69
     * // builder with defult tables:  `users` and `accounts` AS `a`
70
     * $builder = new Builder(['users', 'accounts' => 'a'])
71
     *
72
     * // change default settings
73
     * $builder = new Builder('users', new Mysql(), ['autoQuote' => false]);
74
     * ```
75
     *
76
     * @param  string|array $table table[s] to build upon
77
     * @param  DialectInterface $dialect default dialect is `Mysql`
78
     * @param  array $settings builder settings
79
     * @access public
80
     */
81
    public function __construct(
82
        $table = '',
83
        DialectInterface $dialect = null,
84
        array $settings = []
85
    ) {
86
        $this
87
            ->initParameter()
88
            ->setSettings(array_replace($this->defaultSettings(), $settings))
89
            ->setDialect($dialect)
90
            ->table($table);
91
    }
92
93
    /**
94
     * Change table[s]
95
     *
96
     * @param  $table change to table[s]
97
     * @return $this
98
     * @access public
99
     */
100
    public function __invoke($table)
101
    {
102
        return $this->table($table);
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function expr()/*# : ExpressionInterface */
109
    {
110
        return new Expression($this);
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function raw(
117
        /*# string */ $string,
118
        array $values = []
119
    )/*# : OutputInterface */ {
120
        if (!empty($values)) {
121
            $string = $this->getParameter()->replaceQuestionMark($string, $values);
122
        }
123
        return new Raw($string);
124
    }
125
126
    /**
127
     * If has existing tables, return a new instance with provided table[s]
128
     *
129
     * {@inheritDoc}
130
     */
131
    public function table($table, /*# string */ $alias = '')
132
    {
133
        $tbl = $this->fixTable($table, $alias);
134
        $clone = [] === $this->tables ? $this : clone $this;
135
        $clone->tables = $tbl;
136
        return $clone;
137
    }
138
139
    /**
140
     * Append to existing tables
141
     *
142
     * {@inheritDoc}
143
     */
144
    public function from($table, /*# string */ $alias = '')
145
    {
146
        $tbl = $this->fixTable($table, $alias);
147
        $this->tables = array_merge($this->tables, $tbl);
148
        return $this;
149
    }
150
151
    /**
152
     * {@inheritDoc}
153
     */
154
    public function select()/*# : SelectStatementInterface */
155
    {
156
        /* @var SelectStatementInterface $select */
157
        $select = $this->getDialect()->select($this);
158
        return $select->table($this->tables)->col(func_get_args());
159
    }
160
161
    /**
162
     * {@inheritDoc}
163
     */
164
    public function insert(array $values = [])/*# : InsertStatementInterface */
165
    {
166
        /* @var InsertStatementInterface $insert */
167
        $insert = $this->getDialect()->insert($this);
168
        return $insert->into(current($this->tables))->set($values);
169
    }
170
171
    /**
172
     * {@inheritDoc}
173
     */
174
    public function replace(array $values = [])/*# : InsertStatementInterface */
175
    {
176
        /* @var InsertStatementInterface $insert */
177
        $replace = $this->getDialect()->replace($this);
178
        return $replace->into(current($this->tables))->set($values);
179
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184
    public function update(array $values = [])/*# : UpdateStatementInterface */
185
    {
186
        /* @var UpdateStatementInterface $update */
187
        $update = $this->getDialect()->update($this);
188
        return $update->from(current($this->tables))->set($values);
189
    }
190
191
    /**
192
     * {@inheritDoc}
193
     */
194
    public function delete()/*# : DeleteStatementInterface */
195
    {
196
        /* @var DeleteStatementInterface $delete */
197
        $delete = $this->getDialect()->delete($this)->from(current($this->tables));
198
199
        if ($delete instanceof ColInterface) { // multiple table deletion
200
            $delete->col(func_get_args());
201
        }
202
        return $delete;
203
    }
204
205
    /**
206
     * {@inheritDoc}
207
     */
208
    public function union()/*# : UnionStatementInterface */
209
    {
210
        /* @var UnionStatementInterface $union */
211
        $union = $this->getDialect()->union($this);
212
        if (func_num_args() > 0) { // acception variable parameters
213
            $args = func_get_args();
214
            foreach ($args as $arg) {
215
                $union->union($arg);
216
            }
217
        }
218
        return $union;
219
    }
220
221
    /**
222
     * Convert to [$table => alias] or [$table]
223
     *
224
     * @param  string|string[] $table
225
     * @param  string $alias
226
     * @return array
227
     * @access protected
228
     */
229
    protected function fixTable(
230
        $table,
231
        /*# string */ $alias = ''
232
    )/*# : array */ {
233
        if (empty($table)) {
234
            $table = [];
235
        } elseif (!is_array($table)) {
236
            $table = empty($alias) ? [$table] : [$table => $alias];
237
        }
238
        return $table;
239
    }
240
241
    /**
242
     * Builder default settings
243
     *
244
     * @return array
245
     * @access protected
246
     */
247
    protected function defaultSettings()/*# : array */
248
    {
249
        return [
250
            'autoQuote' => true,
251
            'positionedParam' => true,
252
            'namedParam' => false,
253
            'seperator' => ' ',
254
            'indent' => '',
255
            'escapeFunction' => null,
256
            'useNullAsDefault' => false,
257
        ];
258
    }
259
}
260