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.

58 lines
1.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package postgres
  2. import (
  3. "fmt"
  4. _ "github.com/lib/pq"
  5. "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
  6. )
  7. type SqlGenPostgres struct {
  8. CreateTableSqlTemplate string
  9. DropTableSqlTemplate string
  10. UpsertQueryTemplate string
  11. }
  12. var (
  13. _ = abstract_sql.SqlGenerator(&SqlGenPostgres{})
  14. )
  15. func (gen *SqlGenPostgres) 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($1,$2,$3,$4)`, tableName)
  20. }
  21. }
  22. func (gen *SqlGenPostgres) GetSqlUpdate(tableName string) string {
  23. return fmt.Sprintf(`UPDATE "%s" SET meta=$1 WHERE dirhash=$2 AND name=$3 AND directory=$4`, tableName)
  24. }
  25. func (gen *SqlGenPostgres) GetSqlFind(tableName string) string {
  26. return fmt.Sprintf(`SELECT meta FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName)
  27. }
  28. func (gen *SqlGenPostgres) GetSqlDelete(tableName string) string {
  29. return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName)
  30. }
  31. func (gen *SqlGenPostgres) GetSqlDeleteFolderChildren(tableName string) string {
  32. return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND directory=$2`, tableName)
  33. }
  34. func (gen *SqlGenPostgres) GetSqlListExclusive(tableName string) string {
  35. return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND name>$2 AND directory=$3 AND name like $4 ORDER BY NAME ASC LIMIT $5`, tableName)
  36. }
  37. func (gen *SqlGenPostgres) GetSqlListInclusive(tableName string) string {
  38. return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND name>=$2 AND directory=$3 AND name like $4 ORDER BY NAME ASC LIMIT $5`, tableName)
  39. }
  40. func (gen *SqlGenPostgres) GetSqlCreateTable(tableName string) string {
  41. return fmt.Sprintf(gen.CreateTableSqlTemplate, tableName)
  42. }
  43. func (gen *SqlGenPostgres) GetSqlDropTable(tableName string) string {
  44. return fmt.Sprintf(gen.DropTableSqlTemplate, tableName)
  45. }