Skip to content

Commit 6f51363

Browse files
committed
added where binary conditions
1 parent 8866dd8 commit 6f51363

16 files changed

+338
-116
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Foundation
2+
3+
public class BinaryOperator: Expression {
4+
var lhs: Expression
5+
var rhs: Expression
6+
var operatorType: BinaryOperatorType
7+
8+
init(lhs: Expression, rhs: Expression, operatorType: BinaryOperatorType) {
9+
self.lhs = lhs
10+
self.rhs = rhs
11+
self.operatorType = operatorType
12+
}
13+
}
14+
15+
extension BinaryOperator: Serializable {
16+
public func serialize(with schemaProvider: SchemaProvider) throws -> String {
17+
let leftString = try self.lhs.serialize(with: schemaProvider)
18+
let rightString = try self.rhs.serialize(with: schemaProvider)
19+
return "\(leftString) \(self.operatorType) \(rightString)"
20+
}
21+
}
22+
23+
public func equal(lhs: Expression, rhs: Expression) -> BinaryOperator {
24+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .equal)
25+
}
26+
27+
public func notEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
28+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .notEqual)
29+
}
30+
31+
public func lesserThan(lhs: Expression, rhs: Expression) -> BinaryOperator {
32+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .lesserThan)
33+
}
34+
35+
public func lesserOrEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
36+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .lesserOrEqual)
37+
}
38+
39+
public func greaterThan(lhs: Expression, rhs: Expression) -> BinaryOperator {
40+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .greaterThan)
41+
}
42+
43+
public func greaterOrEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
44+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .greaterOrEqual)
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Foundation
2+
3+
enum BinaryOperatorType {
4+
// case add
5+
// case sub
6+
// case mul
7+
// case div
8+
case equal
9+
case notEqual
10+
case lesserThan
11+
case lesserOrEqual
12+
case greaterThan
13+
case greaterOrEqual
14+
}
15+
16+
extension BinaryOperatorType: CustomStringConvertible {
17+
var description: String {
18+
switch self {
19+
case .equal: return "=="
20+
case .notEqual: return "!="
21+
case .lesserThan: return "<"
22+
case .greaterThan: return ">"
23+
case .lesserOrEqual: return "<="
24+
case .greaterOrEqual: return ">="
25+
}
26+
}
27+
}

Sources/SQLiteORM/ConflictClause.swift renamed to Sources/SQLiteORM/AST/ConflictClause.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public enum ConflictClause {
99
}
1010

1111
extension ConflictClause: Serializable {
12-
func serialize() -> String {
12+
public func serialize(with schemaProvider: SchemaProvider) -> String {
1313
var res = "ON CONFLICT "
1414
switch self {
1515
case .rollback: res += "ROLLBACK"

Sources/SQLiteORM/Order.swift renamed to Sources/SQLiteORM/AST/Order.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public enum Order {
66
}
77

88
extension Order: Serializable {
9-
func serialize() -> String {
9+
public func serialize(with schemaProvider: SchemaProvider) -> String {
1010
switch self {
1111
case .asc: return "ASC"
1212
case .desc: return "DESC"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Foundation
2+
3+
public protocol SelectConstraint: Serializable {
4+
5+
}

Sources/SQLiteORM/AST/Where.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Foundation
2+
3+
public class Where: SelectConstraint {
4+
var expression: Expression
5+
6+
init(expression: Expression) {
7+
self.expression = expression
8+
}
9+
}
10+
11+
extension Where: Serializable {
12+
public func serialize(with schemaProvider: SchemaProvider) throws -> String {
13+
let expressionString = try self.expression.serialize(with: schemaProvider)
14+
return "WHERE \(expressionString)"
15+
}
16+
}
17+
18+
public func where_(_ expression: Expression) -> Where {
19+
return Where(expression: expression)
20+
}

Sources/SQLiteORM/AnyColumn.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ public class AnyColumn: NSObject {
7777
}
7878

7979
extension AnyColumn: Serializable {
80-
func serialize() -> String {
80+
public func serialize(with schemaProvider: SchemaProvider) -> String {
8181
let typeString = self.sqliteTypeName
8282
var res = "\(self.name) \(typeString)"
8383
for constraint in self.constraints {
84-
let constraintString = constraint.serialize()
84+
let constraintString = constraint.serialize(with: schemaProvider)
8585
res += " "
8686
res += constraintString
8787
}

Sources/SQLiteORM/ColumnConstraint.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ public enum ColumnConstraint {
77
}
88

99
extension ColumnConstraint: Serializable {
10-
func serialize() -> String {
10+
public func serialize(with schemaProvider: SchemaProvider) -> String {
1111
switch self {
1212
case .primaryKey(let orderMaybe, let conflictClauseMaybe, let autoincrement):
1313
var res = "PRIMARY KEY"
1414
if let order = orderMaybe {
15-
let orderString = order.serialize()
15+
let orderString = order.serialize(with: schemaProvider)
1616
res += " "
1717
res += orderString
1818
}
1919
if let conflictClause = conflictClauseMaybe {
20-
let conflictClauseString = conflictClause.serialize()
20+
let conflictClauseString = conflictClause.serialize(with: schemaProvider)
2121
res += " "
2222
res += conflictClauseString
2323
}
@@ -28,15 +28,15 @@ extension ColumnConstraint: Serializable {
2828
case .notNull(let conflictClauseMaybe):
2929
var res = "NOT NULL"
3030
if let conflictClause = conflictClauseMaybe {
31-
let conflictClauseString = conflictClause.serialize()
31+
let conflictClauseString = conflictClause.serialize(with: schemaProvider)
3232
res += " "
3333
res += conflictClauseString
3434
}
3535
return res
3636
case .unique(let conflictClauseMaybe):
3737
var res = "UNIQUE"
3838
if let conflictClause = conflictClauseMaybe {
39-
let conflictClauseString = conflictClause.serialize()
39+
let conflictClauseString = conflictClause.serialize(with: schemaProvider)
4040
res += " "
4141
res += conflictClauseString
4242
}

Sources/SQLiteORM/Expression.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
protocol Expression: Any {
3+
public protocol Expression: Serializable {
44

55
}
66

@@ -11,3 +11,11 @@ extension Int: Expression {
1111
extension Bool: Expression {
1212

1313
}
14+
15+
extension String: Expression {
16+
17+
}
18+
19+
extension KeyPath: Expression {
20+
21+
}

Sources/SQLiteORM/SelectConstraints.swift

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)