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.
 
 
 
 
 

77 lines
2.3 KiB

  1. package db
  2. import "fmt"
  3. type DialectType int
  4. const (
  5. DialectSQLite DialectType = iota
  6. DialectMySQL DialectType = iota
  7. )
  8. func (d DialectType) Column(name string, t ColumnType, size OptionalInt) *Column {
  9. switch d {
  10. case DialectSQLite:
  11. return &Column{Dialect: DialectSQLite, Name: name, Type: t, Size: size}
  12. case DialectMySQL:
  13. return &Column{Dialect: DialectMySQL, Name: name, Type: t, Size: size}
  14. default:
  15. panic(fmt.Sprintf("unexpected dialect: %d", d))
  16. }
  17. }
  18. func (d DialectType) Table(name string) *CreateTableSqlBuilder {
  19. switch d {
  20. case DialectSQLite:
  21. return &CreateTableSqlBuilder{Dialect: DialectSQLite, Name: name}
  22. case DialectMySQL:
  23. return &CreateTableSqlBuilder{Dialect: DialectMySQL, Name: name}
  24. default:
  25. panic(fmt.Sprintf("unexpected dialect: %d", d))
  26. }
  27. }
  28. func (d DialectType) AlterTable(name string) *AlterTableSqlBuilder {
  29. switch d {
  30. case DialectSQLite:
  31. return &AlterTableSqlBuilder{Dialect: DialectSQLite, Name: name}
  32. case DialectMySQL:
  33. return &AlterTableSqlBuilder{Dialect: DialectMySQL, Name: name}
  34. default:
  35. panic(fmt.Sprintf("unexpected dialect: %d", d))
  36. }
  37. }
  38. func (d DialectType) CreateUniqueIndex(name, table string, columns ...string) *CreateIndexSqlBuilder {
  39. switch d {
  40. case DialectSQLite:
  41. return &CreateIndexSqlBuilder{Dialect: DialectSQLite, Name: name, Table: table, Unique: true, Columns: columns}
  42. case DialectMySQL:
  43. return &CreateIndexSqlBuilder{Dialect: DialectMySQL, Name: name, Table: table, Unique: true, Columns: columns}
  44. default:
  45. panic(fmt.Sprintf("unexpected dialect: %d", d))
  46. }
  47. }
  48. func (d DialectType) CreateIndex(name, table string, columns ...string) *CreateIndexSqlBuilder {
  49. switch d {
  50. case DialectSQLite:
  51. return &CreateIndexSqlBuilder{Dialect: DialectSQLite, Name: name, Table: table, Unique: false, Columns: columns}
  52. case DialectMySQL:
  53. return &CreateIndexSqlBuilder{Dialect: DialectMySQL, Name: name, Table: table, Unique: false, Columns: columns}
  54. default:
  55. panic(fmt.Sprintf("unexpected dialect: %d", d))
  56. }
  57. }
  58. func (d DialectType) DropIndex(name, table string) *DropIndexSqlBuilder {
  59. switch d {
  60. case DialectSQLite:
  61. return &DropIndexSqlBuilder{Dialect: DialectSQLite, Name: name, Table: table}
  62. case DialectMySQL:
  63. return &DropIndexSqlBuilder{Dialect: DialectMySQL, Name: name, Table: table}
  64. default:
  65. panic(fmt.Sprintf("unexpected dialect: %d", d))
  66. }
  67. }