stat.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package sql
  17. import (
  18. "database/sql"
  19. "strings"
  20. "github.com/siyuan-note/logging"
  21. "github.com/siyuan-note/siyuan/kernel/util"
  22. )
  23. type Stat struct {
  24. Key string `json:"key"`
  25. Val string `json:"value"`
  26. }
  27. func getDatabaseVer() (ret string) {
  28. key := "siyuan_database_ver"
  29. stmt := "SELECT value FROM stat WHERE `key` = ?"
  30. row := db.QueryRow(stmt, key)
  31. if err := row.Scan(&ret); nil != err {
  32. if !strings.Contains(err.Error(), "no such table") {
  33. logging.LogErrorf("query database version failed: %s", err)
  34. }
  35. }
  36. return
  37. }
  38. func setDatabaseVer() {
  39. key := "siyuan_database_ver"
  40. tx, err := beginTx()
  41. if nil != err {
  42. return
  43. }
  44. if err = putStat(tx, key, util.DatabaseVer); nil != err {
  45. return
  46. }
  47. commitTx(tx)
  48. }
  49. func putStat(tx *sql.Tx, key, value string) (err error) {
  50. stmt := "DELETE FROM stat WHERE `key` = '" + key + "'"
  51. if err = execStmtTx(tx, stmt); nil != err {
  52. return
  53. }
  54. stmt = "INSERT INTO stat VALUES ('" + key + "', '" + value + "')"
  55. err = execStmtTx(tx, stmt)
  56. return
  57. }
  58. func getStat(key string) (ret string) {
  59. stmt := "SELECT value FROM stat WHERE `key` = '" + key + "'"
  60. row := queryRow(stmt)
  61. row.Scan(&ret)
  62. return
  63. }