A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
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.
 
 
 
 
 

54 lines
966 B

  1. package db
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type CreateIndexSqlBuilder struct {
  7. Dialect DialectType
  8. Name string
  9. Table string
  10. Unique bool
  11. Columns []string
  12. }
  13. type DropIndexSqlBuilder struct {
  14. Dialect DialectType
  15. Name string
  16. Table string
  17. }
  18. func (b *CreateIndexSqlBuilder) ToSQL() (string, error) {
  19. var str strings.Builder
  20. str.WriteString("CREATE ")
  21. if b.Unique {
  22. str.WriteString("UNIQUE ")
  23. }
  24. str.WriteString("INDEX ")
  25. str.WriteString(b.Name)
  26. str.WriteString(" on ")
  27. str.WriteString(b.Table)
  28. if len(b.Columns) == 0 {
  29. return "", fmt.Errorf("columns provided for this index: %s", b.Name)
  30. }
  31. str.WriteString(" (")
  32. columnCount := len(b.Columns)
  33. for i, thing := range b.Columns {
  34. str.WriteString(thing)
  35. if i < columnCount-1 {
  36. str.WriteString(", ")
  37. }
  38. }
  39. str.WriteString(")")
  40. return str.String(), nil
  41. }
  42. func (b *DropIndexSqlBuilder) ToSQL() (string, error) {
  43. return fmt.Sprintf("DROP INDEX %s on %s", b.Name, b.Table), nil
  44. }