Passed
Push — master ( c94b6d...d25cd5 )
by Nikolaos
02:58
created

Insert   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 64.47%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 161
ccs 49
cts 76
cp 0.6447
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A returning() 0 8 1
A buildColumns() 0 13 2
A set() 0 11 2
A into() 0 5 1
A reset() 0 6 1
A getStatement() 0 7 1
A columns() 0 11 3
A __construct() 0 6 1
A getLastInsertId() 0 3 1
A column() 0 9 2
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AtlasPHP
12
 *
13
 * @link    https://github.com/atlasphp/Atlas.Query
14
 * @license https://github.com/atlasphp/Atlas.Query/blob/1.x/LICENSE.md
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\DataMapper\Query;
20
21
use Phalcon\DataMapper\Pdo\Connection;
22
23
use function array_keys;
24
use function is_int;
25
26
/**
27
 * Class Insert
28
 */
29
class Insert extends AbstractQuery
30
{
31
    /**
32
     * Insert constructor.
33
     *
34
     * @param Connection $connection
35
     * @param Bind       $bind
36
     */
37 5
    public function __construct(Connection $connection, Bind $bind)
38
    {
39 5
        parent::__construct($connection, $bind);
40
41 5
        $this->store["FROM"]      = "";
42 5
        $this->store["RETURNING"] = [];
43 5
    }
44
45
    /**
46
     * Sets a column for the `INSERT` query
47
     *
48
     * @param string     $column
49
     * @param mixed|null $value
50
     * @param int        $type
51
     *
52
     * @return Insert
53
     */
54 3
    public function column(string $column, $value = null, int $type = -1): Insert
55
    {
56 3
        $this->store["COLUMNS"][$column] = ":" . $column;
57
58 3
        if (null !== $value) {
59 3
            $this->bind->setValue($column, $value, $type);
60
        }
61
62 3
        return $this;
63
    }
64
65
    /**
66
     * Mass sets columns and values for the `INSERT`
67
     *
68
     * @param array $columns
69
     *
70
     * @return Insert
71
     */
72 3
    public function columns(array $columns): Insert
73
    {
74 3
        foreach ($columns as $column => $value) {
75 3
            if (is_int($column)) {
76 1
                $this->column($value);
77
            } else {
78 3
                $this->column($column, $value);
79
            }
80
        }
81
82 3
        return $this;
83
    }
84
85
    /**
86
     * Adds table(s) in the query
87
     *
88
     * @param string $table
89
     *
90
     * @return Insert
91
     */
92 3
    public function into(string $table): Insert
93
    {
94 3
        $this->store["FROM"] = $table;
95
96 3
        return $this;
97
    }
98
99
    /**
100
     * Returns the id of the last inserted record
101
     *
102
     * @param string|null $name
103
     *
104
     * @return string
105
     */
106 2
    public function getLastInsertId(string $name = null): string
107
    {
108 2
        return $this->connection->lastInsertId($name);
109
    }
110
111
    /**
112
     * @return string
113
     */
114 2
    public function getStatement(): string
115
    {
116
        return "INSERT"
117 2
            . $this->buildFlags()
118 2
            . " INTO " . $this->store["FROM"]
119 2
            . $this->buildColumns()
120 2
            . $this->buildReturning();
121
    }
122
123
    /**
124
     * Adds the `RETURNING` clause
125
     *
126
     * @param array $columns
127
     *
128
     * @return Insert
129
     */
130 1
    public function returning(array $columns): Insert
131
    {
132 1
        $this->store["RETURNING"] = array_merge(
133 1
            $this->store["RETURNING"],
134
            $columns
135
        );
136
137 1
        return $this;
138
    }
139
140
    /**
141
     * Resets the internal store
142
     */
143 5
    public function reset(): void
144
    {
145 5
        parent::reset();
146
147 5
        $this->store["FROM"]      = "";
148 5
        $this->store["RETURNING"] = [];
149 5
    }
150
151
    /**
152
     * Sets a column = value condition
153
     *
154
     * @param string     $column
155
     * @param mixed|null $value
156
     *
157
     * @return Insert
158
     */
159 3
    public function set(string $column, $value = null): Insert
160
    {
161 3
        if (null === $value) {
162 1
            $value = "NULL";
163
        }
164
165 3
        $this->store["COLUMNS"][$column] = $value;
166
167 3
        $this->bind->remove($column);
168
169 3
        return $this;
170
    }
171
172
    /**
173
     * Builds the column list
174
     *
175
     * @return string
176
     */
177 2
    private function buildColumns(): string
178
    {
179 2
        $columns = [];
180
181 2
        foreach (array_keys($this->store["COLUMNS"]) as $column) {
182 2
            $columns[] = $this->quoteIdentifier($column);
183
        }
184
185
        return " ("
186 2
            . ltrim($this->indent($columns, ","))
187 2
            . ") VALUES ("
188 2
            . ltrim($this->indent(array_values($this->store["COLUMNS"]), ","))
189 2
            . ")";
190
    }
191
}
192