Completed
Push — master ( ec2154...751b89 )
by Rougin
04:27
created

SQLiteDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use PDO;
6
use Rougin\Describe\Column;
7
8
/**
9
 * SQLite Driver
10
 *
11
 * A database driver extension for SQLite.
12
 *
13
 * @package  Describe
14
 * @category Driver
15
 * @author   Rougin Royce Gutib <[email protected]>
16
 */
17
class SQLiteDriver implements DriverInterface
18
{
19
    protected $pdo;
20
21
    /**
22
     * @param PDO $pdo
23
     */
24 9
    public function __construct(PDO $pdo)
25
    {
26 9
        $this->pdo = $pdo;
27 9
    }
28
29
    /**
30
     * Returns the result.
31
     *
32
     * @return array
33
     */
34 6
    public function getTable($table)
35
    {
36 6
        $columns = [];
37
38
        // Gets list of columns
39 6
        $query = 'PRAGMA table_info("' . $table . '");';
40 6
        $information = $this->pdo->prepare($query);
41
42 6
        $information->execute();
43 6
        $information->setFetchMode(PDO::FETCH_OBJ);
44
45 6
        while ($row = $information->fetch()) {
46 6
            $column = new Column;
47
48 6
            if (! $row->notnull) {
49 6
                $column->setNull(true);
50 6
            }
51
52 6
            if ($row->pk) {
53 6
                $column->setPrimary(true);
54 6
                $column->setAutoIncrement(true);
55 6
            }
56
57 6
            $column->setDefaultValue($row->dflt_value);
58 6
            $column->setField($row->name);
59 6
            $column->setDataType(strtolower($row->type));
60
61 6
            array_push($columns, $column);
62 6
        }
63
64
        // Gets list of foreign keys
65 6
        $query = 'PRAGMA foreign_key_list("' . $table . '");';
66 6
        $information = $this->pdo->prepare($query);
67
68 6
        $information->execute();
69 6
        $information->setFetchMode(PDO::FETCH_OBJ);
70
71 6
        while ($row = $information->fetch()) {
72 6
            foreach ($columns as $column) {
73 6
                if ($column->getField() == $row->from) {
74 6
                    $column->setForeign(true);
75
76 6
                    $column->setReferencedField($row->to);
77 6
                    $column->setReferencedTable($row->table);
78 6
                }
79 6
            }
80 6
        }
81
82 6
        return $columns;
83
    }
84
85
    /**
86
     * Shows the list of tables.
87
     *
88
     * @return array
89
     */
90 3
    public function showTables()
91
    {
92 3
        $tables = [];
93
94
        // Gets list of columns
95 3
        $query = 'SELECT name FROM sqlite_master WHERE type = "table";';
96 3
        $information = $this->pdo->prepare($query);
97
98 3
        $information->execute();
99 3
        $information->setFetchMode(PDO::FETCH_OBJ);
100
101 3
        while ($row = $information->fetch()) {
102 3
            if ($row->name != 'sqlite_sequence') {
103 3
                array_push($tables, $row->name);
104 3
            }
105 3
        }
106
107 3
        return $tables;
108
    }
109
}
110