You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.7 KiB

4 years ago
4 years ago
4 years ago
  1. package mysql2
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql"
  10. "github.com/chrislusf/seaweedfs/weed/filer/mysql"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. _ "github.com/go-sql-driver/mysql"
  13. )
  14. const (
  15. CONNECTION_URL_PATTERN = "%s:%s@tcp(%s:%d)/%s?charset=utf8"
  16. )
  17. func init() {
  18. filer.Stores = append(filer.Stores, &MysqlStore2{})
  19. }
  20. type MysqlStore2 struct {
  21. abstract_sql.AbstractSqlStore
  22. }
  23. func (store *MysqlStore2) GetName() string {
  24. return "mysql2"
  25. }
  26. func (store *MysqlStore2) Initialize(configuration util.Configuration, prefix string) (err error) {
  27. return store.initialize(
  28. configuration.GetString(prefix+"createTable"),
  29. configuration.GetString(prefix+"upsertQuery"),
  30. configuration.GetBool(prefix+"enableUpsert"),
  31. configuration.GetString(prefix+"username"),
  32. configuration.GetString(prefix+"password"),
  33. configuration.GetString(prefix+"hostname"),
  34. configuration.GetInt(prefix+"port"),
  35. configuration.GetString(prefix+"database"),
  36. configuration.GetInt(prefix+"connection_max_idle"),
  37. configuration.GetInt(prefix+"connection_max_open"),
  38. configuration.GetInt(prefix+"connection_max_lifetime_seconds"),
  39. configuration.GetBool(prefix+"interpolateParams"),
  40. )
  41. }
  42. func (store *MysqlStore2) initialize(createTable, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database string, maxIdle, maxOpen,
  43. maxLifetimeSeconds int, interpolateParams bool) (err error) {
  44. store.SupportBucketTable = true
  45. if !enableUpsert {
  46. upsertQuery = ""
  47. }
  48. store.SqlGenerator = &mysql.SqlGenMysql{
  49. CreateTableSqlTemplate: createTable,
  50. DropTableSqlTemplate: "drop table `%s`",
  51. UpsertQueryTemplate: upsertQuery,
  52. }
  53. sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database)
  54. adaptedSqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, "<ADAPTED>", hostname, port, database)
  55. if interpolateParams {
  56. sqlUrl += "&interpolateParams=true"
  57. adaptedSqlUrl += "&interpolateParams=true"
  58. }
  59. var dbErr error
  60. store.DB, dbErr = sql.Open("mysql", sqlUrl)
  61. if dbErr != nil {
  62. store.DB.Close()
  63. store.DB = nil
  64. return fmt.Errorf("can not connect to %s error:%v", adaptedSqlUrl, err)
  65. }
  66. store.DB.SetMaxIdleConns(maxIdle)
  67. store.DB.SetMaxOpenConns(maxOpen)
  68. store.DB.SetConnMaxLifetime(time.Duration(maxLifetimeSeconds) * time.Second)
  69. if err = store.DB.Ping(); err != nil {
  70. return fmt.Errorf("connect to %s error:%v", sqlUrl, err)
  71. }
  72. if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil && !strings.Contains(err.Error(), "table already exist") {
  73. return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
  74. }
  75. return nil
  76. }