Skip to content

Commit d28a8aa

Browse files
authored
Remove sparse index checks (#476)
* Remove sparse index checks * update tests to account for null param
1 parent 0345778 commit d28a8aa

File tree

3 files changed

+17
-51
lines changed

3 files changed

+17
-51
lines changed

src/main/java/org/sqlite/core/CorePreparedStatement.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323

2424
import java.sql.Date;
2525
import java.sql.SQLException;
26-
import java.util.BitSet;
2726
import java.util.Calendar;
2827

2928
public abstract class CorePreparedStatement extends JDBC4Statement
3029
{
3130
protected int columnCount;
3231
protected int paramCount;
3332
protected int batchQueryCount;
34-
protected BitSet paramValid;
3533

3634
/**
3735
* Constructs a prepared statement on a provided connection.
@@ -48,7 +46,6 @@ protected CorePreparedStatement(SQLiteConnection conn, String sql) throws SQLExc
4846
rs.colsMeta = db.column_names(pointer);
4947
columnCount = db.column_count(pointer);
5048
paramCount = db.bind_parameter_count(pointer);
51-
paramValid = new BitSet(paramCount);
5249
batchQueryCount = 0;
5350
batch = null;
5451
batchPos = 0;
@@ -62,15 +59,6 @@ protected void finalize() throws SQLException {
6259
close();
6360
}
6461

65-
/**
66-
* Checks if values are bound to statement parameters.
67-
* @throws SQLException
68-
*/
69-
protected void checkParameters() throws SQLException {
70-
if (paramValid.cardinality() != paramCount)
71-
throw new SQLException("Values not bound to statement");
72-
}
73-
7462
/**
7563
* @see org.sqlite.jdbc3.JDBC3Statement#executeBatch()
7664
*/
@@ -80,8 +68,6 @@ public int[] executeBatch() throws SQLException {
8068
return new int[] {};
8169
}
8270

83-
checkParameters();
84-
8571
try {
8672
return conn.getDatabase().executeBatch(pointer, batchQueryCount, batch, conn.getAutoCommit());
8773
}
@@ -96,7 +82,6 @@ public int[] executeBatch() throws SQLException {
9682
@Override
9783
public void clearBatch() throws SQLException {
9884
super.clearBatch();
99-
paramValid.clear();
10085
batchQueryCount = 0;
10186
}
10287

@@ -125,10 +110,8 @@ protected void batch(int pos, Object value) throws SQLException {
125110
checkOpen();
126111
if (batch == null) {
127112
batch = new Object[paramCount];
128-
paramValid.clear();
129113
}
130114
batch[batchPos + pos - 1] = value;
131-
paramValid.set(pos - 1);
132115
}
133116

134117

src/main/java/org/sqlite/jdbc3/JDBC3PreparedStatement.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ protected JDBC3PreparedStatement(SQLiteConnection conn, String sql) throws SQLEx
3636
public void clearParameters() throws SQLException {
3737
checkOpen();
3838
conn.getDatabase().clear_bindings(pointer);
39-
paramValid.clear();
4039
if (batch != null)
4140
for (int i = batchPos; i < batchPos + paramCount; i++)
4241
batch[i] = null;
@@ -49,7 +48,6 @@ public boolean execute() throws SQLException {
4948
checkOpen();
5049
rs.close();
5150
conn.getDatabase().reset(pointer);
52-
checkParameters();
5351

5452
boolean success = false;
5553
try {
@@ -73,7 +71,6 @@ public ResultSet executeQuery() throws SQLException {
7371

7472
rs.close();
7573
conn.getDatabase().reset(pointer);
76-
checkParameters();
7774

7875
boolean success = false;
7976
try {
@@ -97,7 +94,6 @@ public int executeUpdate() throws SQLException {
9794

9895
rs.close();
9996
conn.getDatabase().reset(pointer);
100-
checkParameters();
10197

10298
return conn.getDatabase().executeUpdate(this, batch);
10399
}
@@ -107,12 +103,10 @@ public int executeUpdate() throws SQLException {
107103
*/
108104
public void addBatch() throws SQLException {
109105
checkOpen();
110-
checkParameters();
111106
batchPos += paramCount;
112107
batchQueryCount++;
113108
if (batch == null) {
114109
batch = new Object[paramCount];
115-
paramValid.clear();
116110
}
117111
if (batchPos + paramCount > batch.length) {
118112
Object[] nb = new Object[batch.length * 2];

src/test/java/org/sqlite/PrepStmtTest.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ public void reusingSetValues() throws SQLException {
578578
public void clearParameters() throws SQLException {
579579
stat.executeUpdate("create table tbl (colid integer primary key AUTOINCREMENT, col varchar)");
580580
stat.executeUpdate("insert into tbl(col) values (\"foo\")");
581+
stat.executeUpdate("insert into tbl(col) values (?)");
581582

582583
PreparedStatement prep = conn.prepareStatement("select colid from tbl where col = ?");
583584

@@ -591,36 +592,26 @@ public void clearParameters() throws SQLException {
591592

592593
rs.close();
593594

594-
try {
595-
prep.execute();
596-
fail("Returned result when values not bound to prepared statement");
597-
} catch (Exception e) {
598-
assertEquals("Values not bound to statement", e.getMessage());
599-
}
595+
// should not throw
596+
prep.execute();
600597

601-
try {
602-
rs = prep.executeQuery();
603-
fail("Returned result when values not bound to prepared statement");
604-
} catch (Exception e) {
605-
assertEquals("Values not bound to statement", e.getMessage());
606-
}
598+
// should not throw
599+
PreparedStatement nullPrep = conn.prepareStatement("select colid from tbl where col is null");
600+
rs = nullPrep.executeQuery();
601+
rs.next();
607602

608-
prep.close();
603+
// gets the row with the NULL column
604+
assertEquals(2, rs.getInt(1));
609605

610-
try {
611-
prep = conn.prepareStatement("insert into tbl(col) values (?)");
612-
prep.clearParameters();
613-
prep.executeUpdate();
614-
fail("Returned result when values not bound to prepared statement");
615-
} catch (Exception e) {
616-
assertEquals("Values not bound to statement", e.getMessage());
617-
}
606+
rs.close();
607+
nullPrep.close();
618608
}
619609

620-
@Test(expected = SQLException.class)
621-
public void preparedStatementShouldThrowIfNotAllParamsSet() throws SQLException {
610+
public void preparedStatementShouldNotThrowIfNotAllParamsSet() throws SQLException {
622611
PreparedStatement prep = conn.prepareStatement("select ? as col1, ? as col2, ? as col3;");
623612
ResultSetMetaData meta = prep.getMetaData();
613+
614+
// leaves 0 and 1 unbound
624615
assertEquals(meta.getColumnCount(), 3);
625616

626617
// we only set one 1 param of the expected 3 params
@@ -629,15 +620,13 @@ public void preparedStatementShouldThrowIfNotAllParamsSet() throws SQLException
629620
prep.close();
630621
}
631622

632-
@Test(expected = SQLException.class)
633-
public void preparedStatementShouldThrowIfNotAllParamsSetBatch() throws SQLException {
623+
public void preparedStatementShouldNotThrowIfNotAllParamsSetBatch() throws SQLException {
634624
stat.executeUpdate("create table test (c1, c2);");
635625
PreparedStatement prep = conn.prepareStatement("insert into test values (?,?);");
626+
627+
// leaves param 0 unbound
636628
prep.setInt(1, 1);
637629

638-
// addBatch should throw since we added a command with invalid params set
639-
// which becomes immutable once added to the batch so it makes sense to verify
640-
// at the point when you add a command instead of delaying till batch execution
641630
prep.addBatch();
642631
}
643632

0 commit comments

Comments
 (0)