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

Commit ebb9332

Browse files
author
Barry Lind
committed
Attached is a patch against the CVS repository that fixes the ResultSet absolute() problem.
There's also a little fix for the getRow() method. While fixing absolute(), I noticed that getRow() wasn't quite following the spec: it wasn't returning 0 when the ResultSet wasn't positioned on a row. I've started a ResultSet test case and included it as well. Liam Stewart
1 parent c97a787 commit ebb9332

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

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

+17-8
Original file line numberDiff line numberDiff line change
@@ -836,23 +836,27 @@ public boolean absolute(int index) throws SQLException
836836
//if index<0, count from the end of the result set, but check
837837
//to be sure that it is not beyond the first index
838838
if (index < 0)
839+
{
839840
if (index >= -rows_size)
840841
internalIndex = rows_size + index;
841842
else
842843
{
843844
beforeFirst();
844845
return false;
845846
}
846-
847-
//must be the case that index>0,
848-
//find the correct place, assuming that
849-
//the index is not too large
850-
if (index <= rows_size)
851-
internalIndex = index - 1;
847+
}
852848
else
853849
{
854-
afterLast();
855-
return false;
850+
//must be the case that index>0,
851+
//find the correct place, assuming that
852+
//the index is not too large
853+
if (index <= rows_size)
854+
internalIndex = index - 1;
855+
else
856+
{
857+
afterLast();
858+
return false;
859+
}
856860
}
857861

858862
current_row = internalIndex;
@@ -1074,6 +1078,11 @@ public Ref getRef(int i) throws SQLException
10741078

10751079
public int getRow() throws SQLException
10761080
{
1081+
final int rows_size = rows.size();
1082+
1083+
if (current_row < 0 || current_row >= rows_size)
1084+
return 0;
1085+
10771086
return current_row + 1;
10781087
}
10791088

src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ public static TestSuite suite()
205205
// Connectivity/Protocols
206206

207207
// ResultSet
208+
suite.addTestSuite(ResultSetTest.class);
208209

209210
// Time, Date, Timestamp
210211
suite.addTestSuite(DateTest.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.postgresql.test.jdbc2;
2+
3+
import org.postgresql.test.JDBC2Tests;
4+
import junit.framework.TestCase;
5+
import java.io.*;
6+
import java.sql.*;
7+
8+
/**
9+
* ResultSet tests.
10+
*/
11+
public class ResultSetTest extends TestCase
12+
{
13+
private Connection con;
14+
15+
public ResultSetTest(String name)
16+
{
17+
super(name);
18+
}
19+
20+
protected void setUp() throws Exception
21+
{
22+
con = JDBC2Tests.openDB();
23+
Statement stmt = con.createStatement();
24+
25+
JDBC2Tests.createTable(con, "testrs", "id integer");
26+
27+
stmt.executeUpdate("INSERT INTO testrs VALUES (1)");
28+
stmt.executeUpdate("INSERT INTO testrs VALUES (2)");
29+
stmt.executeUpdate("INSERT INTO testrs VALUES (3)");
30+
stmt.executeUpdate("INSERT INTO testrs VALUES (4)");
31+
stmt.executeUpdate("INSERT INTO testrs VALUES (6)");
32+
stmt.executeUpdate("INSERT INTO testrs VALUES (9)");
33+
34+
stmt.close();
35+
}
36+
37+
protected void tearDown() throws Exception
38+
{
39+
JDBC2Tests.dropTable(con, "testrs");
40+
JDBC2Tests.closeDB(con);
41+
}
42+
43+
public void testAbsolute() throws Exception
44+
{
45+
Statement stmt = con.createStatement();
46+
ResultSet rs = stmt.executeQuery("SELECT * FROM testrs");
47+
48+
assertTrue(rs.absolute(-1));
49+
assertEquals(6, rs.getRow());
50+
51+
assertTrue(rs.absolute(1));
52+
assertEquals(1, rs.getRow());
53+
54+
assertTrue(!rs.absolute(-10));
55+
assertEquals(0, rs.getRow());
56+
assertTrue(rs.next());
57+
assertEquals(1, rs.getRow());
58+
59+
assertTrue(!rs.absolute(10));
60+
assertEquals(0, rs.getRow());
61+
assertTrue(rs.previous());
62+
assertEquals(6, rs.getRow());
63+
64+
stmt.close();
65+
}
66+
}

0 commit comments

Comments
 (0)