-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathMetaDataInts.java
More file actions
114 lines (99 loc) · 2.99 KB
/
Copy pathMetaDataInts.java
File metadata and controls
114 lines (99 loc) · 2.99 KB
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
/*
* Copyright (c) 2004-2020 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
* which accompanies this distribution, and is available at
* http://opensource.org/licenses/BSD-3-Clause
*
* Contributors:
* Tada AB
* Filip Hrbek
* Chapman Flack
*/
package org.postgresql.pljava.example;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.logging.Logger;
import org.postgresql.pljava.ResultSetProvider;
/**
* Example returning (varchar,int) rows, one for each int-valued
* attribute in the JDBC {@link DatabaseMetaData}.
* @author Filip Hrbek
*/
public class MetaDataInts implements ResultSetProvider {
public static ResultSetProvider getDatabaseMetaDataInts()
throws SQLException {
try {
return new MetaDataInts();
} catch (SQLException e) {
throw new SQLException("Error reading DatabaseMetaData",
e.getMessage());
}
}
String[] methodNames;
Integer[] methodResults;
public MetaDataInts() throws SQLException {
Logger log = Logger.getAnonymousLogger();
class MethodComparator implements Comparator<Method> {
@Override
public int compare(Method a, Method b) {
return a.getName().compareTo(b.getName());
}
}
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
DatabaseMetaData md = conn.getMetaData();
Method[] m = DatabaseMetaData.class.getMethods();
Arrays.sort(m, new MethodComparator());
Class<?> prototype[];
Class<?> returntype;
Object[] args = new Object[0];
Integer result = null;
ArrayList<String> mn = new ArrayList<>();
ArrayList<Integer> mr = new ArrayList<>();
for (int i = 0; i < m.length; i++) {
prototype = m[i].getParameterTypes();
if (prototype.length > 0)
continue;
returntype = m[i].getReturnType();
if (!returntype.equals(int.class))
continue;
try {
result = (Integer) m[i].invoke(md, args);
} catch (InvocationTargetException e) {
log.info("Method: " + m[i].getName() + " => "
+ e.getTargetException().getMessage());
result = -1;
} catch (Exception e) {
log.info("Method: " + m[i].getName() + " => " + e.getMessage());
result = -1;
}
mn.add(m[i].getName());
mr.add(result);
}
methodNames = mn.toArray(new String[mn.size()]);
methodResults = mr.toArray(new Integer[mr.size()]);
}
@Override
public boolean assignRowValues(ResultSet receiver, int currentRow)
throws SQLException {
if (currentRow < methodNames.length) {
receiver.updateString(1, methodNames[currentRow]);
receiver.updateInt(2, methodResults[currentRow].intValue());
return true;
}
return false;
}
@Override
public void close() {
}
}