Passed
Push — master ( d25cd5...5cf75b )
by Nikolaos
03:08
created

Insert   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 161
ccs 0
cts 76
cp 0
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
    public function __construct(Connection $connection, Bind $bind)
38
    {
39
        parent::__construct($connection, $bind);
40
41
        $this->store["FROM"]      = "";
42
        $this->store["RETURNING"] = [];
43
    }
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
    public function column(string $column, $value = null, int $type = -1): Insert
55
    {
56
        $this->store["COLUMNS"][$column] = ":" . $column;
57
58
        if (null !== $value) {
59
            $this->bind->setValue($column, $value, $type);
60
        }
61
62
        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
    public function columns(array $columns): Insert
73
    {
74
        foreach ($columns as $column => $value) {
75
            if (is_int($column)) {
76
                $this->column($value);
77
            } else {
78
                $this->column($column, $value);
79
            }
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * Adds table(s) in the query
87
     *
88
     * @param string $table
89
     *
90
     * @return Insert
91
     */
92
    public function into(string $table): Insert
93
    {
94
        $this->store["FROM"] = $table;
95
96
        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
    public function getLastInsertId(string $name = null): string
107
    {
108
        return $this->connection->lastInsertId($name);
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getStatement(): string
115
    {
116
        return "INSERT"
117
            . $this->buildFlags()
118
            . " INTO " . $this->store["FROM"]
119
            . $this->buildColumns()
120
            . $this->buildReturning();
121
    }
122
123
    /**
124
     * Adds the `RETURNING` clause
125
     *
126
     * @param array $columns
127
     *
128
     * @return Insert
129
     */
130
    public function returning(array $columns): Insert
131
    {
132
        $this->store["RETURNING"] = array_merge(
133
            $this->store["RETURNING"],
134
            $columns
135
        );
136
137
        return $this;
138
    }
139
140
    /**
141
     * Resets the internal store
142
     */
143
    public function reset(): void
144
    {
145
        parent::reset();
146
147
        $this->store["FROM"]      = "";
148
        $this->store["RETURNING"] = [];
149
    }
150
151
    /**
152
     * Sets a column = value condition
153
     *
154
     * @param string     $column
155
     * @param mixed|null $value
156
     *
157
     * @return Insert
158
     */
159
    public function set(string $column, $value = null): Insert
160
    {
161
        if (null === $value) {
162
            $value = "NULL";
163
        }
164
165
        $this->store["COLUMNS"][$column] = $value;
166
167
        $this->bind->remove($column);
168
169
        return $this;
170
    }
171
172
    /**
173
     * Builds the column list
174
     *
175
     * @return string
176
     */
177
    private function buildColumns(): string
178
    {
179
        $columns = [];
180
181
        foreach (array_keys($this->store["COLUMNS"]) as $column) {
182
            $columns[] = $this->quoteIdentifier($column);
183
        }
184
185
        return " ("
186
            . ltrim($this->indent($columns, ","))
187
            . ") VALUES ("
188
            . ltrim($this->indent(array_values($this->store["COLUMNS"]), ","))
189
            . ")";
190
    }
191
}
192