-
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #70
base: main
Are you sure you want to change the base?
Alias issue #70
Conversation
builder/query_test.go
Outdated
}, | ||
{ | ||
result: "SELECT `c`.`id`,`c`.`name` FROM `contacts` `c`;", | ||
query: rel.Select("c.id").Select("c.name").From("contacts c"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why not using single Select function? seems this change is also not related to the alias fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that is not related to aliases things, but I was thinking it would be beneficial and more convenient for everyone if we bring this small improvement as well.
My issue was to include some extra columns based on the condition, and it was more intuitive. I thought something else was broken, not my code when I was using it:
var query = rel.Select("id", "title").From("pools")
if includeAvgScore {
query.Select("avgScore")
} else {
query.Select("score")
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please feel free to make any changes. That was my opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, then can you create separate PR for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, Now we have alias changes only.
refactor escape func and fix failed tests
Trim spaces just to be more consistent
builder/buffer.go
Outdated
@@ -120,6 +120,19 @@ func (b *Buffer) WriteEscape(value string) { | |||
b.WriteString(b.escape("", value)) | |||
} | |||
|
|||
func (b Buffer) escape_schema(table string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please change to escapeSchema
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
builder/buffer.go
Outdated
} | ||
escaped_table = strings.Join(parts, ".") | ||
escaped_table = b.escape_schema(table[:i]) + " AS " + b.Quoter.ID(strings.TrimSpace(table[i+4:])) | ||
} else if i := strings.Index(strings.ToLower(table), " "); i > -1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will break if the table name contains spaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean "table name contains spaces"? Is it possible?
I just wanted to avoid any issues if someone adds extra spaces like From("books b")
or From("books as b")
.
See TrimSpace in b.Quoter.ID(strings.TrimSpace(table[i+4:]))
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I think I know what you are talking about.
} else if i := strings.Index(strings.ToLower(table), " "); i > -1 {
So you think someone could have something like:
.From("my books")
.From("my books b")
// whereb
is an alias.From("my books AS b")
.From("[my books] b")
I have never saw any examples where table name contains spaces in my lifetime. And I am not sure if such would properly work in any ORM system.
But it is a good point and I am not quite sure how we can identify it. I mean the examples 1 and 2 have no indication whether it is an alias or a table name. It could be either this [my] AS [books]
or this [my books]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or you think we should remove that line and force everyone to use AS
? So the syntax .From("books b")
would not be valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But based on probability people would want to write non space table queries more often rather then those who use spaces. The example with spaces is just some joke. You might as someone if they could define a variable with space:
var my book = rel.From("books")
This is just my opinion. Once we allow to have spaces in table names, we are doomed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who would convert the table name into the variable? Struct? Sorry we must keep some consistency.
It smells like fire to me.
You are talking about something which does not exist, and it shout not exists in back end world.
Please think about the rules of the backend, it must be strong and consistent.
Let me know if I need to change anything in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we forcing people write AS
everywhere because of space in the table name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, please have a look at my last commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not saying we should force user to use AS, but we need to find a good way if we want to support that
As for now, I think bug in example 2 is more critical
Closes #69