mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Documentation formatting
This commit is contained in:
1 parent
889b15a967
commit
fe0bd6a0c0
14 files changed
+398
-176
No files matched your search
+26
-21
@@ -246,8 +246,9 @@ extension Database {
|
||||
///
|
||||
/// This method may throw a DatabaseError.
|
||||
///
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A DatabaseChanges.
|
||||
/// - throws: A DatabaseError whenever a SQLite error occurs.
|
||||
public func execute(sql: String, arguments: StatementArguments? = nil) throws -> DatabaseChanges {
|
||||
@@ -441,17 +442,19 @@ public class DatabaseFunction : Hashable {
|
||||
/// db.addFunction(fn)
|
||||
/// Int.fetchOne(db, "SELECT succ(1)")! // 2
|
||||
///
|
||||
/// - parameter name: The function name.
|
||||
/// - parameter argumentCount: The number of arguments of the function. If
|
||||
/// omitted, or nil, the function accepts any number of arguments.
|
||||
/// - parameter pure: Whether the function is "pure", which means that its
|
||||
/// results only depends on its inputs. When a function is pure, SQLite
|
||||
/// has the opportunity to perform additional optimizations. Default value
|
||||
/// is false.
|
||||
/// - parameter function: A function that takes an array of DatabaseValue
|
||||
/// arguments, and returns an optional DatabaseValueConvertible such as
|
||||
/// Int, String, NSDate, etc. The array is guaranteed to have exactly
|
||||
/// *argumentCount* elements, provided *argumentCount* is not nil.
|
||||
/// - parameters:
|
||||
/// - name: The function name.
|
||||
/// - argumentCount: The number of arguments of the function. If
|
||||
/// omitted, or nil, the function accepts any number of arguments.
|
||||
/// - pure: Whether the function is "pure", which means that its results
|
||||
/// only depends on its inputs. When a function is pure, SQLite has
|
||||
/// the opportunity to perform additional optimizations. Default value
|
||||
/// is false.
|
||||
/// - function: A function that takes an array of DatabaseValue
|
||||
/// arguments, and returns an optional DatabaseValueConvertible such
|
||||
/// as Int, String, NSDate, etc. The array is guaranteed to have
|
||||
/// exactly *argumentCount* elements, provided *argumentCount* is
|
||||
/// not nil.
|
||||
public init(_ name: String, argumentCount: Int32? = nil, pure: Bool = false, function: [DatabaseValue] throws -> DatabaseValueConvertible?) {
|
||||
self.name = name
|
||||
self.argumentCount = argumentCount ?? -1
|
||||
@@ -533,8 +536,9 @@ public class DatabaseCollation : Hashable {
|
||||
/// db.addCollation(collation)
|
||||
/// try db.execute("CREATE TABLE files (name TEXT COLLATE LOCALIZED_STANDARD")
|
||||
///
|
||||
/// - parameter name: The function name.
|
||||
/// - parameter function: A function that compares two strings.
|
||||
/// - parameters:
|
||||
/// - name: The function name.
|
||||
/// - function: A function that compares two strings.
|
||||
public init(_ name: String, function: (String, String) -> NSComparisonResult) {
|
||||
self.name = name
|
||||
self.function = function
|
||||
@@ -744,12 +748,13 @@ extension Database {
|
||||
///
|
||||
/// This method is not reentrant: you can't nest transactions.
|
||||
///
|
||||
/// - parameter kind: The transaction type (default nil). If nil, the
|
||||
/// transaction type is configuration.defaultTransactionKind, which itself
|
||||
/// defaults to .Immediate. See https://www.sqlite.org/lang_transaction.html
|
||||
/// for more information.
|
||||
/// - parameter block: A block that executes SQL statements and return
|
||||
/// either .Commit or .Rollback.
|
||||
/// - parameters:
|
||||
/// - kind: The transaction type (default nil). If nil, the transaction
|
||||
/// type is configuration.defaultTransactionKind, which itself
|
||||
/// defaults to .Immediate. See https://www.sqlite.org/lang_transaction.html
|
||||
/// for more information.
|
||||
/// - block: A block that executes SQL statements and return either
|
||||
/// .Commit or .Rollback.
|
||||
/// - throws: The error thrown by the block.
|
||||
public func inTransaction(kind: TransactionKind? = nil, block: () throws -> TransactionCompletion) throws {
|
||||
preconditionValidQueue()
|
||||
|
||||
@@ -19,8 +19,9 @@ public final class DatabaseQueue {
|
||||
///
|
||||
/// Database connections get closed when the database queue gets deallocated.
|
||||
///
|
||||
/// - parameter path: The path to the database file.
|
||||
/// - parameter configuration: A configuration
|
||||
/// - parameters:
|
||||
/// - path: The path to the database file.
|
||||
/// - configuration: A configuration.
|
||||
/// - throws: A DatabaseError whenever a SQLite error occurs.
|
||||
public convenience init(path: String, var configuration: Configuration = Configuration()) throws {
|
||||
// IMPLEMENTATION NOTE
|
||||
@@ -48,7 +49,7 @@ public final class DatabaseQueue {
|
||||
///
|
||||
/// Database memory is released when the database queue gets deallocated.
|
||||
///
|
||||
/// - parameter configuration: A configuration
|
||||
/// - parameter configuration: A configuration.
|
||||
public convenience init(var configuration: Configuration = Configuration()) {
|
||||
configuration.threadingMode = .MultiThread // See IMPLEMENTATION NOTE in init(_:configuration:)
|
||||
self.init(database: Database(configuration: configuration))
|
||||
@@ -103,12 +104,13 @@ public final class DatabaseQueue {
|
||||
///
|
||||
/// This method is *not* reentrant.
|
||||
///
|
||||
/// - parameter kind: The transaction type (default nil). If nil, the
|
||||
/// transaction type is configuration.defaultTransactionKind, which itself
|
||||
/// defaults to .Immediate. See https://www.sqlite.org/lang_transaction.html
|
||||
/// for more information.
|
||||
/// - parameter block: A block that executes SQL statements and return
|
||||
/// either .Commit or .Rollback.
|
||||
/// - parameters:
|
||||
/// - kind: The transaction type (default nil). If nil, the transaction
|
||||
/// type is configuration.defaultTransactionKind, which itself
|
||||
/// defaults to .Immediate. See https://www.sqlite.org/lang_transaction.html
|
||||
/// for more information.
|
||||
/// - block: A block that executes SQL statements and return either
|
||||
/// .Commit or .Rollback.
|
||||
/// - throws: The error thrown by the block.
|
||||
public func inTransaction(kind: TransactionKind? = nil, block: (db: Database) throws -> TransactionCompletion) throws {
|
||||
try inQueue {
|
||||
|
||||
@@ -58,8 +58,9 @@ public extension DatabaseValueConvertible {
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence.
|
||||
@warn_unused_result
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
@@ -74,8 +75,9 @@ public extension DatabaseValueConvertible {
|
||||
/// let statement = db.selectStatement("SELECT name FROM ...")
|
||||
/// let names = String.fetchAll(statement) // [String]
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An array.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Self] {
|
||||
@@ -90,8 +92,9 @@ public extension DatabaseValueConvertible {
|
||||
/// let statement = db.selectStatement("SELECT name FROM ...")
|
||||
/// let name = String.fetchOne(statement) // String?
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
@@ -123,9 +126,10 @@ public extension DatabaseValueConvertible {
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence.
|
||||
@warn_unused_result
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
@@ -136,9 +140,10 @@ public extension DatabaseValueConvertible {
|
||||
///
|
||||
/// let names = String.fetchAll(db, "SELECT name FROM ...") // [String]
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An array.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Self] {
|
||||
@@ -152,9 +157,10 @@ public extension DatabaseValueConvertible {
|
||||
///
|
||||
/// let name = String.fetchOne(db, "SELECT name FROM ...") // String?
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Self? {
|
||||
@@ -196,8 +202,9 @@ public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of optional values.
|
||||
@warn_unused_result
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Wrapped?> {
|
||||
|
||||
@@ -77,8 +77,9 @@ public protocol MutablePersistable : TableMapping {
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// - parameter rowID: The inserted rowID.
|
||||
/// - parameter column: The name of the eventual INTEGER PRIMARY KEY column.
|
||||
/// - parameters:
|
||||
/// - rowID: The inserted rowID.
|
||||
/// - column: The name of the eventual INTEGER PRIMARY KEY column.
|
||||
mutating func didInsertWithRowID(rowID: Int64, forColumn column: String?)
|
||||
|
||||
|
||||
@@ -322,8 +323,9 @@ public protocol Persistable : MutablePersistable {
|
||||
/// If you need a mutating variant of this method, adopt the
|
||||
/// MutablePersistable protocol instead.
|
||||
///
|
||||
/// - parameter rowID: The inserted rowID.
|
||||
/// - parameter column: The name of the eventual INTEGER PRIMARY KEY column.
|
||||
/// - parameters:
|
||||
/// - rowID: The inserted rowID.
|
||||
/// - column: The name of the eventual INTEGER PRIMARY KEY column.
|
||||
func didInsertWithRowID(rowID: Int64, forColumn column: String?)
|
||||
|
||||
/// Executes an INSERT statement.
|
||||
|
||||
@@ -58,8 +58,9 @@ extension RowConvertible {
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence.
|
||||
@warn_unused_result
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
@@ -77,8 +78,9 @@ extension RowConvertible {
|
||||
/// let statement = db.selectStatement("SELECT * FROM persons")
|
||||
/// let persons = Person.fetchAll(statement) // [Person]
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An array.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Self] {
|
||||
@@ -90,8 +92,9 @@ extension RowConvertible {
|
||||
/// let statement = db.selectStatement("SELECT * FROM persons")
|
||||
/// let person = Person.fetchOne(statement) // Person?
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
@@ -117,9 +120,10 @@ extension RowConvertible {
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence.
|
||||
@warn_unused_result
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
@@ -130,9 +134,10 @@ extension RowConvertible {
|
||||
///
|
||||
/// let persons = Person.fetchAll(db, "SELECT * FROM persons") // [Person]
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An array.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Self] {
|
||||
@@ -143,9 +148,10 @@ extension RowConvertible {
|
||||
///
|
||||
/// let person = Person.fetchOne(db, "SELECT * FROM persons") // Person?
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Self? {
|
||||
|
||||
@@ -17,8 +17,9 @@ public protocol SQLiteStatementConvertible {
|
||||
///
|
||||
/// See https://www.sqlite.org/c3ref/column_blob.html for more information.
|
||||
///
|
||||
/// - parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - parameter index: The column index.
|
||||
/// - parameters:
|
||||
/// - sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - index: The column index.
|
||||
init(sqliteStatement: SQLiteStatement, index: Int32)
|
||||
}
|
||||
|
||||
@@ -50,8 +51,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of values.
|
||||
@warn_unused_result
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
@@ -69,8 +71,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
/// let statement = db.selectStatement("SELECT name FROM ...")
|
||||
/// let names = String.fetchAll(statement) // [String]
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An array of values.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Self] {
|
||||
@@ -82,8 +85,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
/// let statement = db.selectStatement("SELECT name FROM ...")
|
||||
/// let name = String.fetchOne(statement) // String?
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
@@ -116,9 +120,10 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of values.
|
||||
@warn_unused_result
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
@@ -129,9 +134,10 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
///
|
||||
/// let names = String.fetchAll(db, "SELECT name FROM ...") // [String]
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An array of values.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Self] {
|
||||
@@ -142,9 +148,10 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
///
|
||||
/// let name = String.fetchOne(db, "SELECT name FROM ...") // String?
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Self? {
|
||||
|
||||
@@ -23,8 +23,9 @@ extension RowConvertible where Self: TableMapping {
|
||||
///
|
||||
/// The order of values in the returned sequence is undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter keys: An array of primary keys.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - keys: A sequence of primary keys.
|
||||
/// - returns: A sequence.
|
||||
@warn_unused_result
|
||||
public static func fetch<Sequence: SequenceType where Sequence.Generator.Element: DatabaseValueConvertible>(db: Database, keys: Sequence) -> DatabaseSequence<Self> {
|
||||
@@ -40,8 +41,9 @@ extension RowConvertible where Self: TableMapping {
|
||||
///
|
||||
/// The order of values in the returned array is undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter keys: An array of primary keys.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - keys: A sequence of primary keys.
|
||||
/// - returns: An array.
|
||||
@warn_unused_result
|
||||
public static func fetchAll<Sequence: SequenceType where Sequence.Generator.Element: DatabaseValueConvertible>(db: Database, keys: Sequence) -> [Self] {
|
||||
@@ -55,8 +57,9 @@ extension RowConvertible where Self: TableMapping {
|
||||
///
|
||||
/// let person = Person.fetchOne(db, key: 123) // Person?
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter key: A primary key value.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - key: A primary key value.
|
||||
/// - returns: An optional value.
|
||||
@warn_unused_result
|
||||
public static func fetchOne<PrimaryKeyType: DatabaseValueConvertible>(db: Database, key: PrimaryKeyType?) -> Self? {
|
||||
@@ -110,8 +113,9 @@ extension RowConvertible where Self: TableMapping {
|
||||
///
|
||||
/// The order of values in the returned sequence is undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter keys: An array of key dictionaries.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - keys: An array of key dictionaries.
|
||||
/// - returns: A sequence.
|
||||
@warn_unused_result
|
||||
public static func fetch(db: Database, keys: [[String: DatabaseValueConvertible?]]) -> DatabaseSequence<Self> {
|
||||
@@ -127,8 +131,9 @@ extension RowConvertible where Self: TableMapping {
|
||||
///
|
||||
/// The order of values in the returned array is undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter keys: An array of key dictionaries.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - keys: An array of key dictionaries.
|
||||
/// - returns: An array.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(db: Database, keys: [[String: DatabaseValueConvertible?]]) -> [Self] {
|
||||
@@ -142,8 +147,9 @@ extension RowConvertible where Self: TableMapping {
|
||||
///
|
||||
/// let person = Person.fetchOne(db, key: ["name": Arthur"]) // Person?
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter key: A dictionary of values.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - key: A dictionary of values.
|
||||
/// - returns: An optional value.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(db: Database, key: [String: DatabaseValueConvertible?]) -> Self? {
|
||||
|
||||
+22
-16
@@ -421,9 +421,10 @@ public final class Row: CollectionType {
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements of the sequence are undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of rows.
|
||||
@warn_unused_result
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
@@ -437,8 +438,9 @@ public final class Row: CollectionType {
|
||||
/// let statement = db.selectStatement("SELECT ...")
|
||||
/// let rows = Row.fetchAll(statement)
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An array of rows.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Row] {
|
||||
@@ -453,8 +455,9 @@ public final class Row: CollectionType {
|
||||
/// let statement = db.selectStatement("SELECT ...")
|
||||
/// let row = Row.fetchOne(statement)
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - statement: The statement to run.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An optional row.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Row? {
|
||||
@@ -494,9 +497,10 @@ public final class Row: CollectionType {
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements of the sequence are undefined.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of rows.
|
||||
@warn_unused_result
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
@@ -507,9 +511,10 @@ public final class Row: CollectionType {
|
||||
///
|
||||
/// let rows = Row.fetchAll(db, "SELECT ...")
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An array of rows.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Row] {
|
||||
@@ -520,9 +525,10 @@ public final class Row: CollectionType {
|
||||
///
|
||||
/// let row = Row.fetchOne(db, "SELECT ...")
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - parameters:
|
||||
/// - db: A Database.
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - returns: An optional row.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Row? {
|
||||
|
||||
@@ -46,9 +46,10 @@ public struct DatabaseDateComponents : DatabaseValueConvertible {
|
||||
///
|
||||
/// The result is nil if and only if *dateComponents* is nil.
|
||||
///
|
||||
/// - parameter dateComponents: An optional NSDateComponents.
|
||||
/// - parameter format: The format used for storing the date components in
|
||||
/// the database.
|
||||
/// - parameters:
|
||||
/// - dateComponents: An optional NSDateComponents.
|
||||
/// - format: The format used for storing the date components in
|
||||
/// the database.
|
||||
/// - returns: An optional DatabaseDateComponents.
|
||||
public init?(_ dateComponents: NSDateComponents?, format: Format) {
|
||||
guard let dateComponents = dateComponents else {
|
||||
|
||||
@@ -65,10 +65,11 @@ public struct DatabaseMigrator {
|
||||
/// "ALTER TABLE new_persons RENAME TO persons;")
|
||||
/// }
|
||||
///
|
||||
/// - parameter identifier: The migration identifier. It must be unique.
|
||||
/// - parameter disabledForeignKeyChecks: If true, the migration is run with
|
||||
/// disabled foreign key checks.
|
||||
/// - parameter block: The migration block that performs SQL statements.
|
||||
/// - parameters:
|
||||
/// - identifier: The migration identifier. It must be unique.
|
||||
/// - disabledForeignKeyChecks: If true, the migration is run with
|
||||
// disabled foreign key checks.
|
||||
/// - block: The migration block that performs SQL statements.
|
||||
public mutating func registerMigration(identifier: String, withDisabledForeignKeyChecks disabledForeignKeyChecks: Bool = false, migrate: (db: Database) throws -> Void) {
|
||||
registerMigration(Migration(identifier: identifier, disabledForeignKeyChecks: disabledForeignKeyChecks, migrate: migrate))
|
||||
}
|
||||
|
||||
@@ -91,8 +91,9 @@ public class Record : RowConvertible, TableMapping, Persistable {
|
||||
///
|
||||
/// The default implementation does nothing.
|
||||
///
|
||||
/// - parameter rowID: The inserted rowID.
|
||||
/// - parameter column: The name of the eventual INTEGER PRIMARY KEY column.
|
||||
/// - parameters:
|
||||
/// - rowID: The inserted rowID.
|
||||
/// - column: The name of the eventual INTEGER PRIMARY KEY column.
|
||||
public func didInsertWithRowID(rowID: Int64, forColumn column: String?) {
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,9 @@ extension Bool: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
|
||||
/// Returns a value initialized from a raw SQLite statement pointer.
|
||||
///
|
||||
/// - parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - parameter index: The column index.
|
||||
/// - parameters:
|
||||
/// - sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - index: The column index.
|
||||
public init(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
self = sqlite3_column_int64(sqliteStatement, index) != 0
|
||||
}
|
||||
@@ -88,8 +89,9 @@ extension Int: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
|
||||
/// Returns a value initialized from a raw SQLite statement pointer.
|
||||
///
|
||||
/// - parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - parameter index: The column index.
|
||||
/// - parameters:
|
||||
/// - sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - index: The column index.
|
||||
public init(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
self = Int(sqlite3_column_int64(sqliteStatement, index))
|
||||
}
|
||||
@@ -117,8 +119,9 @@ extension Int32: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
|
||||
/// Returns a value initialized from a raw SQLite statement pointer.
|
||||
///
|
||||
/// - parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - parameter index: The column index.
|
||||
/// - parameters:
|
||||
/// - sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - index: The column index.
|
||||
public init(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
self = Int32(sqlite3_column_int64(sqliteStatement, index))
|
||||
}
|
||||
@@ -146,8 +149,9 @@ extension Int64: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
|
||||
/// Returns a value initialized from a raw SQLite statement pointer.
|
||||
///
|
||||
/// - parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - parameter index: The column index.
|
||||
/// - parameters:
|
||||
/// - sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - index: The column index.
|
||||
public init(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
self = sqlite3_column_int64(sqliteStatement, index)
|
||||
}
|
||||
@@ -175,8 +179,9 @@ extension Double: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
|
||||
/// Returns a value initialized from a raw SQLite statement pointer.
|
||||
///
|
||||
/// - parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - parameter index: The column index.
|
||||
/// - parameters:
|
||||
/// - sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - index: The column index.
|
||||
public init(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
self = sqlite3_column_double(sqliteStatement, index)
|
||||
}
|
||||
@@ -204,8 +209,9 @@ extension Float: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
|
||||
/// Returns a value initialized from a raw SQLite statement pointer.
|
||||
///
|
||||
/// - parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - parameter index: The column index.
|
||||
/// - parameters:
|
||||
/// - sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - index: The column index.
|
||||
public init(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
self = Float(sqlite3_column_double(sqliteStatement, index))
|
||||
}
|
||||
@@ -233,8 +239,9 @@ extension String: DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
|
||||
/// Returns a value initialized from a raw SQLite statement pointer.
|
||||
///
|
||||
/// - parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - parameter index: The column index.
|
||||
/// - parameters:
|
||||
/// - sqliteStatement: A pointer to a SQLite statement.
|
||||
/// - index: The column index.
|
||||
public init(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
let cString = UnsafePointer<Int8>(sqlite3_column_text(sqliteStatement, Int32(index)))
|
||||
self = String.fromCString(cString)!
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
import Foundation
|
||||
|
||||
/// Usage
|
||||
///
|
||||
/// let dbQueue = DatabaseQueue(...)
|
||||
/// dbQueue.inDatabase { db in
|
||||
/// let defaults = UserDefaults.inDatabase(db)
|
||||
/// defaults.registerDefaults(["foo": "bar"])
|
||||
/// defaults.setInteger(12, forKey: "baz")
|
||||
/// defaults.integerForKey("bar") // 12
|
||||
/// }
|
||||
public class UserDefaults {
|
||||
private weak var db: Database!
|
||||
private var registrationDictionary: [String: AnyObject] = [:]
|
||||
|
||||
/// Returns the defaults for the database
|
||||
public static func inDatabase(db: Database) -> UserDefaults {
|
||||
registeredDefaults = registeredDefaults.filter { $0.db != nil }
|
||||
for defaults in registeredDefaults where defaults.db === db {
|
||||
return defaults
|
||||
}
|
||||
let defaults = UserDefaults(db: db)
|
||||
registeredDefaults.append(defaults)
|
||||
return defaults
|
||||
}
|
||||
|
||||
private init(db: Database) {
|
||||
self.db = db
|
||||
}
|
||||
|
||||
private static var registeredDefaults: [UserDefaults] = []
|
||||
|
||||
private var needsTable: Bool = true
|
||||
private func createTableIfNeeded() {
|
||||
guard needsTable else { return }
|
||||
if !db.tableExists(UserDefaultsItemTableName) {
|
||||
try! db.execute("CREATE TABLE \(UserDefaultsItemTableName) (key TEXT NOT NULL PRIMARY KEY, value BLOB)")
|
||||
}
|
||||
needsTable = false
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Registering Defaults
|
||||
|
||||
public func registerDefaults(registrationDictionary: [String: AnyObject]) {
|
||||
for (key, value) in registrationDictionary {
|
||||
self.registrationDictionary[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Setting Default Values
|
||||
|
||||
public func removeObjectForKey(key: String) {
|
||||
createTableIfNeeded()
|
||||
try! UserDefaultsItem.fetchOne(db, key: key)?.delete(db)
|
||||
}
|
||||
|
||||
public func setBool(value: Bool, forKey key: String) {
|
||||
setObject(NSNumber(bool: value), forKey: key)
|
||||
}
|
||||
|
||||
public func setDouble(value: Double, forKey key: String) {
|
||||
setObject(NSNumber(double: value), forKey: key)
|
||||
}
|
||||
|
||||
public func setFloat(value: Float, forKey key: String) {
|
||||
setObject(NSNumber(float: value), forKey: key)
|
||||
}
|
||||
|
||||
public func setInteger(value: Int, forKey key: String) {
|
||||
setObject(NSNumber(integer: value), forKey: key)
|
||||
}
|
||||
|
||||
public func setObject(value: AnyObject?, forKey key: String) {
|
||||
createTableIfNeeded()
|
||||
if var item = UserDefaultsItem.fetchOne(db, key: key) {
|
||||
item.value = value
|
||||
try! item.update(db)
|
||||
} else {
|
||||
try! UserDefaultsItem(key: key, value: value).insert(db)
|
||||
}
|
||||
}
|
||||
|
||||
public func setURL(value: NSURL?, forKey key: String) {
|
||||
setObject(value, forKey: key)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reading Default Values
|
||||
|
||||
|
||||
public func arrayForKey(key: String) -> [AnyObject]? {
|
||||
return objectForKey(key) as? [AnyObject]
|
||||
}
|
||||
|
||||
public func boolForKey(key: String) -> Bool {
|
||||
return (objectForKey(key) as? NSNumber)?.boolValue ?? false
|
||||
}
|
||||
|
||||
public func dataForKey(key: String) -> NSData? {
|
||||
return objectForKey(key) as? NSData
|
||||
}
|
||||
|
||||
public func dictionaryForKey(key: String) -> [String: AnyObject]? {
|
||||
return objectForKey(key) as? [String: AnyObject]
|
||||
}
|
||||
|
||||
public func doubleForKey(key: String) -> Double {
|
||||
return (objectForKey(key) as? NSNumber)?.doubleValue ?? 0.0
|
||||
}
|
||||
|
||||
public func floatForKey(key: String) -> Float {
|
||||
return (objectForKey(key) as? NSNumber)?.floatValue ?? 0.0
|
||||
}
|
||||
|
||||
public func integerForKey(key: String) -> Int {
|
||||
return (objectForKey(key) as? NSNumber)?.integerValue ?? 0
|
||||
}
|
||||
|
||||
public func objectForKey(key: String) -> AnyObject? {
|
||||
createTableIfNeeded()
|
||||
if let item = UserDefaultsItem.fetchOne(db, key: key) {
|
||||
return item.value
|
||||
}
|
||||
return registrationDictionary[key]
|
||||
}
|
||||
|
||||
public func stringArrayForKey(key: String) -> [String]? {
|
||||
return objectForKey(key) as? [String]
|
||||
}
|
||||
|
||||
public func stringForKey(key: String) -> String? {
|
||||
return objectForKey(key) as? String
|
||||
}
|
||||
|
||||
public func URLForKey(key: String) -> NSURL? {
|
||||
return objectForKey(key) as? NSURL
|
||||
}
|
||||
}
|
||||
|
||||
private struct UserDefaultsItem {
|
||||
let key: String
|
||||
var value: AnyObject?
|
||||
}
|
||||
|
||||
extension UserDefaultsItem: RowConvertible {
|
||||
static func fromRow(row: Row) -> UserDefaultsItem {
|
||||
if let data = row.dataNoCopy(named: "value") {
|
||||
let value = try! NSPropertyListSerialization.propertyListWithData(data, options: .Immutable, format: nil)
|
||||
return UserDefaultsItem(key: row.value(named: "key"), value: value)
|
||||
} else {
|
||||
return UserDefaultsItem(key: row.value(named: "key"), value: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UserDefaultsItem: Persistable {
|
||||
static func databaseTableName() -> String {
|
||||
return UserDefaultsItemTableName
|
||||
}
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
if let value = value {
|
||||
let data = NSPropertyListSerialization.dataFromPropertyList(value, format: .BinaryFormat_v1_0, errorDescription: nil)!
|
||||
return ["key": key, "value": data]
|
||||
} else {
|
||||
return ["key": key, "value": nil]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private let UserDefaultsItemTableName = "grdb_UserDefaults"
|
||||
@@ -1,45 +1,44 @@
|
||||
/**
|
||||
Given two sorted sequences (left and right), this function emits "merge steps"
|
||||
which tell whether elements are only found on the left, on the right, or on
|
||||
both sides.
|
||||
|
||||
Both sequences do not have to share the same element type. Yet elements must
|
||||
share a common comparable *key*.
|
||||
|
||||
Both sequences must be sorted by this key.
|
||||
|
||||
Keys must be unique in both sequences.
|
||||
|
||||
The example below compare two sequences sorted by integer representation,
|
||||
and prints:
|
||||
|
||||
- Left: 1
|
||||
- Common: 2, 2
|
||||
- Common: 3, 3
|
||||
- Right: 4
|
||||
|
||||
for mergeStep in sortedMerge(
|
||||
left: [1,2,3],
|
||||
right: ["2", "3", "4"],
|
||||
leftKey: { $0 },
|
||||
rightKey: { Int($0)! })
|
||||
{
|
||||
switch mergeStep {
|
||||
case .Left(let left):
|
||||
print("- Left: \(left)")
|
||||
case .Right(let right):
|
||||
print("- Right: \(right)")
|
||||
case .Common(let left, let right):
|
||||
print("- Common: \(left), \(right)")
|
||||
}
|
||||
}
|
||||
|
||||
- parameter left: The left sequence.
|
||||
- parameter right: The right sequence.
|
||||
- parameter leftKey: A function that returns the key of a left element.
|
||||
- parameter rightKey: A function that returns the key of a right element.
|
||||
- returns: A sequence of MergeStep
|
||||
*/
|
||||
/// Given two sorted sequences (left and right), this function emits "merge steps"
|
||||
/// which tell whether elements are only found on the left, on the right, or on
|
||||
/// both sides.
|
||||
///
|
||||
/// Both sequences do not have to share the same element type. Yet elements must
|
||||
/// share a common comparable *key*.
|
||||
///
|
||||
/// Both sequences must be sorted by this key.
|
||||
///
|
||||
/// Keys must be unique in both sequences.
|
||||
///
|
||||
/// The example below compare two sequences sorted by integer representation,
|
||||
/// and prints:
|
||||
///
|
||||
/// - Left: 1
|
||||
/// - Common: 2, 2
|
||||
/// - Common: 3, 3
|
||||
/// - Right: 4
|
||||
///
|
||||
/// for mergeStep in sortedMerge(
|
||||
/// left: [1,2,3],
|
||||
/// right: ["2", "3", "4"],
|
||||
/// leftKey: { $0 },
|
||||
/// rightKey: { Int($0)! })
|
||||
/// {
|
||||
/// switch mergeStep {
|
||||
/// case .Left(let left):
|
||||
/// print("- Left: \(left)")
|
||||
/// case .Right(let right):
|
||||
/// print("- Right: \(right)")
|
||||
/// case .Common(let left, let right):
|
||||
/// print("- Common: \(left), \(right)")
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// - parameters:
|
||||
/// - left: The left sequence.
|
||||
/// - right: The right sequence.
|
||||
/// - leftKey: A function that returns the key of a left element.
|
||||
/// - rightKey: A function that returns the key of a right element.
|
||||
/// - returns: A sequence of MergeStep
|
||||
public func sortedMerge<LeftSequence: SequenceType, RightSequence: SequenceType, Key: Comparable>(
|
||||
left lSeq: LeftSequence,
|
||||
right rSeq: RightSequence,
|
||||
|
||||
Reference in new issue
Block a user