dafnahaktana
dafnahaktana

Reputation: 857

zero results behaviour in SELECT using executeAsync of mozIStorageStatement object,

I run a SQL SELECT statement using executeAsync function of mozIStorageStatement object, the thing is, when the statement get no results, the handleresult function of the callback doesn't run. Is this a normal behaviour or do I have a bug? If it is a normal behaviour, then how do I write a code that will run in the case we have zero results?

Upvotes: 1

Views: 184

Answers (1)

Wayne
Wayne

Reputation: 60414

This is normal behavior. The handleResult method is called only when results are available, which is never (when a query returns the empty set). You can handle this case in the handleCompletion method, which always executes, whether or not the query returns any rows.

Here's a throwaway example to demonstrate:

Components.utils.import("resource://gre/modules/Services.jsm");  
Components.utils.import("resource://gre/modules/FileUtils.jsm");  

var DBTest = {
    // chrome window loaded
    onload: function(e) {
        var appContent = document.getElementById("appcontent");
        appContent.addEventListener("DOMContentLoaded", function(e) {
            try {
                var file = FileUtils.getFile("ProfD", ["cookies.sqlite"]);  
                var dbConn = Services.storage.openDatabase(file);
                var stmt = dbConn.createStatement(
                        "SELECT * FROM moz_cookies WHERE name='trash'");
                stmt.executeAsync({  
                    handleResult: function(aResultSet) {  
                        alert("handleResult");
                    },  
                    handleError: function(aError) {  
                        alert("handleError: " + aError.message);  
                    },      
                    handleCompletion: function(aReason) {  
                        alert("handleCompletion: " + aReason);
                    }  
                });  
            } catch(e) {
                alert(e);
            }
        }, true);
    }
}

window.addEventListener("load", DBTest.onload, false);

The aReason parameter can have the following values:

  • REASON_FINISHED = 0 The statement has finished executing normally.
  • REASON_CANCELED = 1 The statement stopped executing because it was canceled.
  • REASON_ERROR = 2 The statement stopped executing because an error occurred.

Further reading:

Upvotes: 1

Related Questions