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.

66 lines
2.4 KiB

  1. package dameng
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
  5. )
  6. type SqlGenDameng struct {
  7. CreateTableSqlTemplate string
  8. DropTableSqlTemplate string
  9. UpsertQueryTemplate string
  10. }
  11. var (
  12. _ = abstract_sql.SqlGenerator(&SqlGenDameng{})
  13. )
  14. func (gen *SqlGenDameng) GetSqlInsert(tableName string) string {
  15. sql := ""
  16. if gen.UpsertQueryTemplate != "" {
  17. sql = fmt.Sprintf(`MERGE INTO %s AS target
  18. USING (SELECT ? AS dirhash, ? AS name, ? AS directory, ? AS meta FROM dual) AS source
  19. ON (target.dirhash = source.dirhash AND target.name = source.name)
  20. WHEN MATCHED THEN
  21. UPDATE SET target.meta = source.meta
  22. WHEN NOT MATCHED THEN
  23. INSERT (dirhash, name, directory, meta)
  24. VALUES (source.dirhash, source.name, source.directory, source.meta);`, tableName)
  25. } else {
  26. sql = fmt.Sprintf("INSERT INTO %s (dirhash,name,directory,meta) VALUES(?,?,?,?)", tableName)
  27. }
  28. return sql
  29. }
  30. func (gen *SqlGenDameng) GetSqlUpdate(tableName string) string {
  31. return fmt.Sprintf("UPDATE %s SET meta = ? WHERE dirhash = ? AND name = ? AND directory = ?", tableName)
  32. }
  33. func (gen *SqlGenDameng) GetSqlFind(tableName string) string {
  34. return fmt.Sprintf("SELECT meta FROM %s WHERE dirhash = ? AND name = ? AND directory = ?", tableName)
  35. }
  36. func (gen *SqlGenDameng) GetSqlDelete(tableName string) string {
  37. return fmt.Sprintf("DELETE FROM %s WHERE dirhash = ? AND name = ? AND directory = ?", tableName)
  38. }
  39. func (gen *SqlGenDameng) GetSqlDeleteFolderChildren(tableName string) string {
  40. return fmt.Sprintf("DELETE FROM %s WHERE dirhash = ? AND directory = ?", tableName)
  41. }
  42. func (gen *SqlGenDameng) GetSqlListExclusive(tableName string) string {
  43. return fmt.Sprintf("SELECT name, meta FROM %s WHERE dirhash = ? AND rowid > (SELECT IFNULL(MIN(rowid), 0) FROM %s WHERE directory = ? AND name = ?) AND directory = ? ORDER BY rowid ASC LIMIT ?", tableName, tableName)
  44. }
  45. func (gen *SqlGenDameng) GetSqlListInclusive(tableName string) string {
  46. return fmt.Sprintf("SELECT name, meta FROM %s WHERE dirhash = ? AND rowid >= (SELECT IFNULL(MIN(rowid), 0) FROM %s WHERE directory = ? AND name = ?) AND directory = ? ORDER BY rowid ASC LIMIT ?", tableName, tableName)
  47. }
  48. func (gen *SqlGenDameng) GetSqlCreateTable(tableName string) string {
  49. return fmt.Sprintf(gen.CreateTableSqlTemplate, tableName)
  50. }
  51. func (gen *SqlGenDameng) GetSqlDropTable(tableName string) string {
  52. return fmt.Sprintf(gen.DropTableSqlTemplate, tableName)
  53. }