Completed
Push — master ( c1d6d5...768908 )
by Rougin
23:40 queued 21:35
created

SQLiteDriver::getTable()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 50
ccs 34
cts 34
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 18
    public function __construct(PDO $pdo)
26
    {
27 18
        $this->pdo = $pdo;
28 18
    }
29
30
    /**
31
     * Returns the result.
32
     * 
33
     * @return array
34
     */
35 12
    public function getTable($table)
36
    {
37 12
        $columns = [];
38
39
        // 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
        // Gets list of foreign keys
66 12
        $query = 'PRAGMA foreign_key_list("' . $table . '");';
67 12
        $information = $this->pdo->prepare($query);
68
69 12
        $information->execute();
70 12
        $information->setFetchMode(PDO::FETCH_OBJ);
71
72 12
        while ($row = $information->fetch()) {
73 12
            foreach ($columns as $column) {
74 12
                if ($column->getField() == $row->from) {
75 12
                    $column->setForeign(true);
76
77 12
                    $column->setReferencedField($row->to);
78 12
                    $column->setReferencedTable($row->table);
79 12
                }
80 12
            }
81 12
        }
82
83 12
        return $columns;
84
    }
85
86
    /**
87
     * Shows the list of tables.
88
     * 
89
     * @return array
90
     */
91 6
    public function showTables()
92
    {
93 6
        $tables = [];
94
95
        // Gets list of columns
96 6
        $query = 'SELECT name FROM sqlite_master WHERE type = "table";';
97 6
        $information = $this->pdo->prepare($query);
98
99 6
        $information->execute();
100 6
        $information->setFetchMode(PDO::FETCH_OBJ);
101
102 6
        while ($row = $information->fetch()) {
103 6
            if ($row->name != 'sqlite_sequence') {
104 6
                array_push($tables, $row->name);
105 6
            }
106 6
        }
107
108 6
        return $tables;
109
    }
110
}
111