Completed
Pull Request — master (#4)
by Rougin
04:45 queued 01:25
created

SQLiteDriver::setColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
crap 1
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 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 View Code Duplication
    public function getTable($tableName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44 15
        $this->columns = [];
45
46
        try {
47 15
            $query = $this->pdo->prepare('PRAGMA table_info("' . $tableName . '");');
48
49 15
            $query->execute();
50 15
            $query->setFetchMode(\PDO::FETCH_OBJ);
51 15
        } catch (\PDOException $e) {
52
            // Table not found
53
        }
54
55 15
        while ($row = $query->fetch()) {
56 12
            $this->setColumn($tableName, $row);
57 12
        }
58
59 15
        return $this->columns;
60
    }
61
62
    /**
63
     * Prepares the defined columns.
64
     *
65
     * @param  string $tableName
66
     * @param  mixed  $row
67
     * @return void
68
     */
69 12
    protected function setColumn($tableName, $row)
70
    {
71 12
        $column = new Column;
72
73 12
        $this->setProperties($row, $column);
74
75 12
        $column->setDefaultValue($row->dflt_value);
76 12
        $column->setField($row->name);
77 12
        $column->setDataType(strtolower($row->type));
78
79 12
        $this->setForeignColumn($tableName, $row, $column);
80
81 12
        array_push($this->columns, $column);
82 12
    }
83
84
    /**
85
     * Shows the list of tables.
86
     *
87
     * @return array
88
     */
89 6
    public function showTables()
90
    {
91 6
        $tables = [];
92
93 6
        $query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";');
94
95 6
        $query->execute();
96 6
        $query->setFetchMode(\PDO::FETCH_OBJ);
97
98 6
        while ($row = $query->fetch()) {
99 6
            if ($row->name != 'sqlite_sequence') {
100 6
                array_push($tables, $row->name);
101 6
            }
102 6
        }
103
104 6
        return $tables;
105
    }
106
107
    /**
108
     * Sets the properties of the specified column if it does exists.
109
     *
110
     * @param  string                  $tableName
111
     * @param  mixed                   $row
112
     * @param  \Rougin\Describe\Column &$column
113
     * @return void
114
     */
115 12
    protected function setForeignColumn($tableName, $row, Column &$column)
0 ignored issues
show
Unused Code introduced by
The parameter $row is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117 12
        $query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $tableName . '");');
118
119 12
        $query->execute();
120 12
        $query->setFetchMode(\PDO::FETCH_OBJ);
121
122 12
        while ($row = $query->fetch()) {
123 12
            if ($column->getField() == $row->from) {
124 12
                $column->setForeign(true);
125
126 12
                $column->setReferencedField($row->to);
127 12
                $column->setReferencedTable($row->table);
128 12
            }
129 12
        }
130 12
    }
131
132
    /**
133
     * Sets the properties of the specified column.
134
     *
135
     * @param  mixed                   $row
136
     * @param  \Rougin\Describe\Column &$column
137
     * @return void
138
     */
139 12
    protected function setProperties($row, Column &$column)
140
    {
141 12
        if (! $row->notnull) {
142 12
            $column->setNull(true);
143 12
        }
144
145 12
        if ($row->pk) {
146 12
            $column->setPrimary(true);
147 12
            $column->setAutoIncrement(true);
148 12
        }
149 12
    }
150
}
151