From 6c826b6ecf17a60c528a604ff7bfc71f01a8d185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 7 May 2016 11:57:35 +0200 Subject: [PATCH] =?UTF-8?q?Statement:=20don=E2=80=99t=20store=20sql=20stri?= =?UTF-8?q?ng,=20and=20use=20sqlite3=5Fsql=20instead.=20Plus=20some=20clea?= =?UTF-8?q?nup.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GRDB/Core/Database.swift | 20 ++++++++++++++++++-- GRDB/Core/Statement.swift | 37 +++++++++++++------------------------ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 7525a940d..7199274c3 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -145,6 +145,18 @@ public final class Database { guard code == SQLITE_OK else { fatalError(DatabaseError(code: code, message: lastErrorMessage).description) } + + // TODO: callback for actual database closing. + // + // https://www.sqlite.org/capi3ref.html#sqlite3_close: + // > If sqlite3_close_v2() is called on a database connection that still + // > has outstanding prepared statements, BLOB handles, and/or + // > sqlite3_backup objects then it returns SQLITE_OK and the + // > deallocation of resources is deferred until all prepared + // > statements, BLOB handles, and sqlite3_backup objects are also + // > destroyed. + // + // See sqlite3_next_stmt https://www.sqlite.org/capi3ref.html#sqlite3_next_stmt configuration.SQLiteConnectionDidClose?() } @@ -464,7 +476,7 @@ extension Database { } do { - let statement = UpdateStatement(database: self, sql: sql, sqliteStatement: sqliteStatement, invalidatesDatabaseSchemaCache: observer.invalidatesDatabaseSchemaCache) + let statement = UpdateStatement(database: self, sqliteStatement: sqliteStatement, invalidatesDatabaseSchemaCache: observer.invalidatesDatabaseSchemaCache) try statement.execute(arguments: consumeArguments(statement)) } catch let statementError { error = statementError @@ -1101,7 +1113,11 @@ extension Database { } } - func updateStatementDidExecute() { + func updateStatementDidExecute(statement: UpdateStatement) { + if statement.invalidatesDatabaseSchemaCache { + clearSchemaCache() + } + // Reset transactionState before didCommit or didRollback eventually // execute other statements. let transactionState = self.transactionState diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 73c883cfb..3de1bf266 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -25,14 +25,15 @@ public class Statement { public let sqliteStatement: SQLiteStatement /// The SQL query - public let sql: String + public var sql: String { + return String.fromCString(sqlite3_sql(sqliteStatement))! + } /// The database unowned let database: Database - init(database: Database, sql: String, sqliteStatement: SQLiteStatement) { + init(database: Database, sqliteStatement: SQLiteStatement) { self.database = database - self.sql = sql self.sqliteStatement = sqliteStatement } @@ -55,7 +56,6 @@ public class Statement { } self.database = database - self.sql = sql self.sqliteStatement = sqliteStatement guard code == SQLITE_OK else { @@ -418,11 +418,11 @@ public class DatabaseGenerator: GeneratorType { public final class UpdateStatement : Statement { /// If true, the database schema cache gets invalidated after this statement /// is executed. - var invalidatesDatabaseSchemaCache: Bool + private(set) var invalidatesDatabaseSchemaCache: Bool - init(database: Database, sql: String, sqliteStatement: SQLiteStatement, invalidatesDatabaseSchemaCache: Bool) { + init(database: Database, sqliteStatement: SQLiteStatement, invalidatesDatabaseSchemaCache: Bool) { self.invalidatesDatabaseSchemaCache = invalidatesDatabaseSchemaCache - super.init(database: database, sql: sql, sqliteStatement: sqliteStatement) + super.init(database: database, sqliteStatement: sqliteStatement) } init(database: Database, sql: String) throws { @@ -445,12 +445,9 @@ public final class UpdateStatement : Statement { try! reset() switch sqlite3_step(sqliteStatement) { - case SQLITE_DONE: - // Success - break - - case SQLITE_ROW: - // A row? The UpdateStatement is not supposed to return any... + case SQLITE_DONE, SQLITE_ROW: + // When SQLITE_ROW, the statement did return a row. That's + // unexpected from an update statement. // // What are our options? // @@ -469,8 +466,9 @@ public final class UpdateStatement : Statement { // // The problem with 3 is that there is no way to avoid this warning. // - // So let's just silently ignore the row, and return successfully. - break + // So let's just silently ignore the row, and behave just like + // SQLITE_DONE: this is a success. + database.updateStatementDidExecute(self) case let errorCode: // Failure @@ -480,15 +478,6 @@ public final class UpdateStatement : Statement { throw DatabaseError(code: errorCode, message: database.lastErrorMessage, sql: sql, arguments: self.arguments) // Error uses self.arguments, not the optional arguments parameter. } - - // Now that statement has been executed, clear database schema cache if needed - if invalidatesDatabaseSchemaCache { - database.clearSchemaCache() - } - - // Now that changes information has been loaded, let transaction - // observers do whatever they want: - database.updateStatementDidExecute() } }