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

Fix/column alias #1080

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/SQLite/Typed/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ extension ExpressionType {
" ".join([self, Expression<Void>(literal: "DESC")])
}

public func alias(_ aliasName: String) -> Expressible {
return " ".join([self, Expression<Void>(literal: "AS \"\(aliasName)\"")])
}
}

extension ExpressionType where UnderlyingType: Value {
Expand Down
18 changes: 9 additions & 9 deletions Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ public struct Row {
}

guard let idx = columnNames[column.template] else {
let similar = Array(columnNames.keys).filter { $0.hasSuffix(".\(column.template)") }
let similar = Array(columnNames.keys).filter { $0.hasSuffix(".\(column.template)") || $0.hasSuffix(" AS \(column.template)") }

switch similar.count {
case 0:
Expand Down Expand Up @@ -1228,21 +1228,21 @@ public enum OnConflict: String {

public struct QueryClauses {

var select = (distinct: false, columns: [Expression<Void>(literal: "*") as Expressible])
public internal(set) var select = (distinct: false, columns: [Expression<Void>(literal: "*") as Expressible])

var from: (name: String, alias: String?, database: String?)
public internal(set) var from: (name: String, alias: String?, database: String?)

var join = [(type: JoinType, query: QueryType, condition: Expressible)]()
public internal(set) var join = [(type: JoinType, query: QueryType, condition: Expressible)]()

var filters: Expression<Bool?>?
public internal(set) var filters: Expression<Bool?>?

var group: (by: [Expressible], having: Expression<Bool?>?)?
public internal(set) var group: (by: [Expressible], having: Expression<Bool?>?)?

var order = [Expressible]()
public internal(set) var order = [Expressible]()

var limit: (length: Int, offset: Int?)?
public internal(set) var limit: (length: Int, offset: Int?)?

var union = [QueryType]()
public internal(set) var union = [QueryType]()

fileprivate init(_ name: String, alias: String?, database: String?) {
from = (name, alias, database)
Expand Down