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

Alias issue #69

Closed
fairking opened this issue Apr 16, 2024 · 5 comments · Fixed by #73 · May be fixed by #70
Closed

Alias issue #69

fairking opened this issue Apr 16, 2024 · 5 comments · Fixed by #73 · May be fixed by #70

Comments

@fairking
Copy link

fairking commented Apr 16, 2024

I have found the following issues around aliases.

Example 1

Query:
rel.Select("c.id", "c.name").From("contacts c")
Expected:
SELECT "c"."id","c"."name" FROM "contacts" "c";
Actual:
SELECT "c"."id","c"."name" FROM "contacts c";

Example 2

Query:
rel.Select("c.id", "c.name").From("contacts as c")
Expected:
SELECT "c"."id","c"."name" FROM "contacts" as "c";
Actual:
SELECT "contacts" AS "c","contacts" AS "c" FROM "contacts" AS "c";

Playground

Please find more advanced test query with aliases using the playground: https://go.dev/play/p/u9LoFRsSh2h

package main

import (
	"fmt"

	"github.com/go-rel/rel"
	"github.com/go-rel/rel/where"
	"github.com/go-rel/sql/builder"
)

func main() {
	var query = rel.
		Select("a.id", "a.name", "^COUNT(b.id) as books_count", "^AVG(b.score) as books_score").
		From("authors a").
		JoinWith("LEFT JOIN", "books b", "b.author_id", "a.id").
		Where(where.Or(where.Nil("b.archived"), where.Eq("b.archived", false))).
		Group("a.id", "a.name").
		Sort("a.name")

	var sql, params = queryBuilder.Build(query)

	fmt.Println("Query: ", sql, params)
}

var queryBuilder = builder.Query{
	BufferFactory: builder.BufferFactory{ArgumentPlaceholder: "?", Quoter: builder.Quote{IDPrefix: "`", IDSuffix: "`", IDSuffixEscapeChar: "`", ValueQuote: "'", ValueQuoteEscapeChar: "'"}},
	Filter:        builder.Filter{},
}
fairking pushed a commit to fairking/sql that referenced this issue Apr 16, 2024
This was referenced Apr 16, 2024
@Fs02
Copy link
Member

Fs02 commented Apr 19, 2024

seems the real issue is "Example 2" is generating completely wrong query 🤔
maybe related to cache

@Fs02
Copy link
Member

Fs02 commented Apr 21, 2024

@fairking can you check if your issue is resolved in #73?

@fairking
Copy link
Author

fairking commented Apr 21, 2024

@fairking can you check if your issue is resolved in #73?

Yes, it fixes my issue with aliases:

image

The only one left is this one: go-rel/rel#370

@Fs02 Fs02 closed this as completed in #73 Apr 22, 2024
@fairking
Copy link
Author

fairking commented May 23, 2024

@Fs02 When are you going to release it? Or there something else needs to be completed? I am forced to write raw sql queries in my project because of that alias bug.

@Fs02
Copy link
Member

Fs02 commented May 25, 2024

released https://github.com/go-rel/sql/releases/tag/v0.17.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants