mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Statement: don’t store sql string, and use sqlite3_sql instead. Plus some cleanup.
This commit is contained in:
1 parent
8e864b9df4
commit
6c826b6ecf
2 files changed
+31
-26
No files matched your search
@@ -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
|
||||
|
||||
+13
-24
@@ -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<Element>: 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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user