From 1136d367c75155e5ec55a2d068e704e42c0595bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 13 Apr 2016 08:59:32 +0200 Subject: [PATCH] Code organization --- GRDB/Core/Database.swift | 224 +----------------- .../StandardLibrary/StandardLibrary.swift | 220 +++++++++++++++++ 2 files changed, 225 insertions(+), 219 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 7aca1b828..b4eb74c63 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -579,125 +579,6 @@ public func ==(lhs: DatabaseFunction, rhs: DatabaseFunction) -> Bool { return lhs.name == rhs.name && lhs.argumentCount == rhs.argumentCount } -extension DatabaseFunction { - /// An SQL function that returns the Swift built-in capitalizedString - /// String property. - /// - /// The function returns NULL for non-strings values. - /// - /// This function is automatically added by GRDB to your database - /// connections. It is the function used by the query interface's - /// capitalizedString: - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn.capitalizedString) - /// let names = String.fetchAll(dbQueue, request) // [String] - public static let capitalizedString = DatabaseFunction("swiftCapitalizedString", argumentCount: 1, pure: true) { databaseValues in - guard let string: String = databaseValues[0].failableValue() else { - return nil - } - return string.capitalizedString - } - - /// An SQL function that returns the Swift built-in lowercaseString - /// String property. - /// - /// The function returns NULL for non-strings values. - /// - /// This function is automatically added by GRDB to your database - /// connections. It is the function used by the query interface's - /// lowercaseString: - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn.lowercaseString) - /// let names = String.fetchAll(dbQueue, request) // [String] - public static let lowercaseString = DatabaseFunction("swiftLowercaseString", argumentCount: 1, pure: true) { databaseValues in - guard let string: String = databaseValues[0].failableValue() else { - return nil - } - return string.lowercaseString - } - - /// An SQL function that returns the Swift built-in uppercaseString - /// String property. - /// - /// The function returns NULL for non-strings values. - /// - /// This function is automatically added by GRDB to your database - /// connections. It is the function used by the query interface's - /// uppercaseString: - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn.uppercaseString) - /// let names = String.fetchAll(dbQueue, request) // [String] - public static let uppercaseString = DatabaseFunction("swiftUppercaseString", argumentCount: 1, pure: true) { databaseValues in - guard let string: String = databaseValues[0].failableValue() else { - return nil - } - return string.uppercaseString - } -} - -@available(iOS 9.0, OSX 10.11, *) -extension DatabaseFunction { - /// An SQL function that returns the Swift built-in - /// localizedCapitalizedString String property. - /// - /// The function returns NULL for non-strings values. - /// - /// This function is automatically added by GRDB to your database - /// connections. It is the function used by the query interface's - /// localizedCapitalizedString: - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn.localizedCapitalizedString) - /// let names = String.fetchAll(dbQueue, request) // [String] - public static let localizedCapitalizedString = DatabaseFunction("swiftLocalizedCapitalizedString", argumentCount: 1, pure: true) { databaseValues in - guard let string: String = databaseValues[0].failableValue() else { - return nil - } - return string.localizedCapitalizedString - } - - /// An SQL function that returns the Swift built-in - /// localizedLowercaseString String property. - /// - /// The function returns NULL for non-strings values. - /// - /// This function is automatically added by GRDB to your database - /// connections. It is the function used by the query interface's - /// localizedLowercaseString: - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn.localizedLowercaseString) - /// let names = String.fetchAll(dbQueue, request) // [String] - public static let localizedLowercaseString = DatabaseFunction("swiftLocalizedLowercaseString", argumentCount: 1, pure: true) { databaseValues in - guard let string: String = databaseValues[0].failableValue() else { - return nil - } - return string.localizedLowercaseString - } - - /// An SQL function that returns the Swift built-in - /// localizedUppercaseString String property. - /// - /// The function returns NULL for non-strings values. - /// - /// This function is automatically added by GRDB to your database - /// connections. It is the function used by the query interface's - /// localizedUppercaseString: - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn.localizedUppercaseString) - /// let names = String.fetchAll(dbQueue, request) // [String] - public static let localizedUppercaseString = DatabaseFunction("swiftLocalizedUppercaseString", argumentCount: 1, pure: true) { databaseValues in - guard let string: String = databaseValues[0].failableValue() else { - return nil - } - return string.localizedUppercaseString - } -} - // ========================================================================= // MARK: - Collations @@ -782,98 +663,6 @@ public func ==(lhs: DatabaseCollation, rhs: DatabaseCollation) -> Bool { return sqlite3_stricmp(lhs.name, lhs.name) == 0 } -extension DatabaseCollation { - /// A collation, or SQL string comparison function, that compares strings - /// according to the the Swift built-in == and <= operators. - /// - /// This collation is automatically added by GRDB to your database - /// connections. - /// - /// You can use it when creating database tables: - /// - /// let collationName = DatabaseCollation.caseInsensitiveCompare.name - /// dbQueue.execute( - /// "CREATE TABLE persons (" + - /// "name TEXT COLLATE \(collationName)" + - /// ")" - /// ) - public static let unicodeCompare = DatabaseCollation("swiftCompare") { (lhs, rhs) in - return (lhs < rhs) ? .OrderedAscending : ((lhs == rhs) ? .OrderedSame : .OrderedDescending) - } - - /// A collation, or SQL string comparison function, that compares strings - /// according to the the Swift built-in caseInsensitiveCompare(_:) method. - /// - /// This collation is automatically added by GRDB to your database - /// connections. - /// - /// You can use it when creating database tables: - /// - /// let collationName = DatabaseCollation.caseInsensitiveCompare.name - /// dbQueue.execute( - /// "CREATE TABLE persons (" + - /// "name TEXT COLLATE \(collationName)" + - /// ")" - /// ) - public static let caseInsensitiveCompare = DatabaseCollation("swiftCaseInsensitiveCompare") { (lhs, rhs) in - return lhs.caseInsensitiveCompare(rhs) - } - - /// A collation, or SQL string comparison function, that compares strings - /// according to the the Swift built-in localizedCaseInsensitiveCompare(_:) method. - /// - /// This collation is automatically added by GRDB to your database - /// connections. - /// - /// You can use it when creating database tables: - /// - /// let collationName = DatabaseCollation.localizedCaseInsensitiveCompare.name - /// dbQueue.execute( - /// "CREATE TABLE persons (" + - /// "name TEXT COLLATE \(collationName)" + - /// ")" - /// ) - public static let localizedCaseInsensitiveCompare = DatabaseCollation("swiftLocalizedCaseInsensitiveCompare") { (lhs, rhs) in - return lhs.localizedCaseInsensitiveCompare(rhs) - } - - /// A collation, or SQL string comparison function, that compares strings - /// according to the the Swift built-in localizedCompare(_:) method. - /// - /// This collation is automatically added by GRDB to your database - /// connections. - /// - /// You can use it when creating database tables: - /// - /// let collationName = DatabaseCollation.localizedCompare.name - /// dbQueue.execute( - /// "CREATE TABLE persons (" + - /// "name TEXT COLLATE \(collationName)" + - /// ")" - /// ) - public static let localizedCompare = DatabaseCollation("swiftLocalizedCompare") { (lhs, rhs) in - return lhs.localizedCompare(rhs) - } - - /// A collation, or SQL string comparison function, that compares strings - /// according to the the Swift built-in localizedStandardCompare(_:) method. - /// - /// This collation is automatically added by GRDB to your database - /// connections. - /// - /// You can use it when creating database tables: - /// - /// let collationName = DatabaseCollation.localizedStandardCompare.name - /// dbQueue.execute( - /// "CREATE TABLE persons (" + - /// "name TEXT COLLATE \(collationName)" + - /// ")" - /// ) - public static let localizedStandardCompare = DatabaseCollation("swiftLocalizedStandardCompare") { (lhs, rhs) in - return lhs.localizedStandardCompare(rhs) - } -} - // ========================================================================= // MARK: - Encryption @@ -1159,13 +948,10 @@ extension Database { /// .Commit or .Rollback. /// - throws: The error thrown by the block. public func inTransaction(kind: TransactionKind? = nil, @noescape _ block: () throws -> TransactionCompletion) throws { - preconditionValidQueue() + try beginTransaction(kind ?? configuration.defaultTransactionKind) var completion: TransactionCompletion = .Rollback var blockError: ErrorType? = nil - - try beginTransaction(kind) - do { completion = try block() } catch { @@ -1195,8 +981,8 @@ extension Database { // > transaction has already been rolled back automatically by the // > error response, then the ROLLBACK command will fail with an // > error, but no harm is caused by this. - if let blockError = blockError as? DatabaseError { - switch Int32(blockError.code) { + if let databaseError = blockError as? DatabaseError { + switch Int32(databaseError.code) { case SQLITE_FULL, SQLITE_IOERR, SQLITE_BUSY, SQLITE_NOMEM: do { try rollback() } catch { } default: @@ -1212,8 +998,8 @@ extension Database { } } - private func beginTransaction(kind: TransactionKind? = nil) throws { - switch kind ?? configuration.defaultTransactionKind { + private func beginTransaction(kind: TransactionKind) throws { + switch kind { case .Deferred: try execute("BEGIN DEFERRED TRANSACTION") case .Immediate: diff --git a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift index 147d451d0..3d92d8b9f 100644 --- a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift +++ b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift @@ -10,6 +10,9 @@ #endif #endif + +// MARK: - Value Types + /// Bool adopts DatabaseValueConvertible and StatementColumnConvertible. extension Bool: DatabaseValueConvertible, StatementColumnConvertible { @@ -274,3 +277,220 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible { } } } + + +// MARK: - SQL Functions + +extension DatabaseFunction { + /// An SQL function that returns the Swift built-in capitalizedString + /// String property. + /// + /// The function returns NULL for non-strings values. + /// + /// This function is automatically added by GRDB to your database + /// connections. It is the function used by the query interface's + /// capitalizedString: + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn.capitalizedString) + /// let names = String.fetchAll(dbQueue, request) // [String] + public static let capitalizedString = DatabaseFunction("swiftCapitalizedString", argumentCount: 1, pure: true) { databaseValues in + guard let string: String = databaseValues[0].failableValue() else { + return nil + } + return string.capitalizedString + } + + /// An SQL function that returns the Swift built-in lowercaseString + /// String property. + /// + /// The function returns NULL for non-strings values. + /// + /// This function is automatically added by GRDB to your database + /// connections. It is the function used by the query interface's + /// lowercaseString: + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn.lowercaseString) + /// let names = String.fetchAll(dbQueue, request) // [String] + public static let lowercaseString = DatabaseFunction("swiftLowercaseString", argumentCount: 1, pure: true) { databaseValues in + guard let string: String = databaseValues[0].failableValue() else { + return nil + } + return string.lowercaseString + } + + /// An SQL function that returns the Swift built-in uppercaseString + /// String property. + /// + /// The function returns NULL for non-strings values. + /// + /// This function is automatically added by GRDB to your database + /// connections. It is the function used by the query interface's + /// uppercaseString: + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn.uppercaseString) + /// let names = String.fetchAll(dbQueue, request) // [String] + public static let uppercaseString = DatabaseFunction("swiftUppercaseString", argumentCount: 1, pure: true) { databaseValues in + guard let string: String = databaseValues[0].failableValue() else { + return nil + } + return string.uppercaseString + } +} + +@available(iOS 9.0, OSX 10.11, *) +extension DatabaseFunction { + /// An SQL function that returns the Swift built-in + /// localizedCapitalizedString String property. + /// + /// The function returns NULL for non-strings values. + /// + /// This function is automatically added by GRDB to your database + /// connections. It is the function used by the query interface's + /// localizedCapitalizedString: + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn.localizedCapitalizedString) + /// let names = String.fetchAll(dbQueue, request) // [String] + public static let localizedCapitalizedString = DatabaseFunction("swiftLocalizedCapitalizedString", argumentCount: 1, pure: true) { databaseValues in + guard let string: String = databaseValues[0].failableValue() else { + return nil + } + return string.localizedCapitalizedString + } + + /// An SQL function that returns the Swift built-in + /// localizedLowercaseString String property. + /// + /// The function returns NULL for non-strings values. + /// + /// This function is automatically added by GRDB to your database + /// connections. It is the function used by the query interface's + /// localizedLowercaseString: + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn.localizedLowercaseString) + /// let names = String.fetchAll(dbQueue, request) // [String] + public static let localizedLowercaseString = DatabaseFunction("swiftLocalizedLowercaseString", argumentCount: 1, pure: true) { databaseValues in + guard let string: String = databaseValues[0].failableValue() else { + return nil + } + return string.localizedLowercaseString + } + + /// An SQL function that returns the Swift built-in + /// localizedUppercaseString String property. + /// + /// The function returns NULL for non-strings values. + /// + /// This function is automatically added by GRDB to your database + /// connections. It is the function used by the query interface's + /// localizedUppercaseString: + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn.localizedUppercaseString) + /// let names = String.fetchAll(dbQueue, request) // [String] + public static let localizedUppercaseString = DatabaseFunction("swiftLocalizedUppercaseString", argumentCount: 1, pure: true) { databaseValues in + guard let string: String = databaseValues[0].failableValue() else { + return nil + } + return string.localizedUppercaseString + } +} + + +// MARK: - SQLite Collations + +extension DatabaseCollation { + /// A collation, or SQL string comparison function, that compares strings + /// according to the the Swift built-in == and <= operators. + /// + /// This collation is automatically added by GRDB to your database + /// connections. + /// + /// You can use it when creating database tables: + /// + /// let collationName = DatabaseCollation.caseInsensitiveCompare.name + /// dbQueue.execute( + /// "CREATE TABLE persons (" + + /// "name TEXT COLLATE \(collationName)" + + /// ")" + /// ) + public static let unicodeCompare = DatabaseCollation("swiftCompare") { (lhs, rhs) in + return (lhs < rhs) ? .OrderedAscending : ((lhs == rhs) ? .OrderedSame : .OrderedDescending) + } + + /// A collation, or SQL string comparison function, that compares strings + /// according to the the Swift built-in caseInsensitiveCompare(_:) method. + /// + /// This collation is automatically added by GRDB to your database + /// connections. + /// + /// You can use it when creating database tables: + /// + /// let collationName = DatabaseCollation.caseInsensitiveCompare.name + /// dbQueue.execute( + /// "CREATE TABLE persons (" + + /// "name TEXT COLLATE \(collationName)" + + /// ")" + /// ) + public static let caseInsensitiveCompare = DatabaseCollation("swiftCaseInsensitiveCompare") { (lhs, rhs) in + return lhs.caseInsensitiveCompare(rhs) + } + + /// A collation, or SQL string comparison function, that compares strings + /// according to the the Swift built-in localizedCaseInsensitiveCompare(_:) method. + /// + /// This collation is automatically added by GRDB to your database + /// connections. + /// + /// You can use it when creating database tables: + /// + /// let collationName = DatabaseCollation.localizedCaseInsensitiveCompare.name + /// dbQueue.execute( + /// "CREATE TABLE persons (" + + /// "name TEXT COLLATE \(collationName)" + + /// ")" + /// ) + public static let localizedCaseInsensitiveCompare = DatabaseCollation("swiftLocalizedCaseInsensitiveCompare") { (lhs, rhs) in + return lhs.localizedCaseInsensitiveCompare(rhs) + } + + /// A collation, or SQL string comparison function, that compares strings + /// according to the the Swift built-in localizedCompare(_:) method. + /// + /// This collation is automatically added by GRDB to your database + /// connections. + /// + /// You can use it when creating database tables: + /// + /// let collationName = DatabaseCollation.localizedCompare.name + /// dbQueue.execute( + /// "CREATE TABLE persons (" + + /// "name TEXT COLLATE \(collationName)" + + /// ")" + /// ) + public static let localizedCompare = DatabaseCollation("swiftLocalizedCompare") { (lhs, rhs) in + return lhs.localizedCompare(rhs) + } + + /// A collation, or SQL string comparison function, that compares strings + /// according to the the Swift built-in localizedStandardCompare(_:) method. + /// + /// This collation is automatically added by GRDB to your database + /// connections. + /// + /// You can use it when creating database tables: + /// + /// let collationName = DatabaseCollation.localizedStandardCompare.name + /// dbQueue.execute( + /// "CREATE TABLE persons (" + + /// "name TEXT COLLATE \(collationName)" + + /// ")" + /// ) + public static let localizedStandardCompare = DatabaseCollation("swiftLocalizedStandardCompare") { (lhs, rhs) in + return lhs.localizedStandardCompare(rhs) + } +}