|
@@ -0,0 +1,264 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace Luticate\Utils\DataAccess;
|
|
4
|
+use Luticate\Utils\Business\LuStringUtils;
|
|
5
|
+
|
|
6
|
+/**
|
|
7
|
+ * Created by PhpStorm.
|
|
8
|
+ * User: robin
|
|
9
|
+ * Date: 6/10/16
|
|
10
|
+ * Time: 11:05 AM
|
|
11
|
+ */
|
|
12
|
+class PgSqlDataAccess extends AbstractDbDataAccess
|
|
13
|
+{
|
|
14
|
+ public static $types = ["SERIAL" => "integer",
|
|
15
|
+ "BIGSERIAL" => "integer",
|
|
16
|
+ "abstime" => "datetime",
|
|
17
|
+ "aclitem" => "",
|
|
18
|
+ "any" => "",
|
|
19
|
+ "anyarray" => "",
|
|
20
|
+ "anyelement" => "",
|
|
21
|
+ "anyenum" => "",
|
|
22
|
+ "anynonarray" => "",
|
|
23
|
+ "anyrange" => "",
|
|
24
|
+ "bigint" => "integer",
|
|
25
|
+ "bit" => "",
|
|
26
|
+ "bit varying" => "",
|
|
27
|
+ "boolean" => "boolean",
|
|
28
|
+ "box" => "",
|
|
29
|
+ "bytea" => "string",
|
|
30
|
+ "char" => "string",
|
|
31
|
+ "character" => "string",
|
|
32
|
+ "character varying" => "string",
|
|
33
|
+ "cid" => "",
|
|
34
|
+ "cidr" => "",
|
|
35
|
+ "circle" => "",
|
|
36
|
+ "cstring" => "",
|
|
37
|
+ "date" => "datetime",
|
|
38
|
+ "daterange" => "",
|
|
39
|
+ "decimal" => "float",
|
|
40
|
+ "double precision" => "float",
|
|
41
|
+ "event_trigger" => "",
|
|
42
|
+ "fdw_handler" => "",
|
|
43
|
+ "gtsvector" => "",
|
|
44
|
+ "inet" => "",
|
|
45
|
+ "information_schema.cardinal_number" => "",
|
|
46
|
+ "information_schema.character_data" => "",
|
|
47
|
+ "information_schema.sql_identifier" => "",
|
|
48
|
+ "information_schema.time_stamp" => "",
|
|
49
|
+ "information_schema.yes_or_no" => "",
|
|
50
|
+ "int2vector" => "",
|
|
51
|
+ "int4range" => "",
|
|
52
|
+ "int8range" => "",
|
|
53
|
+ "integer" => "integer",
|
|
54
|
+ "internal" => "",
|
|
55
|
+ "interval" => "",
|
|
56
|
+ "json" => "",
|
|
57
|
+ "jsonb" => "",
|
|
58
|
+ "language_handler" => "",
|
|
59
|
+ "line" => "",
|
|
60
|
+ "lseg" => "",
|
|
61
|
+ "macaddr" => "",
|
|
62
|
+ "money" => "string",
|
|
63
|
+ "name" => "",
|
|
64
|
+ "numeric" => "float",
|
|
65
|
+ "numrange" => "",
|
|
66
|
+ "oid" => "",
|
|
67
|
+ "oidvector" => "",
|
|
68
|
+ "opaque" => "",
|
|
69
|
+ "path" => "",
|
|
70
|
+ "pg_lsn" => "",
|
|
71
|
+ "pg_node_tree" => "",
|
|
72
|
+ "point" => "",
|
|
73
|
+ "polygon" => "",
|
|
74
|
+ "real" => "float",
|
|
75
|
+ "record" => "",
|
|
76
|
+ "refcursor" => "",
|
|
77
|
+ "regclass" => "",
|
|
78
|
+ "regconfig" => "",
|
|
79
|
+ "regdictionary" => "",
|
|
80
|
+ "regoper" => "",
|
|
81
|
+ "regoperator" => "",
|
|
82
|
+ "regproc" => "",
|
|
83
|
+ "regprocedure" => "",
|
|
84
|
+ "regtype" => "",
|
|
85
|
+ "reltime" => "",
|
|
86
|
+ "smallint" => "integer",
|
|
87
|
+ "smallserial" => "integer",
|
|
88
|
+ "smgr" => "",
|
|
89
|
+ "text" => "string",
|
|
90
|
+ "tid" => "",
|
|
91
|
+ "timestamp without time zone" => "datetime",
|
|
92
|
+ "timestamp with time zone" => "datetime",
|
|
93
|
+ "time without time zone" => "datetime",
|
|
94
|
+ "time with time zone" => "datetime",
|
|
95
|
+ "tinterval" => "",
|
|
96
|
+ "trigger" => "",
|
|
97
|
+ "tsquery" => "",
|
|
98
|
+ "tsrange" => "",
|
|
99
|
+ "tstzrange" => "",
|
|
100
|
+ "tsvector" => "",
|
|
101
|
+ "txid_snapshot" => "",
|
|
102
|
+ "unknown" => "",
|
|
103
|
+ "uuid" => "",
|
|
104
|
+ "void" => "",
|
|
105
|
+ "xid" => "",
|
|
106
|
+ "xml" => ""];
|
|
107
|
+
|
|
108
|
+ /**
|
|
109
|
+ * @param string $sqlType
|
|
110
|
+ * @return string
|
|
111
|
+ */
|
|
112
|
+ public function sqlTypeToPhpType($sqlType)
|
|
113
|
+ {
|
|
114
|
+ $isArray = false;
|
|
115
|
+ if (LuStringUtils::endsWith($sqlType, "[]")) {
|
|
116
|
+ $isArray = true;
|
|
117
|
+ $sqlType = substr($sqlType, 0, strlen($sqlType) - 2);
|
|
118
|
+ }
|
|
119
|
+ if (!isset(static::$types) || static::$types[$sqlType] == "") {
|
|
120
|
+ return "string";
|
|
121
|
+ }
|
|
122
|
+ return static::$types[$sqlType] . ($isArray ? "[]" : "");
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ /**
|
|
126
|
+ * @param \PDO $pdo
|
|
127
|
+ * @return array|null
|
|
128
|
+ */
|
|
129
|
+ public function getTablesFull($pdo)
|
|
130
|
+ {
|
|
131
|
+ $tables_names = $this->getTables($pdo);
|
|
132
|
+ if (is_null($tables_names)) {
|
|
133
|
+ return null;
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ $tables = [];
|
|
137
|
+ foreach ($tables_names as $table_name) {
|
|
138
|
+ $columns = $this->getColumns($pdo, $table_name);
|
|
139
|
+ if (is_null($columns)) {
|
|
140
|
+ return null;
|
|
141
|
+ }
|
|
142
|
+ $tables[$table_name] = $columns;
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ return $tables;
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ /**
|
|
149
|
+ * @param $pdo \PDO
|
|
150
|
+ * @return string[]|null
|
|
151
|
+ */
|
|
152
|
+ public function getTables($pdo)
|
|
153
|
+ {
|
|
154
|
+ $tablesQuery = $pdo->prepare("SELECT table_name AS name
|
|
155
|
+FROM information_schema.tables
|
|
156
|
+WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')");
|
|
157
|
+ if ($tablesQuery->execute(array())) {
|
|
158
|
+ $tables = $tablesQuery->fetchAll();
|
|
159
|
+ $tables = array_map(function($table)
|
|
160
|
+ {
|
|
161
|
+ return $table["name"];
|
|
162
|
+ }, $tables);
|
|
163
|
+ return $tables;
|
|
164
|
+ }
|
|
165
|
+ else
|
|
166
|
+ return null;
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ /**
|
|
170
|
+ * @param $pdo \PDO
|
|
171
|
+ * @param $table_name string
|
|
172
|
+ * @return array|null
|
|
173
|
+ */
|
|
174
|
+ public function getColumns($pdo, $table_name)
|
|
175
|
+ {
|
|
176
|
+ $columnsQuery = $pdo->prepare("SELECT column_name as name, data_type as data_type, is_nullable as nullable FROM information_schema.columns WHERE table_name = :table_name");
|
|
177
|
+ if ($columnsQuery->execute(array(":table_name" => $table_name)))
|
|
178
|
+ {
|
|
179
|
+ $columns = $columnsQuery->fetchAll();
|
|
180
|
+ return $columns;
|
|
181
|
+ }
|
|
182
|
+ else
|
|
183
|
+ return null;
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ /**
|
|
187
|
+ * @param \PDO $pdo
|
|
188
|
+ * @return array|null
|
|
189
|
+ */
|
|
190
|
+ public function getStoredProceduresFull($pdo)
|
|
191
|
+ {
|
|
192
|
+ $sps = $this->getStoredProcedures($pdo);
|
|
193
|
+ if (is_null($sps)) {
|
|
194
|
+ return null;
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ foreach ($sps as $sp_name => $sp) {
|
|
198
|
+ $args = $this->getStoredProceduresArguments($pdo, $sp_name);
|
|
199
|
+ if (is_null($args)) {
|
|
200
|
+ return null;
|
|
201
|
+ }
|
|
202
|
+ $sps[$sp_name]["args"] = $args;
|
|
203
|
+ }
|
|
204
|
+ return $sps;
|
|
205
|
+ }
|
|
206
|
+
|
|
207
|
+ /**
|
|
208
|
+ * @param $pdo \PDO
|
|
209
|
+ * @return array|null
|
|
210
|
+ */
|
|
211
|
+ public function getStoredProcedures($pdo)
|
|
212
|
+ {
|
|
213
|
+ $spQuery = $pdo->prepare("SELECT r.routine_name AS sp_name, r.data_type AS data_type, proc.proretset AS proretset, proc.prosrc AS prosrc
|
|
214
|
+FROM information_schema.routines r
|
|
215
|
+LEFT JOIN pg_catalog.pg_proc proc ON proc.proname = r.routine_name
|
|
216
|
+WHERE r.specific_schema='public'");
|
|
217
|
+ if ($spQuery->execute())
|
|
218
|
+ {
|
|
219
|
+ $sps = $spQuery->fetchAll();
|
|
220
|
+ $data = [];
|
|
221
|
+ foreach ($sps as $sp) {
|
|
222
|
+ $data[$sp["name"]] = ["sp" => $sp];
|
|
223
|
+ }
|
|
224
|
+ return $data;
|
|
225
|
+ }
|
|
226
|
+ else
|
|
227
|
+ return null;
|
|
228
|
+ }
|
|
229
|
+
|
|
230
|
+ /**
|
|
231
|
+ * @param $pdo \PDO
|
|
232
|
+ * @param $sp string
|
|
233
|
+ * @return array|null
|
|
234
|
+ */
|
|
235
|
+ public function getStoredProceduresArguments($pdo, $sp)
|
|
236
|
+ {
|
|
237
|
+ $sp_name = $sp["sp_name"];
|
|
238
|
+ $spQuery = $pdo->prepare("SELECT parameters.parameter_name as name, parameters.data_type, parameters.parameter_mode
|
|
239
|
+FROM information_schema.routines
|
|
240
|
+JOIN information_schema.parameters ON routines.specific_name=parameters.specific_name
|
|
241
|
+WHERE routines.specific_schema='public' AND routines.routine_name = :sp_name
|
|
242
|
+ORDER BY parameters.ordinal_position;");
|
|
243
|
+ if ($spQuery->execute(array("sp_name" => $sp_name)))
|
|
244
|
+ {
|
|
245
|
+ $sp_ = $spQuery->fetchAll();
|
|
246
|
+ $sps = array("in" => array(), "out" => array());
|
|
247
|
+ foreach ($sp_ as $p)
|
|
248
|
+ $sps[strtolower($p["parameter_mode"])][] = $p;
|
|
249
|
+ $out_count = count($sps['out']);
|
|
250
|
+ if ($out_count == 0)
|
|
251
|
+ {
|
|
252
|
+ $sps['out'][] = array(
|
|
253
|
+ "name" => $sp_name,
|
|
254
|
+ "data_type" => $sp["data_type"],
|
|
255
|
+ "parameter_mode" => "OUT"
|
|
256
|
+ );
|
|
257
|
+ $out_count = 1;
|
|
258
|
+ }
|
|
259
|
+ return $sps;
|
|
260
|
+ }
|
|
261
|
+ else
|
|
262
|
+ return null;
|
|
263
|
+ }
|
|
264
|
+}
|