doug-martin / goqu

SQL builder and query library for golang

Home Page:http://doug-martin.github.io/goqu/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CTEs do not inherit dialect of the main dataset

sgrishanin opened this issue · comments

Describe the bug
WithDialect changes the dialect of the receiver dataset, but datasets of CTEs inside it keep the default dialect (unless specified explicitly). The resulting query contains multiple incompatible dialects.

To Reproduce

ds := goqu.
        Select("field_1").
        From("table_1").
        Where(goqu.I("field_2").Eq("2"))

// cte with unspecified dialect
cte := goqu.
        Select("field_3").
        From("table_2").
        Where(goqu.I("field_4").Eq("4"))

ds = ds.With("cte", cte)

ds = ds.WithDialect("postgres") // expected to set dialect for the entire query
ds = ds.Prepared(true)

query, _, _ := ds.ToSQL()
fmt.Println(query) // WITH cte AS (SELECT "field_3" FROM "table_2" WHERE ("field_4" = ?)) SELECT "field_1" FROM "table_1" WHERE ("field_2" = $2)

Expected behavior
When the query is being build, I expect it to have a dialect of the main dataset I call .ToSQL() on. I think it should never produce a potentially impossible query.

Dialect:

  • postgres
  • mysql
  • sqlite3

Additional context
Explicitly setting the same dialect on every CTE does the job of course, but it is not obvious and simply cumbersome.

This bit me as well, and the bug that came out of it was very challenging to debug.