diff --git a/GRDB/Bindings.swift b/GRDB/Bindings.swift index 66d801b3c..a77300f24 100644 --- a/GRDB/Bindings.swift +++ b/GRDB/Bindings.swift @@ -44,10 +44,9 @@ Dictionary literals are automatically converted to Bindings: db.execute("INSERT ... (:name, :age)", bindings: ["name": "Arthur", "age": 41]) -SQLite supports other syntaxes for named parameters. GRDB.swift only supports -the colon-prefixed ones. - -See https://www.sqlite.org/lang_expr.html#varparam for more information. +GRDB.swift only supports colon-prefixed named parameters, even though SQLite +supports other syntaxes. See https://www.sqlite.org/lang_expr.html#varparam for +more information. */ public struct Bindings { @@ -122,6 +121,10 @@ public struct Bindings { let values: [String: String?] = ["firstName": nil, "lastName": "Miller"] db.execute("INSERT ... (:firstName, :lastName)", bindings: Bindings(values)) + GRDB.swift only supports colon-prefixed named parameters, even though SQLite + supports other syntaxes. See https://www.sqlite.org/lang_expr.html#varparam + for more information. + - parameter dictionary: A dictionary of optional values that adopt the SQLiteValueConvertible protocol. - returns: A Bindings. @@ -139,6 +142,10 @@ public struct Bindings { let values: NSDictionary = ["firstName": "Arthur", "lastName": "Miller"] db.execute("INSERT ... (?,?,?)", bindings: Bindings(values)) + GRDB.swift only supports colon-prefixed named parameters, even though SQLite + supports other syntaxes. See https://www.sqlite.org/lang_expr.html#varparam + for more information. + - parameter dictionary: An NSDictionary - returns: A Bindings. */ diff --git a/GRDB/Database.swift b/GRDB/Database.swift index e5017aca2..dc59987be 100644 --- a/GRDB/Database.swift +++ b/GRDB/Database.swift @@ -172,7 +172,7 @@ public final class Database { - returns: true if the table exists. */ public func tableExists(tableName: String) -> Bool { - if let _ = fetchOneRow("SELECT [sql] FROM sqlite_master WHERE [type] = 'table' AND LOWER(name) = ?", bindings: [tableName.lowercaseString]) { + if let _ = fetchOneRow("SELECT \"sql\" FROM sqlite_master WHERE \"type\" = 'table' AND LOWER(name) = ?", bindings: [tableName.lowercaseString]) { return true } else { return false @@ -182,6 +182,7 @@ public final class Database { // MARK: - Non public + /// The SQLite connection handle let sqliteConnection = SQLiteConnection() init(path: String, configuration: Configuration) throws { diff --git a/GRDB/SelectStatement.swift b/GRDB/SelectStatement.swift index 9373368a1..027270096 100644 --- a/GRDB/SelectStatement.swift +++ b/GRDB/SelectStatement.swift @@ -24,7 +24,7 @@ import Foundation -/// A subclass of Statement that implements SELECT statements. +/// A subclass of Statement that fetches database rows. public final class SelectStatement : Statement { /// The number of columns in the results. diff --git a/GRDB/Statement.swift b/GRDB/Statement.swift index 445ef4232..49e748740 100644 --- a/GRDB/Statement.swift +++ b/GRDB/Statement.swift @@ -22,12 +22,13 @@ // THE SOFTWARE. -private let SQLITE_TRANSIENT = unsafeBitCast(COpaquePointer(bitPattern: -1), sqlite3_destructor_type.self) +/** +A statement represents the execution of a SQL query. +It is the base class of UpdateStatement that executes *update statements*, and +SelectStatement that fetches rows. +*/ public class Statement { - let database: Database - let sqliteStatement = SQLiteStatement() - let databaseQueueID: DatabaseQueueID public lazy var sql: String = String.fromCString(UnsafePointer(sqlite3_sql(self.sqliteStatement)))! public var bindings: Bindings? { didSet { @@ -39,6 +40,17 @@ public class Statement { } } + // MARK: - Not public + + /// The database + let database: Database + + /// The SQLite statement handle + let sqliteStatement = SQLiteStatement() + + /// The identity of the DatabaseQueue where the statement was created. + let databaseQueueID: DatabaseQueueID + init(database: Database, sql: String, bindings: Bindings?) throws { // See https://www.sqlite.org/c3ref/prepare.html self.database = database @@ -86,7 +98,6 @@ public class Statement { } } - // TODO: document that we only support the colon prefix (like FMDB). // Exposed for Bindings. Don't make this one public unless we keep the bindings property in sync. final func bind(value: SQLiteValueConvertible?, forKey key: String) { let index = Int(sqlite3_bind_parameter_index(sqliteStatement, ":\(key)")) @@ -117,3 +128,5 @@ public class Statement { } } } + +private let SQLITE_TRANSIENT = unsafeBitCast(COpaquePointer(bitPattern: -1), sqlite3_destructor_type.self) diff --git a/README.md b/README.md index 53c1c79be..80b518078 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,8 @@ dbQueue.inDatabase { db in } ``` -Bindings are optional arrays or dictionaries that fill the `?` and `:name` parameters in the query: +Bindings are optional arrays or dictionaries that fill the positional `?` and named parameters like `:name` in the query. GRDB.swift only supports colon-prefixed named parameters, even though SQLite supports [other syntaxes](https://www.sqlite.org/lang_expr.html#varparam). + ```swift db.fetchRows("SELECT * FROM persons WHERE name = ?", bindings: ["Arthur"])