CreateTables.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // CreateTables.swift
  3. // O2OA
  4. //
  5. // Created by FancyLou on 2019/9/15.
  6. // Copyright © 2019 O2OA. All rights reserved.
  7. //
  8. import FMDB
  9. import CocoaLumberjack
  10. class CreateTables {
  11. public static let TBALE_NAME_RUN_USER = "o2_runUser"
  12. public static let TBALE_NAME_RUN_TRACK = "o2_runTrack"
  13. public static let TBALE_NAME_RUN_TRACK_POINT = "o2_runTrackPoint"
  14. public static let TBALE_NAME_O2_APP = "o2_appInfo"
  15. public static let TBALE_NAME_O2_CLOUD_FILE = "o2_cloud_file"
  16. public static let TABLE_NAME_O2_CONTACT_FAV = "o2_contact_favorite"
  17. private var dbQueue: FMDatabaseQueue!
  18. init(dbQueue: FMDatabaseQueue) {
  19. self.dbQueue = dbQueue
  20. }
  21. func createTables() {
  22. DDLogDebug("begin create tables 。。。。")
  23. self.createTableIfNotExist(tableName: CreateTables.TBALE_NAME_RUN_USER)
  24. self.createTableIfNotExist(tableName: CreateTables.TBALE_NAME_RUN_TRACK)
  25. self.createTableIfNotExist(tableName: CreateTables.TBALE_NAME_RUN_TRACK_POINT)
  26. self.createTableIfNotExist(tableName: CreateTables.TBALE_NAME_O2_APP)
  27. self.createTableIfNotExist(tableName: CreateTables.TBALE_NAME_O2_CLOUD_FILE)
  28. self.createTableIfNotExist(tableName: CreateTables.TABLE_NAME_O2_CONTACT_FAV)
  29. DDLogDebug("end create tables 。。。。")
  30. }
  31. private func createTableIfNotExist(tableName: String) {
  32. self.dbQueue.inDatabase { (db) in
  33. if !self.checkTableExist(tableName: tableName, db: db) {
  34. let createSql = self.createTableSqlByTableName(tableName: tableName)
  35. if createSql != "" {
  36. if let result = db?.executeUpdate(createSql, withArgumentsIn: []) {
  37. if result {
  38. DDLogDebug("创建表\(tableName) 成功!")
  39. } else {
  40. DDLogDebug("创建表\(tableName) 失败!")
  41. }
  42. } else {
  43. DDLogDebug("创建表\(tableName) 失败!")
  44. }
  45. } else {
  46. DDLogDebug("创建表\(tableName) 失败 没有sql语句!")
  47. }
  48. } else {
  49. DDLogDebug("表\(tableName) 已存在!!")
  50. }
  51. }
  52. }
  53. private func checkTableExist(tableName: String, db: FMDatabase?) -> Bool {
  54. let sql = self.checkTableNameSql(tableName: tableName)
  55. var c = 0
  56. if let result = db?.executeQuery(sql, withArgumentsIn: []) {
  57. if result.next() {
  58. c = result.long(forColumnIndex: 0)
  59. }
  60. result.close()
  61. }
  62. DDLogDebug("table exist, count: \(c)")
  63. return c > 0
  64. }
  65. private func createTableSqlByTableName(tableName: String) -> String {
  66. switch tableName {
  67. case CreateTables.TBALE_NAME_RUN_USER:
  68. return self.createO2RunUserTableSql()
  69. case CreateTables.TBALE_NAME_RUN_TRACK:
  70. return self.createO2RunTrackTableSql()
  71. case CreateTables.TBALE_NAME_RUN_TRACK_POINT:
  72. return self.createO2RunTrackPointTableSql()
  73. case CreateTables.TBALE_NAME_O2_APP:
  74. return self.createO2AppTableSql()
  75. case CreateTables.TBALE_NAME_O2_CLOUD_FILE:
  76. return self.createO2CloudFileSql()
  77. case CreateTables.TABLE_NAME_O2_CONTACT_FAV:
  78. return self.createO2ContactFavoriteSql()
  79. default:
  80. return ""
  81. }
  82. }
  83. private func checkTableNameSql(tableName: String) -> String {
  84. return "SELECT COUNT(*) FROM sqlite_master where type=\"table\" and name=\"\(tableName)\""
  85. }
  86. private func createO2RunUserTableSql() -> String {
  87. return "CREATE TABLE IF NOT EXISTS \(CreateTables.TBALE_NAME_RUN_USER) (\"userDN\" TEXT PRIMARY KEY, \"weight\" REAL,\"totalDistance\" REAL,\"totalRunTimes\" INTEGER,\"totalUseTime\" INTEGER,\"totalCalories\" INTEGER ,\"updateTime\" INTEGER )"
  88. }
  89. private func createO2RunTrackTableSql() -> String {
  90. return "CREATE TABLE IF NOT EXISTS \(CreateTables.TBALE_NAME_RUN_TRACK) ( \"id\" TEXT PRIMARY KEY, \"userDN\" TEXT NOT NULL, \"runDate\" INTEGER NOT NULL,\"startTime\" INTEGER NOT NULL,\"endTime\" INTEGER NOT NULL,\"useTime\" INTEGER ,\"createTime\" INTEGER ,\"distance\" REAL,\"calories\" INTEGER,\"stepNumber\" INTEGER,\"speed\" REAL,\"entityName\" TEXT)"
  91. }
  92. private func createO2RunTrackPointTableSql() -> String {
  93. return "CREATE TABLE IF NOT EXISTS \(CreateTables.TBALE_NAME_RUN_TRACK_POINT) ( \"id\" TEXT PRIMARY KEY , \"runId\" TEXT NOT NULL,\"pointTime\" INTEGER ,\"createTime\" INTEGER ,\"longitude\" REAL, \"latitude\" REAL )"
  94. }
  95. private func createO2AppTableSql() -> String {
  96. return "create table IF NOT EXISTS \(CreateTables.TBALE_NAME_O2_APP) (`id` integer primary key autoincrement,`title` text not null,`appid` text not null,`storyboard` text,`vcname` text,`segueidentifier` text,`normalicon` text,`selectedicon` text,`order` integer,`mainorder` integer,`categorytype` integer)"
  97. }
  98. private func createO2CloudFileSql() -> String {
  99. return "create table IF NOT EXISTS \(CreateTables.TBALE_NAME_O2_CLOUD_FILE) (`fileid` TEXT PRIMARY KEY,`filename` text not null,`filepath` text not null,`fileext` text)"
  100. }
  101. private func createO2ContactFavoriteSql() -> String {
  102. return "create table IF NOT EXISTS \(CreateTables.TABLE_NAME_O2_CONTACT_FAV) ( `id` TEXT NOT NULL, `distinguishedName` TEXT NOT NULL, `employee` TEXT, `genderType` TEXT, `lastLoginAddress` TEXT, `lastLoginClient` TEXT, `lastLoginTime` TEXT, `mail` TEXT, `mobile` TEXT, `name` TEXT, `changePasswordTime` TEXT, `superior` TEXT, `signature` TEXT, `pinyin` TEXT, `pinyinInitial` TEXT, `qq` TEXT, `unique` TEXT, `updateTime` TEXT, `weixin` TEXT, `officePhone` TEXT, `boardDate` TEXT, `birthday` TEXT, `orderNumber` INTEGER , `ownerid` TEXT NOT NULL)"
  103. }
  104. }