1

I'm using Stephen Celis iOS lib for handling SQLite3 databases, here is the github link.

Taking the example on the git :

try db.transaction {
    let rowid = try db.run(users.insert(email <- "betty@icloud.com"))
    try db.run(users.insert(email <- "cathy@icloud.com", managerId <- rowid))
}
// BEGIN DEFERRED TRANSACTION
// INSERT INTO "users" ("email") VALUES ('betty@icloud.com')
// INSERT INTO "users" ("email", "manager_id") VALUES ('cathy@icloud.com', 2)
// COMMIT TRANSACTION

I tried to implement the commitHook block but it is fired for each insert. I'd like to fire an action only when all the requests are sent :-D

What should I do ?

Cheers

Edit : Here is how I implemented the commit hook.

for bay in list{
    try! self.themanager.db.transaction {
        try! self.themanager.db.run(self.themanager.bays.insert(
            //insert values    
        ))

        self.themanager.db.commitHook({
            print("end commit hook")
        })
    }
}

Maybe it's related to my main loop :/

Super Guillaume
  • 87
  • 1
  • 11
  • Please show how you did implement the commitHook block: there surely lies the problem. – Gwendal Roué May 13 '16 at 07:06
  • Hi Gwendal, please see my edit :) – Super Guillaume May 18 '16 at 09:32
  • The closure given to the [Connection.commitHook function](https://github.com/stephencelis/SQLite.swift/blob/0.10.1/SQLite/Core/Connection.swift#L428-L433) is invoked whenever a transaction is committed. This concerns *all* transactions committed after the hook installation. Including *implicit* transactions that wrap statements executed outside of an explicit BEGIN... END. Besides, I don't quite know how SQLite behaves when the hook is installed *inside* a transaction, as in the code above -- it's looks like a misuse. Do these elements shed some light on your trouble? – Gwendal Roué May 19 '16 at 03:27

1 Answers1

1

From SQLite TRIGGER docs:

At this time SQLite supports only FOR EACH ROW triggers, not FOR EACH STATEMENT triggers. Hence explicitly specifying FOR EACH ROW is optional. FOR EACH ROW implies that the SQL statements specified in the trigger may be executed (depending on the WHEN clause) for each database row being inserted, updated or deleted by the statement causing the trigger to fire.

Commit hooks work like triggers. Unfortunately, the "FOR EACH STATEMENT" behavior is not supported yet.

Community
  • 1
  • 1
iamreptar
  • 1,461
  • 16
  • 29