1
0

SQLite.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // SQLite.swift
  3. // Swifter
  4. //
  5. // Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import Foundation
  8. public enum SQLiteError: ErrorType {
  9. case OpenFailed(String?)
  10. case ExecFailed(String?)
  11. case BindFailed(String?)
  12. }
  13. public class SQLite {
  14. private let databaseConnection: COpaquePointer
  15. public static func open(path: String) throws -> SQLite {
  16. var databaseConnection = COpaquePointer()
  17. let openResult = path.withCString { sqlite3_open($0, &databaseConnection) }
  18. guard openResult == SQLITE_OK else {
  19. throw SQLiteError.OpenFailed(String.fromCString(sqlite3_errmsg(databaseConnection)))
  20. }
  21. return SQLite(databaseConnection)
  22. }
  23. private init(_ databaseConnection: COpaquePointer) {
  24. self.databaseConnection = databaseConnection
  25. }
  26. private struct ExecCContext { var callback: ([String: String] -> Void)? }
  27. public func exec(sql: String) throws {
  28. try exec(sql, nil)
  29. }
  30. public func exec(sql: String, _ params: [String?]? = nil, _ step: ([String: String?] -> Void)? = nil) throws {
  31. var statement = COpaquePointer()
  32. let prepareResult = sql.withCString { sqlite3_prepare_v2(databaseConnection, $0, Int32(sql.utf8.count), &statement, nil) }
  33. guard prepareResult == SQLITE_OK else {
  34. throw SQLiteError.ExecFailed(String.fromCString(sqlite3_errmsg(databaseConnection)))
  35. }
  36. for (index, value) in (params ?? [String?]()).enumerate() {
  37. let bindResult = value?.withCString({ sqlite3_bind_text(statement, index + 1, $0, -1 /* take zero terminator. */) { _ in } })
  38. ?? sqlite3_bind_null(statement, index + 1)
  39. guard bindResult == SQLITE_OK else {
  40. throw SQLiteError.BindFailed(String.fromCString(sqlite3_errmsg(databaseConnection)))
  41. }
  42. }
  43. while true {
  44. let stepResult = sqlite3_step(statement)
  45. switch stepResult {
  46. case SQLITE_ROW:
  47. var content = [String: String?]()
  48. for i in 0..<sqlite3_column_count(statement) {
  49. if let name = String.fromCString(UnsafePointer<CChar>(sqlite3_column_name(statement, i))) {
  50. let pointer = sqlite3_column_text(statement, i)
  51. if pointer == nil {
  52. content[name] = nil
  53. } else {
  54. content[name] = String.fromCString(UnsafePointer<CChar>(pointer))
  55. }
  56. }
  57. }
  58. step?(content)
  59. case SQLITE_DONE:
  60. return
  61. case SQLITE_ERROR:
  62. throw SQLiteError.ExecFailed("sqlite3_step() returned SQLITE_ERROR.")
  63. default:
  64. throw SQLiteError.ExecFailed("Unknown result for sqlite3_step(): \(stepResult)")
  65. }
  66. }
  67. }
  68. public func enumerate(sql: String) throws -> StatmentSequence {
  69. var statement = COpaquePointer()
  70. let prepareResult = sql.withCString { sqlite3_prepare_v2(databaseConnection, $0, Int32(sql.utf8.count), &statement, nil) }
  71. guard prepareResult == SQLITE_OK else {
  72. throw SQLiteError.ExecFailed(String.fromCString(sqlite3_errmsg(databaseConnection)))
  73. }
  74. return StatmentSequence(statement: statement)
  75. }
  76. public struct StatmentGenerator: GeneratorType {
  77. public let statement: COpaquePointer
  78. public func next() -> [String: String]? {
  79. switch sqlite3_step(statement) {
  80. case SQLITE_ROW:
  81. var content = [String: String]()
  82. for i in 0..<sqlite3_column_count(statement) {
  83. if let name = String.fromCString(UnsafePointer<CChar>(sqlite3_column_name(statement, i))) {
  84. let pointer = sqlite3_column_text(statement, i)
  85. if pointer == nil {
  86. content[name] = nil
  87. } else {
  88. content[name] = String.fromCString(UnsafePointer<CChar>(pointer))
  89. }
  90. }
  91. }
  92. return content
  93. default:
  94. sqlite3_finalize(statement)
  95. return nil
  96. }
  97. }
  98. }
  99. public struct StatmentSequence: SequenceType {
  100. public let statement: COpaquePointer
  101. public func generate() -> StatmentGenerator {
  102. return StatmentGenerator(statement: statement)
  103. }
  104. }
  105. public func close() {
  106. sqlite3_close(databaseConnection)
  107. }
  108. }