Paul wrote:
>> Paul wrote:
>>> How do you check if structure is initializad in an abstract databse?
>
> struct SqliteDatabase {
> ...
>
> /// Callback is called once database is created. Strictly one time.
>     virtual bool on_create();
> ...
> };
>
> struct FooDatabase : public SqliteDatabase
> {
>     bool on_create()
>     {
>         return EXEC_SQL("CREATE TABLE foo (id INTEGER);");
>     }
>
> };

SQLiteDatabase::open(filename) // or constructor
{
    sqlite3_open_v2(...filename...);
    EXEC_SQL("BEGIN");
    if (database_is_empty())
        on_create();
    EXEC_SQL("COMMIT");
    // error handling omitted
}

> Note: I cannot wrap *on_create* in transaction because I want to allow user 
> who
> specifies his  own *on_create* callback to be able to use transactions there.
> ... When database is missing I create lock file ...

So you are creating *another* transaction mechanism on top of the
existing one?

The important thing is that the if() and the on_create() _must_ execute
atomically.  The easiest way to do this is to use the existing
transaction mechanism.

If you want to allow transactions in on_create(), you should implement
nested transactions, i.e., your own begin()/etc. functions issue SQL
commands only at the outermost level, and let that transaction succeed
only if all nested transactions succeeded.

> So my motivation was to check if community has some standard solutions.

As it happens, this is pretty much the mechanism used by Android's
SQLiteOpenHelper class.


Regards,
Clemens
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to