-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
How to alias selected columns in swift 2 ? #225
Comments
Aliasing functionality has been limited to tables during the rewrite, where the functionality is required for joins to work. Aliasing column/expressions doesn't seem to help in our expression layer, although I may have missed a use case. Would you please provide your use case? |
My use case: I'm selecting all columns in table a, joining table b and selecting just one column from table b that happens to share the name of a column in table a. If I want to access the that column in table b, it means all of my existing models/queries that reference the column of the same name in table a needs to be updated to support namespacing for just this one scenario. If I were able to select the column in table b aliased to a different name, all of my existing code would work without modification. Essentially I'd like the ability to build this query: |
My experimentation showed that these aliases are actually meaningless outside of the SQLite console. You should be able do do this, though: let a = Table("a")
let b = Table("b")
for row in db.prepare(a.join(b, on: condition)) {
let aName = row[a[name]]
let bName = row[b[name]]
} It doesn't limit table |
So what I'm actually doing is a little closer to this: class Model {
let name: String
init(row: Row) {
self.name = row[name]
}
}
let a = Table("a")
let b = Table("b")
for row in db.prepare(a.join(b, on: condition)) {
let model = Model(row)
// Some how use b[name]
} I initially tried to just update model to do fatal error: no such column '"a"."name"' in columns: [""name"", ... ] If this is supposed to work, than I suppose that solution would be ok, but it would be easier if I could do something along the lines of: let a = Table("a")
let b = Table("b")
let otherName = b[name].alias("otherName")
for row in db.prepare(a.select(a[*], otherName).join(b, on: condition)) {
let model = Model(row)
let bName = row[otherName]
} This would allow me to isolate this specific bit of code to where I need it, and allow everything else to remain as it is for when namespacing isn't necessary. |
My use case is providing query results to other iOS Apps via an External API. There is a person table and a measurement table. There are many measurements for any one person, and the column person_oid joins them. For one particular query to the external Apps, I need to provide for each person the quantity of measurements associated with that person (along with the other columns of a person record). Since this is an external API and some external App that I did not write is getting the results, I want to provide that count with a nice easily understood alias column name rather some default column name including the function. My SQL statement that works quite well on Android SQLite and a server-based mySQL: I can just about build the equivalent query in SQLite.swift, except for the alias.
Of course what I would like is the ability to do something like: Looking at the Expression.swift file, in
And indeed in a fork of the SQLite.swift source code that I'm using in developing my iOS App, I added said 4 lines of code, and it works properly.
resulted in: |
Another place this is useful is when trying to use Codable for the result -- trying to get a CodingKeys case to satisfy the generated Table.Column name is very painful (maybe impossible?). I ended up just creating the objects manually instead |
_extension ExpressionType {
}_ This is not working for me. Error: join' is inaccessible due to 'internal' protection level |
any movement on this? I have the same issue as #225 (comment) and would like to provide specific column names to aggregate columns |
On branch swift_2.0, alias func was removed in Expression.swift. I dont see any document about the new way to alias columns.
Thanks.
The text was updated successfully, but these errors were encountered: