Completed
Push — master ( 2911b9...ef01ce )
by Rougin
11:32
created

SQLiteDriver::getTable()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7

Importance

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