From 717c2836ec69a40eff5ff8346e3973556372670b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 23 May 2023 08:15:04 +0200 Subject: [PATCH] Bitwise operators & | ~ << >> --- GRDB/QueryInterface/SQL/SQLExpression.swift | 57 +++++-- GRDB/QueryInterface/SQL/SQLOperators.swift | 69 ++++++++ README.md | 9 ++ .../QueryInterfaceExpressionsTests.swift | 150 ++++++++++++++++++ 4 files changed, 271 insertions(+), 14 deletions(-) diff --git a/GRDB/QueryInterface/SQL/SQLExpression.swift b/GRDB/QueryInterface/SQL/SQLExpression.swift index b3cba98b9..a085c1d2c 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression.swift @@ -215,7 +215,7 @@ public struct SQLExpression { /// If true, (a • b) • c is strictly equal to a • (b • c). /// - /// `AND`, `OR`, `||` (concat) are stricly associative. + /// `AND`, `OR`, `||` (concat), `&`, `|` are stricly associative. /// /// `+` and `*` are not stricly associative when applied to floating /// point values. @@ -226,7 +226,7 @@ public struct SQLExpression { /// /// `||` (concat) is bijective. /// - /// `AND`, `OR`, `+` and `*` are not. + /// `AND`, `OR`, `+` and `*`, `&`, `|` are not. let isBijective: Bool /// Creates a binary operator @@ -306,6 +306,34 @@ public struct SQLExpression { neutralValue: "".databaseValue, strictlyAssociative: true, bijective: true) + + /// The `&` bitwise AND SQL operator. + /// + /// For example: + /// + /// ```swift + /// // mask & 2 + /// [Column("mask"), 2.databaseValue].joined(operator: .bitwiseAnd) + /// ``` + public static let bitwiseAnd = AssociativeBinaryOperator( + sql: "&", + neutralValue: (-1).databaseValue, + strictlyAssociative: true, + bijective: false) + + /// The `|` bitwise OR SQL operator. + /// + /// For example: + /// + /// ```swift + /// // mask | 2 + /// [Column("mask"), 2.databaseValue].joined(operator: .bitwiseOr) + /// ``` + public static let bitwiseOr = AssociativeBinaryOperator( + sql: "|", + neutralValue: 0.databaseValue, + strictlyAssociative: true, + bijective: false) } /// `BinaryOperator` is an SQLite binary operator, such as `>`, `=`, etc. @@ -343,6 +371,12 @@ public struct SQLExpression { /// The `MATCH` binary operator static let match = BinaryOperator("MATCH") + + /// The `<<` bitwise left shift operator + static let leftShift = BinaryOperator("<<") + + /// The `>>` bitwise right shift operator + static let rightShift = BinaryOperator(">>") } /// `EscapableBinaryOperator` is an SQLite binary operator that accepts an @@ -399,19 +433,16 @@ public struct SQLExpression { /// The SQL operator let sql: String - /// If true GRDB puts a white space between the operator and the operand. - let needsRightSpace: Bool - - /// Creates an unary operator - /// - /// UnaryOperator("~", needsRightSpace: false) - init(_ sql: String, needsRightSpace: Bool) { + /// Creates an unary operator. + init(_ sql: String) { self.sql = sql - self.needsRightSpace = needsRightSpace } /// The `-` unary operator - static let minus = UnaryOperator("-", needsRightSpace: false) + static let minus = UnaryOperator("-") + + /// The `~` unary operator + static let bitwiseNot = UnaryOperator("~") } } @@ -1021,9 +1052,7 @@ extension SQLExpression { return resultSQL case let .unary(op, expression): - var resultSQL = try op.sql - + (op.needsRightSpace ? " " : "") - + expression.sql(context, wrappedInParenthesis: true) + var resultSQL = try op.sql + expression.sql(context, wrappedInParenthesis: true) if wrappedInParenthesis { resultSQL = "(\(resultSQL))" } diff --git a/GRDB/QueryInterface/SQL/SQLOperators.swift b/GRDB/QueryInterface/SQL/SQLOperators.swift index 6277924f0..993ee7bc5 100644 --- a/GRDB/QueryInterface/SQL/SQLOperators.swift +++ b/GRDB/QueryInterface/SQL/SQLOperators.swift @@ -436,6 +436,75 @@ extension SQLSpecificExpressible { } } +// MARK: - Bitwise Operators (&, |, ~, <<, >>) + +extension SQLSpecificExpressible { + /// The `&` SQL operator. + public static func & (lhs: Self, rhs: some SQLExpressible) -> SQLExpression { + .associativeBinary(.bitwiseAnd, [lhs.sqlExpression, rhs.sqlExpression]) + } + + /// The `&` SQL operator. + public static func & (lhs: some SQLExpressible, rhs: Self) -> SQLExpression { + .associativeBinary(.bitwiseAnd, [lhs.sqlExpression, rhs.sqlExpression]) + } + + /// The `&` SQL operator. + public static func & (lhs: Self, rhs: some SQLSpecificExpressible) -> SQLExpression { + .associativeBinary(.bitwiseAnd, [lhs.sqlExpression, rhs.sqlExpression]) + } + + /// The `|` SQL operator. + public static func | (lhs: Self, rhs: some SQLExpressible) -> SQLExpression { + .associativeBinary(.bitwiseOr, [lhs.sqlExpression, rhs.sqlExpression]) + } + + /// The `|` SQL operator. + public static func | (lhs: some SQLExpressible, rhs: Self) -> SQLExpression { + .associativeBinary(.bitwiseOr, [lhs.sqlExpression, rhs.sqlExpression]) + } + + /// The `|` SQL operator. + public static func | (lhs: Self, rhs: some SQLSpecificExpressible) -> SQLExpression { + .associativeBinary(.bitwiseOr, [lhs.sqlExpression, rhs.sqlExpression]) + } + + /// The `<<` SQL operator. + public static func << (lhs: Self, rhs: some SQLExpressible) -> SQLExpression { + .binary(.leftShift, lhs.sqlExpression, rhs.sqlExpression) + } + + /// The `<<` SQL operator. + public static func << (lhs: some SQLExpressible, rhs: Self) -> SQLExpression { + .binary(.leftShift, lhs.sqlExpression, rhs.sqlExpression) + } + + /// The `<<` SQL operator. + public static func << (lhs: Self, rhs: some SQLSpecificExpressible) -> SQLExpression { + .binary(.leftShift, lhs.sqlExpression, rhs.sqlExpression) + } + + /// The `>>` SQL operator. + public static func >> (lhs: Self, rhs: some SQLExpressible) -> SQLExpression { + .binary(.rightShift, lhs.sqlExpression, rhs.sqlExpression) + } + + /// The `>>` SQL operator. + public static func >> (lhs: some SQLExpressible, rhs: Self) -> SQLExpression { + .binary(.rightShift, lhs.sqlExpression, rhs.sqlExpression) + } + + /// The `>>` SQL operator. + public static func >> (lhs: Self, rhs: some SQLSpecificExpressible) -> SQLExpression { + .binary(.rightShift, lhs.sqlExpression, rhs.sqlExpression) + } + + /// The `~` SQL operator. + public static prefix func ~ (value: Self) -> SQLExpression { + .unary(.bitwiseNot, value.sqlExpression) + } +} + // MARK: - Like Operator extension SQLSpecificExpressible { diff --git a/README.md b/README.md index 945cffc81..70709602b 100644 --- a/README.md +++ b/README.md @@ -4033,6 +4033,15 @@ GRDB comes with a Swift version of many SQLite [built-in operators](https://sqli When the sequence is empty, `joined(operator: .add)` returns 0, and `joined(operator: .multiply)` returns 1. +- `&`, `|`, `~`, `<<`, `>>` + + Bitwise operations (bitwise and, or, not, left shift, right shift) are derived from their Swift equivalent: + + ```swift + // SELECT mask & 2 AS isRocky FROM planet + Planet.select((Column("mask") & 2).forKey("isRocky")) + ``` + - `||` Concatenate several strings: diff --git a/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift b/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift index c4fc94e47..57a2863cf 100644 --- a/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift +++ b/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift @@ -1311,6 +1311,156 @@ class QueryInterfaceExpressionsTests: GRDBTestCase { } + // MARK: - Bitwise expressions + + func testPrefixBitwiseNotOperator() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(~Col.age)), + "SELECT * FROM \"readers\" WHERE ~\"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(~(Col.age + 1))), + "SELECT * FROM \"readers\" WHERE ~(\"age\" + 1)") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(~(~Col.age + 1))), + "SELECT * FROM \"readers\" WHERE ~((~\"age\") + 1)") + } + + func testInfixLeftShiftOperator() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(Col.age << 2)), + "SELECT * FROM \"readers\" WHERE \"age\" << 2") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(2 << Col.age)), + "SELECT * FROM \"readers\" WHERE 2 << \"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filterWhenConnected { _ in 2 << 2 }), + "SELECT * FROM \"readers\" WHERE 8") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(Col.age << Col.age)), + "SELECT * FROM \"readers\" WHERE \"age\" << \"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter((Col.age << Col.age) << 1)), + "SELECT * FROM \"readers\" WHERE (\"age\" << \"age\") << 1") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(1 << [Col.age > 1, Col.age == nil].joined(operator: .and))), + "SELECT * FROM \"readers\" WHERE 1 << ((\"age\" > 1) AND (\"age\" IS NULL))") + } + + func testInfixRightShiftOperator() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(Col.age >> 2)), + "SELECT * FROM \"readers\" WHERE \"age\" >> 2") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(2 >> Col.age)), + "SELECT * FROM \"readers\" WHERE 2 >> \"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filterWhenConnected { _ in 8 >> 2 }), + "SELECT * FROM \"readers\" WHERE 2") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(Col.age >> Col.age)), + "SELECT * FROM \"readers\" WHERE \"age\" >> \"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter((Col.age >> Col.age) >> 1)), + "SELECT * FROM \"readers\" WHERE (\"age\" >> \"age\") >> 1") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(1 >> [Col.age > 1, Col.age == nil].joined(operator: .and))), + "SELECT * FROM \"readers\" WHERE 1 >> ((\"age\" > 1) AND (\"age\" IS NULL))") + } + + func testInfixBitwiseAndOperator() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(Col.age & 2)), + "SELECT * FROM \"readers\" WHERE \"age\" & 2") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(2 & Col.age)), + "SELECT * FROM \"readers\" WHERE 2 & \"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filterWhenConnected { _ in 2 & 2 }), + "SELECT * FROM \"readers\" WHERE 2") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(Col.age & Col.age)), + "SELECT * FROM \"readers\" WHERE \"age\" & \"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(2 & (Col.age & Col.age))), + "SELECT * FROM \"readers\" WHERE 2 & \"age\" & \"age\"") + } + + func testJoinedBitwiseAndOperator() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.select([].joined(operator: .bitwiseAnd))), + "SELECT -1 FROM \"readers\"") + + XCTAssertEqual( + sql(dbQueue, tableRequest.select([Col.age, Col.age].joined(operator: .bitwiseAnd))), + "SELECT \"age\" & \"age\" FROM \"readers\"") + + XCTAssertEqual( + sql(dbQueue, tableRequest.select([Col.age, 2.databaseValue, Col.age].joined(operator: .bitwiseAnd))), + "SELECT \"age\" & 2 & \"age\" FROM \"readers\"") + + // Flattened + XCTAssertEqual( + sql(dbQueue, tableRequest.select([ + [Col.age, 1.databaseValue].joined(operator: .bitwiseAnd), + [2.databaseValue, Col.age].joined(operator: .bitwiseAnd), + ].joined(operator: .bitwiseAnd))), + "SELECT \"age\" & 1 & 2 & \"age\" FROM \"readers\"") + } + + func testInfixBitwiseOrOperator() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(Col.age | 2)), + "SELECT * FROM \"readers\" WHERE \"age\" | 2") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(2 | Col.age)), + "SELECT * FROM \"readers\" WHERE 2 | \"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filterWhenConnected { _ in 2 | 2 }), + "SELECT * FROM \"readers\" WHERE 2") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(Col.age | Col.age)), + "SELECT * FROM \"readers\" WHERE \"age\" | \"age\"") + XCTAssertEqual( + sql(dbQueue, tableRequest.filter(2 | (Col.age | Col.age))), + "SELECT * FROM \"readers\" WHERE 2 | \"age\" | \"age\"") + } + + func testJoinedBitwiseOrOperator() throws { + let dbQueue = try makeDatabaseQueue() + + XCTAssertEqual( + sql(dbQueue, tableRequest.select([].joined(operator: .bitwiseOr))), + "SELECT 0 FROM \"readers\"") + + XCTAssertEqual( + sql(dbQueue, tableRequest.select([Col.age, Col.age].joined(operator: .bitwiseOr))), + "SELECT \"age\" | \"age\" FROM \"readers\"") + + XCTAssertEqual( + sql(dbQueue, tableRequest.select([Col.age, 2.databaseValue, Col.age].joined(operator: .bitwiseOr))), + "SELECT \"age\" | 2 | \"age\" FROM \"readers\"") + + // Flattened + XCTAssertEqual( + sql(dbQueue, tableRequest.select([ + [Col.age, 1.databaseValue].joined(operator: .bitwiseOr), + [2.databaseValue, Col.age].joined(operator: .bitwiseOr), + ].joined(operator: .bitwiseOr))), + "SELECT \"age\" | 1 | 2 | \"age\" FROM \"readers\"") + } + // MARK: - IFNULL expression func testIfNull() throws {