From b715206502decb7acc46203762587c363306051f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 6 Dec 2020 16:05:12 +0100 Subject: [PATCH] Row values --- GRDB.xcodeproj/project.pbxproj | 10 +++++++ .../SQL/SQLExpressionVisitor.swift | 2 ++ GRDB/QueryInterface/SQL/SQLRowValue.swift | 28 +++++++++++++++++++ .../SQLExpressionGenerator.swift | 7 +++++ .../SQLGeneration/SQLOrderingGenerator.swift | 6 +++- .../SQLGeneration/SQLQueryGenerator.swift | 24 ++++++++++++++++ .../SQLSelectableCountGenerator.swift | 4 +++ .../SQLSelectableResultColumnGenerator.swift | 4 +++ GRDBCustom.xcodeproj/project.pbxproj | 6 ++++ 9 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 GRDB/QueryInterface/SQL/SQLRowValue.swift diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index a141298f8..143118fa3 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -821,6 +821,10 @@ 56D51D001EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D51CFF1EA789FA0074638A /* FetchableRecord+TableRecord.swift */; }; 56D51D031EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D51CFF1EA789FA0074638A /* FetchableRecord+TableRecord.swift */; }; 56D51D061EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D51CFF1EA789FA0074638A /* FetchableRecord+TableRecord.swift */; }; + 56D8B3FF257D0FAA008AA1F7 /* SQLRowValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D8B3FE257D0FAA008AA1F7 /* SQLRowValue.swift */; }; + 56D8B400257D0FAA008AA1F7 /* SQLRowValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D8B3FE257D0FAA008AA1F7 /* SQLRowValue.swift */; }; + 56D8B401257D0FAA008AA1F7 /* SQLRowValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D8B3FE257D0FAA008AA1F7 /* SQLRowValue.swift */; }; + 56D8B402257D0FAA008AA1F7 /* SQLRowValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D8B3FE257D0FAA008AA1F7 /* SQLRowValue.swift */; }; 56D91AA22205E03700770D8D /* SQLRelation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D91AA12205E03700770D8D /* SQLRelation.swift */; }; 56D91AA32205E03700770D8D /* SQLRelation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D91AA12205E03700770D8D /* SQLRelation.swift */; }; 56D91AA42205E03700770D8D /* SQLRelation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D91AA12205E03700770D8D /* SQLRelation.swift */; }; @@ -1621,6 +1625,7 @@ 56CEB5441EAA359A00BFAF62 /* SQLSelectable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLSelectable.swift; sourceTree = ""; }; 56D5075D1F6BAE8600AE1C5B /* PrimaryKeyInfoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrimaryKeyInfoTests.swift; sourceTree = ""; }; 56D51CFF1EA789FA0074638A /* FetchableRecord+TableRecord.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "FetchableRecord+TableRecord.swift"; sourceTree = ""; }; + 56D8B3FE257D0FAA008AA1F7 /* SQLRowValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRowValue.swift; sourceTree = ""; }; 56D91AA12205E03700770D8D /* SQLRelation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRelation.swift; sourceTree = ""; }; 56D91AA82205F2F000770D8D /* DatabasePromise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePromise.swift; sourceTree = ""; }; 56D91AAF2205F8AC00770D8D /* SQLQuery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLQuery.swift; sourceTree = ""; }; @@ -2125,6 +2130,7 @@ 56F34FE024B0E915007513FC /* SQLOrderingTermVisitor.swift */, 56D91AAF2205F8AC00770D8D /* SQLQuery.swift */, 56D91AA12205E03700770D8D /* SQLRelation.swift */, + 56D8B3FE257D0FAA008AA1F7 /* SQLRowValue.swift */, 56CEB5441EAA359A00BFAF62 /* SQLSelectable.swift */, 566475A11D9810A400FF74B8 /* SQLSelectable+QueryInterface.swift */, 56F34FC824B0DD6A007513FC /* SQLSelectableVisitor.swift */, @@ -3063,6 +3069,7 @@ 565490D61D5AE252005622CB /* StandardLibrary.swift in Sources */, 565490B81D5AE236005622CB /* DatabaseError.swift in Sources */, 56DAA2E11DE9C827006E10C8 /* Cursor.swift in Sources */, + 56D8B401257D0FAA008AA1F7 /* SQLRowValue.swift in Sources */, 5653EB0520944C7C00F46237 /* BelongsToAssociation.swift in Sources */, 5644DE6F20F8C32E001FFDDE /* DatabaseValueConversion.swift in Sources */, 56A2FA3C24424F4800E97D23 /* Export.swift in Sources */, @@ -3210,6 +3217,7 @@ 5653EB0D20944C7C00F46237 /* HasManyAssociation.swift in Sources */, 567ECE502222E431009245CA /* GRDB-5.0.swift in Sources */, 566AD8B51D5318F4002EC1A8 /* TableDefinition.swift in Sources */, + 56D8B400257D0FAA008AA1F7 /* SQLRowValue.swift in Sources */, 5698AD241DABAEFA0056AF8C /* FTS5WrapperTokenizer.swift in Sources */, 56FBFED92210731A00945324 /* SQLRequest.swift in Sources */, 5644DE6E20F8C32E001FFDDE /* DatabaseValueConversion.swift in Sources */, @@ -3808,6 +3816,7 @@ AAA4DCD2230F1E0600C74B15 /* HasManyAssociation.swift in Sources */, AAA4DCD3230F1E0600C74B15 /* GRDB-5.0.swift in Sources */, AAA4DCD4230F1E0600C74B15 /* TableDefinition.swift in Sources */, + 56D8B402257D0FAA008AA1F7 /* SQLRowValue.swift in Sources */, AAA4DCD5230F1E0600C74B15 /* FTS5WrapperTokenizer.swift in Sources */, AAA4DCD6230F1E0600C74B15 /* SQLRequest.swift in Sources */, AAA4DCD7230F1E0600C74B15 /* DatabaseValueConversion.swift in Sources */, @@ -4180,6 +4189,7 @@ 56B964B91DA51D0A0002DA19 /* FTS5Pattern.swift in Sources */, 56FBFEDA2210731A00945324 /* SQLRequest.swift in Sources */, 563363C01C942C04000BE133 /* DatabaseReader.swift in Sources */, + 56D8B3FF257D0FAA008AA1F7 /* SQLRowValue.swift in Sources */, 564CE43121AA901800652B19 /* ValueObserver.swift in Sources */, 5605F1651C672E4000235C62 /* NSNull.swift in Sources */, 56CEB5191EAA328900BFAF62 /* FTS5+QueryInterface.swift in Sources */, diff --git a/GRDB/QueryInterface/SQL/SQLExpressionVisitor.swift b/GRDB/QueryInterface/SQL/SQLExpressionVisitor.swift index 6101369be..24d99821a 100644 --- a/GRDB/QueryInterface/SQL/SQLExpressionVisitor.swift +++ b/GRDB/QueryInterface/SQL/SQLExpressionVisitor.swift @@ -3,6 +3,8 @@ public protocol _SQLExpressionVisitor: _FetchRequestVisitor { mutating func visit(_ dbValue: DatabaseValue) throws mutating func visit(_ column: Column) throws mutating func visit(_ column: _SQLQualifiedColumn) throws + /// - precondition: expr.expressions.count > 1 + mutating func visit(_ expr: _SQLRowValue) throws mutating func visit(_ expr: _SQLExpressionBetween) throws mutating func visit(_ expr: _SQLExpressionBinary) throws /// - precondition: expr.expressions.count > 1 diff --git a/GRDB/QueryInterface/SQL/SQLRowValue.swift b/GRDB/QueryInterface/SQL/SQLRowValue.swift new file mode 100644 index 000000000..b386f3a2c --- /dev/null +++ b/GRDB/QueryInterface/SQL/SQLRowValue.swift @@ -0,0 +1,28 @@ +/// A [row value](https://www.sqlite.org/rowvalue.html). +/// +/// :nodoc: +public struct _SQLRowValue: SQLExpression { + let expressions: [SQLExpression] + + /// SQLite row values were shipped in SQLite 3.15: + /// https://www.sqlite.org/releaselog/3_15_0.html + public /* public for tests */ static let isAvailable = (sqlite3_libversion_number() >= 3015000) + + /// - precondition: `expressions` is not empty + init(_ expressions: [SQLExpression]) { + assert(!expressions.isEmpty) + self.expressions = expressions + } + + public func _qualifiedExpression(with alias: TableAlias) -> SQLExpression { + _SQLRowValue(expressions.map { $0._qualifiedExpression(with: alias) }) + } + + public func _accept(_ visitor: inout Visitor) throws { + if let expression = expressions.first, expressions.count == 1 { + try expression._accept(&visitor) + } else { + try visitor.visit(self) + } + } +} diff --git a/GRDB/QueryInterface/SQLGeneration/SQLExpressionGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLExpressionGenerator.swift index f9f1b09c4..0321cb9ae 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLExpressionGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLExpressionGenerator.swift @@ -41,6 +41,13 @@ private struct SQLExpressionGenerator: _SQLExpressionVisitor { resultSQL += column.name.quotedDatabaseIdentifier } + mutating func visit(_ expr: _SQLRowValue) throws { + let values = try expr + .expressions + .map { try $0.expressionSQL(context, wrappedInParenthesis: false) } + resultSQL = "(\(values.joined(separator: ", ")))" + } + mutating func visit(_ expr: _SQLExpressionBetween) throws { resultSQL = try """ \(expr.expression.expressionSQL(context, wrappedInParenthesis: true)) \ diff --git a/GRDB/QueryInterface/SQLGeneration/SQLOrderingGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLOrderingGenerator.swift index cf161f103..75efa42d1 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLOrderingGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLOrderingGenerator.swift @@ -36,7 +36,7 @@ private struct SQLOrderingGenerator: _SQLOrderingTermVisitor { mutating func visit(_ ordering: _SQLOrderingLiteral) throws { resultSQL = try ordering.sqlLiteral.sql(context) } - + // MARK: - _SQLExpressionVisitor mutating func visit(_ expr: DatabaseValue) throws { @@ -51,6 +51,10 @@ private struct SQLOrderingGenerator: _SQLOrderingTermVisitor { resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) } + mutating func visit(_ expr: _SQLRowValue) throws { + resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) + } + mutating func visit(_ expr: _SQLExpressionBetween) throws { resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) } diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 501ae3ad5..7631a9217 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -870,6 +870,10 @@ private struct SQLExpressionIsTrue: _SQLExpressionVisitor { try setFalse() } + mutating func visit(_ expr: _SQLRowValue) throws { + try setFalse() + } + mutating func visit(_ expr: _SQLExpressionBetween) throws { try setFalse() } @@ -995,6 +999,12 @@ private struct SQLExpressionIsConstantInRequest: _SQLExpressionVisitor { try setNotConstant() } + mutating func visit(_ expr: _SQLRowValue) throws { + for expression in expr.expressions { + try expression._accept(&self) + } + } + mutating func visit(_ expr: _SQLExpressionBetween) throws { try expr.expression._accept(&self) try expr.lowerBound._accept(&self) @@ -1139,6 +1149,8 @@ private struct SQLTableColumnVisitor: _SQLExpressionVisitor { } } + mutating func visit(_ expr: _SQLRowValue) throws { } + mutating func visit(_ expr: _SQLExpressionBetween) throws { } mutating func visit(_ expr: _SQLExpressionBinary) throws { @@ -1243,6 +1255,7 @@ extension SQLExpression { } } +#warning("TODO: what should we do with _SQLRowValue? (a, b) == (1, 2) for example?") /// Support for `SQLExpression.identifyingColums(_:for:)` private struct SQLIdentifyingColumns: _SQLExpressionVisitor { struct BreakError: Error { } @@ -1256,6 +1269,8 @@ private struct SQLIdentifyingColumns: _SQLExpressionVisitor { mutating func visit(_ column: _SQLQualifiedColumn) throws { } + mutating func visit(_ expr: _SQLRowValue) throws { } + mutating func visit(_ expr: _SQLExpressionBetween) throws { } mutating func visit(_ expr: _SQLExpressionBinary) throws { } @@ -1353,6 +1368,7 @@ extension SQLExpression { } } +#warning("TODO: what should we do with _SQLRowValue? (a, b) == (1, 2) for example?") /// Support for `SQLExpression.identifyingRowIDs(_:for:)` private struct SQLIdentifyingRowIDs: _SQLExpressionVisitor { let db: Database @@ -1369,6 +1385,8 @@ private struct SQLIdentifyingRowIDs: _SQLExpressionVisitor { mutating func visit(_ column: _SQLQualifiedColumn) throws { } + mutating func visit(_ expr: _SQLRowValue) throws { } + mutating func visit(_ expr: _SQLExpressionBetween) throws { } mutating func visit(_ expr: _SQLExpressionBinary) throws { } @@ -1525,6 +1543,12 @@ private struct SQLSelectableIsAggregate: _SQLSelectableVisitor { mutating func visit(_ selectable: _SQLQualifiedAllColumns) throws { } + mutating func visit(_ expr: _SQLRowValue) throws { + for expression in expr.expressions { + try expression._accept(&self) + } + } + mutating func visit(_ selectable: _SQLSelectionLiteral) throws { // Don't know - assume not an aggregate } diff --git a/GRDB/QueryInterface/SQLGeneration/SQLSelectableCountGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLSelectableCountGenerator.swift index a4c1e7bb3..18c82f09d 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLSelectableCountGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLSelectableCountGenerator.swift @@ -60,6 +60,10 @@ private struct SQLSelectableCountGenerator: _SQLSelectableVisitor { resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) } + mutating func visit(_ expr: _SQLRowValue) throws { + resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) + } + mutating func visit(_ expr: _SQLExpressionBetween) throws { resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) } diff --git a/GRDB/QueryInterface/SQLGeneration/SQLSelectableResultColumnGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLSelectableResultColumnGenerator.swift index 2a5c330d6..1b9d0d2a1 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLSelectableResultColumnGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLSelectableResultColumnGenerator.swift @@ -59,6 +59,10 @@ private struct SQLSelectableResultColumnGenerator: _SQLSelectableVisitor { resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) } + mutating func visit(_ expr: _SQLRowValue) throws { + resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) + } + mutating func visit(_ expr: _SQLExpressionBetween) throws { resultSQL = try expr.expressionSQL(context, wrappedInParenthesis: false) } diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index b5295f03f..44755329d 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -487,6 +487,8 @@ 56D507651F6BAE8600AE1C5B /* PrimaryKeyInfoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D5075D1F6BAE8600AE1C5B /* PrimaryKeyInfoTests.swift */; }; 56D51D021EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D51CFF1EA789FA0074638A /* FetchableRecord+TableRecord.swift */; }; 56D51D051EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D51CFF1EA789FA0074638A /* FetchableRecord+TableRecord.swift */; }; + 56D8B40D257D0FBE008AA1F7 /* SQLRowValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D8B40B257D0FBE008AA1F7 /* SQLRowValue.swift */; }; + 56D8B40E257D0FBE008AA1F7 /* SQLRowValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D8B40B257D0FBE008AA1F7 /* SQLRowValue.swift */; }; 56DAA2D51DE99DAB006E10C8 /* DatabaseCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DAA2D11DE99DAB006E10C8 /* DatabaseCursorTests.swift */; }; 56DAA2D91DE99DAB006E10C8 /* DatabaseCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DAA2D11DE99DAB006E10C8 /* DatabaseCursorTests.swift */; }; 56DAA2DD1DE9C827006E10C8 /* Cursor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56DAA2DA1DE9C827006E10C8 /* Cursor.swift */; }; @@ -1105,6 +1107,7 @@ 56CEB4F91EAA2F4D00BFAF62 /* FTS4.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS4.swift; sourceTree = ""; }; 56D5075D1F6BAE8600AE1C5B /* PrimaryKeyInfoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrimaryKeyInfoTests.swift; sourceTree = ""; }; 56D51CFF1EA789FA0074638A /* FetchableRecord+TableRecord.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "FetchableRecord+TableRecord.swift"; sourceTree = ""; }; + 56D8B40B257D0FBE008AA1F7 /* SQLRowValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRowValue.swift; sourceTree = ""; }; 56DAA2D11DE99DAB006E10C8 /* DatabaseCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCursorTests.swift; sourceTree = ""; }; 56DAA2DA1DE9C827006E10C8 /* Cursor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cursor.swift; sourceTree = ""; }; 56DE7B101C3D93ED00861EB8 /* StatementArgumentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementArgumentsTests.swift; sourceTree = ""; }; @@ -1604,6 +1607,7 @@ 56F34FE524B0E94C007513FC /* SQLOrderingTermVisitor.swift */, 5656A8502295BD56001FF3FF /* SQLQuery.swift */, 5656A84E2295BD56001FF3FF /* SQLRelation.swift */, + 56D8B40B257D0FBE008AA1F7 /* SQLRowValue.swift */, 5656A8492295BD56001FF3FF /* SQLSelectable.swift */, 5656A84F2295BD56001FF3FF /* SQLSelectable+QueryInterface.swift */, 56F34FCD24B0DE0B007513FC /* SQLSelectableVisitor.swift */, @@ -2415,6 +2419,7 @@ F3BA80331CFB28A4003DC1BA /* PersistableRecord.swift in Sources */, 5656A8802295BD56001FF3FF /* SQLForeignKeyRequest.swift in Sources */, 56B964BE1DA51D0A0002DA19 /* FTS5Pattern.swift in Sources */, + 56D8B40E257D0FBE008AA1F7 /* SQLRowValue.swift in Sources */, 561408A1221596A100DAD589 /* GRDB-5.0.swift in Sources */, F3BA80211CFB288C003DC1BA /* NSNull.swift in Sources */, 56FBFED72210731100945324 /* SQLRequest.swift in Sources */, @@ -2787,6 +2792,7 @@ F3BA808F1CFB2E7A003DC1BA /* PersistableRecord.swift in Sources */, 5656A87F2295BD56001FF3FF /* SQLForeignKeyRequest.swift in Sources */, 56B964BB1DA51D0A0002DA19 /* FTS5Pattern.swift in Sources */, + 56D8B40D257D0FBE008AA1F7 /* SQLRowValue.swift in Sources */, 561408A0221596A100DAD589 /* GRDB-5.0.swift in Sources */, F3BA807D1CFB2E61003DC1BA /* NSNull.swift in Sources */, 56FBFED62210731100945324 /* SQLRequest.swift in Sources */,