mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Code organization
This commit is contained in:
1 parent
e7b1c9b7cd
commit
1136d367c7
2 files changed
+225
-219
No files matched your search
+5
-219
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user