|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Describe; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Describe |
|
7
|
|
|
* |
|
8
|
|
|
* Gets information of a table schema from a database. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Describe |
|
11
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Describe |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var \Rougin\Describe\Driver\DriverInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $driver; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param \Rougin\Describe\Driver\DriverInterface $driver |
|
22
|
|
|
*/ |
|
23
|
84 |
|
public function __construct(Driver\DriverInterface $driver) |
|
24
|
|
|
{ |
|
25
|
84 |
|
$this->driver = $driver; |
|
26
|
84 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns a listing of columns from the specified table. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $tableName |
|
32
|
|
|
* @return array |
|
33
|
|
|
* @throws \Rougin\Describe\Exceptions\TableNameNotFoundException |
|
34
|
|
|
*/ |
|
35
|
69 |
|
public function getColumns($tableName) |
|
36
|
|
|
{ |
|
37
|
69 |
|
$table = $this->driver->getTable($tableName); |
|
38
|
|
|
|
|
39
|
69 |
|
if (empty($table) || is_null($table)) { |
|
40
|
6 |
|
throw new Exceptions\TableNameNotFoundException; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
63 |
|
return $table; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Gets the primary key in the specified table. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $tableName |
|
50
|
|
|
* @param boolean $object |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
15 |
|
public function getPrimaryKey($tableName, $object = false) |
|
54
|
|
|
{ |
|
55
|
15 |
|
$columns = $this->getColumns($tableName); |
|
56
|
15 |
|
$result = ''; |
|
57
|
|
|
|
|
58
|
15 |
|
foreach ($columns as $column) { |
|
59
|
15 |
|
if ($column->isPrimaryKey()) { |
|
60
|
15 |
|
$result = $column; |
|
61
|
15 |
|
} |
|
62
|
15 |
|
} |
|
63
|
|
|
|
|
64
|
15 |
|
return ($object === true) ? $result : $result->getField(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns a listing of columns from the specified table. |
|
69
|
|
|
* NOTE: To be removed in v2.0.0. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $tableName |
|
72
|
|
|
* @return array |
|
73
|
|
|
* @throws \Rougin\Describe\Exceptions\TableNameNotFoundException |
|
74
|
|
|
*/ |
|
75
|
54 |
|
public function getTable($tableName) |
|
76
|
|
|
{ |
|
77
|
54 |
|
return $this->getColumns($tableName); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns a listing of tables from the specified database. |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
15 |
|
public function getTableNames() |
|
86
|
|
|
{ |
|
87
|
15 |
|
return $this->driver->showTables(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Shows the list of tables. |
|
92
|
|
|
* NOTE: To be removed in v2.0.0. |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
15 |
|
public function showTables() |
|
97
|
|
|
{ |
|
98
|
15 |
|
return $this->getTableNames(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Calls methods from this class in underscore case. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $method |
|
105
|
|
|
* @param mixed $parameters |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function __call($method, $parameters) |
|
109
|
|
|
{ |
|
110
|
3 |
|
$method = \Doctrine\Common\Inflector\Inflector::camelize($method); |
|
111
|
3 |
|
$result = $this; |
|
112
|
|
|
|
|
113
|
3 |
|
if (method_exists($this, $method)) { |
|
114
|
3 |
|
$result = call_user_func_array([ $this, $method ], $parameters); |
|
115
|
3 |
|
} |
|
116
|
|
|
|
|
117
|
3 |
|
return $result; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|