确定一个SQL select是否异步返回一个空集? [英] Determing if a SQL select returns an empty set asynchronously?

查看:141
本文介绍了确定一个SQL select是否异步返回一个空集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很容易吗?看来只有当结果不是空集时,handleResult方法才会被执行。

我想过的一个想法是将handleResult和handleCompletion作为对象的成员函数,有handleResult更新handleCompletion可以检查的成员变量。如果变量被设置,不为空,如果变量未设置,则为空,并可以相应地执行。

似乎过于复杂,希望有一个更好的解决方案?

解决方案

< (编辑2:每个评论我下面)

  function sql( ){
this.results = false;
var me = this;

this.handleResult = function(aResultSet){

for(var row = aResultSet.getNextRow(); row; row = aResultSet.getNextRow()){
me.results = true;

var value = row.getResultByName(name);
}
};

this.handleError = function(aError){
.... //处理错误
};

this.handleCompletion = function(aReason){
if(me.results){
.... // results
} else {
.... //无结果
}

if(aReason!= Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED){
.... //处理这些
};
};

s = new sql();
$ b $ statement.executeAsync({
handleResult:s.handleResult,
handleError:s.handleError,
handleCompletion:s.handleCompletion
});

是否被认为是解决这个问题的好方法?

edit1:这不符合我所期望的方式(它的工作原理,但不是100%确定为什么)。即,如果handleResult永远不会运行,则this.results变量是未定义的(而不是false)。所以它看起来好像handleResult和handleCompletion正在操作一个不同的变量集合比我所期望的。

任何帮助了解我做错了将不胜感激。


Is this possible easily? It seems the handleResult method is only executed if the result isn't the empty set.

A thought I had was to have handleResult and handleCompletion be member functions of an object and have handleResult update a member variable that handleCompletion can check. If the variable is set, not empty, if variable unset, empty and can act accordingly.

seems to be overly complicated and hoping there's a better solution?

解决方案

to sketch out a solution (the thought i had above) (edit2: per comment I made below)

function sql() {
    this.results = false;
    var me = this;

    this.handleResult = function(aResultSet) {

        for (var row = aResultSet.getNextRow(); row;  row = aResultSet.getNextRow()) {
            me.results = true;

            var value = row.getResultByName("name");
        }
    };

    this.handleError = function(aError) {
        .... //deal with error
    };

    this.handleCompletion = function(aReason) {
        if (me.results) {
            ....//results
        } else {
            ....//no results
        }

        if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED) {
            ....//handle these
    };
};

s = new sql(); 

statement.executeAsync({
    handleResult: s.handleResult,
    handleError: s.handleError,
    handleCompletion: s.handleCompletion
});

is this considered a good way to solve this problem?

edit1: this doesn't behave in the manner I'd expect (it works, but not 100% sure why). i.e. the this.results variable is undefined (not false), if handleResult never runs. So it appers as if handleResult and handleCompletion are operating on a different set of variables than I'd expect.

any help to understand what I'm doing wrong would be appreciated.

这篇关于确定一个SQL select是否异步返回一个空集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆