Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using default dialect for subquery in FromQuery() #223

Closed
2 of 3 tasks
torwald-sergesson opened this issue May 23, 2020 · 2 comments
Closed
2 of 3 tasks

Using default dialect for subquery in FromQuery() #223

torwald-sergesson opened this issue May 23, 2020 · 2 comments

Comments

@torwald-sergesson
Copy link

torwald-sergesson commented May 23, 2020

Describe the bug
Actually I'm not sure that this is bug but... I found a little confusing behaviour. It happens when I try to use in Insert().FromQuery() a subquery created with simple goqu.From, not with a concrete database dialect.

To Reproduce
If applicable provide a small code snippet to reproduce the issue (it's based on suite from mysql_test.go):

func (mt *mysqlTest) TestInsertFromSelect() {
	ds := mt.db.From("entry")
        // subquery created with goqu, if use here mt.db.Select... it works fine
	subquery := goqu.Select(
		goqu.V(11),
		goqu.V(11),
		goqu.C("float"),
		goqu.C("string"),
		goqu.C("time"),
		goqu.C("bool"),
		goqu.C("bytes"),
	).From(goqu.T("entry")).Where(goqu.C("int").Eq(9))
	query := ds.Insert().Cols().FromQuery(subquery)
	s, _, err := query.ToSQL()
	fmt.Println(s)
	mt.NoError(err)
	_, err = query.Executor().Exec()
	mt.NoError(err)
}

Output:

=== RUN   TestMysqlSuite
=== RUN   TestMysqlSuite/TestInsertFromSelect
INSERT INTO `entry` SELECT 11, 11, "float", "string", "time", "bool", "bytes" FROM "entry" WHERE ("int" = 9)
--- FAIL: TestMysqlSuite (0.03s)
    --- FAIL: TestMysqlSuite/TestInsertFromSelect (0.03s)
        mysql_test.go:400: 
            	Error Trace:	mysql_test.go:400
            	Error:      	Received unexpected error:
            	            	Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"entry" WHERE ("int" = 9)' at line 1
            	Test:       	TestMysqlSuite/TestInsertFromSelect

Here we see that subquery used a default dialect's escaping for columns and table names.

Expected behavior
I expected to have the same dialect as I used in main insert query.

Dialect:

  • postgres (I did not check it on postgres, but I think it will be the same)
  • mysql
  • sqlite3 (SQLite has the same escaping rules as default dialect, so it does not happen)

Additional context
Add any other context about the problem here.

@torwald-sergesson torwald-sergesson changed the title Using default dialect for goqu.T() in FromQuery(). Using default dialect for subquery in FromQuery() May 23, 2020
@torwald-sergesson
Copy link
Author

I guess this happens because subquery during call FromQuery or generator during ToSQL do not override subquery dialect. And I'm not sure shall it be fixed, fail with error that queries used different dialects or just mentioned in docs as a requirement.

@vlanse
Copy link
Contributor

vlanse commented May 23, 2020

IMO it could be convenient to write subqueries without explicit dialect specification, at least it will be less verbose.
The fix could be trivial, just override nested SELECT dialect, something like

// Adds a subquery to the insert. See examples.
func (id *InsertDataset) FromQuery(from exp.AppendableExpression) *InsertDataset {
	if sds, ok := from.(*SelectDataset); ok {
		sds.dialect = id.dialect
	}
	return id.copy(id.clauses.SetFrom(from))
}

@doug-martin what do you think?

doug-martin added a commit that referenced this issue Sep 17, 2020
* [FIXED] SELECT inherits dialect from INSERT in INSERT FROM SELECT.  #229, #223 - @vlanse
* [FIXED] SQLServer dialect: support prepared statements with TOP.  #230, #225 - @vlanse
* [ADDED] IsPrepared to SQLExpression interface.  #231 - @vlanse
@doug-martin doug-martin mentioned this issue Sep 17, 2020
doug-martin added a commit that referenced this issue Sep 17, 2020
* [FIXED] SELECT inherits dialect from INSERT in INSERT FROM SELECT.  #229, #223 - @vlanse
* [FIXED] SQLServer dialect: support prepared statements with TOP.  #230, #225 - @vlanse
* [ADDED] IsPrepared to SQLExpression interface.  #231 - @vlanse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants