1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Describe; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
use Rougin\Describe\Exceptions\TableNotFoundException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Describe |
10
|
|
|
* |
11
|
|
|
* Gets information of a table schema from a database. |
12
|
|
|
* NOTE: To be removed in v2.0.0. Use Table class instead. |
13
|
|
|
* |
14
|
|
|
* @package Describe |
15
|
|
|
* @author Rougin Gutib <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Describe |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Rougin\Describe\Driver\DriverInterface |
21
|
|
|
*/ |
22
|
|
|
protected $driver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param \Rougin\Describe\Driver\DriverInterface $driver |
26
|
|
|
*/ |
27
|
162 |
|
public function __construct(Driver\DriverInterface $driver) |
28
|
|
|
{ |
29
|
162 |
|
$this->driver = $driver; |
30
|
162 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns an array of Column instances from a table. |
34
|
|
|
* |
35
|
|
|
* @param string $table |
36
|
|
|
* @return \Rougin\Describe\Column[] |
37
|
|
|
* |
38
|
|
|
* @throws \Rougin\Describe\Exceptions\TableNotFoundException |
39
|
|
|
*/ |
40
|
24 |
|
public function columns($table) |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
24 |
|
$columns = $this->driver->columns($table); |
44
|
|
|
|
45
|
12 |
|
return $columns; |
46
|
12 |
|
} catch (\PDOException $error) { |
47
|
12 |
|
$text = (string) $error->getMessage(); |
48
|
|
|
|
49
|
12 |
|
throw new TableNotFoundException($text); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns an array of Column instances from a table. |
55
|
|
|
* NOTE: To be removed in v2.0.0. Use columns() instead. |
56
|
|
|
* |
57
|
|
|
* @param string $table |
58
|
|
|
* @return \Rougin\Describe\Column[] |
59
|
|
|
*/ |
60
|
24 |
|
public function getColumns($table) |
61
|
|
|
{ |
62
|
24 |
|
return $this->driver->getColumns($table); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the primary key of a table. |
67
|
|
|
* NOTE: To be removed in v2.0.0. Use primary() instead. |
68
|
|
|
* |
69
|
|
|
* @param string $table |
70
|
|
|
* @param boolean $object |
71
|
|
|
* @return \Rougin\Describe\Column|string |
72
|
|
|
*/ |
73
|
12 |
|
public function getPrimaryKey($table, $object = false) |
74
|
|
|
{ |
75
|
12 |
|
return $this->primary($table, $object); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns an array of columns from a table. |
80
|
|
|
* NOTE: To be removed in v2.0.0. Use getColumns() instead. |
81
|
|
|
* |
82
|
|
|
* @param string $table |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
51 |
|
public function getTable($table) |
86
|
|
|
{ |
87
|
51 |
|
return $this->driver->getTable($table); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns an array of table names. |
92
|
|
|
* NOTE: To be removed in v2.0.0. Use tables() instead. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
12 |
|
public function getTableNames() |
97
|
|
|
{ |
98
|
12 |
|
return $this->driver->getTableNames(); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the primary key of a table. |
103
|
|
|
* |
104
|
|
|
* @param string $table |
105
|
|
|
* @param boolean $object |
106
|
|
|
* @return \Rougin\Describe\Column|string |
107
|
|
|
*/ |
108
|
24 |
|
public function primary($table, $object = false) |
109
|
|
|
{ |
110
|
24 |
|
$table = new Table($table, $this->driver); |
111
|
|
|
|
112
|
24 |
|
($result = $table->primary()) === null && $result = ''; |
113
|
|
|
|
114
|
24 |
|
return $object ? $result : $result->getField(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns an array of table names. |
119
|
|
|
* NOTE: To be removed in v2.0.0. Use getTableNames() instead. |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
12 |
|
public function showTables() |
124
|
|
|
{ |
125
|
12 |
|
return $this->driver->showTables(); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns an array of Table instances. |
130
|
|
|
* |
131
|
|
|
* @return \Rougin\Describe\Table[] |
132
|
|
|
*/ |
133
|
12 |
|
public function tables() |
134
|
|
|
{ |
135
|
12 |
|
return $this->driver->tables(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Calls methods from this class in underscore case. |
140
|
|
|
* NOTE: To be removed in v2.0.0. All new methods are now in one word. |
141
|
|
|
* |
142
|
|
|
* @param string $method |
143
|
|
|
* @param mixed $parameters |
144
|
|
|
* @return mixed |
145
|
|
|
*/ |
146
|
12 |
|
public function __call($method, $parameters) |
147
|
|
|
{ |
148
|
12 |
|
$method = (string) Inflector::camelize($method); |
|
|
|
|
149
|
|
|
|
150
|
12 |
|
$instance = array($this, $method); |
151
|
|
|
|
152
|
12 |
|
return call_user_func_array($instance, $parameters); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|