Your Search Results

    User talk:sdwilsh

    Async Storage Statements

    This is loosely based off of the HTML 5 SQL spec. This is targeting Firefox 3.1 and whatever version of Gecko that will correlate with.

    Sample Code

    This is an attempt at some sample code on using async statements.

    JavaScript
    // conn is a mozIStorageConnection
    let stmt = conn.createStatement("SELECT * FROM moz_downloads WHERE state = ?1");
    stmt.bindInt32Parameter(0, Ci.nsIDownloadManager.DOWNLOAD_FINISHED);
    
    stmt.executeAsync(
      {
        handleResult: function(aResultSet)
        {
          let tuple;
          while (tuple = aResultSet.getNextTuple()) {
            let value1 = tuple.getResultByName('columnName');
            let value2 = tuple.getResultByIndex(0);
          }
        },
        handleError: function(aError)
        {
          // update UI accordingly
        },
        handleCompletion: function()
        {
          // update UI accordingly
        }
      }
    );
    
    C++
    class Listener : public Callback {
      NS_IMETHODIMP HandleResult(ResultSet aResult)
      {
        nsCOMPtr<storageITuple> tuple;
        while (NS_SUCCEEDED(aResult->GetNextTuple(tuple)) && tuple) {
          // do something with result
        }
      }
    
      NS_IMETHODIMP HandleError(SQLError aError)
      {
      }
    
      NS_IMETHODIMP HandleCompletion()
      {
      }
    };
    
    // conn is a mozIStorageConnection
    nsCOMPtr<mozIStorageStatement> stmt;
    (void)conn->CreateStatement(NS_LITERAL_CSTRING(
      "SELECT * FROM moz_downloads WHERE state = ?1"), getter_AddRefs(stmt));
    (void)stmt->BindInt32Parameter(0, nsIDownloadManager::DOWNLOAD_FINISHED);
    
    nsCOMPtr<nsICancelable> canceler; // This shouldn't be necessary to pass in.
    (void)stmt->ExecuteAsync(new Listener(), getter_AddRefs(canceler));
    

    Proposed Interface

    This is a work in progress. Comments welcome.

    mozIStorageStatement

    These are necessary changes to the mozIStorageStatement interface.


    nsICancelable executeAsync([optional] in storageIStatementCallback aCallback);

    Executes a prepared mozIStorageStatement and calls the callback (if provided) when a result is returned. This statement is then reset, and can be reused synchronously or asynchronously again immediately. Returns an object that can be used to cancel the execution of the statement immediately.

    storageIStatementCallback

    Interface handles some sort of callback on the calling thread. This does not have to be the main thread.

    void handleResult(in storageIResultSet aResultSet);

    Handles a successful execution of the statement. This function may be called more than once with a different storageIResultSet each time representing new data.

    void handleError(in storageIError aError);

    Handles an error of the statement.

    const unsigned short REASON_FINISHED = 0;
    const unsigned short REASON_CANCELED = 1;
    const unsigned short REASON_ERROR = 2;
    void handleCompletion(in unsigned short aReason);

    Called when there are no more results to be obtained. This is called only once with the reason why it is being called. REASON_FINISHED means that it finished successfully, REASON_CANCELED means that it was canceled, and REASON_ERROR means a fatal error occurred that execution could not continue from.

    storageIResultSet

    Interface used to get the data back. In JS, this should be able to be used as an Iterator.

    storageITuple getNextTuple();

    Obtains the next tuple from the result set and advances the cursor. Null if no results available.

    storageITuple

    Interface used to get data for a specific tuple. Inherits from mozIStorageValueArray.

    nsIVariant getResultByIndex(in unsigned long aIndex);

    Obtains the result for a given index in a JS friendly way.

    nsIVariant getResultByName(in AUTF8String aName);

    Obtains the result for a given named column in the tuple in a JS friendly way.

    storageIError

    Interface used to give information about an error.

    TODO: What type of information do we want to give here?

    Discussion

    Please provide feedback in mozilla.dev.planning

    Document Tags and Contributors

    Contributors to this page: Mardak, sdwilsh, Ancestor
    Last updated by: Mardak,