From c74ddeade0d7014babcfef4b1870011af9ec458a Mon Sep 17 00:00:00 2001 From: Adam Wulf Date: Wed, 1 Sep 2021 16:54:33 -0500 Subject: [PATCH 1/6] Add support for aliasing columns --- Sources/SQLite/Typed/Expression.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/SQLite/Typed/Expression.swift b/Sources/SQLite/Typed/Expression.swift index 95cdd3b9..a9fbcf56 100644 --- a/Sources/SQLite/Typed/Expression.swift +++ b/Sources/SQLite/Typed/Expression.swift @@ -106,6 +106,9 @@ extension ExpressionType { " ".join([self, Expression(literal: "DESC")]) } + public func alias(name:String) -> Expressible { + return " ".join([self, Expression(literal: "AS \(name)")]) + } } extension ExpressionType where UnderlyingType: Value { From ad0ea976110362d1c6ff709c807502f28f1c6192 Mon Sep 17 00:00:00 2001 From: Adam Wulf Date: Wed, 1 Sep 2021 16:57:19 -0500 Subject: [PATCH 2/6] cleanup --- Sources/SQLite/Typed/Expression.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/SQLite/Typed/Expression.swift b/Sources/SQLite/Typed/Expression.swift index a9fbcf56..c04c7963 100644 --- a/Sources/SQLite/Typed/Expression.swift +++ b/Sources/SQLite/Typed/Expression.swift @@ -106,8 +106,8 @@ extension ExpressionType { " ".join([self, Expression(literal: "DESC")]) } - public func alias(name:String) -> Expressible { - return " ".join([self, Expression(literal: "AS \(name)")]) + public func alias(_ aliasName: String) -> Expressible { + return " ".join([self, Expression(literal: "AS \(aliasName)")]) } } From b74a67eff60fc5aaacc470b4f99e322bc6b0e782 Mon Sep 17 00:00:00 2001 From: Adam Wulf Date: Wed, 1 Sep 2021 17:18:02 -0500 Subject: [PATCH 3/6] make clauses public readable --- Sources/SQLite/Typed/Query.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/SQLite/Typed/Query.swift b/Sources/SQLite/Typed/Query.swift index 2d88db63..060df1aa 100644 --- a/Sources/SQLite/Typed/Query.swift +++ b/Sources/SQLite/Typed/Query.swift @@ -1228,21 +1228,21 @@ public enum OnConflict: String { public struct QueryClauses { - var select = (distinct: false, columns: [Expression(literal: "*") as Expressible]) + public internal(set) var select = (distinct: false, columns: [Expression(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? + public internal(set) var filters: Expression? - var group: (by: [Expressible], having: Expression?)? + public internal(set) var group: (by: [Expressible], having: Expression?)? - 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) From 5c882612a0df55bf3aaec7143243522f9c9d0b2a Mon Sep 17 00:00:00 2001 From: Adam Wulf Date: Wed, 1 Sep 2021 17:25:22 -0500 Subject: [PATCH 4/6] match columns that have been renamed --- Sources/SQLite/Typed/Query.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SQLite/Typed/Query.swift b/Sources/SQLite/Typed/Query.swift index 060df1aa..2a2510d0 100644 --- a/Sources/SQLite/Typed/Query.swift +++ b/Sources/SQLite/Typed/Query.swift @@ -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: From e100dcd1ef3b4632fa7bda609bc0f6140c9f0831 Mon Sep 17 00:00:00 2001 From: Adam Wulf Date: Wed, 1 Sep 2021 17:28:47 -0500 Subject: [PATCH 5/6] wrap column name in quotes --- Sources/SQLite/Typed/Expression.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SQLite/Typed/Expression.swift b/Sources/SQLite/Typed/Expression.swift index c04c7963..b1c5ea4a 100644 --- a/Sources/SQLite/Typed/Expression.swift +++ b/Sources/SQLite/Typed/Expression.swift @@ -107,7 +107,7 @@ extension ExpressionType { } public func alias(_ aliasName: String) -> Expressible { - return " ".join([self, Expression(literal: "AS \(aliasName)")]) + return " ".join([self, Expression(literal: "AS \"\(aliasName)\"")]) } } From 05aad5cccaa8c1373b1de54e028a4a39822abe7b Mon Sep 17 00:00:00 2001 From: Adam Wulf Date: Thu, 2 Sep 2021 15:56:33 -0500 Subject: [PATCH 6/6] Allow Rows to be created publicly --- Sources/SQLite/Typed/Query.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SQLite/Typed/Query.swift b/Sources/SQLite/Typed/Query.swift index 2a2510d0..1323bb17 100644 --- a/Sources/SQLite/Typed/Query.swift +++ b/Sources/SQLite/Typed/Query.swift @@ -1137,7 +1137,7 @@ public struct Row { fileprivate let values: [Binding?] - internal init(_ columnNames: [String: Int], _ values: [Binding?]) { + public init(_ columnNames: [String: Int], _ values: [Binding?]) { self.columnNames = columnNames self.values = values }