scheme.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2022 gorse Project Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "database/sql"
  17. "net/url"
  18. "strings"
  19. "github.com/go-sql-driver/mysql"
  20. "github.com/juju/errors"
  21. "github.com/samber/lo"
  22. "github.com/zhenghaoz/gorse/base/log"
  23. "gorm.io/gorm"
  24. "gorm.io/gorm/schema"
  25. "moul.io/zapgorm2"
  26. )
  27. const (
  28. MySQLPrefix = "mysql://"
  29. MongoPrefix = "mongodb://"
  30. MongoSrvPrefix = "mongodb+srv://"
  31. PostgresPrefix = "postgres://"
  32. PostgreSQLPrefix = "postgresql://"
  33. SQLitePrefix = "sqlite://"
  34. RedisPrefix = "redis://"
  35. RedissPrefix = "rediss://"
  36. )
  37. func AppendURLParams(rawURL string, params []lo.Tuple2[string, string]) (string, error) {
  38. parsed, err := url.Parse(rawURL)
  39. if err != nil {
  40. return "", errors.Trace(err)
  41. }
  42. q := parsed.Query()
  43. for _, tuple := range params {
  44. q.Add(tuple.A, tuple.B)
  45. }
  46. parsed.RawQuery = q.Encode()
  47. return parsed.String(), nil
  48. }
  49. func AppendMySQLParams(dsn string, params map[string]string) (string, error) {
  50. cfg, err := mysql.ParseDSN(dsn)
  51. if err != nil {
  52. return "", errors.Trace(err)
  53. }
  54. if cfg.Params == nil {
  55. cfg.Params = make(map[string]string)
  56. }
  57. for key, value := range params {
  58. if _, exist := cfg.Params[key]; !exist {
  59. cfg.Params[key] = value
  60. }
  61. }
  62. return cfg.FormatDSN(), nil
  63. }
  64. func ProbeMySQLIsolationVariableName(dsn string) (string, error) {
  65. connection, err := sql.Open("mysql", dsn)
  66. if err != nil {
  67. return "", errors.Trace(err)
  68. }
  69. defer connection.Close()
  70. rows, err := connection.Query("SHOW VARIABLES LIKE '%isolation%'")
  71. if err != nil {
  72. return "", errors.Trace(err)
  73. }
  74. defer rows.Close()
  75. var name, value string
  76. if rows.Next() {
  77. if err = rows.Scan(&name, &value); err != nil {
  78. return "", errors.Trace(err)
  79. }
  80. }
  81. return name, nil
  82. }
  83. type TablePrefix string
  84. func (tp TablePrefix) ValuesTable() string {
  85. return string(tp) + "values"
  86. }
  87. func (tp TablePrefix) SetsTable() string {
  88. return string(tp) + "sets"
  89. }
  90. func (tp TablePrefix) MessageTable() string {
  91. return string(tp) + "message"
  92. }
  93. func (tp TablePrefix) DocumentTable() string {
  94. return string(tp) + "documents"
  95. }
  96. func (tp TablePrefix) PointsTable() string {
  97. return string(tp) + "time_series_points"
  98. }
  99. func (tp TablePrefix) UsersTable() string {
  100. return string(tp) + "users"
  101. }
  102. func (tp TablePrefix) ItemsTable() string {
  103. return string(tp) + "items"
  104. }
  105. func (tp TablePrefix) FeedbackTable() string {
  106. return string(tp) + "feedback"
  107. }
  108. func (tp TablePrefix) Key(key string) string {
  109. return string(tp) + key
  110. }
  111. func NewGORMConfig(tablePrefix string) *gorm.Config {
  112. return &gorm.Config{
  113. Logger: zapgorm2.New(log.Logger()),
  114. CreateBatchSize: 1000,
  115. SkipDefaultTransaction: true,
  116. NamingStrategy: schema.NamingStrategy{
  117. TablePrefix: tablePrefix,
  118. SingularTable: true,
  119. NameReplacer: strings.NewReplacer(
  120. "SQLValue", "Values",
  121. "SQLSet", "Sets",
  122. "SQLUser", "Users",
  123. "SQLItem", "Items",
  124. "SQLFeedback", "Feedback",
  125. "SQLDocument", "Documents",
  126. "PostgresDocument", "Documents",
  127. "TimeSeriesPoint", "time_series_points",
  128. ),
  129. },
  130. }
  131. }