如何组织依赖于以前结果的WebOS SQL代码?

时间:2011-05-16 20:23:08

标签: database webos

我想做一个简单的WebOS Mojo应用程序,它将日期添加到数据库,但在此之前,它需要通过询问数据库来检查几个条件。鉴于在WebOS中访问数据库的异步模式,我想知道编写此类代码的最有效和最简短的方法是什么。

例如,我需要确保新日期不在数据库中。然后我需要得到最接近的日期,以便我可以计算天数的差异,如果差异太小则抛出错误。然后我需要插入新日期,然后计算平均值。

这很容易以同步的方式访问数据库,但我真的不喜欢在执行的不同sql语句的多个成功处理程序中编写部分代码的想法。有更优雅的解决方案吗?

1 个答案:

答案 0 :(得分:1)

您可以对HTML 5关系数据库函数使用内联回调:

function createProject(project, onSuccess) { 
if (project.projectId)
    throw new StorageError("project already exists");
if (project.path)
    throw new StorageError("project already has a path");

project.projectId = ++Tracker.maxLocalId * 4096;
project.path = calcNextProjectPath();
project.normalize();

Tracker.db.transaction(
    function (transaction) {
        transaction.executeSql(
            "INSERT OR ROLLBACK INTO item (dbId, path, hasChildren, kind, summaryText, place, work, responsible, responsibleClass) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
            [project.projectId, project.path, project.hasChildren, project.kind, project.title, project.place, project.work, project.responsible, project.responsibleClass],

            function (transaction, resultSet) {
                Mojo.Log.info("DB: inserted new project item", resultSet.insertId, project.title);

                transaction.executeSql(
                    "INSERT OR ROLLBACK INTO project (projectId, accountId, title, backendKind, backendStatus, backendLastChanged, lastSuccessfulDown) \
                            VALUES (?, ?, ?, ?, ?, ?, ?)",
                    [project.projectId, project.accountId, project.title, project.backendKind, project.backendStatus, project.backendLastChanged, project.lastSuccessfulDown],

                    function (transaction, resultSet) {
                        Warn.logInfo("created new project", "projectId=" + resultSet.insertId, project.title);
                        if (onSuccess)
                            onSuccess();
                    },

                    function (transaction, sqlError) {
                        Warn.bannerError("create", $L("Quit and reset your phone."), "project row insertion failed: ", sqlError.message, sqlError.code, project);
                        return true;   // abort whole transaction
                    }
                );
            },

            function (transaction, sqlError) {   // failure of insert project item
                if (sqlError.code === 1 && sqlError.message === "constraint failed" && Mojo.appInfo.id !== "com.outlinetracker.outlinetracker") {
                    upgradeAlert(true);
                } else {
                    Warn.bannerError("create", $L("Quit and reset your phone."), "project item insertion failed: ", sqlError.message, sqlError.code);
                }
                return true;   // abort whole transaction
            }
        );                   
     },   // end transaction function

    function (sqlError) {   // seems to only be called for exceptions in callbacks
        Warn.logError($L("Quit and reset your phone."), "transaction 'insert project' failed", sqlError.message, sqlError.code);
    }
);   // end transaction call

}