2
votes
/* 
// this is the trigger definition
CREATE TEMPORARY TRIGGER 'insertTrigger' INSTEAD OF INSERT ON 'foo' 
BEGIN 
    SELECT bar(NEW.id, NEW.timestamp); "
END; 
//*/

void insert(sqlite3 *db, char *id, char *timestamp, void *context){
    exec_query(db, "INSERT INTO foo(id,timestamp) VALUES(?,?)", id, timestamp);
}

void bar(sqlite3_context *context, int argc, sqlite3_value **argv){
    char *id = (char*)sqlite3_value_text(argv[0]);
    char *timestamp = (char*)sqlite3_value_text(argv[1]);
    //how do I get context* ???
    //this must be a thread safe function (i.e. multiple threads have their own sqlite3* and all execute queries (possibly including this one)
}

Is there some workaround to enable this? Just as an idea:

void insert(sqlite3 *db, char *id, char *timestamp, void *context){
    sqlite3_mutex_enter(sqlite3_db_mutex(db));
    sqlite3_user_setdata(db, context); //this doesnt exist
    exec_query(db, "INSERT INTO foo(id,timestamp) VALUES(?,?)", id, timestamp);
    sqlite3_mutex_leave(sqlite3_db_mutex(db));
}

void bar(sqlite3_context *context, int argc, sqlite3_value **argv){
    char *id = (char*)sqlite3_value_text(argv[0]);
    char *timestamp = (char*)sqlite3_value_text(argv[1]);
    void *context_ = sqlite3_user_data(context);
}

There are a few other methods it seems that might be able to accomplish this such as the sqlite3_get_auxdata functions, but I dont really understand how that api works.

1
There is no such thing as "C/C++". Are you using C or C++? Please remove the other tag.ThiefMaster
@ThiefMaster the question applies to both languages.chacham15
@chacham15 Then it should be two separate questions. Just having a quick first glance at the code, there are void *s in it. That in itself is a good reason to treat it a radically different way in the two languages.user529758
@H2CO3 No, I dont want to treat it any differently. The code needs to work in both c and c++.chacham15
@H2CO3 dude, dont assume that you know what I know (and that you know it all). Despite the fact that C is not a proper subset of C++, you can write code using common features of both and not go outside those bounds. This is legitimate beyond a shadow of a doubt and I dont want to argue that with you.chacham15

1 Answers

1
votes

context is not part of your query so it will not be available in the trigger function.

After all, the SQL statement you send to update your table might (should) not know about the triggers at all!