Completed
Pull Request — master (#4)
by Rougin
02:39
created

SQLiteDriver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 123
ccs 48
cts 48
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTable() 0 8 1
A setColumn() 0 14 1
A showTables() 0 17 3
A setForeignColumn() 0 16 3
A setProperties() 0 11 3
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use Rougin\Describe\Column;
6
7
/**
8
 * SQLite Driver
9
 *
10
 * A database driver extension for SQLite.
11
 *
12
 * @package  Describe
13
 * @category Driver
14
 * @author   Rougin Royce Gutib <[email protected]>
15
 */
16
class SQLiteDriver extends AbstractDriver implements DriverInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $columns = [];
22
23
    /**
24
     * @var \PDO
25
     */
26
    protected $pdo;
27
28
    /**
29
     * @param PDO $pdo
30
     */
31 21
    public function __construct(\PDO $pdo)
32
    {
33 21
        $this->pdo = $pdo;
34 21
    }
35
36
    /**
37
     * Returns the result.
38
     *
39
     * @param  string $tableName
40
     * @return array
41
     */
42 15
    public function getTable($tableName)
43
    {
44 15
        $this->columns = [];
45
46 15
        $this->getQuery($tableName, 'PRAGMA table_info("' . $tableName . '");');
47
48 15
        return $this->columns;
49
    }
50
51
    /**
52
     * Prepares the defined columns.
53
     *
54
     * @param  string $tableName
55
     * @param  mixed  $row
56
     * @return void
57
     */
58 12
    protected function setColumn($tableName, $row)
59
    {
60 12
        $column = new Column;
61
62 12
        $this->setProperties($row, $column);
63
64 12
        $column->setDefaultValue($row->dflt_value);
65 12
        $column->setField($row->name);
66 12
        $column->setDataType(strtolower($row->type));
67
68 12
        $this->setForeignColumn($tableName, $column);
69
70 12
        array_push($this->columns, $column);
71 12
    }
72
73
    /**
74
     * Shows the list of tables.
75
     *
76
     * @return array
77
     */
78 6
    public function showTables()
79
    {
80 6
        $tables = [];
81
82 6
        $query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";');
83
84 6
        $query->execute();
85 6
        $query->setFetchMode(\PDO::FETCH_OBJ);
86
87 6
        while ($row = $query->fetch()) {
88 6
            if ($row->name != 'sqlite_sequence') {
89 6
                array_push($tables, $row->name);
90 6
            }
91 6
        }
92
93 6
        return $tables;
94
    }
95
96
    /**
97
     * Sets the properties of the specified column if it does exists.
98
     *
99
     * @param  string                  $tableName
100
     * @param  \Rougin\Describe\Column &$column
101
     * @return void
102
     */
103 12
    protected function setForeignColumn($tableName, Column &$column)
104
    {
105 12
        $query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $tableName . '");');
106
107 12
        $query->execute();
108 12
        $query->setFetchMode(\PDO::FETCH_OBJ);
109
110 12
        while ($row = $query->fetch()) {
111 12
            if ($column->getField() == $row->from) {
112 12
                $column->setForeign(true);
113
114 12
                $column->setReferencedField($row->to);
115 12
                $column->setReferencedTable($row->table);
116 12
            }
117 12
        }
118 12
    }
119
120
    /**
121
     * Sets the properties of the specified column.
122
     *
123
     * @param  mixed                   $row
124
     * @param  \Rougin\Describe\Column &$column
125
     * @return void
126
     */
127 12
    protected function setProperties($row, Column &$column)
128
    {
129 12
        if (! $row->notnull) {
130 12
            $column->setNull(true);
131 12
        }
132
133 12
        if ($row->pk) {
134 12
            $column->setPrimary(true);
135 12
            $column->setAutoIncrement(true);
136 12
        }
137 12
    }
138
}
139