懒人记时 代码仓库
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

63 righe
2.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AppTime
  7. {
  8. /**
  9. *
  10. */
  11. class InitDB
  12. {
  13. private DB db;
  14. public InitDB()
  15. {
  16. db = DB.Instance;
  17. }
  18. public void Start()
  19. {
  20. // 创建数据表
  21. db.Execute("CREATE TABLE IF NOT EXISTS \"app\" (\"id\" INTEGER NOT NULL, \"process\" text NOT NULL," +
  22. "\"text\" TEXT NOT NULL DEFAULT process, " +
  23. "\"tagId\" INTEGER NOT NULL DEFAULT(0), " +
  24. "PRIMARY KEY(\"id\") ) WITHOUT ROWID");
  25. db.Execute("CREATE TABLE IF NOT EXISTS \"period\" ( \"timeStart\" DATETIME NOT NULL, \"timeEnd\" DATETIME NOT NULL, " +
  26. "\"winId\" INTEGER NOT NULL," +
  27. "PRIMARY KEY(\"timeStart\")," +
  28. "UNIQUE(\"timeStart\" ASC) )WITHOUT ROWID");
  29. db.Execute("CREATE TABLE IF NOT EXISTS \"tag\" ( \"id\" INTEGER NOT NULL, \"text\" TEXT NOT NULL," +
  30. "PRIMARY KEY(\"id\"), UNIQUE(\"id\" ASC), UNIQUE(\"text\" ASC) ) WITHOUT ROWID");
  31. db.Execute("CREATE TABLE IF NOT EXISTS \"win\" (\"id\" INTEGER NOT NULL," +
  32. "\"appId\" INTEGER NOT NULL, \"text\" TEXT NOT NULL, " +
  33. "\"tagId\" INTEGER NOT NULL DEFAULT(0), " +
  34. "PRIMARY KEY(\"id\") ) WITHOUT ROWID");
  35. // 创建索引
  36. long existIndex = (long)db.ExecuteData("SELECT count(*) FROM sqlite_master WHERE type=\"table\" AND name =\'is_index\'")[0][0];
  37. // 判断是否已经存在索引
  38. if (existIndex == 0)
  39. {
  40. db.Execute("CREATE UNIQUE INDEX \"ix_app\" ON \"app\"( \"process\" ASC )");
  41. db.Execute("CREATE INDEX \"ix_app_tagId\" ON \"app\" ( \"tagId\" ASC )");
  42. db.Execute("CREATE UNIQUE INDEX \"ix_period\" ON \"period\" ( \"timeStart\" ASC, \"timeEnd\" ASC )");
  43. db.Execute("CREATE UNIQUE INDEX \"ix_win\" ON \"win\" ( \"appId\" ASC, \"text\" ASC )");
  44. db.Execute("CREATE INDEX \"ix_win_tagId\" ON \"win\" (\"tagId\" ASC )");
  45. // 此表仅用于标识索引已经创建完成
  46. db.Execute("CREATE TABLE IF NOT EXISTS \"is_index\"( \"id\" INTEGER NOT NULL, PRIMARY KEY(\"id\"))");
  47. }
  48. }
  49. }
  50. }