Completed
Branch feature/pre-split (d89158)
by Anton
04:43
created

SQLiteReference::sqlStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 15
rs 9.4285
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\Schemas;
10
11
use Spiral\Database\Entities\Driver;
12
use Spiral\Database\Schemas\Prototypes\AbstractReference;
13
14
class SQLiteReference extends AbstractReference
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function sqlStatement(Driver $driver): string
20
    {
21
        $statement = [];
22
23
        $statement[] = 'FOREIGN KEY';
24
        $statement[] = '(' . $driver->identifier($this->column) . ')';
25
26
        $statement[] = 'REFERENCES ' . $driver->identifier($this->foreignTable);
27
        $statement[] = '(' . $driver->identifier($this->foreignKey) . ')';
28
29
        $statement[] = "ON DELETE {$this->deleteRule}";
30
        $statement[] = "ON UPDATE {$this->updateRule}";
31
32
        return implode(' ', $statement);
33
    }
34
35
    /**
36
     * @param string $table
37
     * @param string $tablePrefix
38
     * @param array  $schema
39
     *
40
     * @return SQLiteReference
41
     */
42
    public static function createInstance(string $table, string $tablePrefix, array $schema): self
43
    {
44
        $reference = new self($table, $tablePrefix, $schema['id']);
45
46
        $reference->column = $schema['from'];
47
48
        $reference->foreignTable = $schema['table'];
49
        $reference->foreignKey = $schema['to'];
50
51
        $reference->deleteRule = $schema['on_delete'];
52
        $reference->updateRule = $schema['on_update'];
53
54
        return $reference;
55
    }
56
}
57