mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Documentation WIP
This commit is contained in:
1 parent
7269326b5b
commit
607fdf9327
5 files changed
+34
-12
No files matched your search
+11
-4
@@ -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.
|
||||
*/
|
||||
|
||||
+2
-1
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
+18
-5
@@ -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<Int8>(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)
|
||||
@@ -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"])
|
||||
|
||||
Reference in new issue
Block a user