I'd like to submit this patch which finishes my earlier additions to 
DatabaseEventListener. Now, while running a query, 
DatabaseEventListener.setProgress is called periodically to indicate how 
many rows have been handled so far. There's a new state called 
STATE_STATEMENT_PROGRESS. 

I've been testing this thoroughly with some large databases (e.g. 5 
Gigabytes), and with a GUI that uses the DatabaseEventListener to show 
progress. It now allows me to accurately inform the user of the progress 
being made in long-running queries.

I wrote the code, it's mine, and I'm contributing it to H2 for distribution 
multiple-licensed under the H2 License, version 1.0, and under the Eclipse 
Public License, version 1.0 (http://h2database.com/html/license.html).

I also tried adding a similar notification to DDL commands such as DROP 
TABLE, but alas, I failed to find a way that didn't involve making 
significant changes to H2 code.

Regards,

Steve 
---------------------------------------------------
Steve McLeod
Founder, Poker Copilot
http://www.pokercopilot.com

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/h2-database/-/8RP4lf3mgvQJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

Index: src/test/org/h2/test/jdbc/TestDatabaseEventListener.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>MacRoman
===================================================================
--- src/test/org/h2/test/jdbc/TestDatabaseEventListener.java	(revision 4211)
+++ src/test/org/h2/test/jdbc/TestDatabaseEventListener.java	(revision )
@@ -23,7 +23,7 @@
 public class TestDatabaseEventListener extends TestBase implements DatabaseEventListener {
 
     private static boolean calledOpened, calledClosingDatabase, calledScan, calledCreateIndex;
-    private static boolean calledStatementStart, calledStatementEnd;
+    private static boolean calledStatementStart, calledStatementEnd, calledStatementProgress;
 
     /**
      * Run just this test.
@@ -218,6 +218,7 @@
         p.setProperty("password", "sa");
         calledStatementStart = false;
         calledStatementEnd = false;
+        calledStatementProgress = false;
         p.put("DATABASE_EVENT_LISTENER", getClass().getName());
         org.h2.Driver.load();
         String url = "jdbc:h2:mem:databaseEventListener";
@@ -228,6 +229,7 @@
         conn.close();
         assertTrue(calledStatementStart);
         assertTrue(calledStatementEnd);
+        assertTrue(calledStatementProgress);
     }
 
     public void closingDatabase() {
@@ -263,6 +265,11 @@
         if (state == STATE_STATEMENT_END) {
             if (name.equals("select * from test")) {
                 calledStatementEnd = true;
+            }
+        }
+        if (state == STATE_STATEMENT_PROGRESS) {
+            if (name.equals("select * from test")) {
+                calledStatementProgress = true;
             }
         }
     }
Index: src/docsrc/html/changelog.html
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/docsrc/html/changelog.html	(revision 4211)
+++ src/docsrc/html/changelog.html	(revision )
@@ -29,6 +29,7 @@
 </li><li>Then reading from a resource using the prefix "classpath:", the ContextClassLoader
     is now used if the resource can't be read otherwise.
 </li><li>DatabaseEventListener now calls setProgress whenever a statement starts and ends.
+</li><li>DatabaseEventListener now calls setProgress periodically while a statement is running.
 </li><li>The table INFORMATION_SCHEMA.FUNCTION_ALIASES now includes a column TYPE_NAME.
 </li><li>Issue 378: when using views, the wrong values were bound to a parameter in some cases.
 </li><li>Terrence Huang has translated the error messages to Chinese. Thanks a lot!
Index: src/main/org/h2/command/Prepared.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>MacRoman
===================================================================
--- src/main/org/h2/command/Prepared.java	(revision 4211)
+++ src/main/org/h2/command/Prepared.java	(revision )
@@ -7,6 +7,7 @@
 package org.h2.command;
 
 import java.util.ArrayList;
+import org.h2.api.DatabaseEventListener;
 import org.h2.constant.ErrorCode;
 import org.h2.engine.Database;
 import org.h2.engine.Session;
@@ -331,6 +332,7 @@
             checkCanceled();
         }
         this.currentRowNumber = rowNumber;
+        setProgress();
     }
 
     /**
@@ -340,6 +342,15 @@
      */
     public int getCurrentRowNumber() {
         return currentRowNumber;
+    }
+
+    /**
+     * Notifies query progress via the DatabaseEventListener
+     */
+    private void setProgress() {
+        if ((currentRowNumber & 127) == 0) {
+            session.getDatabase().setProgress(DatabaseEventListener.STATE_STATEMENT_PROGRESS, sqlStatement, currentRowNumber, 0);
+        }
     }
 
     /**
Index: src/main/org/h2/api/DatabaseEventListener.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>MacRoman
===================================================================
--- src/main/org/h2/api/DatabaseEventListener.java	(revision 4211)
+++ src/main/org/h2/api/DatabaseEventListener.java	(revision )
@@ -55,6 +55,11 @@
     int STATE_STATEMENT_END = 6;
 
     /**
+     * This state is used for periodic notification during long-running queries
+     */
+    int STATE_STATEMENT_PROGRESS = 7;
+
+    /**
      * This method is called just after creating the object.
      * This is done when opening the database if the listener is specified
      * in the database URL, but may be later if the listener is set at
Index: src/test/org/h2/test/db/TestListener.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>MacRoman
===================================================================
--- src/test/org/h2/test/db/TestListener.java	(revision 4211)
+++ src/test/org/h2/test/db/TestListener.java	(revision )
@@ -69,7 +69,7 @@
         if (state == lastState && time < last + 1000) {
             return;
         }
-        if (state == STATE_STATEMENT_START || state == STATE_STATEMENT_END) {
+        if (state == STATE_STATEMENT_START || state == STATE_STATEMENT_END || state == STATE_STATEMENT_PROGRESS) {
             return;
         }
         if (name.length() > 30) {

Reply via email to