Thursday 1 June 2017

c++ - How can I pass variables meant for a trigger function in SQLite?

/* 
// 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.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...