Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
Insert | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
statement | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Projom\Storage\SQL\Statement; |
6 | |
7 | use Stringable; |
8 | |
9 | use Projom\Storage\SQL\Component\Set; |
10 | use Projom\Storage\SQL\Component\Table; |
11 | use Projom\Storage\SQL\Statement\StatementInterface; |
12 | use Projom\Storage\SQL\QueryObject; |
13 | use Projom\Storage\SQL\Util; |
14 | |
15 | class Insert implements StatementInterface, Stringable |
16 | { |
17 | private readonly Table $table; |
18 | private readonly Set $set; |
19 | |
20 | public function __construct(QueryObject $queryInsert) |
21 | { |
22 | $this->table = Table::create($queryInsert->collections); |
23 | $this->set = Set::create($queryInsert->fieldsWithValues); |
24 | } |
25 | |
26 | public function __toString(): string |
27 | { |
28 | [$statement, $params] = $this->statement(); |
29 | return $statement; |
30 | } |
31 | |
32 | public static function create(QueryObject $queryInsert): Insert |
33 | { |
34 | return new Insert($queryInsert); |
35 | } |
36 | |
37 | public function statement(): array |
38 | { |
39 | $positionalFields = $this->set->positionalFields(); |
40 | $positionalParams = $this->set->positionalParams(); |
41 | |
42 | $queryParts[] = "INSERT INTO {$this->table} ({$positionalFields}) VALUES"; |
43 | |
44 | $paramParts = []; |
45 | foreach ($positionalParams as $params) |
46 | $paramParts[] = "({$params})"; |
47 | |
48 | $queryParts[] = Util::join($paramParts, ', '); |
49 | |
50 | $query = Util::join($queryParts, ' '); |
51 | $params = $this->set->positionalParamValues() ?: null; |
52 | |
53 | return [ |
54 | $query, |
55 | $params |
56 | ]; |
57 | } |
58 | } |