Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
# Can't test Musl because of FluentBenchmarks and SQLKitBenchmarks

android-unit:
if: ${{ !(github.event.pull_request.draft || false) && inputs.with_android }}
if: ${{ !(github.event.pull_request.draft || false) }}
strategy:
fail-fast: false
matrix:
Expand All @@ -75,4 +75,4 @@ jobs:
uses: skiptools/swift-android-action@v2
with:
swift-version: ${{ matrix.swift-version }}
swift-test-flags: --enable-all-traits
swift-test-flags: --enable-all-traits

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if FluentSQLKitExtras
import FluentKit
import SQLKit

extension SQLExpression {
/// Convenience method for creating a ``SQLCastExpression`` expression using a Fluent keypath for the value and a string
/// for the desired type.
public static func cast(
_ column: KeyPath<some Schema, some QueryAddressableProperty>,
to type: some StringProtocol
) -> Self where Self == SQLCastExpression {
.cast(.column(column), to: type)
}

/// Convenience method for creating a ``SQLCastExpression`` expression using a Fluent keypath for the value and an expression
/// for the desired type.
public static func cast(
_ column: KeyPath<some Schema, some QueryAddressableProperty>,
to type: some SQLExpression
) -> Self where Self == SQLCastExpression {
.cast(.column(column), to: type)
}
}
#endif
65 changes: 0 additions & 65 deletions Sources/SQLKitExtras/PostgreSQLKitExtras/PostgreSQLCast.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import SQLKit

/// Represents a `CAST` expression in SQL, which converts a value from one type to another.
public struct SQLCastExpression: SQLExpression {
/// The original expression to be cast.
public let original: any SQLExpression

/// The desired type to cast the original expression to.
public let desiredType: any SQLExpression

/// Create a new ``SQLCastExpression``.
///
/// - Parameters:
/// - original: The original expression to be cast.
/// - desiredType: The desired type to cast the original expression to.
public init(expr: some SQLExpression, castType: some SQLExpression) {
self.original = expr
self.desiredType = castType
}

/// Convenience initializer for creating a ``SQLCastExpression`` with a string for the original expression and a string for the desired type.
///
/// - Parameters:
/// - original: The original expression to be cast.
/// - desiredType: The desired type to cast the original expression to, represented as a string.
public init(_ original: some SQLExpression, to type: some StringProtocol) {
self.init(expr: original, castType: .identifier(type))
}

/// See `SQLExpression.serialize(to:)`.
public func serialize(to serializer: inout SQLSerializer) {
SQLFunction("CAST", args: SQLAlias(self.original, as: self.desiredType))
.serialize(to: &serializer)
}
}

extension SQLExpression {
/// Convenience method for creating a ``SQLCastExpression`` using a column name and a string for the desired type.
public static func cast(
_ column: some StringProtocol,
to type: some StringProtocol
) -> Self where Self == SQLCastExpression {
.cast(.column(column), to: .identifier(type))
}

/// Convenience method for creating a ``SQLCastExpression`` using a column name and a string for the desired type.
public static func cast(
_ column: some SQLExpression,
to type: some StringProtocol
) -> Self where Self == SQLCastExpression {
.cast(column, to: .identifier(type))
}

/// Convenience method for creating a ``SQLCastExpression`` using a column name and an expression for the desired type.
public static func cast(
_ column: some SQLExpression,
to type: some SQLExpression
) -> Self where Self == SQLCastExpression {
.init(expr: column, castType: type)
}
}
6 changes: 6 additions & 0 deletions Tests/SQLKitExtrasTests/FluentSQLKitExtrasTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ struct FluentSQLKitExtrasTests {
#expect(serialize(.expr(.not, \FooModel.$field)) == #"NOT "foos"."field""#)
#expect(serialize(.not(\FooModel.$field)) == #"NOT "foos"."field""#)
}

@Test
func castExpression() {
#expect(serialize(.cast(\FooModel.$field, to: "text")) == #"CAST("foos"."field" AS "text")"#)
#expect(serialize(.cast(\FooModel.$field, to: .unsafeRaw("text"))) == #"CAST("foos"."field" AS text)"#)
}
}
}

Expand Down
16 changes: 0 additions & 16 deletions Tests/SQLKitExtrasTests/PostgreSQLKitExtrasTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ struct PostgreSQLKitExtrasTests {
#expect(serialize(.notSimilarTo()) == #"NOT SIMILAR TO"#)
}

@Test
func castExpression() {
#expect(serialize(PostgreSQLCast(.column("foo"), as: "text")) == #""foo"::"text""#)
#expect(serialize(PostgreSQLCast(expr: .column("foo"), castType: .unsafeRaw("text"))) == #""foo"::text"#)

#expect(serialize(.psql_cast("foo", to: "text")) == #""foo"::"text""#)
#expect(serialize(.psql_cast(.column("foo"), to: "text")) == #""foo"::"text""#)
#expect(serialize(.psql_cast(.column("foo"), to: .unsafeRaw("text"))) == #""foo"::text"#)
}

@Test
func setQuery() {
#expect(serialize(PostgreSQLSetQuery(name: .identifier("foo.bar"), value: .literal("baz"))) == #"SET "foo.bar" = 'baz'"#)
Expand All @@ -60,12 +50,6 @@ struct PostgreSQLKitExtrasTests {
#expect(serialize(.psql_subscript(\FooModel.$field, at: .literal(0))) == #""foos"."field"[0]"#)
#expect(serialize(.psql_subscript(.identifier("foo"), at: \FooModel.$id)) == #""foo"["foos"."id"]"#)
}

@Test
func castExpression() {
#expect(serialize(.psql_cast(\FooModel.$field, to: "text")) == #""foos"."field"::"text""#)
#expect(serialize(.psql_cast(\FooModel.$field, to: .unsafeRaw("text"))) == #""foos"."field"::text"#)
}
}
#endif
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/SQLKitExtrasTests/SQLKitExtrasTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ struct SQLKitExtrasTests {
#expect(serialize(.case(when: .literal(0), then: .literal(1), when: .literal(1), then: .literal(2), when: .literal(2), then: .literal(3), when: .literal(3), then: .literal(4), when: .literal(4), then: .literal(5), else: .some(.literal(0)))) == #"CASE WHEN 0 THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 3 WHEN 3 THEN 4 WHEN 4 THEN 5 ELSE 0 END"#)
}

@Test
func castExpression() {
#expect(serialize(SQLCastExpression(.column("foo"), to: "text")) == #"CAST("foo" AS "text")"#)
#expect(serialize(SQLCastExpression(expr: .column("foo"), castType: .unsafeRaw("text"))) == #"CAST("foo" AS text)"#)

#expect(serialize(.cast("foo", to: "text")) == #"CAST("foo" AS "text")"#)
#expect(serialize(.cast(.column("foo"), to: "text")) == #"CAST("foo" AS "text")"#)
#expect(serialize(.cast(.column("foo"), to: .unsafeRaw("text"))) == #"CAST("foo" AS text)"#)
}

@Test
func currentTimestampExpression() {
#expect(serialize(SQLCurrentTimestampExpression()) == #"current_timestamp()"#)
Expand Down
Loading