Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c37adac

Browse files
committed
New stuff for 6.4 jdbc.
Peter mount
1 parent d318315 commit c37adac

File tree

5 files changed

+551
-3
lines changed

5 files changed

+551
-3
lines changed

src/interfaces/jdbc/Makefile

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Makefile for Java JDBC interface
55
#
66
# IDENTIFICATION
7-
# $Header: /cvsroot/pgsql/src/interfaces/jdbc/Attic/Makefile,v 1.8 1998/06/03 18:56:09 scrappy Exp $
7+
# $Header: /cvsroot/pgsql/src/interfaces/jdbc/Attic/Makefile,v 1.9 1998/09/03 02:29:41 momjian Exp $
88
#
99
#-------------------------------------------------------------------------
1010

@@ -70,12 +70,14 @@ OBJS= postgresql/CallableStatement.class \
7070
postgresql/fastpath/FastpathArg.class \
7171
postgresql/geometric/PGbox.class \
7272
postgresql/geometric/PGcircle.class \
73+
postgresql/geometric/PGline.class \
7374
postgresql/geometric/PGlseg.class \
7475
postgresql/geometric/PGpath.class \
7576
postgresql/geometric/PGpoint.class \
7677
postgresql/geometric/PGpolygon.class \
7778
postgresql/largeobject/LargeObject.class \
7879
postgresql/largeobject/LargeObjectManager.class \
80+
postgresql/util/PGmoney.class \
7981
postgresql/util/PGobject.class \
8082
postgresql/util/PGtokenizer.class \
8183
postgresql/util/Serialize.class \
@@ -122,6 +124,7 @@ postgresql/geometric/PGpoint.class: postgresql/geometric/PGpoint.java
122124
postgresql/geometric/PGpolygon.class: postgresql/geometric/PGpolygon.java
123125
postgresql/largeobject/LargeObject.class: postgresql/largeobject/LargeObject.java
124126
postgresql/largeobject/LargeObjectManager.class: postgresql/largeobject/LargeObjectManager.java
127+
postgresql/util/PGmoney.class: postgresql/util/PGmoney.java
125128
postgresql/util/PGobject.class: postgresql/util/PGobject.java
126129
postgresql/util/PGtokenizer.class: postgresql/util/PGtokenizer.java
127130
postgresql/util/Serialize.class: postgresql/util/Serialize.java
@@ -134,7 +137,8 @@ EX= example/basic.class \
134137
example/datestyle.class \
135138
example/psql.class \
136139
example/ImageViewer.class \
137-
example/Objects.class
140+
example/metadata.class
141+
# example/Objects.class
138142

139143
# This rule builds the examples
140144
examples: postgresql.jar $(EX)
@@ -155,6 +159,8 @@ examples: postgresql.jar $(EX)
155159
@echo " example.ImageViewer Example application storing images"
156160
@echo " example.psql Simple java implementation of psql"
157161
@echo " example.Objects Demonstrates Object Serialisation"
162+
@echo " "
163+
@echo " example.metadata Tests various metadata methods"
158164
@echo ------------------------------------------------------------
159165
@echo
160166

@@ -163,5 +169,7 @@ example/blobtest.class: example/blobtest.java
163169
example/datestyle.class: example/datestyle.java
164170
example/psql.class: example/psql.java
165171
example/ImageViewer.class: example/ImageViewer.java
166-
example/Objects.class: example/Objects.java
172+
#example/Objects.class: example/Objects.java
173+
174+
example/metadata.class: example/metadata.java
167175
#######################################################################
+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
package example;
2+
3+
import java.io.*;
4+
import java.sql.*;
5+
import java.text.*;
6+
7+
/**
8+
* This example application is not really an example. It actually performs
9+
* some tests on various methods in the DatabaseMetaData and ResultSetMetaData
10+
* classes.
11+
*
12+
* To use it, simply have a database created. It will create some work tables
13+
* and run tests on them.
14+
*/
15+
16+
public class metadata
17+
{
18+
Connection db; // The connection to the database
19+
Statement st; // Our statement to run queries with
20+
DatabaseMetaData dbmd; // This defines the structure of the database
21+
22+
/**
23+
* These are the available tests on DatabaseMetaData
24+
*/
25+
public void doDatabaseMetaData() throws SQLException {
26+
if(doTest("getProcedures() - should show all available procedures"))
27+
displayResult(dbmd.getProcedures(null,null,null));
28+
29+
if(doTest("getProcedures() with pattern - should show all circle procedures"))
30+
displayResult(dbmd.getProcedures(null,null,"circle%"));
31+
32+
if(doTest("getProcedureColumns() on circle procedures"))
33+
displayResult(dbmd.getProcedureColumns(null,null,"circle%",null));
34+
35+
if(doTest("getTables()"))
36+
displayResult(dbmd.getTables(null,null,null,null));
37+
38+
if(doTest("getColumns() - should show all tables, can take a while to run"))
39+
displayResult(dbmd.getColumns(null,null,null,null));
40+
41+
if(doTest("getColumns() - should show the test_b table"))
42+
displayResult(dbmd.getColumns(null,null,"test_b",null));
43+
44+
if(doTest("getColumnPrivileges() - should show all tables"))
45+
displayResult(dbmd.getColumnPrivileges(null,null,null,null));
46+
47+
if(doTest("getPrimaryKeys()"))
48+
displayResult(dbmd.getPrimaryKeys(null,null,null));
49+
50+
if(doTest("getTypeInfo()"))
51+
displayResult(dbmd.getTypeInfo());
52+
53+
}
54+
55+
/**
56+
* These are the available tests on ResultSetMetaData
57+
*/
58+
public void doResultSetMetaData() throws SQLException {
59+
60+
String sql = "select imagename,descr,source,cost from test_a,test_b,test_c where test_a.id=test_b.imageid and test_a.id=test_c.imageid";
61+
62+
System.out.println("Executing query for tests");
63+
ResultSet rs = st.executeQuery(sql);
64+
ResultSetMetaData rsmd = rs.getMetaData();
65+
66+
if(doTest("isCurrency()"))
67+
System.out.println("isCurrency on col 1 = "+rsmd.isCurrency(1)+" should be false\nisCurrency on col 4 = "+rsmd.isCurrency(4)+" should be true");
68+
69+
// Finally close the query. Now give the user a chance to display the
70+
// ResultSet.
71+
//
72+
// NB: displayResult() actually closes the ResultSet.
73+
if(doTest("Display query result")) {
74+
System.out.println("Query: "+sql);
75+
displayResult(rs);
76+
} else
77+
rs.close();
78+
}
79+
80+
/**
81+
* This creates some test data
82+
*/
83+
public void init() throws SQLException {
84+
System.out.println("Creating some tables");
85+
cleanup();
86+
st.executeUpdate("create table test_a (imagename name,image oid,id int4)");
87+
st.executeUpdate("create table test_b (descr text,imageid int4,id int4)");
88+
st.executeUpdate("create table test_c (source text,cost money,imageid int4)");
89+
90+
System.out.println("Adding some data");
91+
st.executeUpdate("insert into test_a values ('test1',0,1)");
92+
st.executeUpdate("insert into test_b values ('A test description',1,2)");
93+
st.executeUpdate("insert into test_c values ('nowhere particular','$10.99',1)");
94+
}
95+
96+
/**
97+
* This removes the test data
98+
*/
99+
public void cleanup() throws SQLException {
100+
try {
101+
st.executeUpdate("drop table test_a");
102+
st.executeUpdate("drop table test_b");
103+
st.executeUpdate("drop table test_c");
104+
} catch(Exception ex) {
105+
// We ignore any errors here
106+
}
107+
}
108+
109+
public metadata(String args[]) throws ClassNotFoundException, FileNotFoundException, IOException, SQLException
110+
{
111+
String url = args[0];
112+
String usr = args[1];
113+
String pwd = args[2];
114+
115+
// Load the driver
116+
Class.forName("postgresql.Driver");
117+
118+
// Connect to database
119+
System.out.println("Connecting to Database URL = " + url);
120+
db = DriverManager.getConnection(url, usr, pwd);
121+
122+
dbmd = db.getMetaData();
123+
st = db.createStatement();
124+
125+
// This prints the backend's version
126+
System.out.println("Connected to "+dbmd.getDatabaseProductName()+" "+dbmd.getDatabaseProductVersion());
127+
128+
init();
129+
130+
System.out.println();
131+
132+
// Now the tests
133+
if(doTest("Test DatabaseMetaData"))
134+
doDatabaseMetaData();
135+
136+
if(doTest("Test ResultSetMetaData"))
137+
doResultSetMetaData();
138+
139+
System.out.println("\nNow closing the connection");
140+
st.close();
141+
db.close();
142+
143+
cleanup();
144+
}
145+
146+
/**
147+
* This asks if the user requires to run a test.
148+
*/
149+
public boolean doTest(String s) {
150+
System.out.println();
151+
System.out.print(s);
152+
System.out.print(" Perform test? Y or N:");
153+
System.out.flush();
154+
char c = ' ';
155+
try {
156+
while(!(c=='n' || c=='y' || c=='N' || c=='Y')) {
157+
c=(char)System.in.read();
158+
}
159+
} catch(IOException ioe) {
160+
return false;
161+
}
162+
163+
return c=='y' || c=='Y';
164+
}
165+
166+
/**
167+
* This displays a result set.
168+
* Note: it closes the result once complete.
169+
*/
170+
public void displayResult(ResultSet rs) throws SQLException
171+
{
172+
ResultSetMetaData rsmd = rs.getMetaData();
173+
int count=0;
174+
175+
// Print the result column names
176+
int cols = rsmd.getColumnCount();
177+
for(int i=1;i<=cols;i++)
178+
System.out.print(rsmd.getColumnLabel(i)+(i<cols?"\t":"\n"));
179+
180+
// now the results
181+
while(rs.next()) {
182+
count++;
183+
for(int i=1;i<=cols;i++) {
184+
Object o = rs.getObject(i);
185+
if(rs.wasNull())
186+
System.out.print("{null}"+(i<cols?"\t":"\n"));
187+
else
188+
System.out.print(o.toString()+(i<cols?"\t":"\n"));
189+
}
190+
}
191+
192+
System.out.println("Result returned "+count+" rows.");
193+
194+
// finally close the result set
195+
rs.close();
196+
}
197+
198+
/**
199+
* This process / commands (for now just /d)
200+
*/
201+
public void processSlashCommand(String line) throws SQLException
202+
{
203+
if(line.startsWith("\\d")) {
204+
205+
if(line.startsWith("\\d ")) {
206+
// Display details about a table
207+
String table=line.substring(3);
208+
displayResult(dbmd.getColumns(null,null,table,"%"));
209+
} else {
210+
String types[] = null;
211+
if(line.equals("\\d"))
212+
types=allUserTables;
213+
else if(line.equals("\\di"))
214+
types=usrIndices;
215+
else if(line.equals("\\dt"))
216+
types=usrTables;
217+
else if(line.equals("\\ds"))
218+
types=usrSequences;
219+
else if(line.equals("\\dS"))
220+
types=sysTables;
221+
else
222+
throw new SQLException("Unsupported \\d command: "+line);
223+
224+
// Display details about all system tables
225+
//
226+
// Note: the first two arguments are ignored. To keep to the spec,
227+
// you must put null here
228+
//
229+
displayResult(dbmd.getTables(null,null,"%",types));
230+
}
231+
} else
232+
throw new SQLException("Unsupported \\ command: "+line);
233+
}
234+
235+
private static final String allUserTables[] = {"TABLE","INDEX","SEQUENCE"};
236+
private static final String usrIndices[] = {"INDEX"};
237+
private static final String usrTables[] = {"TABLE"};
238+
private static final String usrSequences[] = {"SEQUENCE"};
239+
private static final String sysTables[] = {"SYSTEM TABLE","SYSTEM INDEX"};
240+
241+
/**
242+
* Display some instructions on how to run the example
243+
*/
244+
public static void instructions()
245+
{
246+
System.out.println("\nThis is not really an example, but is used to test the various methods in\nthe DatabaseMetaData and ResultSetMetaData classes.\n");
247+
System.out.println("Useage:\n java example.metadata jdbc:postgresql:database user password [debug]\n\nThe debug field can be anything. It's presence will enable DriverManager's\ndebug trace. Unless you want to see screens of debug items, don't put anything in\nhere.");
248+
System.exit(1);
249+
}
250+
251+
/**
252+
* This little lot starts the test
253+
*/
254+
public static void main(String args[])
255+
{
256+
System.out.println("PostgreSQL metdata tester v6.4 rev 1\n");
257+
258+
if(args.length<3)
259+
instructions();
260+
261+
// This line outputs debug information to stderr. To enable this, simply
262+
// add an extra parameter to the command line
263+
if(args.length>3)
264+
DriverManager.setLogStream(System.err);
265+
266+
// Now run the tests
267+
try {
268+
metadata test = new metadata(args);
269+
} catch(Exception ex) {
270+
System.err.println("Exception caught.\n"+ex);
271+
ex.printStackTrace();
272+
}
273+
}
274+
}

0 commit comments

Comments
 (0)