mysql_sql_gen.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package mysql
  2. import (
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
  6. )
  7. type SqlGenMysql struct {
  8. CreateTableSqlTemplate string
  9. DropTableSqlTemplate string
  10. UpsertQueryTemplate string
  11. }
  12. var (
  13. _ = abstract_sql.SqlGenerator(&SqlGenMysql{})
  14. )
  15. func (gen *SqlGenMysql) GetSqlInsert(tableName string) string {
  16. if gen.UpsertQueryTemplate != "" {
  17. return fmt.Sprintf(gen.UpsertQueryTemplate, tableName)
  18. } else {
  19. return fmt.Sprintf("INSERT INTO `%s` (dirhash,name,directory,meta) VALUES(?,?,?,?)", tableName)
  20. }
  21. }
  22. func (gen *SqlGenMysql) GetSqlUpdate(tableName string) string {
  23. return fmt.Sprintf("UPDATE `%s` SET meta=? WHERE dirhash=? AND name=? AND directory=?", tableName)
  24. }
  25. func (gen *SqlGenMysql) GetSqlFind(tableName string) string {
  26. return fmt.Sprintf("SELECT meta FROM `%s` WHERE dirhash=? AND name=? AND directory=?", tableName)
  27. }
  28. func (gen *SqlGenMysql) GetSqlDelete(tableName string) string {
  29. return fmt.Sprintf("DELETE FROM `%s` WHERE dirhash=? AND name=? AND directory=?", tableName)
  30. }
  31. func (gen *SqlGenMysql) GetSqlDeleteFolderChildren(tableName string) string {
  32. return fmt.Sprintf("DELETE FROM `%s` WHERE dirhash=? AND directory=?", tableName)
  33. }
  34. func (gen *SqlGenMysql) GetSqlListExclusive(tableName string) string {
  35. return fmt.Sprintf("SELECT NAME, meta FROM `%s` WHERE dirhash=? AND name>? AND directory=? AND name like ? ORDER BY NAME ASC LIMIT ?", tableName)
  36. }
  37. func (gen *SqlGenMysql) GetSqlListInclusive(tableName string) string {
  38. return fmt.Sprintf("SELECT NAME, meta FROM `%s` WHERE dirhash=? AND name>=? AND directory=? AND name like ? ORDER BY NAME ASC LIMIT ?", tableName)
  39. }
  40. func (gen *SqlGenMysql) GetSqlCreateTable(tableName string) string {
  41. return fmt.Sprintf(gen.CreateTableSqlTemplate, tableName)
  42. }
  43. func (gen *SqlGenMysql) GetSqlDropTable(tableName string) string {
  44. return fmt.Sprintf(gen.DropTableSqlTemplate, tableName)
  45. }