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

Commit 8439a83

Browse files
author
Peter Mount
committed
Tue Jan 30 22:24:00 GMT 2001 peter@retep.org.uk
- Fixed bug where Statement.setMaxRows() was a global setting. Now limited to just itself. - Changed LargeObject.read(byte[],int,int) to return the actual number of bytes read (used to be void). - LargeObject now supports InputStream's! - PreparedStatement.setBinaryStream() now works! - ResultSet.getBinaryStream() now returns an InputStream that doesn't copy the blob into memory first! - Connection.isClosed() now tests to see if the connection is still alive rather than if it thinks it's alive.
1 parent dca0762 commit 8439a83

File tree

12 files changed

+442
-97
lines changed

12 files changed

+442
-97
lines changed

src/interfaces/jdbc/CHANGELOG

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
Tue Jan 30 22:24:00 GMT 2001 peter@retep.org.uk
2+
- Fixed bug where Statement.setMaxRows() was a global setting. Now
3+
limited to just itself.
4+
- Changed LargeObject.read(byte[],int,int) to return the actual number
5+
of bytes read (used to be void).
6+
- LargeObject now supports InputStream's!
7+
- PreparedStatement.setBinaryStream() now works!
8+
- ResultSet.getBinaryStream() now returns an InputStream that doesn't
9+
copy the blob into memory first!
10+
- Connection.isClosed() now tests to see if the connection is still alive
11+
rather than if it thinks it's alive.
112
Thu Jan 25 09:11:00 GMT 2001 peter@retep.org.uk
213
- Added an alternative constructor to PGSQLException so that debugging
314
some more osteric bugs is easier. If only 1 arg is supplied and it's

src/interfaces/jdbc/example/basic.java

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
*
9-
* $Id: basic.java,v 1.5 2000/06/06 11:05:57 peter Exp $
9+
* $Id: basic.java,v 1.6 2001/01/31 08:26:01 peter Exp $
1010
*
1111
* This example tests the basic components of the JDBC driver, and shows
1212
* how even the simplest of queries can be implemented.
@@ -22,40 +22,40 @@ public class basic
2222
{
2323
Connection db; // The connection to the database
2424
Statement st; // Our statement to run queries with
25-
25+
2626
public basic(String args[]) throws ClassNotFoundException, FileNotFoundException, IOException, SQLException
2727
{
2828
String url = args[0];
2929
String usr = args[1];
3030
String pwd = args[2];
31-
31+
3232
// Load the driver
3333
Class.forName("org.postgresql.Driver");
34-
34+
3535
// Connect to database
3636
System.out.println("Connecting to Database URL = " + url);
3737
db = DriverManager.getConnection(url, usr, pwd);
38-
38+
3939
System.out.println("Connected...Now creating a statement");
4040
st = db.createStatement();
41-
41+
4242
// Clean up the database (in case we failed earlier) then initialise
4343
cleanup();
44-
44+
4545
// Now run tests using JDBC methods
4646
doexample();
47-
47+
4848
// Clean up the database
4949
cleanup();
50-
50+
5151
// Finally close the database
5252
System.out.println("Now closing the connection");
5353
st.close();
5454
db.close();
55-
55+
5656
//throw postgresql.Driver.notImplemented();
5757
}
58-
58+
5959
/**
6060
* This drops the table (if it existed). No errors are reported.
6161
*/
@@ -67,35 +67,36 @@ public void cleanup()
6767
// We ignore any errors here
6868
}
6969
}
70-
70+
7171
/**
7272
* This performs the example
7373
*/
7474
public void doexample() throws SQLException
7575
{
7676
System.out.println("\nRunning tests:");
77-
77+
7878
// First we need a table to store data in
7979
st.executeUpdate("create table basic (a int2, b int2)");
80-
80+
8181
// Now insert some data, using the Statement
8282
st.executeUpdate("insert into basic values (1,1)");
8383
st.executeUpdate("insert into basic values (2,1)");
8484
st.executeUpdate("insert into basic values (3,1)");
85-
85+
8686
// This shows how to get the oid of a just inserted row
87+
// updated for 7.1
8788
st.executeUpdate("insert into basic values (4,1)");
88-
int insertedOID = ((org.postgresql.ResultSet)st.getResultSet()).getInsertedOID();
89+
int insertedOID = ((org.postgresql.jdbc2.Statement)st).getInsertedOID();
8990
System.out.println("Inserted row with oid "+insertedOID);
90-
91+
9192
// Now change the value of b from 1 to 8
9293
st.executeUpdate("update basic set b=8");
9394
System.out.println("Updated "+st.getUpdateCount()+" rows");
94-
95+
9596
// Now delete 2 rows
9697
st.executeUpdate("delete from basic where a<3");
9798
System.out.println("deleted "+st.getUpdateCount()+" rows");
98-
99+
99100
// For large inserts, a PreparedStatement is more efficient, because it
100101
// supports the idea of precompiling the SQL statement, and to store
101102
// directly, a Java object into any column. PostgreSQL doesnt support
@@ -112,7 +113,7 @@ public void doexample() throws SQLException
112113
ps.executeUpdate(); // executeUpdate because insert returns no data
113114
}
114115
ps.close(); // Always close when we are done with it
115-
116+
116117
// Finally perform a query on the table
117118
System.out.println("performing a query");
118119
ResultSet rs = st.executeQuery("select a, b from basic");
@@ -126,7 +127,7 @@ public void doexample() throws SQLException
126127
}
127128
rs.close(); // again, you must close the result when done
128129
}
129-
130+
130131
// Now run the query again, showing a more efficient way of getting the
131132
// result if you don't know what column number a value is in
132133
System.out.println("performing another query");
@@ -140,7 +141,7 @@ public void doexample() throws SQLException
140141
//
141142
int col_a = rs.findColumn("a");
142143
int col_b = rs.findColumn("b");
143-
144+
144145
// Now we run through the result set, printing out the result.
145146
// Again, we must call .next() before attempting to read any results
146147
while(rs.next()) {
@@ -150,11 +151,22 @@ public void doexample() throws SQLException
150151
}
151152
rs.close(); // again, you must close the result when done
152153
}
153-
154+
155+
// Now test maxrows by setting it to 3 rows
156+
st.setMaxRows(3);
157+
System.out.println("performing a query limited to "+st.getMaxRows());
158+
rs = st.executeQuery("select a, b from basic");
159+
while(rs.next()) {
160+
int a = rs.getInt("a"); // This shows how to get the value by name
161+
int b = rs.getInt(2); // This shows how to get the value by column
162+
System.out.println(" a="+a+" b="+b);
163+
}
164+
rs.close(); // again, you must close the result when done
165+
154166
// The last thing to do is to drop the table. This is done in the
155167
// cleanup() method.
156168
}
157-
169+
158170
/**
159171
* Display some instructions on how to run the example
160172
*/
@@ -164,22 +176,22 @@ public static void instructions()
164176
System.out.println("Useage:\n java example.basic 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 items, don't put anything in\nhere.");
165177
System.exit(1);
166178
}
167-
179+
168180
/**
169181
* This little lot starts the test
170182
*/
171183
public static void main(String args[])
172184
{
173185
System.out.println("PostgreSQL basic test v6.3 rev 1\n");
174-
186+
175187
if(args.length<3)
176188
instructions();
177-
189+
178190
// This line outputs debug information to stderr. To enable this, simply
179191
// add an extra parameter to the command line
180192
if(args.length>3)
181193
DriverManager.setLogStream(System.err);
182-
194+
183195
// Now run the tests
184196
try {
185197
basic test = new basic(args);

src/interfaces/jdbc/jdbc.jpx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<property category="sys" name="CheckStable" value="1" />
1010
<property category="sys" name="Company" value="" />
1111
<property category="sys" name="Copyright" value="Copyright (c) 2001" />
12-
<property category="sys" name="DefaultPackage" value="org.postgresql.core" />
12+
<property category="sys" name="DefaultPackage" value="org.postgresql.largeobject" />
1313
<property category="sys" name="Description" value="" />
1414
<property category="sys" name="DocPath" value="doc" />
1515
<property category="sys" name="ExcludeClassEnabled" value="0" />

src/interfaces/jdbc/org/postgresql/Connection.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.postgresql.util.*;
1111

1212
/**
13-
* $Id: Connection.java,v 1.13 2001/01/18 17:37:12 peter Exp $
13+
* $Id: Connection.java,v 1.14 2001/01/31 08:26:01 peter Exp $
1414
*
1515
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
1616
* JDBC2 versions of the Connection class.
@@ -22,7 +22,7 @@ public abstract class Connection
2222
public PG_Stream pg_stream;
2323

2424
// This is set by org.postgresql.Statement.setMaxRows()
25-
public int maxrows = 0; // maximum no. of rows; 0 = unlimited
25+
//public int maxrows = 0; // maximum no. of rows; 0 = unlimited
2626

2727
private String PG_HOST;
2828
private int PG_PORT;
@@ -414,6 +414,11 @@ public java.sql.ResultSet ExecSQL(String sql) throws SQLException
414414
*/
415415
public java.sql.ResultSet ExecSQL(String sql,java.sql.Statement stat) throws SQLException
416416
{
417+
// added Jan 30 2001 to correct maxrows per statement
418+
int maxrows=0;
419+
if(stat!=null)
420+
maxrows=stat.getMaxRows();
421+
417422
// added Oct 7 1998 to give us thread safety.
418423
synchronized(pg_stream) {
419424
// Deallocate all resources in the stream associated

src/interfaces/jdbc/org/postgresql/jdbc1/Statement.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Statement implements java.sql.Statement
2929
SQLWarning warnings = null; // The warnings chain.
3030
int timeout = 0; // The timeout for a query (not used)
3131
boolean escapeProcessing = true;// escape processing flag
32+
int maxrows=0;
3233

3334
/**
3435
* Constructor for a Statement. It simply sets the connection
@@ -129,7 +130,7 @@ public void setMaxFieldSize(int max) throws SQLException
129130
*/
130131
public int getMaxRows() throws SQLException
131132
{
132-
return connection.maxrows;
133+
return maxrows;
133134
}
134135

135136
/**
@@ -141,7 +142,7 @@ public int getMaxRows() throws SQLException
141142
*/
142143
public void setMaxRows(int max) throws SQLException
143144
{
144-
connection.maxrows = max;
145+
maxrows = max;
145146
}
146147

147148
/**

src/interfaces/jdbc/org/postgresql/jdbc2/Connection.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.postgresql.util.*;
1818

1919
/**
20-
* $Id: Connection.java,v 1.5 2001/01/18 17:37:14 peter Exp $
20+
* $Id: Connection.java,v 1.6 2001/01/31 08:26:02 peter Exp $
2121
*
2222
* A Connection represents a session with a specific database. Within the
2323
* context of a Connection, SQL statements are executed and results are
@@ -265,7 +265,27 @@ public void close() throws SQLException
265265
*/
266266
public boolean isClosed() throws SQLException
267267
{
268-
return (pg_stream == null);
268+
// If the stream is gone, then close() was called
269+
if(pg_stream == null)
270+
return true;
271+
272+
// ok, test the connection
273+
try {
274+
// by sending an empty query. If we are dead, then an SQLException should
275+
// be thrown
276+
java.sql.ResultSet rs = ExecSQL(" ");
277+
if(rs!=null)
278+
rs.close();
279+
280+
// By now, we must be alive
281+
return false;
282+
} catch(SQLException se) {
283+
// Why throw an SQLException as this may fail without throwing one,
284+
// ie isClosed() is called incase the connection has died, and we don't
285+
// want to find out by an Exception, so instead we return true, as its
286+
// most likely why it was thrown in the first place.
287+
return true;
288+
}
269289
}
270290

271291
/**

0 commit comments

Comments
 (0)