Skip to content

Commit 0d1dcd1

Browse files
committed
Implement the new SQLDialect.nestedSubpathExpression(in:for:) method for MySQL syntax.
Depends on vapor/sql-kit#169
1 parent ba3dcb5 commit 0d1dcd1

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let package = Package(
1414
],
1515
dependencies: [
1616
.package(url: "https://github.com/vapor/mysql-nio.git", from: "1.0.0"),
17-
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.16.0"),
17+
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.28.0"),
1818
.package(url: "https://github.com/vapor/async-kit.git", from: "1.0.0"),
1919
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
2020
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),

Sources/MySQLKit/MySQLDialect.swift

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ public struct MySQLDialect: SQLDialect {
99
"mysql"
1010
}
1111

12-
public var identifierQuote: SQLExpression {
12+
public var identifierQuote: any SQLExpression {
1313
return SQLRaw("`")
1414
}
1515

16-
public var literalStringQuote: SQLExpression {
16+
public var literalStringQuote: any SQLExpression {
1717
return SQLRaw("'")
1818
}
1919

20-
public func bindPlaceholder(at position: Int) -> SQLExpression {
20+
public func bindPlaceholder(at position: Int) -> any SQLExpression {
2121
return SQLRaw("?")
2222
}
2323

24-
public func literalBoolean(_ value: Bool) -> SQLExpression {
24+
public func literalBoolean(_ value: Bool) -> any SQLExpression {
2525
switch value {
2626
case false:
2727
return SQLRaw("0")
@@ -30,7 +30,7 @@ public struct MySQLDialect: SQLDialect {
3030
}
3131
}
3232

33-
public var autoIncrementClause: SQLExpression {
33+
public var autoIncrementClause: any SQLExpression {
3434
return SQLRaw("AUTO_INCREMENT")
3535
}
3636

@@ -42,7 +42,7 @@ public struct MySQLDialect: SQLDialect {
4242
.inline
4343
}
4444

45-
public func customDataType(for dataType: SQLDataType) -> SQLExpression? {
45+
public func customDataType(for dataType: SQLDataType) -> (any SQLExpression)? {
4646
switch dataType {
4747
case .text:
4848
return SQLRaw("VARCHAR(255)")
@@ -58,7 +58,7 @@ public struct MySQLDialect: SQLDialect {
5858
)
5959
}
6060

61-
public func normalizeSQLConstraint(identifier: SQLExpression) -> SQLExpression {
61+
public func normalizeSQLConstraint(identifier: any SQLExpression) -> any SQLExpression {
6262
if let sqlIdentifier = identifier as? SQLIdentifier {
6363
return SQLIdentifier(Insecure.SHA1.hash(data: Data(sqlIdentifier.string.utf8)).hexRepresentation)
6464
} else {
@@ -78,13 +78,26 @@ public struct MySQLDialect: SQLDialect {
7878
[.union, .unionAll, .explicitDistinct, .parenthesizedSubqueries]
7979
}
8080

81-
public var sharedSelectLockExpression: SQLExpression? {
81+
public var sharedSelectLockExpression: (any SQLExpression)? {
8282
SQLRaw("LOCK IN SHARE MODE")
8383
}
8484

85-
public var exclusiveSelectLockExpression: SQLExpression? {
85+
public var exclusiveSelectLockExpression: (any SQLExpression)? {
8686
SQLRaw("FOR UPDATE")
8787
}
88+
89+
public func nestedSubpathExpression(in column: any SQLExpression, for path: [String]) -> (any SQLExpression)? {
90+
guard !path.isEmpty else { return nil }
91+
92+
// N.B.: While MySQL has had the `->` and `->>` operators since 5.7.13, there are still implementations with
93+
// which they do not work properly (most notably AWS's Aurora 2.x), so we use the legacy functions instead.
94+
return SQLFunction("json_unquote", args:
95+
SQLFunction("json_extract", args: [
96+
column,
97+
SQLLiteral.string("$.\(path.joined(separator: "."))")
98+
]
99+
))
100+
}
88101
}
89102

90103
fileprivate let hexTable: [UInt8] = [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66]

0 commit comments

Comments
 (0)