Completed
Pull Request — master (#1)
by Rougin
07:28 queued 01:07
created

SQLiteDriver::setProperties()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 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 implements DriverInterface
17
{
18
    /**
19
     * @var \PDO
20
     */
21
    protected $pdo;
22
23
    /**
24
     * @param PDO $pdo
25
     */
26 18
    public function __construct(\PDO $pdo)
27
    {
28 18
        $this->pdo = $pdo;
29 18
    }
30
31
    /**
32
     * Returns the result.
33
     *
34
     * @return array
35
     */
36 12
    public function getTable($table)
37
    {
38 12
        $columns = [];
39
40
        // Gets list of columns
41 12
        $query = 'PRAGMA table_info("' . $table . '");';
42 12
        $information = $this->pdo->prepare($query);
43
44 12
        $information->execute();
45 12
        $information->setFetchMode(\PDO::FETCH_OBJ);
46
47 12
        while ($row = $information->fetch()) {
48 12
            $column = new Column;
49
50 12
            $this->setProperties($row, $column);
51
52 12
            $column->setDefaultValue($row->dflt_value);
53 12
            $column->setField($row->name);
54 12
            $column->setDataType(strtolower($row->type));
55
56 12
            array_push($columns, $column);
57 12
        }
58
59
        // Gets list of foreign keys, if any
60 12
        $query = 'PRAGMA foreign_key_list("' . $table . '");';
61 12
        $foreignTable = $this->pdo->prepare($query);
62
63 12
        $foreignTable->execute();
64 12
        $foreignTable->setFetchMode(\PDO::FETCH_OBJ);
65
66 12
        return $this->setForeignColumns($foreignTable, $columns);
67
    }
68
69
    /**
70
     * Shows the list of tables.
71
     *
72
     * @return array
73
     */
74 6
    public function showTables()
75
    {
76 6
        $tables = [];
77
78
        // Gets list of columns
79 6
        $query = 'SELECT name FROM sqlite_master WHERE type = "table";';
80 6
        $information = $this->pdo->prepare($query);
81
82 6
        $information->execute();
83 6
        $information->setFetchMode(\PDO::FETCH_OBJ);
84
85 6
        while ($row = $information->fetch()) {
86 6
            if ($row->name != 'sqlite_sequence') {
87 6
                array_push($tables, $row->name);
88 6
            }
89 6
        }
90
91 6
        return $tables;
92
    }
93
94
    /**
95
     * Sets the properties of the specified column.
96
     *
97
     * @param  \PDOStatement $foreignTable
98
     * @param  array         $columns
99
     * @return void
100
     */
101 12
    protected function setForeignColumns($foreignTable, array $columns)
102
    {
103 12
        while ($row = $foreignTable->fetch()) {
104 12
            foreach ($columns as $column) {
105 12
                if ($column->getField() == $row->from) {
106 12
                    $column->setForeign(true);
107
108 12
                    $column->setReferencedField($row->to);
109 12
                    $column->setReferencedTable($row->table);
110 12
                }
111 12
            }
112 12
        }
113
114 12
        return $columns;
115
    }
116
117
    /**
118
     * Sets the properties of the specified column.
119
     *
120
     * @param  mixed                   $row
121
     * @param  \Rougin\Describe\Column &$column
122
     * @return void
123
     */
124 12
    protected function setProperties($row, Column &$column)
125
    {
126 12
        if (! $row->notnull) {
127 12
            $column->setNull(true);
128 12
        }
129
130 12
        if ($row->pk) {
131 12
            $column->setPrimary(true);
132 12
            $column->setAutoIncrement(true);
133 12
        }
134 12
    }
135
}
136