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

Add column aliasing #1081

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Conversation

adamwulf
Copy link
Contributor

@adamwulf adamwulf commented Sep 2, 2021

Allows Row objects to be created publicly. This makes it easy to convert statements prepared from raw SQL and run with Connection.run() into the same shape as Connection.prepare(anyTableObject)

@jberkel
Copy link
Collaborator

jberkel commented Sep 6, 2021

Why are there 2 pull requests?

@adamwulf
Copy link
Contributor Author

adamwulf commented Sep 6, 2021

I could make them one request if you like, i wasn't sure if both would be accepted

@jberkel
Copy link
Collaborator

jberkel commented Sep 6, 2021

Was just confused because it looked like both PRs had the same changes, but yes, having it in one would make reviewing simpler.

@adamwulf adamwulf mentioned this pull request Sep 6, 2021
@adamwulf
Copy link
Contributor Author

adamwulf commented Sep 6, 2021

You're completely right - I had messed up the PR and included commits in both. i've closed out the other PR, as this one has everything I've done. Thanks!

@jberkel jberkel linked an issue Sep 6, 2021 that may be closed by this pull request
@jberkel
Copy link
Collaborator

jberkel commented Sep 6, 2021

Is your use case aliasing aggregate functions as well? The normal API usage would be something like this:

let name = Expression<String?>("name")
let age = Expression<Int>("age")

let maxAge = age.max
for row in db.prepare(users.select(maxAge).group(name)) {
    print(row[maxAge])
}

And you'd like to write

let maxAge = age.max.alias("maxAge")
for row in db.prepare(users.select(maxAge).group(name)) {
    print(row[maxAge])
}

?

@jberkel jberkel changed the title Fix/public row Add column aliasing Sep 6, 2021
@adamwulf
Copy link
Contributor Author

adamwulf commented Sep 6, 2021

I'm sending query results to a process(results) sort of function that expects to have rows with column names A, B, C, etc. In some cases, i can SELECT A, B, C from foo (or foo.select(A, B, C)). This gives rows that have known column names. in other cases, i'd like to SELECT A, B, max(C) AS C GROUP BY A, B and send those query results to the process() function too. without the alias, column C doesn't exist since it's named something else by default.

Specifically, the aggregate is happening inside a VIEW that's replicating the structure of the underlying table. I'd like to select from either the table or the aggregate view and get the exact same column names back.

With the existing API, the column name for that aggregate method is max(C) instead of just C. Being able to control the column name of the resulting rows is helpful for consistency.

@jberkel
Copy link
Collaborator

jberkel commented Sep 7, 2021

The problem is that the string alias loses type safety, so it's not possible to do this:

let maxAge = age.max.alias("maxAge")

for row in db.prepare(users.select(maxAge).group(name)) {
    print(row[maxAge])
}

@adamwulf
Copy link
Contributor Author

ah interesting. i'm using it to define a view and i alias to an existing column name, and then use that column object to fetch, so something like:

let myView = view.create(mumbleTable.select(colClock.max.alias("existingColumn"), colStuff).group(colStuff))

... then later ...

for row in db.prepare(myView.select(...)) {
    print(row[existingColumn])
}

I'll have time this weekend to look deeper into this and see what to do about the type safety side of it.

@liyuan116
Copy link

let myView = view.create(mumbleTable.select(colClock.max.alias("existingColumn"), colStuff).group(colStuff))

hi @adamwulf if you delete mumbleTable then create a new table renamed mumbleTable the view myView is work ok?

@gzqyl
Copy link

gzqyl commented Jul 13, 2024

let aliasName = columnName, did a trick, finally I found that I do not need alias

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.

How to alias selected columns in swift 2 ?
4 participants