forked from pgjdbc/pgjdbc
/
DatabaseMetaDataTest.java
497 lines (428 loc) · 20 KB
/
DatabaseMetaDataTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Copyright (c) 2007, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc4;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.postgresql.core.ServerVersion;
import org.postgresql.test.TestUtil;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DatabaseMetaDataTest {
private Connection conn;
@Before
public void setUp() throws Exception {
conn = TestUtil.openDB();
TestUtil.dropSequence(conn, "sercoltest_a_seq");
TestUtil.createTable(conn, "sercoltest", "a serial, b int");
TestUtil.createSchema(conn, "hasfunctions");
TestUtil.createSchema(conn, "nofunctions");
TestUtil.createSchema(conn, "hasprocedures");
TestUtil.createSchema(conn, "noprocedures");
TestUtil.execute("create function hasfunctions.addfunction (integer, integer) "
+ "RETURNS integer AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE", conn);
if (TestUtil.haveMinimumServerVersion(conn, ServerVersion.v11)) {
TestUtil.execute("create procedure hasprocedures.addprocedure() "
+ "LANGUAGE plpgsql AS $$ BEGIN SELECT 1; END; $$", conn);
}
}
@After
public void tearDown() throws Exception {
TestUtil.dropSequence(conn, "sercoltest_a_seq");
TestUtil.dropTable(conn, "sercoltest");
TestUtil.dropSchema(conn, "hasfunctions");
TestUtil.dropSchema(conn, "nofunctions");
TestUtil.dropSchema(conn, "hasprocedures");
TestUtil.dropSchema(conn, "noprocedures");
TestUtil.closeDB(conn);
}
@Test
public void testGetClientInfoProperties() throws Exception {
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getClientInfoProperties();
if (!TestUtil.haveMinimumServerVersion(conn, ServerVersion.v9_0)) {
assertTrue(!rs.next());
return;
}
assertTrue(rs.next());
assertEquals("ApplicationName", rs.getString("NAME"));
}
@Test
public void testGetColumnsForAutoIncrement() throws Exception {
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getColumns("%", "%", "sercoltest", "%");
assertTrue(rs.next());
assertEquals("a", rs.getString("COLUMN_NAME"));
assertEquals("YES", rs.getString("IS_AUTOINCREMENT"));
assertTrue(rs.next());
assertEquals("b", rs.getString("COLUMN_NAME"));
assertEquals("NO", rs.getString("IS_AUTOINCREMENT"));
assertTrue(!rs.next());
}
@Test
public void testGetSchemas() throws SQLException {
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getSchemas("", "publ%");
assertTrue(rs.next());
assertEquals("public", rs.getString("TABLE_SCHEM"));
assertNull(rs.getString("TABLE_CATALOG"));
assertTrue(!rs.next());
}
@Test
public void testGetFunctionsInSchemaForFunctions() throws SQLException {
DatabaseMetaData dbmd = conn.getMetaData();
try (ResultSet rs = dbmd.getFunctions("", "hasfunctions","")) {
List<CatalogObject> list = assertFunctionRSAndReturnList(rs);
assertEquals("There should be one function in the hasfunctions schema", list.size(), 1);
assertListContains("getFunctions('', 'hasfunctions', '') must contain addfunction", list, "hasfunctions", "addfunction");
}
try (ResultSet rs = dbmd.getFunctions("", "hasfunctions", "addfunction")) {
List<CatalogObject> list = assertFunctionRSAndReturnList(rs);
assertEquals("There should be one function in the hasfunctions schema with name addfunction", list.size(), 1);
assertListContains("getFunctions('', 'hasfunctions', 'addfunction') must contain addfunction", list, "hasfunctions", "addfunction");
}
try (ResultSet rs = dbmd.getFunctions("", "nofunctions","")) {
boolean hasFunctions = rs.next();
assertFalse("There should be no functions in the nofunctions schema", hasFunctions);
}
}
@Test
public void testGetFunctionsInSchemaForProcedures() throws SQLException {
// Due to the introduction of actual stored procedures in PostgreSQL 11, getFunctions should not return procedures for PostgreSQL versions 11+
// On older installation we do not create the procedures so the below schemas should all be empty
DatabaseMetaData dbmd = conn.getMetaData();
// Search for functions in schema "hasprocedures"
try (ResultSet rs = dbmd.getFunctions("", "hasprocedures", null)) {
assertFalse("The hasprocedures schema not return procedures from getFunctions", rs.next());
}
// Search for functions in schema "noprocedures" (which should never expect records)
try (ResultSet rs = dbmd.getFunctions("", "noprocedures", null)) {
assertFalse("The noprocedures schema should not have functions", rs.next());
}
// Search for functions by procedure name "addprocedure"
try (ResultSet rs = dbmd.getFunctions("", "hasprocedures", "addprocedure")) {
assertFalse("Should not return procedures from getFunctions by schema + name", rs.next());
}
}
@Test
public void testGetProceduresInSchemaForFunctions() throws SQLException {
// Due to the introduction of actual stored procedures in PostgreSQL 11, getProcedures should not return functions for PostgreSQL versions 11+
DatabaseMetaData dbmd = conn.getMetaData();
// Search for procedures in schema "hasfunctions" (which should expect a record only for PostgreSQL < 11)
try (ResultSet rs = dbmd.getProcedures("", "hasfunctions",null)) {
if (TestUtil.haveMinimumServerVersion(conn, ServerVersion.v11)) {
assertFalse("PostgreSQL11+ should not return functions from getProcedures", rs.next());
} else {
// PostgreSQL prior to 11 should return functions from getProcedures
assertProcedureRS(rs);
}
}
// Search for procedures in schema "nofunctions" (which should never expect records)
try (ResultSet rs = dbmd.getProcedures("", "nofunctions", null)) {
assertFalse("getProcedures(...) should not return procedures for schema nofunctions", rs.next());
}
// Search for procedures by function name "addfunction" within schema "hasfunctions" (which should expect a record for PostgreSQL < 11)
try (ResultSet rs = dbmd.getProcedures("", "hasfunctions", "addfunction")) {
if (TestUtil.haveMinimumServerVersion(conn, ServerVersion.v11)) {
assertFalse("PostgreSQL11+ should not return functions from getProcedures", rs.next());
} else {
// PostgreSQL prior to 11 should return functions from getProcedures
assertProcedureRS(rs);
}
}
// Search for procedures by function name "addfunction" within schema "nofunctions" (which should never expect records)
try (ResultSet rs = dbmd.getProcedures("", "nofunctions", "addfunction")) {
assertFalse("getProcedures(...) should not return procedures for schema nofunctions + addfunction", rs.next());
}
}
@Test
public void testGetProceduresInSchemaForProcedures() throws SQLException {
// Only run this test for PostgreSQL version 11+; assertions for versions prior would be vacuously true as we don't create a procedure in the setup for older versions
Assume.assumeTrue(TestUtil.haveMinimumServerVersion(conn, ServerVersion.v11));
DatabaseMetaData dbmd = conn.getMetaData();
try (ResultSet rs = dbmd.getProcedures("", "hasprocedures", null)) {
int count = assertProcedureRS(rs);
assertTrue("getProcedures() should be non-empty for the hasprocedures schema", count == 1);
}
try (ResultSet rs = dbmd.getProcedures("", "noprocedures", null)) {
assertFalse("getProcedures() should be empty for the hasprocedures schema", rs.next());
}
try (ResultSet rs = dbmd.getProcedures("", "hasfunctions", null)) {
assertFalse("getProcedures() should be empty for the nofunctions schema", rs.next());
}
try (ResultSet rs = dbmd.getProcedures("", "nofunctions", null)) {
assertFalse("getProcedures() should be empty for the nofunctions schema", rs.next());
}
}
@Test
public void testGetFunctionsWithBlankPatterns() throws SQLException {
int minFuncCount = 1000;
DatabaseMetaData dbmd = conn.getMetaData();
final int totalCount;
try (ResultSet rs = dbmd.getFunctions("", "", "")) {
List<CatalogObject> list = assertFunctionRSAndReturnList(rs);
totalCount = list.size(); // Rest of this test will validate against this value
assertThat(totalCount > minFuncCount, is(true));
assertListContains("getFunctions('', '', '') must contain addfunction", list, "hasfunctions", "addfunction");
}
// Should be same as blank pattern
try (ResultSet rs = dbmd.getFunctions(null, null, null)) {
int count = assertGetFunctionRS(rs);
assertThat(count, is(totalCount));
}
// Catalog parameter has no affect on our getFunctions filtering
try (ResultSet rs = dbmd.getFunctions("ANYTHING_WILL_WORK", null, null)) {
int count = assertGetFunctionRS(rs);
assertThat(count, is(totalCount));
}
// Filter by schema
try (ResultSet rs = dbmd.getFunctions("", "pg_catalog", null)) {
int count = assertGetFunctionRS(rs);
assertThat(count > minFuncCount, is(true));
}
// Filter by schema and function name
try (ResultSet rs = dbmd.getFunctions("", "pg_catalog", "abs")) {
int count = assertGetFunctionRS(rs);
assertThat(count >= 1, is(true));
}
// Filter by function name only
try (ResultSet rs = dbmd.getFunctions("", "", "abs")) {
int count = assertGetFunctionRS(rs);
assertThat(count >= 1, is(true));
}
}
private static class CatalogObject implements Comparable<CatalogObject> {
private final String catalog;
private final String schema;
private final String name;
private final String specificName;
private CatalogObject(String catalog, String schema, String name, String specificName) {
this.catalog = catalog;
this.schema = schema;
this.name = name;
this.specificName = specificName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((catalog == null) ? 0 : catalog.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((schema == null) ? 0 : schema.hashCode());
result = prime * result + ((specificName == null) ? 0 : specificName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
} else if (obj == this) {
return true;
}
return compareTo((CatalogObject)obj) == 0;
}
@Override
public int compareTo(CatalogObject other) {
int comp = catalog.compareTo(other.catalog);
if (comp != 0) {
return comp;
}
comp = schema.compareTo(other.schema);
if (comp != 0) {
return comp;
}
comp = name.compareTo(other.name);
if (comp != 0) {
return comp;
}
comp = specificName.compareTo(other.specificName);
if (comp != 0) {
return comp;
}
return 0;
}
}
/** Assert some basic result from ResultSet of a GetFunctions method. Return the total row count. */
private int assertGetFunctionRS(ResultSet rs) throws SQLException {
return assertFunctionRSAndReturnList(rs).size();
}
private List<CatalogObject> assertFunctionRSAndReturnList(ResultSet rs) throws SQLException {
// There should be at least one row
assertThat(rs.next(), is(true));
assertThat(rs.getString("FUNCTION_CAT"), is(System.getProperty("database")));
assertThat(rs.getString("FUNCTION_SCHEM"), notNullValue());
assertThat(rs.getString("FUNCTION_NAME"), notNullValue());
assertThat(rs.getShort("FUNCTION_TYPE") >= 0, is(true));
assertThat(rs.getString("SPECIFIC_NAME"), notNullValue());
// Ensure there is enough column and column value retrieve by index should be same as column name (ordered)
assertThat(rs.getMetaData().getColumnCount(), is(6));
assertThat(rs.getString(1), is(rs.getString("FUNCTION_CAT")));
assertThat(rs.getString(2), is(rs.getString("FUNCTION_SCHEM")));
assertThat(rs.getString(3), is(rs.getString("FUNCTION_NAME")));
assertThat(rs.getString(4), is(rs.getString("REMARKS")));
assertThat(rs.getShort(5), is(rs.getShort("FUNCTION_TYPE")));
assertThat(rs.getString(6), is(rs.getString("SPECIFIC_NAME")));
// Get all result and assert they are ordered per javadoc spec:
// FUNCTION_CAT, FUNCTION_SCHEM, FUNCTION_NAME and SPECIFIC_NAME
List<CatalogObject> result = new ArrayList<>();
do {
CatalogObject obj = new CatalogObject(
rs.getString("FUNCTION_CAT"),
rs.getString("FUNCTION_SCHEM"),
rs.getString("FUNCTION_NAME"),
rs.getString("SPECIFIC_NAME"));
result.add(obj);
} while (rs.next());
List<CatalogObject> orderedResult = new ArrayList<>(result);
Collections.sort(orderedResult);
assertThat(result, is(orderedResult));
return result;
}
private int assertProcedureRS(ResultSet rs) throws SQLException {
return assertProcedureRSAndReturnList(rs).size();
}
private List<CatalogObject> assertProcedureRSAndReturnList(ResultSet rs) throws SQLException {
// There should be at least one row
assertThat(rs.next(), is(true));
assertThat(rs.getString("PROCEDURE_CAT"), nullValue());
assertThat(rs.getString("PROCEDURE_SCHEM"), notNullValue());
assertThat(rs.getString("PROCEDURE_NAME"), notNullValue());
assertThat(rs.getShort("PROCEDURE_TYPE") >= 0, is(true));
assertThat(rs.getString("SPECIFIC_NAME"), notNullValue());
// Ensure there is enough column and column value retrieve by index should be same as column name (ordered)
assertThat(rs.getMetaData().getColumnCount(), is(9));
assertThat(rs.getString(1), is(rs.getString("PROCEDURE_CAT")));
assertThat(rs.getString(2), is(rs.getString("PROCEDURE_SCHEM")));
assertThat(rs.getString(3), is(rs.getString("PROCEDURE_NAME")));
// Per JDBC spec, indexes 4, 5, and 6 are reserved for future use
assertThat(rs.getString(7), is(rs.getString("REMARKS")));
assertThat(rs.getShort(8), is(rs.getShort("PROCEDURE_TYPE")));
assertThat(rs.getString(9), is(rs.getString("SPECIFIC_NAME")));
// Get all result and assert they are ordered per javadoc spec:
// FUNCTION_CAT, FUNCTION_SCHEM, FUNCTION_NAME and SPECIFIC_NAME
List<CatalogObject> result = new ArrayList<>();
do {
CatalogObject obj = new CatalogObject(
rs.getString("PROCEDURE_CAT"),
rs.getString("PROCEDURE_SCHEM"),
rs.getString("PROCEDURE_NAME"),
rs.getString("SPECIFIC_NAME"));
result.add(obj);
} while (rs.next());
List<CatalogObject> orderedResult = new ArrayList<>(result);
Collections.sort(orderedResult);
assertThat(result, is(orderedResult));
return result;
}
private void assertListContains(String message, List<CatalogObject> list, String schema, String name) throws SQLException {
boolean found = list.stream().anyMatch(item -> item.schema.equals(schema) && item.name.equals(name));
assertTrue(message + "; schema=" + schema + " name=" + name, found);
}
@Test
public void testGetFunctionsWithSpecificTypes() throws SQLException {
// These function creation are borrow from jdbc2/DatabaseMetaDataTest
// We modify to ensure new function created are returned by getFunctions()
DatabaseMetaData dbmd = conn.getMetaData();
if (TestUtil.haveMinimumServerVersion(conn, ServerVersion.v8_4)) {
Statement stmt = conn.createStatement();
stmt.execute(
"CREATE OR REPLACE FUNCTION getfunc_f1(int, varchar) RETURNS int AS 'SELECT 1;' LANGUAGE SQL");
ResultSet rs = dbmd.getFunctions("", "", "getfunc_f1");
assertThat(rs.next(), is(true));
assertThat(rs.getString("FUNCTION_NAME"), is("getfunc_f1"));
assertThat(rs.getShort("FUNCTION_TYPE"), is((short)DatabaseMetaData.functionNoTable));
assertThat(rs.next(), is(false));
rs.close();
stmt.execute("DROP FUNCTION getfunc_f1(int, varchar)");
stmt.execute(
"CREATE OR REPLACE FUNCTION getfunc_f3(IN a int, INOUT b varchar, OUT c timestamptz) AS $f$ BEGIN b := 'a'; c := now(); return; END; $f$ LANGUAGE plpgsql");
rs = dbmd.getFunctions("", "", "getfunc_f3");
assertThat(rs.next(), is(true));
assertThat(rs.getString("FUNCTION_NAME"), is("getfunc_f3"));
assertThat(rs.getShort("FUNCTION_TYPE"), is((short)DatabaseMetaData.functionNoTable));
assertThat(rs.next(), is(false));
rs.close();
stmt.execute("DROP FUNCTION getfunc_f3(int, varchar)");
// RETURNS TABLE requires PostgreSQL 8.4+
stmt.execute(
"CREATE OR REPLACE FUNCTION getfunc_f5() RETURNS TABLE (i int) LANGUAGE sql AS 'SELECT 1'");
rs = dbmd.getFunctions("", "", "getfunc_f5");
assertThat(rs.next(), is(true));
assertThat(rs.getString("FUNCTION_NAME"), is("getfunc_f5"));
assertThat(rs.getShort("FUNCTION_TYPE"), is((short)DatabaseMetaData.functionReturnsTable));
assertThat(rs.next(), is(false));
rs.close();
stmt.execute("DROP FUNCTION getfunc_f5()");
} else {
// For PG 8.3 or 8.2 it will resulted in unknown function type
Statement stmt = conn.createStatement();
stmt.execute(
"CREATE OR REPLACE FUNCTION getfunc_f1(int, varchar) RETURNS int AS 'SELECT 1;' LANGUAGE SQL");
ResultSet rs = dbmd.getFunctions("", "", "getfunc_f1");
assertThat(rs.next(), is(true));
assertThat(rs.getString("FUNCTION_NAME"), is("getfunc_f1"));
assertThat(rs.getShort("FUNCTION_TYPE"), is((short)DatabaseMetaData.functionResultUnknown));
assertThat(rs.next(), is(false));
rs.close();
stmt.execute("DROP FUNCTION getfunc_f1(int, varchar)");
stmt.execute(
"CREATE OR REPLACE FUNCTION getfunc_f3(IN a int, INOUT b varchar, OUT c timestamptz) AS $f$ BEGIN b := 'a'; c := now(); return; END; $f$ LANGUAGE plpgsql");
rs = dbmd.getFunctions("", "", "getfunc_f3");
assertThat(rs.next(), is(true));
assertThat(rs.getString("FUNCTION_NAME"), is("getfunc_f3"));
assertThat(rs.getShort("FUNCTION_TYPE"), is((short)DatabaseMetaData.functionResultUnknown));
assertThat(rs.next(), is(false));
rs.close();
stmt.execute("DROP FUNCTION getfunc_f3(int, varchar)");
}
}
@Test
public void testSortedDataTypes() throws SQLException {
// https://github.com/pgjdbc/pgjdbc/issues/716
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getTypeInfo();
int lastType = Integer.MIN_VALUE;
while (rs.next()) {
int type = rs.getInt("DATA_TYPE");
assertTrue(lastType <= type);
lastType = type;
}
}
@Test
public void testGetSqlTypes() throws SQLException {
if (TestUtil.haveMinimumServerVersion(conn, ServerVersion.v10)) {
try (Connection privileged = TestUtil.openPrivilegedDB()) {
try (Statement stmt = privileged.createStatement()) {
// create a function called array_in
stmt.execute("CREATE OR REPLACE FUNCTION public.array_in(anyarray, oid, integer)\n"
+ " RETURNS anyarray\n"
+ " LANGUAGE internal\n"
+ " STABLE PARALLEL SAFE STRICT\n"
+ "AS $function$array_in$function$");
}
DatabaseMetaData dbmd = privileged.getMetaData();
ResultSet rs = dbmd.getTypeInfo();
try (Statement stmt = privileged.createStatement()) {
stmt.execute("drop function public.array_in(anyarray, oid, integer)");
}
}
}
}
}