Commits

Grace Batumbya committed 1568679

Fixes #16: DDL statements return result other than 0

Comments (0)

Files changed (6)

src/main/java/org/sqlite/DB.java

     abstract int changes() throws SQLException;
 
     /**
+     * @return Number of rows that were changed, inserted or deleted by the last
+     * SQL statement
+     * @throws SQLException
+     * @see <a href="http://www.sqlite.org/c3ref/changes.html">http://www.sqlite.org/c3ref/changes.html</a>
+     */
+    abstract int total_changes() throws SQLException;
+
+    /**
      * Enables or disables the sharing of the database cache and schema data structures between
      * connections to the same database.
      * @param enable True to enable; false otherwise.

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

     return sqlite3_changes(gethandle(env, this));
 }
 
+JNIEXPORT jint JNICALL Java_org_sqlite_NativeDB_total_1changes(
+        JNIEnv *env, jobject this)
+{
+    return sqlite3_total_changes(gethandle(env, this));
+}
+
 JNIEXPORT jint JNICALL Java_org_sqlite_NativeDB_finalize(
         JNIEnv *env, jobject this, jlong stmt)
 {

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

     native synchronized int changes();
 
     /**
+     * @see org.sqlite.DB#total_changes()
+     */
+    @Override
+    native synchronized int total_changes();
+
+    /**
      * @see org.sqlite.DB#finalize(long)
      */
     @Override

src/main/java/org/sqlite/NestedDB.java

     }
 
     /**
+     * @see org.sqlite.DB#total_changes()
+     */
+    @Override
+    synchronized int total_changes() throws SQLException {
+        return call("sqlite3_total_changes", handle);
+    }
+
+    /**
      * @see org.sqlite.DB#_exec(java.lang.String)
      */
     @Override

src/main/java/org/sqlite/Stmt.java

         }
         else {
             try {
+                changes = db.total_changes();
                 //db.prepare(this);
                 //changes = db.executeUpdate(this, null);
 
                 if (statusCode != SQLITE_OK)
                     throw DB.newSQLException(statusCode, "");
 
-                changes = db.changes();
+                changes = db.total_changes() - changes;
             }
             finally {
                 close();

src/test/java/org/sqlite/StatementTest.java

     }
 
     @Test
-    public void stmtUpdate() throws SQLException {
+    public void executeUpdate() throws SQLException {
         assertEquals(stat.executeUpdate("create table s1 (c1);"), 0);
         assertEquals(stat.executeUpdate("insert into s1 values (0);"), 1);
         assertEquals(stat.executeUpdate("insert into s1 values (1);"), 1);
         assertEquals(stat.executeUpdate("update s1 set c1 = 5;"), 3);
         // count_changes_pgrama. truncate_optimization
         assertEquals(stat.executeUpdate("delete from s1;"), 3);
-        assertEquals(stat.executeUpdate("drop table s1;"), 3);
+
+        // multiple SQL statements
+        assertEquals(
+            stat.executeUpdate("insert into s1 values (11);" +
+                               "insert into s1 values (12)"),
+            2);
+        assertEquals(
+            stat.executeUpdate("update s1 set c1 = 21 where c1 = 11;" +
+                               "update s1 set c1 = 22 where c1 = 12;" +
+                               "update s1 set c1 = 23 where c1 = 13"),
+            2); // c1 = 13 does not exist
+        assertEquals(
+            stat.executeUpdate("delete from s1 where c1 = 21;" +
+                               "delete from s1 where c1 = 22;" +
+                               "delete from s1 where c1 = 23"),
+            2); // c1 = 23 does not exist
+
+        assertEquals(stat.executeUpdate("drop table s1;"), 0);
     }
 
     @Test