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.

120 lines
3.4 KiB

7 years ago
4 years ago
6 years ago
4 years ago
4 years ago
  1. package mysql
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. _ "github.com/go-sql-driver/mysql"
  10. )
  11. const (
  12. CONNECTION_URL_PATTERN = "%s:%s@tcp(%s:%d)/%s?charset=utf8"
  13. )
  14. type SqlGenMysql struct {
  15. createTableSqlTemplate string
  16. dropTableSqlTemplate string
  17. }
  18. var (
  19. _ = abstract_sql.SqlGenerator(&SqlGenMysql{})
  20. )
  21. func (gen *SqlGenMysql) GetSqlInsert(bucket string) string {
  22. return "INSERT INTO filemeta (dirhash,name,directory,meta) VALUES(?,?,?,?)"
  23. }
  24. func (gen *SqlGenMysql) GetSqlUpdate(bucket string) string {
  25. return "UPDATE filemeta SET meta=? WHERE dirhash=? AND name=? AND directory=?"
  26. }
  27. func (gen *SqlGenMysql) GetSqlFind(bucket string) string {
  28. return "SELECT meta FROM filemeta WHERE dirhash=? AND name=? AND directory=?"
  29. }
  30. func (gen *SqlGenMysql) GetSqlDelete(bucket string) string {
  31. return "DELETE FROM filemeta WHERE dirhash=? AND name=? AND directory=?"
  32. }
  33. func (gen *SqlGenMysql) GetSqlDeleteFolderChildren(bucket string) string {
  34. return "DELETE FROM filemeta WHERE dirhash=? AND directory=?"
  35. }
  36. func (gen *SqlGenMysql) GetSqlListExclusive(bucket string) string {
  37. return "SELECT NAME, meta FROM filemeta WHERE dirhash=? AND name>? AND directory=? AND name like ? ORDER BY NAME ASC LIMIT ?"
  38. }
  39. func (gen *SqlGenMysql) GetSqlListInclusive(bucket string) string {
  40. return "SELECT NAME, meta FROM filemeta WHERE dirhash=? AND name>=? AND directory=? AND name like ? ORDER BY NAME ASC LIMIT ?"
  41. }
  42. func (gen *SqlGenMysql) GetSqlCreateTable(bucket string) string {
  43. return fmt.Sprintf(gen.createTableSqlTemplate, bucket)
  44. }
  45. func (gen *SqlGenMysql) GetSqlDropTable(bucket string) string {
  46. return fmt.Sprintf(gen.dropTableSqlTemplate, bucket)
  47. }
  48. func init() {
  49. filer.Stores = append(filer.Stores, &MysqlStore{})
  50. }
  51. type MysqlStore struct {
  52. abstract_sql.AbstractSqlStore
  53. }
  54. func (store *MysqlStore) GetName() string {
  55. return "mysql"
  56. }
  57. func (store *MysqlStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  58. return store.initialize(
  59. configuration.GetString(prefix+"username"),
  60. configuration.GetString(prefix+"password"),
  61. configuration.GetString(prefix+"hostname"),
  62. configuration.GetInt(prefix+"port"),
  63. configuration.GetString(prefix+"database"),
  64. configuration.GetInt(prefix+"connection_max_idle"),
  65. configuration.GetInt(prefix+"connection_max_open"),
  66. configuration.GetInt(prefix+"connection_max_lifetime_seconds"),
  67. configuration.GetBool(prefix+"interpolateParams"),
  68. )
  69. }
  70. func (store *MysqlStore) initialize(user, password, hostname string, port int, database string, maxIdle, maxOpen,
  71. maxLifetimeSeconds int, interpolateParams bool) (err error) {
  72. store.SupportBucketTable = false
  73. store.SqlGenerator = &SqlGenMysql{
  74. createTableSqlTemplate: "",
  75. dropTableSqlTemplate: "drop table %s",
  76. }
  77. sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database)
  78. if interpolateParams {
  79. sqlUrl += "&interpolateParams=true"
  80. }
  81. var dbErr error
  82. store.DB, dbErr = sql.Open("mysql", sqlUrl)
  83. if dbErr != nil {
  84. store.DB.Close()
  85. store.DB = nil
  86. return fmt.Errorf("can not connect to %s error:%v", sqlUrl, err)
  87. }
  88. store.DB.SetMaxIdleConns(maxIdle)
  89. store.DB.SetMaxOpenConns(maxOpen)
  90. store.DB.SetConnMaxLifetime(time.Duration(maxLifetimeSeconds) * time.Second)
  91. if err = store.DB.Ping(); err != nil {
  92. return fmt.Errorf("connect to %s error:%v", sqlUrl, err)
  93. }
  94. return nil
  95. }