Completed
Branch feature/pre-split (76ded7)
by Anton
03:22
created

InsertQuery   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
B sqlStatement() 0 22 5
A run() 0 7 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Drivers\Postgres;
10
11
use Spiral\Database\Builders\InsertQuery as CommonInsertQuery;
12
use Spiral\Database\Drivers\Postgres\QueryCompiler as PostgresCompiler;
13
use Spiral\Database\Entities\QueryCompiler as AbstractCompiler;
14
use Spiral\Database\Exceptions\BuilderException;
15
16
/**
17
 * Postgres driver requires little bit different way to handle last insert id.
18
 */
19
class InsertQuery extends CommonInsertQuery
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function sqlStatement(AbstractCompiler $compiler = null): string
25
    {
26
        if (
27
            !$this->driver instanceof PostgresDriver
28
            || (!empty($compiler) && !$compiler instanceof PostgresCompiler)
29
        ) {
30
            throw new BuilderException(
31
                'Postgres InsertQuery can be used only with Postgres driver and compiler'
0 ignored issues
show
Unused Code introduced by
The call to BuilderException::__construct() has too many arguments starting with 'Postgres InsertQuery ca...es driver and compiler'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
32
            );
33
        }
34
35
        if (empty($compiler)) {
36
            $compiler = $this->compiler->resetQuoter();
37
        }
38
39
        return $compiler->compileInsert(
40
            $this->table,
41
            $this->columns,
42
            $this->rowsets,
43
            $this->driver->getPrimary($this->compiler->getPrefix(), $this->table)
0 ignored issues
show
Unused Code introduced by
The call to QueryCompiler::compileInsert() has too many arguments starting with $this->driver->getPrimar...Prefix(), $this->table).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
44
        );
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function run(): int
51
    {
52
        return (int)$this->driver->statement(
53
            $this->sqlStatement(),
54
            $this->getParameters()
55
        )->fetchColumn();
56
    }
57
}
58