JCVerificationInfoDB.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // JCContacterDBManager.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/5/4.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import FMDB
  10. import JMessage
  11. final class JCVerificationInfoDB: NSObject {
  12. static let shareInstance = JCVerificationInfoDB()
  13. var queue: FMDatabaseQueue?
  14. private override init() {
  15. super.init()
  16. _createDB()
  17. }
  18. private func _createDB() {
  19. let user = JMSGUser.myInfo()
  20. if user.username.isEmpty {
  21. return
  22. }
  23. let dir = user.username + user.appKey!
  24. var path = "\(NSHomeDirectory())/Documents/\(dir)"
  25. if createPath(path) {
  26. path = path + "/VerificationInfo.db"
  27. print(path)
  28. queue = FMDatabaseQueue(path: path)
  29. let sql = "create table IF NOT EXISTS VerificationInfo(id integer primary key autoincrement,username text not null,appkey text not null,nickname text,resaon text,state integer)"
  30. queue?.inDatabase { (db) in
  31. db?.executeUpdate(sql, withArgumentsIn: nil)
  32. }
  33. }
  34. }
  35. func dropTable() {
  36. if queue == nil {
  37. _createDB()
  38. dropTable()
  39. }
  40. let sql = "drop table if exists VerificationInfo"
  41. queue?.inDatabase { (db) in
  42. db?.executeUpdate(sql, withArgumentsIn: nil)
  43. }
  44. }
  45. func updateData(_ info: JCVerificationInfo) {
  46. if queue == nil {
  47. _createDB()
  48. insertData(info)
  49. }
  50. let sql = "UPDATE VerificationInfo SET nickname='\(info.nickname)', state = \(String(describing: info.state)) WHERE username='\(info.username)' and appkey='\(info.appkey)' and state != 2"
  51. queue?.inDatabase { (db) in
  52. db?.executeUpdate(sql, withArgumentsIn: nil)
  53. }
  54. }
  55. func insertData(_ info: JCVerificationInfo) {
  56. if queue == nil {
  57. _createDB()
  58. insertData(info)
  59. }
  60. let delSql = "delete from VerificationInfo where username='\(info.username)' and appkey='\(info.appkey)' and state=\(String(describing: info.state))"
  61. queue?.inDatabase { (db) in
  62. db?.executeUpdate(delSql, withArgumentsIn: nil)
  63. }
  64. let sql = "insert into VerificationInfo (username,nickname,appkey,resaon,state) values ('\(info.username)','\(info.nickname)','\(info.appkey)','\(info.resaon)',\(String(describing: info.state)))"
  65. queue?.inDatabase { (db) in
  66. db?.executeUpdate(sql, withArgumentsIn: nil)
  67. }
  68. }
  69. func delete(_ info: JCVerificationInfo) {
  70. if queue == nil {
  71. _createDB()
  72. insertData(info)
  73. }
  74. let delSql = "delete from VerificationInfo where id=\(String(describing: info.id))"
  75. queue?.inDatabase { (db) in
  76. db?.executeUpdate(delSql, withArgumentsIn: nil)
  77. }
  78. }
  79. func quaryData() -> [JCVerificationInfo] {
  80. if queue == nil {
  81. _createDB()
  82. let _ = quaryData()
  83. }
  84. var infos: [JCVerificationInfo] = []
  85. let sql = "select * from VerificationInfo ORDER BY id DESC"
  86. queue?.inDatabase { (db) in
  87. let resultSet = db?.executeQuery(sql, withArgumentsIn: nil)
  88. while (resultSet?.next())! {
  89. let info = JCVerificationInfo()
  90. info.id = Int((resultSet?.int(forColumn: "id"))!)
  91. info.username = (resultSet?.string(forColumn: "username"))!
  92. info.nickname = (resultSet?.string(forColumn: "nickname"))!
  93. info.appkey = (resultSet?.string(forColumn: "appkey"))!
  94. info.resaon = (resultSet?.string(forColumn: "resaon"))!
  95. info.state = Int((resultSet?.int(forColumn: "state"))!)
  96. infos.append(info)
  97. }
  98. }
  99. return infos
  100. }
  101. // MARK: - private func
  102. private func createPath(_ path: String) -> Bool {
  103. var isDir: ObjCBool = ObjCBool(false)
  104. if FileManager.default.fileExists(atPath: path, isDirectory: &isDir) && isDir.boolValue {
  105. return true
  106. } else {
  107. try! FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
  108. return true
  109. }
  110. }
  111. }