Commits

Grace Batumbya  committed b51cca9 Merge

Merged gbatumbya/xerial-sqlite-jdbc into default

  • Participants
  • Parent commits e7117bc, 9185323

Comments (0)

Files changed (8)

 	$(MVN) package
 
 clean-native:
-	rm -rf $(SQLITE_OUT)
+	rm -rf $(TARGET)/$(sqlite)-$(OS_NAME)*
 
 clean-java:
 	rm -rf $(TARGET)/*classes

File src/main/java/org/sqlite/NativeDB.c

 JNIEXPORT jint JNICALL Java_org_sqlite_NativeDB_clear_1bindings(
         JNIEnv *env, jobject this, jlong stmt)
 {
-    int i;
-    int count = sqlite3_bind_parameter_count(toref(stmt));
-    jint rc = SQLITE_OK;
-    for(i=1; rc==SQLITE_OK && i <= count; i++) {
-        rc = sqlite3_bind_null(toref(stmt), i);
-    }
-    return rc;
+    return sqlite3_clear_bindings(toref(stmt));
 }
 
 JNIEXPORT jint JNICALL Java_org_sqlite_NativeDB_bind_1parameter_1count(

File src/main/java/org/sqlite/PrepStmt.java

         rs.colsMeta = db.column_names(pointer);
         columnCount = db.column_count(pointer);
         paramCount = db.bind_parameter_count(pointer);
-        batch = new Object[paramCount];
+        batch = null;
         batchPos = 0;
     }
 
      */
     public void clearParameters() throws SQLException {
         checkOpen();
-        db.reset(pointer);
-        clearBatch();
+        db.clear_bindings(pointer);
+        batch = null;
     }
 
     /**
     }
 
     /**
+     * Checks if values are bound to statement parameters.
+     * @throws SQLException
+     */
+    private void checkParameters() throws SQLException {
+        if (batch == null && paramCount > 0)
+            throw new SQLException("Values not bound to statement");
+    }
+
+    /**
      * @see java.sql.PreparedStatement#execute()
      */
     public boolean execute() throws SQLException {
         checkOpen();
         rs.close();
         db.reset(pointer);
+        checkParameters();
+
         resultsWaiting = db.execute(this, batch);
         return columnCount != 0;
     }
      */
     public ResultSet executeQuery() throws SQLException {
         checkOpen();
+
         if (columnCount == 0) {
-            throw new SQLException("query does not return results");
+            throw new SQLException("Query does not return results");
         }
+
         rs.close();
         db.reset(pointer);
+        checkParameters();
+
         resultsWaiting = db.execute(this, batch);
         return getResultSet();
     }
      */
     public int executeUpdate() throws SQLException {
         checkOpen();
+
         if (columnCount != 0) {
-            throw new SQLException("query returns results");
+            throw new SQLException("Query returns results");
         }
+
         rs.close();
         db.reset(pointer);
+        checkParameters();
+
         return db.executeUpdate(this, batch);
     }
 
         if (batchPos == 0) {
             return new int[] {};
         }
+
+        checkParameters();
+
         try {
             return db.executeBatch(pointer, batchPos / paramCount, batch);
         }
     @Override
     public int getUpdateCount() throws SQLException {
         checkOpen();
+
         if (pointer == 0 || resultsWaiting) {
             return -1;
         }
+
         return db.changes();
     }
 

File src/main/resources/org/sqlite/native/Linux/amd64/libsqlitejdbc.so

Binary file removed.

File src/main/resources/org/sqlite/native/Linux/i386/libsqlitejdbc.so

Binary file removed.

File src/main/resources/org/sqlite/native/Windows/amd64/sqlitejdbc.dll

Binary file modified.

File src/main/resources/org/sqlite/native/Windows/x86/sqlitejdbc.dll

Binary file modified.

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

         rs.close();
     }
 
-    @Ignore
     @Test
     public void getObject() throws SQLException {
         stat.executeUpdate("create table testobj (" + "c1 integer, c2 float, c3, c4 varchar, c5 bit, c6, c7);");
         assertEquals(meta.getColumnType(2), Types.INTEGER);
         assertEquals(meta.getColumnType(3), Types.INTEGER);*/
 
+        prep.setInt(1, 2);prep.setInt(2, 3);prep.setInt(1, -1);
         meta = prep.executeQuery().getMetaData();
         assertEquals(meta.getColumnCount(), 3);
         prep.close();
         prep.close();
     }
 
+    @Test
+    public void clearParameters() throws SQLException {
+        stat.executeUpdate("create table tbl (colid integer primary key AUTOINCREMENT, col varchar)");
+        stat.executeUpdate("insert into tbl(col) values (\"foo\")");
+
+        PreparedStatement prep = conn.prepareStatement("select colid from tbl where col = ?");
+
+        prep.setString(1, "foo");
+
+        ResultSet rs = prep.executeQuery();
+        prep.clearParameters();
+        rs.next();
+
+        assertEquals(1, rs.getInt(1));
+
+        rs.close();
+
+        try {
+            prep.execute();
+            fail("Returned result when values not bound to prepared statement");
+        } catch (Exception e) {
+            assertEquals("Values not bound to statement", e.getMessage());
+        }
+
+        try {
+            rs = prep.executeQuery();
+            fail("Returned result when values not bound to prepared statement");
+        } catch (Exception e) {
+            assertEquals("Values not bound to statement", e.getMessage());
+        }
+
+        prep.close();
+
+        try {
+            prep = conn.prepareStatement("insert into tbl(col) values (?)");
+            prep.clearParameters();
+            prep.executeUpdate();
+            fail("Returned result when values not bound to prepared statement");
+        } catch (Exception e) {
+            assertEquals("Values not bound to statement", e.getMessage());
+        }
+    }
+
     @Test(expected = SQLException.class)
     public void noSuchTable() throws SQLException {
         PreparedStatement prep = conn.prepareStatement("select * from doesnotexist;");