|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2014 ekow. |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace ntentan\atiaa\descriptors; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* An abstract descriptor for DBs which use the information_schema tables. |
|
31
|
|
|
* Database systems which implement the ANSI information_schema standard can |
|
32
|
|
|
* extend this descriptor. |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract class InformationSchemaDescriptor extends \ntentan\atiaa\Descriptor |
|
35
|
|
|
{ |
|
36
|
7 |
|
protected function getColumns(&$table) |
|
37
|
|
|
{ |
|
38
|
7 |
|
return $this->driver->quotedQuery( |
|
39
|
7 |
|
'select |
|
40
|
|
|
"column_name" as "name", |
|
41
|
|
|
"data_type" as "type", |
|
42
|
|
|
"is_nullable" as "nulls", |
|
43
|
|
|
"column_default" as "default", |
|
44
|
|
|
"character_maximum_length" as "length" |
|
45
|
|
|
from "information_schema"."columns" |
|
46
|
|
|
where "table_name" = ? and "table_schema"=? |
|
47
|
|
|
order by "column_name"', |
|
48
|
|
|
array( |
|
49
|
7 |
|
$table['name'], |
|
50
|
7 |
|
$table['schema'] |
|
51
|
|
|
) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
11 |
|
protected function getTables($schema, $tables, $includeViews) |
|
56
|
|
|
{ |
|
57
|
11 |
View Code Duplication |
if($includeViews) |
|
58
|
|
|
{ |
|
59
|
5 |
|
$condition = "(table_type = ? or table_type = ?)"; |
|
60
|
5 |
|
$bind = array('BASE TABLE', 'VIEW'); |
|
61
|
|
|
} |
|
62
|
|
|
else |
|
63
|
|
|
{ |
|
64
|
6 |
|
$condition = "table_type = ?"; |
|
65
|
6 |
|
$bind = array('BASE TABLE'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
11 |
|
if(count($tables) > 0) |
|
69
|
|
|
{ |
|
70
|
7 |
|
return $this->driver->quotedQuery( |
|
71
|
|
|
'select "table_schema" as "schema", "table_name" as "name" |
|
72
|
|
|
from "information_schema"."tables" |
|
73
|
7 |
|
where ' . $condition . ' and table_schema = ? |
|
74
|
7 |
|
and table_name in (?' . str_repeat(', ?', count($tables) - 1) . ') |
|
75
|
|
|
order by "table_name"', |
|
76
|
7 |
|
array_merge($bind, array($schema), $tables) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
else |
|
80
|
|
|
{ |
|
81
|
4 |
|
return $this->driver->quotedQuery( |
|
82
|
|
|
'select "table_schema" as "schema", "table_name" as "name" |
|
83
|
|
|
from "information_schema"."tables" |
|
84
|
4 |
|
where ' . $condition . ' and table_schema = ? order by "table_name"', |
|
85
|
4 |
|
array_merge($bind, array($schema)) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
7 |
|
protected function getPrimaryKey(&$table) |
|
91
|
|
|
{ |
|
92
|
7 |
|
return $this->getConstraints($table, 'PRIMARY KEY'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
7 |
|
protected function getUniqueKeys(&$table) |
|
96
|
|
|
{ |
|
97
|
7 |
|
return $this->getConstraints($table, 'UNIQUE'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $type |
|
102
|
|
|
*/ |
|
103
|
7 |
|
private function getConstraints($table, $type) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
7 |
|
return $this->driver->quotedQuery( |
|
106
|
7 |
|
'select "column_name" as "column", "pk"."constraint_name" as "name" |
|
107
|
|
|
from "information_schema"."table_constraints" "pk" |
|
108
|
|
|
join "information_schema"."key_column_usage" "c" on |
|
109
|
|
|
"c"."table_name" = "pk"."table_name" and |
|
110
|
|
|
"c"."constraint_name" = "pk"."constraint_name" and |
|
111
|
|
|
"c"."constraint_schema" = "pk"."table_schema" |
|
112
|
|
|
where "pk"."table_name" = ? and pk.table_schema= ? |
|
113
|
|
|
and constraint_type = ? order by "pk"."constraint_name", "column_name"', |
|
114
|
7 |
|
array($table['name'], $table['schema'], $type) |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
4 |
|
protected function getViews(&$schema) |
|
119
|
|
|
{ |
|
120
|
4 |
|
return $this->driver->quotedQuery( |
|
121
|
4 |
|
'select "table_schema" as "schema", "table_name" as "name", "view_definition" as "definition" |
|
122
|
|
|
from "information_schema"."views" |
|
123
|
|
|
where "table_schema" = ? order by "table_name"', |
|
124
|
4 |
|
array($schema) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.