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

Count distinct #24

Merged
merged 3 commits into from
Nov 8, 2022
Merged
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
18 changes: 17 additions & 1 deletion Sources/PSQLKit/Expressions/CountExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,37 @@ import SQLKit

public struct CountExpression<Content>: AggregateExpression {
let content: Content
let isDistinct: Bool

init(_ content: Content, distinct: Bool) {
self.content = content
self.isDistinct = distinct
}

public init(_ content: Content) {
self.content = content
self.isDistinct = false
}
}

extension CountExpression: SelectSQLExpression where
Content: SelectSQLExpression
{
public var selectSqlExpression: SQLExpression {
_Select(content: self.content)
_Select(content: self.content, distinct: self.isDistinct)
}

private struct _Select: SQLExpression {
let content: Content
let distinct: Bool

func serialize(to serializer: inout SQLSerializer) {
serializer.write("COUNT")
serializer.write("(")
if self.distinct {
serializer.write("DISTINCT")
serializer.writeSpace()
}
self.content.selectSqlExpression.serialize(to: &serializer)
serializer.write(")")
}
Expand Down Expand Up @@ -58,4 +70,8 @@ extension CountExpression {
public func `as`(_ alias: String) -> ExpressionAlias<CountExpression<Content>> {
ExpressionAlias(expression: self, alias: alias)
}

public func distinct(_ isDistinct: Bool = true) -> Self {
.init(self.content, distinct: isDistinct)
}
}
6 changes: 6 additions & 0 deletions Sources/PSQLKit/PSQLExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ extension PSQLExpression where Self: Encodable {
.init(self)
}
}

extension PSQLExpression where Self: RawRepresentable, RawValue: PSQLExpression {
public static var postgresColumnType: PostgresColumnType {
RawValue.postgresColumnType
}
}
4 changes: 4 additions & 0 deletions Sources/PSQLKit/TypeEquatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ import Foundation
public protocol TypeEquatable {
associatedtype CompareType
}

extension TypeEquatable where Self: RawRepresentable, RawValue: TypeEquatable {
public typealias CompareType = RawValue
}
24 changes: 24 additions & 0 deletions Tests/PSQLKitTests/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ final class ExpressionTests: PSQLTestCase {
XCTAssertEqual(psqlkitSerializer.sql, compare)
}

func testCountDistinct() {
SELECT {
COUNT(f.$name)
.distinct()
COUNT(f.$age)
.distinct()
.as("age")
}
.serialize(to: &fluentSerializer)

SELECT {
COUNT(p.$name)
.distinct()
COUNT(p.$age)
.distinct()
.as("age")
}
.serialize(to: &psqlkitSerializer)

let compare = #"SELECT COUNT(DISTINCT "x"."name"::TEXT), COUNT(DISTINCT "x"."age"::INTEGER) AS "age""#
XCTAssertEqual(fluentSerializer.sql, compare)
XCTAssertEqual(psqlkitSerializer.sql, compare)
}

func testSum() {
SELECT {
SUM(f.$name)
Expand Down
14 changes: 14 additions & 0 deletions Tests/PSQLKitTests/PSQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class FluentModel: Model, Table {
var money: Double
@Field(key: "birthday")
var birthday: Date
@Field(key: "category")
var category: Category
@Group(key: "pet")
var pet: Pet

Expand All @@ -44,6 +46,11 @@ final class FluentModel: Model, Table {
init() {}
}
}

enum Category: String, Codable, Equatable, TypeEquatable, PSQLExpression {
case yes
case no
}
}

struct PSQLModel: Table {
Expand All @@ -61,6 +68,8 @@ struct PSQLModel: Table {
var money: Double
@Column(key: "birthday")
var birthday: Date
@Column(key: "category")
var category: Category
@NestedColumn(key: "pet")
var pet: Pet

Expand All @@ -83,6 +92,11 @@ struct PSQLModel: Table {
init() {}
}
}

enum Category: String, Codable, Equatable, TypeEquatable, PSQLExpression {
case yes
case no
}
}

class PSQLTestCase: XCTestCase {
Expand Down
18 changes: 18 additions & 0 deletions Tests/PSQLKitTests/WhereTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ final class WhereTests: PSQLTestCase {
XCTAssertEqual(fluentSerializer.sql, compare)
}

func testEnum() {
WHERE {
FluentModel.$category != FluentModel.$category
FluentModel.$category == FluentModel.Category.yes.rawValue
}
.serialize(to: &fluentSerializer)

WHERE {
PSQLModel.$category != PSQLModel.$category
PSQLModel.$category == PSQLModel.Category.yes.rawValue
}
.serialize(to: &psqlkitSerializer)

let compare = #"WHERE ("my_model"."category" != "my_model"."category") AND ("my_model"."category" = 'yes')"#
XCTAssertEqual(fluentSerializer.sql, compare)
XCTAssertEqual(fluentSerializer.sql, compare)
}

func testMultiple() {
WHERE {
FluentModel.$name == f.$title
Expand Down