1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Describe\Driver; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* CodeIgniter Driver |
7
|
|
|
* |
8
|
|
|
* A database driver specifically used for CodeIgniter. |
9
|
|
|
* |
10
|
|
|
* @package Describe |
11
|
|
|
* @category Driver |
12
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class CodeIgniterDriver implements DriverInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Rougin\Describe\Driver\DriverInterface|null |
18
|
|
|
*/ |
19
|
|
|
protected $driver = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Gets the specified driver from the specified database connection. |
23
|
|
|
* |
24
|
|
|
* @param array $database |
25
|
|
|
*/ |
26
|
24 |
|
public function __construct(array $database) |
27
|
|
|
{ |
28
|
|
|
// NOTE: To be removed in v2.0.0 |
29
|
24 |
|
if (isset($database['default'])) { |
30
|
24 |
|
$database = $database['default']; |
31
|
24 |
|
} |
32
|
|
|
|
33
|
24 |
|
$this->driver = $this->getDriver($database); |
34
|
21 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns a listing of columns from the specified table. |
38
|
|
|
* |
39
|
|
|
* @param string $tableName |
40
|
|
|
* @return array |
41
|
|
|
* @throws \Rougin\Describe\Exceptions\TableNameNotFoundException |
42
|
|
|
*/ |
43
|
15 |
|
public function getColumns($tableName) |
44
|
|
|
{ |
45
|
15 |
|
return $this->driver->getTable($tableName); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns a listing of columns from the specified table. |
50
|
|
|
* NOTE: To be removed in v2.0.0. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
15 |
|
public function getTable($tableName) |
55
|
|
|
{ |
56
|
15 |
|
return $this->getColumns($tableName); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns a listing of tables from the specified database. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
6 |
|
public function getTableNames() |
65
|
|
|
{ |
66
|
6 |
|
return $this->driver->getTableNames(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Shows the list of tables. |
71
|
|
|
* NOTE: To be removed in v2.0.0. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
6 |
|
public function showTables() |
76
|
|
|
{ |
77
|
6 |
|
return $this->getTableNames(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the driver to be used. |
82
|
|
|
* |
83
|
|
|
* @param array $database |
84
|
|
|
* @return \Rougin\Describe\Driver\DriverInterface|null |
85
|
|
|
* @throws \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException |
86
|
|
|
*/ |
87
|
24 |
|
protected function getDriver(array $database) |
88
|
|
|
{ |
89
|
24 |
|
$mysql = [ 'mysql', 'mysqli' ]; |
90
|
24 |
|
$sqlite = [ 'pdo', 'sqlite', 'sqlite3' ]; |
91
|
|
|
|
92
|
24 |
|
if (in_array($database['dbdriver'], $mysql)) { |
93
|
9 |
|
$dsn = 'mysql:host=' . $database['hostname'] . ';dbname=' . $database['database']; |
94
|
9 |
|
$pdo = new \PDO($dsn, $database['username'], $database['password']); |
95
|
|
|
|
96
|
9 |
|
return new MySQLDriver($pdo, $database['database']); |
97
|
|
|
} |
98
|
|
|
|
99
|
15 |
|
if (in_array($database['dbdriver'], $sqlite)) { |
100
|
12 |
|
$pdo = new \PDO($database['hostname']); |
101
|
|
|
|
102
|
12 |
|
return new SQLiteDriver($pdo); |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
throw new \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|