Test Failed
Push — master ( 238921...4a95c6 )
by Sergei
05:54 queued 02:08
created

src/DMLQueryBuilder.php (2 issues)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use Yiisoft\Db\Constraint\Constraint;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Exception\NotSupportedException;
10
use Yiisoft\Db\Expression\Expression;
11
use Yiisoft\Db\Query\QueryInterface;
12
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder;
13
14
use function implode;
15
16
/**
17
 * Implements a DML (Data Manipulation Language) SQL statements for SQLite Server.
18
 */
19
final class DMLQueryBuilder extends AbstractDMLQueryBuilder
20
{
21 1
    public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string
22
    {
23 1
        throw new NotSupportedException(__METHOD__ . '() is not supported by SQLite.');
24
    }
25
26 5
    public function resetSequence(string $table, int|string $value = null): string
27
    {
28 5
        $tableSchema = $this->schema->getTableSchema($table);
29
30 5
        if ($tableSchema === null) {
31 1
            throw new InvalidArgumentException("Table not found: '$table'.");
32
        }
33
34 4
        $sequenceName = $tableSchema->getSequenceName();
35
36 4
        if ($sequenceName === null) {
37 1
            throw new InvalidArgumentException("There is not sequence associated with table '$table'.'");
38
        }
39
40 3
        $tableName = $this->quoter->quoteTableName($table);
41
42 3
        if ($value !== null) {
43 2
            $value = "'" . ((int) $value - 1) . "'";
44
        } else {
45 3
            $key = $tableSchema->getPrimaryKey()[0];
46 3
            $key = $this->quoter->quoteColumnName($key);
47 3
            $value = '(SELECT MAX(' . $key . ') FROM ' . $tableName . ')';
48
        }
49
50 3
        return 'UPDATE sqlite_sequence SET seq=' . $value . " WHERE name='" . $tableSchema->getName() . "'";
51
    }
52
53 36
    public function upsert(
54
        string $table,
55
        QueryInterface|array $insertColumns,
56
        bool|array $updateColumns,
57
        array &$params
58
    ): string {
59
        /** @var Constraint[] $constraints */
60 36
        $constraints = [];
61
62 36
        [$uniqueNames, $insertNames, $updateNames] = $this->prepareUpsertColumns(
63 36
            $table,
64 36
            $insertColumns,
65 36
            $updateColumns,
66 36
            $constraints
67 36
        );
68
69 36
        if (empty($uniqueNames)) {
70 2
            return $this->insert($table, $insertColumns, $params);
71
        }
72
73 34
        [, $placeholders, $values, $params] = $this->prepareInsertValues($table, $insertColumns, $params);
74
75 34
        $quotedTableName = $this->quoter->quoteTableName($table);
76
77 34
        $insertSql = 'INSERT OR IGNORE INTO ' . $quotedTableName
78 34
            . (!empty($insertNames) ? ' (' . implode(', ', $insertNames) . ')' : '')
79 34
            . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : ' ' . $values);
80
81 34
        if ($updateColumns === false) {
0 ignored issues
show
The condition $updateColumns === false is always false.
Loading history...
82 12
            return $insertSql;
83
        }
84
85 22
        $updateCondition = ['or'];
86
87 22
        foreach ($constraints as $constraint) {
88 22
            $constraintCondition = ['and'];
89
            /** @psalm-var string[] $columnNames */
90 22
            $columnNames = $constraint->getColumnNames();
91 22
            foreach ($columnNames as $name) {
92 22
                $quotedName = $this->quoter->quoteColumnName($name);
93 22
                $constraintCondition[] = "$quotedTableName.$quotedName=(SELECT $quotedName FROM `EXCLUDED`)";
94
            }
95 22
            $updateCondition[] = $constraintCondition;
96
        }
97
98 22
        if ($updateColumns === true) {
0 ignored issues
show
The condition $updateColumns === true is always false.
Loading history...
99 12
            $updateColumns = [];
100
            /** @psalm-var string[] $updateNames */
101 12
            foreach ($updateNames as $quotedName) {
102 10
                $updateColumns[$quotedName] = new Expression("(SELECT $quotedName FROM `EXCLUDED`)");
103
            }
104
        }
105
106 22
        if ($updateColumns === []) {
107 2
            return $insertSql;
108
        }
109
110 20
        $updateSql = 'WITH "EXCLUDED" (' . implode(', ', $insertNames) . ') AS ('
111 20
            . (!empty($placeholders) ? 'VALUES (' . implode(', ', $placeholders) . ')' : $values)
112 20
            . ') ' . $this->update($table, $updateColumns, $updateCondition, $params);
113
114 20
        return "$updateSql; $insertSql;";
115
    }
116
}
117