Completed
Branch feature/pre-split (b5c37f)
by Anton
03:43
created

SQLiteCompiler::compileInsert()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 3
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Drivers\SQLite;
10
11
use Spiral\Database\Entities\QueryCompiler as AbstractCompiler;
12
13
/**
14
 * SQLite specific syntax compiler.
15
 */
16
class SQLiteCompiler extends AbstractCompiler
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function compileInsert(string $table, array $columns, array $rowsets): string
22
    {
23
        if (count($rowsets) == 1) {
24
            return parent::compileInsert($table, $columns, $rowsets);
25
        }
26
27
        //SQLite uses alternative syntax
28
        $statement = [];
29
        $statement[] = "INSERT INTO {$this->quote($table, true)} ({$this->prepareColumns($columns)})";
30
31
        foreach ($rowsets as $rowset) {
32
            if (count($statement) == 1) {
33
                $selectColumns = [];
34
                foreach ($columns as $column) {
35
                    $selectColumns[] = "? AS {$this->quote($column)}";
36
                }
37
38
                $statement[] = 'SELECT ' . implode(', ', $selectColumns);
39
            } else {
40
                $statement[] = 'UNION SELECT ' . trim(str_repeat('?, ', count($columns)), ', ');
41
            }
42
        }
43
44
        return implode("\n", $statement);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @link http://stackoverflow.com/questions/10491492/sqllite-with-skip-offset-only-not-limit
51
     */
52 View Code Duplication
    protected function compileLimit(int $limit, int $offset): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        if (empty($limit) && empty($offset)) {
55
            return '';
56
        }
57
58
        $statement = '';
59
60
        if (!empty($limit) || !empty($offset)) {
61
            $statement = 'LIMIT ' . ($limit ?: '-1') . ' ';
62
        }
63
64
        if (!empty($offset)) {
65
            $statement .= "OFFSET {$offset}";
66
        }
67
68
        return trim($statement);
69
    }
70
}
71