Example #1
0
/*
** Usage:  sqlite_bind  VM  IDX  VALUE  FLAGS
**
** Sets the value of the IDX-th occurance of "?" in the original SQL
** string.  VALUE is the new value.  If FLAGS=="null" then VALUE is
** ignored and the value is set to NULL.  If FLAGS=="static" then
** the value is set to the value of a static variable named
** "sqlite_static_bind_value".  If FLAGS=="normal" then a copy
** of the VALUE is made.
*/
static int test_bind(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  sqlite_vm *vm;
  int rc;
  int idx;
  if( argc!=5 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
       " VM IDX VALUE (null|static|normal)\"", 0);
    return TCL_ERROR;
  }
  if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR;
  if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
  if( strcmp(argv[4],"null")==0 ){
    rc = sqlite_bind(vm, idx, 0, 0, 0);
  }else if( strcmp(argv[4],"static")==0 ){
    rc = sqlite_bind(vm, idx, sqlite_static_bind_value, -1, 0);
  }else if( strcmp(argv[4],"normal")==0 ){
    rc = sqlite_bind(vm, idx, argv[3], -1, 1);
  }else{
    Tcl_AppendResult(interp, "4th argument should be "
        "\"null\" or \"static\" or \"normal\"", 0);
    return TCL_ERROR;
  }
  if( rc ){
    char zBuf[50];
    sprintf(zBuf, "(%d) ", rc);
    Tcl_AppendResult(interp, zBuf, sqlite_error_string(rc), 0);
    return TCL_ERROR;
  }
  return TCL_OK;
}
Example #2
0
static int cursor_bind(Value *vret, Value *v, RefNode *node)
{
    RefCursor *rc = Value_vp(*v);

    Value v1 = v[1];
    Value v2 = v[2];

    int idx;
    const RefNode *type = fs->Value_type(v1);
    if (type == fs->cls_int) {
        idx = fs->Value_int64(v1, NULL);
    } else if (type == fs->cls_str) {
        RefStr *rs = Value_vp(v1);
        if (!str_has0(rs->c, rs->size)) {
            idx = sqlite3_bind_parameter_index(rc->stmt, rs->c);
        } else {
            idx = 0;
        }
        if (idx == 0) {
            fs->throw_errorf(mod_sqlite, "SQLiteError", "No parameters names %r", rs);
            return FALSE;
        }
    } else {
        fs->throw_error_select(THROW_ARGMENT_TYPE2__NODE_NODE_NODE_INT, fs->cls_int, fs->cls_str, type, 1);
        return FALSE;
    }
    if (!sqlite_bind(rc->stmt, idx, v2)) {
        return FALSE;
    }

    return TRUE;
}
Example #3
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;
}