Beispiel #1
0
static int is_all_digit(const char *p)
{
    while (*p != '\0') {
        if (!isdigit_fox(*p)) {
            return FALSE;
        }
        p++;
    }
    return TRUE;
}
Beispiel #2
0
static int conn_prepare_sub(sqlite3_stmt **stmt, sqlite3 *conn, Value *v)
{
    Value *v1 = v + 1;
    int argc = fg->stk_top - v1;
    RefStr *sql;
    int result;

    if (conn == NULL) {
        fs->throw_errorf(mod_sqlite, "SQLiteError", "Connection is not opened");
        return FALSE;
    }

    sql = Value_vp(*v1);
    result = sqlite3_prepare_v2(conn, sql->c, -1, stmt, NULL);
    if (result != SQLITE_OK){
        if (result != SQLITE_ERROR_USER) {
            fs->throw_errorf(mod_sqlite, "SQLiteError", "%s", sqlite3_errmsg(conn));
        }
        return FALSE;
    }
    if (*stmt == NULL) {
        fs->throw_errorf(mod_sqlite, "SQLiteError", "SQL string is empty");
        return FALSE;
    }

    if (argc > 1) {
        int i;
        if (argc == 2) {
            const RefNode *type = fs->Value_type(v[2]);
            if (type == fs->cls_list) {
                RefArray *ra = Value_vp(v[2]);
                for (i = 0; i < ra->size; i++) {
                    if (!sqlite_bind(*stmt, i + 1, ra->p[i])) {
                        return FALSE;
                    }
                }
                return TRUE;
            } else if (type == fs->cls_map) {
                RefMap *rm = Value_vp(v[2]);
                for (i = 0; i < rm->entry_num; i++) {
                    HashValueEntry *ep = rm->entry[i];
                    for (; ep != NULL; ep = ep->next) {
                        RefStr *rs;
                        int idx;
                        if (fs->Value_type(ep->key) != fs->cls_str) {
                            fs->throw_errorf(fs->mod_lang, "TypeError", "Map {Str:*, Str:* ...} required");
                            return FALSE;
                        }
                        rs = Value_vp(ep->key);
                        if (rs->size > 0 && !str_has0(rs->c, rs->size)) {
                            // 先頭が記号で始まらない場合は、:を補う
                            char ch = rs->c[0];
                            if (isdigit_fox(ch) || isupper_fox(ch) || islower_fox(ch)) {
                                char *ptr = malloc(rs->size + 2);
                                sprintf(ptr, ":%.*s", rs->size, rs->c);
                                idx = sqlite3_bind_parameter_index(*stmt, ptr);
                                free(ptr);
                            } else {
                                idx = sqlite3_bind_parameter_index(*stmt, rs->c);
                            }
                        } else {
                            idx = 0;
                        }
                        if (idx == 0) {
                            fs->throw_errorf(mod_sqlite, "SQLiteError", "No parameters names %r", rs);
                            return FALSE;
                        }
                        if (!sqlite_bind(*stmt, idx, ep->val)) {
                            return FALSE;
                        }
                    }
                }
                return TRUE;
            }
        }
        for (i = 1; i < argc; i++) {
            if (!sqlite_bind(*stmt, i, v1[i])) {
                return FALSE;
            }
        }
    }

    return TRUE;
}