engine.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package compiler
  2. import (
  3. "fmt"
  4. "github.com/kyleconroy/sqlc/internal/config"
  5. "github.com/kyleconroy/sqlc/internal/engine/dolphin"
  6. "github.com/kyleconroy/sqlc/internal/engine/postgresql"
  7. "github.com/kyleconroy/sqlc/internal/engine/sqlite"
  8. "github.com/kyleconroy/sqlc/internal/opts"
  9. "github.com/kyleconroy/sqlc/internal/sql/catalog"
  10. )
  11. type Compiler struct {
  12. conf config.SQL
  13. combo config.CombinedSettings
  14. catalog *catalog.Catalog
  15. parser Parser
  16. result *Result
  17. }
  18. func NewCompiler(conf config.SQL, combo config.CombinedSettings) *Compiler {
  19. c := &Compiler{conf: conf, combo: combo}
  20. switch conf.Engine {
  21. case config.EngineXLemon:
  22. c.parser = sqlite.NewParser()
  23. c.catalog = sqlite.NewCatalog()
  24. case config.EngineMySQL:
  25. c.parser = dolphin.NewParser()
  26. c.catalog = dolphin.NewCatalog()
  27. case config.EnginePostgreSQL:
  28. c.parser = postgresql.NewParser()
  29. c.catalog = postgresql.NewCatalog()
  30. default:
  31. panic(fmt.Sprintf("unknown engine: %s", conf.Engine))
  32. }
  33. return c
  34. }
  35. func (c *Compiler) Catalog() *catalog.Catalog {
  36. return c.catalog
  37. }
  38. func (c *Compiler) ParseCatalog(schema []string) error {
  39. return c.parseCatalog(schema)
  40. }
  41. func (c *Compiler) ParseQueries(queries []string, o opts.Parser) error {
  42. r, err := c.parseQueries(o)
  43. if err != nil {
  44. return err
  45. }
  46. c.result = r
  47. return nil
  48. }
  49. func (c *Compiler) Result() *Result {
  50. return c.result
  51. }