mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Cleanup
This commit is contained in:
1 parent
a158f63b4d
commit
f2b59e06b2
2 files changed
+19
-26
No files matched your search
@@ -22,18 +22,6 @@ public protocol SQLiteStatementConvertible {
|
||||
init(sqliteStatement: SQLiteStatement, index: Int32)
|
||||
}
|
||||
|
||||
extension SQLiteStatementConvertible {
|
||||
|
||||
static func fromSQLiteStatement(sqliteStatement: SQLiteStatement, index: Int32) -> Self? {
|
||||
if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
|
||||
return nil
|
||||
} else {
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// Types that adopt both DatabaseValueConvertible and
|
||||
/// SQLiteStatementConvertible can be efficiently initialized from
|
||||
@@ -65,10 +53,10 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return statement.fetchSequence(arguments: arguments) {
|
||||
guard let value = fromSQLiteStatement(sqliteStatement, index: 0) else {
|
||||
guard sqlite3_column_type(sqliteStatement, 0) != SQLITE_NULL else {
|
||||
fatalError("could not convert NULL to \(Self.self).")
|
||||
}
|
||||
return value
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: 0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,9 +82,11 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
let sequence = statement.fetchSequence(arguments: arguments) { fromSQLiteStatement(sqliteStatement, index: 0) }
|
||||
if let value = sequence.generate().next() {
|
||||
return value
|
||||
let isNullSequence = statement.fetchSequence(arguments: arguments) {
|
||||
sqlite3_column_type(sqliteStatement, 0) == SQLITE_NULL
|
||||
}
|
||||
if isNullSequence.generate().next() == false {
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: 0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+12
-9
@@ -116,10 +116,13 @@ public final class Row: CollectionType {
|
||||
|
||||
private func unsafeValue<Value: protocol<DatabaseValueConvertible, SQLiteStatementConvertible>>(atIndex index: Int) -> Value? {
|
||||
let sqliteStatement = self.sqliteStatement
|
||||
if sqliteStatement != nil {
|
||||
return Value.fromSQLiteStatement(sqliteStatement, index: Int32(index))
|
||||
guard sqliteStatement != nil else {
|
||||
return impl.databaseValue(atIndex: index).value()
|
||||
}
|
||||
return impl.databaseValue(atIndex: index).value()
|
||||
guard sqlite3_column_type(sqliteStatement, Int32(index)) != SQLITE_NULL else {
|
||||
return nil
|
||||
}
|
||||
return Value.init(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
|
||||
/// Returns the value at given index, converted to the requested type.
|
||||
@@ -181,13 +184,13 @@ public final class Row: CollectionType {
|
||||
|
||||
private func unsafeValue<Value: protocol<DatabaseValueConvertible, SQLiteStatementConvertible>>(atIndex index: Int) -> Value {
|
||||
let sqliteStatement = self.sqliteStatement
|
||||
if sqliteStatement != nil {
|
||||
guard let value = Value.fromSQLiteStatement(sqliteStatement, index: Int32(index)) else {
|
||||
fatalError("could not convert NULL to \(Value.self).")
|
||||
}
|
||||
return value
|
||||
guard sqliteStatement != nil else {
|
||||
return impl.databaseValue(atIndex: index).value()
|
||||
}
|
||||
return impl.databaseValue(atIndex: index).value()
|
||||
guard sqlite3_column_type(sqliteStatement, Int32(index)) != SQLITE_NULL else {
|
||||
fatalError("could not convert NULL to \(Value.self).")
|
||||
}
|
||||
return Value.init(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
|
||||
/// Returns Int64, Double, String, NSData or nil, depending on the value
|
||||
|
||||
Reference in new issue
Block a user