Issues (265)

src/Query/InsertQuery.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Query;
13
14
use Cycle\Database\Driver\CompilerInterface;
15
use Cycle\Database\Injection\Parameter;
16
17
/**
18
 * Insert statement query builder, support singular and batch inserts.
19
 */
20
class InsertQuery extends ActiveQuery
21
{
22
    protected string $table;
23
    protected array $columns = [];
24
    protected array $values = [];
25
26 78
    public function __construct(?string $table = null)
27
    {
28 78
        $this->table = $table ?? '';
29 78
    }
30
31
    /**
32
     * Set target insertion table.
33
     *
34
     * @psalm-param non-empty-string $into
35
     */
36 466
    public function into(string $into): self
37
    {
38 466
        $this->table = $into;
39
40 466
        return $this;
41
    }
42
43
    /**
44
     * Set insertion column names. Names can be provided as array, set of parameters or comma
45
     * separated string.
46
     *
47
     * Examples:
48
     * $insert->columns(["name", "email"]);
49
     * $insert->columns("name", "email");
50
     * $insert->columns("name, email");
51
     */
52 162
    public function columns(array|string ...$columns): self
53
    {
54 162
        $this->columns = $this->fetchIdentifiers($columns);
55
56 162
        return $this;
57
    }
58
59
    /**
60
     * Set insertion rowset values or multiple rowsets. Values can be provided in multiple forms
61
     * (method parameters, array of values, array of rowsets). Columns names will be automatically
62
     * fetched (if not already specified) from first provided rowset based on rowset keys.
63
     *
64
     * Examples:
65
     * $insert->columns("name", "balance")->values("Wolfy-J", 10);
66
     * $insert->values([
67
     *      "name" => "Wolfy-J",
68
     *      "balance" => 10
69
     * ]);
70
     * $insert->values([
71
     *  [
72
     *      "name" => "Wolfy-J",
73
     *      "balance" => 10
74
     *  ],
75
     *  [
76
     *      "name" => "Ben",
77
     *      "balance" => 20
78
     *  ]
79
     * ]);
80
     */
81 466
    public function values(mixed $rowsets): self
82
    {
83 466
        if (!\is_array($rowsets)) {
84 42
            return $this->values(\func_get_args());
85
        }
86
87 466
        if ($rowsets === []) {
88 16
            return $this;
89
        }
90
91
        //Checking if provided set is array of multiple
92 450
        \reset($rowsets);
93
94 450
        if (!\is_array($rowsets[\key($rowsets)])) {
95 346
            if ($this->columns === []) {
96 288
                $this->columns = \array_keys($rowsets);
97
            }
98
99 346
            $this->values[] = new Parameter(\array_values($rowsets));
100
        } else {
101 106
            if ($this->columns === []) {
102 106
                $this->columns = \array_keys($rowsets[\key($rowsets)]);
103
            }
104
105
            foreach ($rowsets as $values) {
106 450
                $this->values[] = new Parameter(\array_values($values));
107
            }
108
        }
109
110
        return $this;
111
    }
112
113
    /**
114 296
     * Run the query and return last insert id.
115
     * Returns an assoc array of values if multiple columns were specified as returning columns.
116 296
     *
117 296
     * @return array<non-empty-string, mixed>|int|non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, ...t|non-empty-string|null at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>|int|non-empty-string|null.
Loading history...
118
     */
119 296
    public function run(): mixed
120
    {
121 296
        $params = new QueryParameters();
122
        $queryString = $this->sqlStatement($params);
123
124 272
        $this->driver->execute(
0 ignored issues
show
The method execute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        $this->driver->/** @scrutinizer ignore-call */ 
125
                       execute(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125 272
            $queryString,
126 268
            $params->getParameters(),
127
        );
128
129 4
        $lastID = $this->driver->lastInsertID();
130
        if (\is_numeric($lastID)) {
131
            return (int) $lastID;
132 462
        }
133
134 462
        return $lastID;
135
    }
136
137 336
    public function getType(): int
138
    {
139
        return CompilerInterface::INSERT_QUERY;
140 336
    }
141 336
142 336
    public function getTokens(): array
143
    {
144
        return [
145
            'table'   => $this->table,
146
            'columns' => $this->columns,
147
            'values'  => $this->values,
148
        ];
149
    }
150
}
151