mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
87 lines
2.5 KiB
Swift
87 lines
2.5 KiB
Swift
//
|
|
// SQLite.swift
|
|
// GRDB
|
|
//
|
|
// Created by Gwendal Roué on 02/07/2015.
|
|
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
|
//
|
|
|
|
typealias SQLiteConnection = COpaquePointer
|
|
typealias SQLiteStatement = COpaquePointer
|
|
|
|
public struct SQLiteError : ErrorType {
|
|
public let _domain: String = "GRDB.SQLiteError"
|
|
public let _code: Int
|
|
|
|
public var code: Int { return _code }
|
|
public let message: String?
|
|
public let sql: String?
|
|
|
|
init(code: Int32, message: String? = nil, sql: String? = nil) {
|
|
self._code = Int(code)
|
|
self.message = message
|
|
self.sql = sql
|
|
}
|
|
|
|
init(code: Int32, sqliteConnection: SQLiteConnection, sql: String? = nil) {
|
|
let message: String?
|
|
let cString = sqlite3_errmsg(sqliteConnection)
|
|
if cString == nil {
|
|
message = nil
|
|
} else {
|
|
message = String.fromCString(cString)
|
|
}
|
|
self.init(code: code, message: message, sql: sql)
|
|
}
|
|
|
|
static func checkCResultCode(code: Int32, sqliteConnection: SQLiteConnection, sql: String? = nil) throws {
|
|
if code != SQLITE_OK {
|
|
throw SQLiteError(code: code, sqliteConnection: sqliteConnection, sql: sql)
|
|
}
|
|
}
|
|
}
|
|
|
|
private let SQLITE_TRANSIENT = unsafeBitCast(COpaquePointer(bitPattern: -1), sqlite3_destructor_type.self)
|
|
|
|
public enum SQLiteValue {
|
|
case Null
|
|
case Integer(Int64)
|
|
case Double(Swift.Double)
|
|
case Text(String)
|
|
case Blob
|
|
|
|
public func value() -> DatabaseValueType? {
|
|
switch self {
|
|
case .Null:
|
|
return nil
|
|
case .Integer(let int):
|
|
return int
|
|
case .Double(let double):
|
|
return double
|
|
case .Text(let string):
|
|
return string
|
|
case .Blob:
|
|
fatalError("Not implemented")
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
}
|