Skip to content

Strings to enum constants #1

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

Merged
merged 1 commit into from
Mar 26, 2019
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
39 changes: 26 additions & 13 deletions Sources/SQLite/Typed/AggregateFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
// THE SOFTWARE.
//

private enum Functions: String {
case count
case max
case min
case avg
case sum
case total

func wrap<T>(_ expression: Expressible) -> Expression<T> {
return self.rawValue.wrap(expression)
}
}

extension ExpressionType where UnderlyingType : Value {

/// Builds a copy of the expression prefixed with the `DISTINCT` keyword.
Expand All @@ -48,7 +61,7 @@ extension ExpressionType where UnderlyingType : Value {
/// - Returns: A copy of the expression wrapped with the `count` aggregate
/// function.
public var count: Expression<Int> {
return "count".wrap(self)
return Functions.count.wrap(self)
}

}
Expand Down Expand Up @@ -79,7 +92,7 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr
/// - Returns: A copy of the expression wrapped with the `count` aggregate
/// function.
public var count: Expression<Int> {
return "count".wrap(self)
return Functions.count.wrap(self)
}

}
Expand All @@ -96,7 +109,7 @@ extension ExpressionType where UnderlyingType : Value, UnderlyingType.Datatype :
/// - Returns: A copy of the expression wrapped with the `max` aggregate
/// function.
public var max: Expression<UnderlyingType?> {
return "max".wrap(self)
return Functions.max.wrap(self)
}

/// Builds a copy of the expression wrapped with the `min` aggregate
Expand All @@ -109,7 +122,7 @@ extension ExpressionType where UnderlyingType : Value, UnderlyingType.Datatype :
/// - Returns: A copy of the expression wrapped with the `min` aggregate
/// function.
public var min: Expression<UnderlyingType?> {
return "min".wrap(self)
return Functions.min.wrap(self)
}

}
Expand All @@ -126,7 +139,7 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr
/// - Returns: A copy of the expression wrapped with the `max` aggregate
/// function.
public var max: Expression<UnderlyingType> {
return "max".wrap(self)
return Functions.max.wrap(self)
}

/// Builds a copy of the expression wrapped with the `min` aggregate
Expand All @@ -139,7 +152,7 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr
/// - Returns: A copy of the expression wrapped with the `min` aggregate
/// function.
public var min: Expression<UnderlyingType> {
return "min".wrap(self)
return Functions.min.wrap(self)
}

}
Expand All @@ -156,7 +169,7 @@ extension ExpressionType where UnderlyingType : Value, UnderlyingType.Datatype :
/// - Returns: A copy of the expression wrapped with the `min` aggregate
/// function.
public var average: Expression<Double?> {
return "avg".wrap(self)
return Functions.avg.wrap(self)
}

/// Builds a copy of the expression wrapped with the `sum` aggregate
Expand All @@ -169,7 +182,7 @@ extension ExpressionType where UnderlyingType : Value, UnderlyingType.Datatype :
/// - Returns: A copy of the expression wrapped with the `min` aggregate
/// function.
public var sum: Expression<UnderlyingType?> {
return "sum".wrap(self)
return Functions.sum.wrap(self)
}

/// Builds a copy of the expression wrapped with the `total` aggregate
Expand All @@ -182,7 +195,7 @@ extension ExpressionType where UnderlyingType : Value, UnderlyingType.Datatype :
/// - Returns: A copy of the expression wrapped with the `min` aggregate
/// function.
public var total: Expression<Double> {
return "total".wrap(self)
return Functions.total.wrap(self)
}

}
Expand All @@ -199,7 +212,7 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr
/// - Returns: A copy of the expression wrapped with the `min` aggregate
/// function.
public var average: Expression<Double?> {
return "avg".wrap(self)
return Functions.avg.wrap(self)
}

/// Builds a copy of the expression wrapped with the `sum` aggregate
Expand All @@ -212,7 +225,7 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr
/// - Returns: A copy of the expression wrapped with the `min` aggregate
/// function.
public var sum: Expression<UnderlyingType> {
return "sum".wrap(self)
return Functions.sum.wrap(self)
}

/// Builds a copy of the expression wrapped with the `total` aggregate
Expand All @@ -225,15 +238,15 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr
/// - Returns: A copy of the expression wrapped with the `min` aggregate
/// function.
public var total: Expression<Double> {
return "total".wrap(self)
return Functions.total.wrap(self)
}

}

extension ExpressionType where UnderlyingType == Int {

static func count(_ star: Star) -> Expression<UnderlyingType> {
return "count".wrap(star(nil, nil))
return Functions.count.wrap(star(nil, nil))
}

}
Expand Down
Loading