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 #70

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Alias issue #70

wants to merge 6 commits into from

Conversation

fairking
Copy link

Closes #69

},
{
result: "SELECT `c`.`id`,`c`.`name` FROM `contacts` `c`;",
query: rel.Select("c.id").Select("c.name").From("contacts c"),
Copy link
Member

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?

Copy link
Author

@fairking fairking Apr 17, 2024

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")
}

Copy link
Author

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.

Copy link
Member

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?

Copy link
Author

@fairking fairking Apr 18, 2024

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
@fairking fairking changed the title Alias issue #69 Alias issue Apr 18, 2024
Trim spaces just to be more consistent
@@ -120,6 +120,19 @@ func (b *Buffer) WriteEscape(value string) {
b.WriteString(b.escape("", value))
}

func (b Buffer) escape_schema(table string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change to escapeSchema

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
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 {
Copy link
Member

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

Copy link
Author

@fairking fairking Apr 19, 2024

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:])).

Copy link
Author

@fairking fairking Apr 19, 2024

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:

  1. .From("my books")
  2. .From("my books b") // where b is an alias
  3. .From("my books AS b")
  4. .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]

Copy link
Author

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.

Copy link
Author

@fairking fairking Apr 19, 2024

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.

Copy link
Author

@fairking fairking Apr 19, 2024

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.

Copy link
Author

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?

Copy link
Author

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.

Copy link
Member

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

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 this pull request may close these issues.

Alias issue
2 participants