mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Replace DatabaseValueType.bindInSQLiteStatement(_:atIndex:) with DatabaseValueType.sqliteValue
This commit is contained in:
1 parent
5bd4a29dc0
commit
3b0ebd64a6
5 files changed
+40
-30
No files matched your search
@@ -21,17 +21,14 @@ struct DatabaseDate: DatabaseValueType {
|
||||
}
|
||||
}
|
||||
|
||||
func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
let timestamp = date.timeIntervalSince1970
|
||||
return timestamp.bindInSQLiteStatement(statement, atIndex: index)
|
||||
var sqliteValue: SQLiteValue {
|
||||
return .Double(date.timeIntervalSince1970)
|
||||
}
|
||||
|
||||
static func fromSQLiteValue(value: SQLiteValue) -> DatabaseDate? {
|
||||
switch value {
|
||||
case .Double(let timestamp):
|
||||
if let timestamp = Double.fromSQLiteValue(value) {
|
||||
return self.init(NSDate(timeIntervalSince1970: timestamp))
|
||||
default:
|
||||
// NULL, integer, text or blob:
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,16 +6,14 @@
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
private let SQLITE_TRANSIENT = unsafeBitCast(COpaquePointer(bitPattern: -1), sqlite3_destructor_type.self)
|
||||
|
||||
public protocol DatabaseValueType {
|
||||
func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32
|
||||
var sqliteValue: SQLiteValue { get }
|
||||
static func fromSQLiteValue(value: SQLiteValue) -> Self?
|
||||
}
|
||||
|
||||
extension Bool: DatabaseValueType {
|
||||
public func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
return sqlite3_bind_int(statement, Int32(index), Int32(self ? 1 : 0))
|
||||
public var sqliteValue: SQLiteValue {
|
||||
return .Integer(self ? 1 : 0)
|
||||
}
|
||||
|
||||
public static func fromSQLiteValue(value: SQLiteValue) -> Bool? {
|
||||
@@ -29,8 +27,8 @@ extension Bool: DatabaseValueType {
|
||||
}
|
||||
|
||||
extension Int: DatabaseValueType {
|
||||
public func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
return sqlite3_bind_int64(statement, Int32(index), Int64(self))
|
||||
public var sqliteValue: SQLiteValue {
|
||||
return .Integer(Int64(self))
|
||||
}
|
||||
|
||||
public static func fromSQLiteValue(value: SQLiteValue) -> Int? {
|
||||
@@ -46,8 +44,8 @@ extension Int: DatabaseValueType {
|
||||
}
|
||||
|
||||
extension Int64: DatabaseValueType {
|
||||
public func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
return sqlite3_bind_int64(statement, Int32(index), self)
|
||||
public var sqliteValue: SQLiteValue {
|
||||
return .Integer(self)
|
||||
}
|
||||
|
||||
public static func fromSQLiteValue(value: SQLiteValue) -> Int64? {
|
||||
@@ -63,8 +61,8 @@ extension Int64: DatabaseValueType {
|
||||
}
|
||||
|
||||
extension Double: DatabaseValueType {
|
||||
public func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
return sqlite3_bind_double(statement, Int32(index), self)
|
||||
public var sqliteValue: SQLiteValue {
|
||||
return .Double(self)
|
||||
}
|
||||
|
||||
public static func fromSQLiteValue(value: SQLiteValue) -> Double? {
|
||||
@@ -80,8 +78,8 @@ extension Double: DatabaseValueType {
|
||||
}
|
||||
|
||||
extension String: DatabaseValueType {
|
||||
public func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
return sqlite3_bind_text(statement, Int32(index), self, -1, SQLITE_TRANSIENT)
|
||||
public var sqliteValue: SQLiteValue {
|
||||
return .Text(self)
|
||||
}
|
||||
|
||||
public static func fromSQLiteValue(value: SQLiteValue) -> String? {
|
||||
|
||||
+18
-2
@@ -7,8 +7,7 @@
|
||||
//
|
||||
|
||||
typealias SQLiteConnection = COpaquePointer
|
||||
|
||||
public typealias SQLiteStatement = COpaquePointer
|
||||
typealias SQLiteStatement = COpaquePointer
|
||||
|
||||
public struct SQLiteError : ErrorType {
|
||||
public let _domain: String = "GRDB.SQLiteError"
|
||||
@@ -42,6 +41,8 @@ public struct SQLiteError : ErrorType {
|
||||
}
|
||||
}
|
||||
|
||||
private let SQLITE_TRANSIENT = unsafeBitCast(COpaquePointer(bitPattern: -1), sqlite3_destructor_type.self)
|
||||
|
||||
public enum SQLiteValue {
|
||||
case Null
|
||||
case Integer(Int64)
|
||||
@@ -67,4 +68,19 @@ public enum SQLiteValue {
|
||||
public func value<DatabaseValue: DatabaseValueType>() -> DatabaseValue? {
|
||||
return DatabaseValue.fromSQLiteValue(self)
|
||||
}
|
||||
|
||||
func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
switch self {
|
||||
case .Null:
|
||||
return sqlite3_bind_null(statement, Int32(index))
|
||||
case .Integer(let int):
|
||||
return sqlite3_bind_int64(statement, Int32(index), int)
|
||||
case .Double(let double):
|
||||
return sqlite3_bind_double(statement, Int32(index), double)
|
||||
case .Text(let text):
|
||||
return sqlite3_bind_text(statement, Int32(index), text, -1, SQLITE_TRANSIENT)
|
||||
case .Blob:
|
||||
fatalError("Not implemented")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public class Statement {
|
||||
final func bind(value: DatabaseValueType?, atIndex index: Int) {
|
||||
let code: Int32
|
||||
if let value = value {
|
||||
code = value.bindInSQLiteStatement(sqliteStatement, atIndex: index)
|
||||
code = value.sqliteValue.bindInSQLiteStatement(sqliteStatement, atIndex: index)
|
||||
} else {
|
||||
code = sqlite3_bind_null(sqliteStatement, Int32(index))
|
||||
}
|
||||
|
||||
@@ -209,17 +209,16 @@ struct DatabaseDate: DatabaseValueType {
|
||||
}
|
||||
}
|
||||
|
||||
func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index: Int) -> Int32 {
|
||||
let timestamp = date.timeIntervalSince1970
|
||||
return timestamp.bindInSQLiteStatement(statement, atIndex: index)
|
||||
// Date -> SQLite
|
||||
var sqliteValue: SQLiteValue {
|
||||
return .Double(date.timeIntervalSince1970)
|
||||
}
|
||||
|
||||
// SQLite -> Date
|
||||
static func fromSQLiteValue(value: SQLiteValue) -> DatabaseDate? {
|
||||
switch value {
|
||||
case .Double(let timestamp):
|
||||
if let timestamp = Double.fromSQLiteValue(value) {
|
||||
return self.init(NSDate(timeIntervalSince1970: timestamp))
|
||||
default:
|
||||
// NULL, integer, text or blob:
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user