@@ -16,6 +16,7 @@ import (
1616 "github.com/kyleconroy/sqlc/internal/dinosql/kotlin"
1717 "github.com/kyleconroy/sqlc/internal/multierr"
1818 "github.com/kyleconroy/sqlc/internal/mysql"
19+ "github.com/kyleconroy/sqlc/internal/pg"
1920)
2021
2122const errMessageNoVersion = `The configuration file must have a version number.
@@ -43,7 +44,7 @@ type outPair struct {
4344 config.SQL
4445}
4546
46- func Generate (dir string , stderr io.Writer ) (map [string ]string , error ) {
47+ func Generate (e Env , dir string , stderr io.Writer ) (map [string ]string , error ) {
4748 var yamlMissing , jsonMissing bool
4849 yamlPath := filepath .Join (dir , "sqlc.yaml" )
4950 jsonPath := filepath .Join (dir , "sqlc.json" )
@@ -135,7 +136,7 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
135136 name = combo .Kotlin .Package
136137 }
137138
138- result , errored = parse (name , dir , sql .SQL , combo , parseOpts , stderr )
139+ result , errored = parse (e , name , dir , sql .SQL , combo , parseOpts , stderr )
139140 if errored {
140141 break
141142 }
@@ -173,7 +174,40 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
173174 return output , nil
174175}
175176
176- func parse (name , dir string , sql config.SQL , combo config.CombinedSettings , parserOpts dinosql.ParserOpts , stderr io.Writer ) (dinosql.Generateable , bool ) {
177+ type postgreEngine interface {
178+ ParseCatalog ([]string ) error
179+ ParseQueries ([]string , dinosql.ParserOpts ) error
180+ Result () dinosql.Generateable
181+ }
182+
183+ type dinosqlEngine struct {
184+ catalog pg.Catalog
185+ result * dinosql.Result
186+ }
187+
188+ func (d * dinosqlEngine ) ParseCatalog (schema []string ) error {
189+ c , err := dinosql .ParseCatalog (schema )
190+ if err != nil {
191+ return err
192+ }
193+ d .catalog = c
194+ return nil
195+ }
196+
197+ func (d * dinosqlEngine ) ParseQueries (queries []string , opts dinosql.ParserOpts ) error {
198+ q , err := dinosql .ParseQueries (d .catalog , queries , opts )
199+ if err != nil {
200+ return err
201+ }
202+ d .result = q
203+ return nil
204+ }
205+
206+ func (d * dinosqlEngine ) Result () dinosql.Generateable {
207+ return & kotlin.Result {Result : d .result }
208+ }
209+
210+ func parse (e Env , name , dir string , sql config.SQL , combo config.CombinedSettings , parserOpts dinosql.ParserOpts , stderr io.Writer ) (dinosql.Generateable , bool ) {
177211 switch sql .Engine {
178212 case config .EngineMySQL :
179213 // Experimental MySQL support
@@ -192,8 +226,13 @@ func parse(name, dir string, sql config.SQL, combo config.CombinedSettings, pars
192226 return q , false
193227
194228 case config .EnginePostgreSQL :
195- c , err := dinosql .ParseCatalog (sql .Schema )
196- if err != nil {
229+ var eng postgreEngine
230+ if e .ExperimentalParser {
231+ eng = compiler .NewEngine (sql , combo )
232+ } else {
233+ eng = & dinosqlEngine {}
234+ }
235+ if err := eng .ParseCatalog (sql .Schema ); err != nil {
197236 fmt .Fprintf (stderr , "# package %s\n " , name )
198237 if parserErr , ok := err .(* multierr.Error ); ok {
199238 for _ , fileErr := range parserErr .Errs () {
@@ -204,9 +243,7 @@ func parse(name, dir string, sql config.SQL, combo config.CombinedSettings, pars
204243 }
205244 return nil , true
206245 }
207-
208- q , err := dinosql .ParseQueries (c , sql .Queries , parserOpts )
209- if err != nil {
246+ if err := eng .ParseQueries (sql .Queries , parserOpts ); err != nil {
210247 fmt .Fprintf (stderr , "# package %s\n " , name )
211248 if parserErr , ok := err .(* multierr.Error ); ok {
212249 for _ , fileErr := range parserErr .Errs () {
@@ -217,7 +254,7 @@ func parse(name, dir string, sql config.SQL, combo config.CombinedSettings, pars
217254 }
218255 return nil , true
219256 }
220- return & kotlin .Result { Result : q } , false
257+ return eng .Result () , false
221258
222259 case config .EngineXLemon , config .EngineXDolphin , config .EngineXElephant :
223260 r , err := compiler .Run (sql , combo )
0 commit comments