Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SQLite.swift
- //
- import Foundation
- import SQLite3
- class
- SQLite
- {
- init(url inURL: URL)
- throws
- {
- let result = sqlite3_open(inURL.path, &self.db)
- try throwIfNotOK(result)
- }
- func
- beginTransaction()
- throws
- {
- try exec(sql: "begin")
- }
- func
- commit()
- throws
- {
- try exec(sql: "commit")
- }
- func
- rollback()
- throws
- {
- try exec(sql: "rollback")
- }
- func
- exec(sql inSQL: String)
- throws
- {
- let result = sqlite3_exec(self.db, inSQL, nil, nil, nil)
- try throwIfNotOK(result)
- }
- func
- prepare(sql inSQL: String)
- throws
- {
- if self.stmt != nil
- {
- }
- let result = sqlite3_prepare_v2(self.db, inSQL, -1, &self.stmt, nil)
- try throwIfNotOK(result)
- }
- func
- bind(idx inIdx: Int, val inVal: Int)
- throws
- {
- guard
- let stmt = self.stmt
- else
- {
- throw Errors.noStatementPrepared
- }
- let result = sqlite3_bind_int64(stmt, Int32(inIdx), Int64(inVal))
- try throwIfNotOK(result)
- }
- func
- bind(idx inIdx: Int, val inVal: String)
- throws
- {
- guard
- let stmt = self.stmt
- else
- {
- throw Errors.noStatementPrepared
- }
- print("Binding idx \(inIdx) to text val [\(inVal)]")
- let result = sqlite3_bind_text(stmt, Int32(inIdx), inVal, -1, nil)
- try throwIfNotOK(result)
- }
- func
- bind(idx inIdx: Int, val inVal: Float)
- throws
- {
- try bind(idx: inIdx, val: Double(inVal))
- }
- func
- bind(idx inIdx: Int, val inVal: Double)
- throws
- {
- guard
- let stmt = self.stmt
- else
- {
- throw Errors.noStatementPrepared
- }
- print("Binding idx \(inIdx) to double val [\(inVal)]")
- let result = sqlite3_bind_double(stmt, Int32(inIdx), inVal)
- try throwIfNotOK(result)
- }
- func
- bind(idx inIdx: Int, val inVal: Date)
- throws
- {
- try bind(idx: inIdx, val: inVal.timeIntervalSinceReferenceDate)
- }
- func
- bind(idx inIdx: Int, val inVal: UUID)
- throws
- {
- try bind(idx: inIdx, val: inVal.uuidString)
- }
- // func
- // bind(idx inIdx: Int, val inVal: UUID)
- // throws
- // {
- // guard
- // let stmt = self.stmt
- // else
- // {
- // throw Errors.noStatementPrepared
- // }
- //
- // let result = sqlite3_bind_blob(stmt, Int32(inIdx), d, len) { (inPtr) in
- // <#code#>
- // }
- // try throwIfNotOK(result)
- // }
- func
- step()
- throws
- {
- guard
- let stmt = self.stmt
- else
- {
- throw Errors.noStatementPrepared
- }
- let result = sqlite3_step(stmt)
- if result == SQLITE_DONE // success
- {
- return
- }
- try throwIfNotOK(result)
- }
- func
- reset()
- throws
- {
- guard
- let stmt = self.stmt
- else
- {
- throw Errors.noStatementPrepared
- }
- let result = sqlite3_reset(stmt)
- try throwIfNotOK(result)
- }
- func
- finalize()
- throws
- {
- guard
- let stmt = self.stmt
- else
- {
- throw Errors.noStatementPrepared
- }
- let result = sqlite3_finalize(stmt)
- try throwIfNotOK(result)
- self.stmt = nil
- }
- func
- throwIfNotOK(_ inResult: Int32)
- throws
- {
- if inResult != SQLITE_OK
- {
- let errMsg = String(cString: sqlite3_errmsg(self.db))
- throw Errors.create(inResult, msg: errMsg)
- }
- }
- var
- stmtString: String?
- {
- guard
- let stmt = self.stmt
- else
- {
- return nil
- }
- guard let s = sqlite3_expanded_sql(stmt) else { return nil }
- let count = strlen(s)
- let ss = s.withMemoryRebound(to: Int8.self, capacity: count) { String(cString: $0) }
- sqlite3_free(s)
- return ss
- }
- fileprivate
- var errorMessage: String?
- {
- if let errorPointer = sqlite3_errmsg(self.db)
- {
- let errorMessage = String(cString: errorPointer)
- return errorMessage
- }
- else
- {
- return nil
- }
- }
- var db: OpaquePointer!
- var stmt: OpaquePointer?
- enum
- Errors : Error
- {
- case unknownError(Int, String?)
- case noStatementPrepared
- static
- func
- create(_ inSQLiteError: Int32, msg inMsg: String? = nil)
- -> Errors
- {
- switch (inSQLiteError)
- {
- default:
- return .unknownError(Int(inSQLiteError), inMsg)
- }
- }
- }
- }
- extension
- UnsafeMutablePointer where Pointee == Int8
- {
- var
- string: String
- {
- let count = strlen(self)
- let message = self.withMemoryRebound(to: Int8.self, capacity: count) { String(cString: $0) }
- sqlite3_free(self)
- return message
- }
- }
- extension
- String
- {
- init?(_ inP: UnsafeMutablePointer<Int8>?)
- {
- guard
- let p = inP
- else
- {
- return nil
- }
- self.init(p.string)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement