Skip to content

Commit

Permalink
added where binary conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
fnc12 committed Oct 12, 2021
1 parent 8866dd8 commit 6f51363
Show file tree
Hide file tree
Showing 16 changed files with 338 additions and 116 deletions.
45 changes: 45 additions & 0 deletions Sources/SQLiteORM/AST/BinaryOperator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Foundation

public class BinaryOperator: Expression {
var lhs: Expression
var rhs: Expression
var operatorType: BinaryOperatorType

init(lhs: Expression, rhs: Expression, operatorType: BinaryOperatorType) {
self.lhs = lhs
self.rhs = rhs
self.operatorType = operatorType
}
}

extension BinaryOperator: Serializable {
public func serialize(with schemaProvider: SchemaProvider) throws -> String {
let leftString = try self.lhs.serialize(with: schemaProvider)
let rightString = try self.rhs.serialize(with: schemaProvider)
return "\(leftString) \(self.operatorType) \(rightString)"
}
}

public func equal(lhs: Expression, rhs: Expression) -> BinaryOperator {
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .equal)
}

public func notEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .notEqual)
}

public func lesserThan(lhs: Expression, rhs: Expression) -> BinaryOperator {
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .lesserThan)
}

public func lesserOrEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .lesserOrEqual)
}

public func greaterThan(lhs: Expression, rhs: Expression) -> BinaryOperator {
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .greaterThan)
}

public func greaterOrEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .greaterOrEqual)
}
27 changes: 27 additions & 0 deletions Sources/SQLiteORM/AST/BinaryOperatorType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation

enum BinaryOperatorType {
// case add
// case sub
// case mul
// case div
case equal
case notEqual
case lesserThan
case lesserOrEqual
case greaterThan
case greaterOrEqual
}

extension BinaryOperatorType: CustomStringConvertible {
var description: String {
switch self {
case .equal: return "=="
case .notEqual: return "!="
case .lesserThan: return "<"
case .greaterThan: return ">"
case .lesserOrEqual: return "<="
case .greaterOrEqual: return ">="
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public enum ConflictClause {
}

extension ConflictClause: Serializable {
func serialize() -> String {
public func serialize(with schemaProvider: SchemaProvider) -> String {
var res = "ON CONFLICT "
switch self {
case .rollback: res += "ROLLBACK"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public enum Order {
}

extension Order: Serializable {
func serialize() -> String {
public func serialize(with schemaProvider: SchemaProvider) -> String {
switch self {
case .asc: return "ASC"
case .desc: return "DESC"
Expand Down
5 changes: 5 additions & 0 deletions Sources/SQLiteORM/AST/SelectConstraint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public protocol SelectConstraint: Serializable {

}
20 changes: 20 additions & 0 deletions Sources/SQLiteORM/AST/Where.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

public class Where: SelectConstraint {
var expression: Expression

init(expression: Expression) {
self.expression = expression
}
}

extension Where: Serializable {
public func serialize(with schemaProvider: SchemaProvider) throws -> String {
let expressionString = try self.expression.serialize(with: schemaProvider)
return "WHERE \(expressionString)"
}
}

public func where_(_ expression: Expression) -> Where {
return Where(expression: expression)
}
4 changes: 2 additions & 2 deletions Sources/SQLiteORM/AnyColumn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public class AnyColumn: NSObject {
}

extension AnyColumn: Serializable {
func serialize() -> String {
public func serialize(with schemaProvider: SchemaProvider) -> String {
let typeString = self.sqliteTypeName
var res = "\(self.name) \(typeString)"
for constraint in self.constraints {
let constraintString = constraint.serialize()
let constraintString = constraint.serialize(with: schemaProvider)
res += " "
res += constraintString
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/SQLiteORM/ColumnConstraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ public enum ColumnConstraint {
}

extension ColumnConstraint: Serializable {
func serialize() -> String {
public func serialize(with schemaProvider: SchemaProvider) -> String {
switch self {
case .primaryKey(let orderMaybe, let conflictClauseMaybe, let autoincrement):
var res = "PRIMARY KEY"
if let order = orderMaybe {
let orderString = order.serialize()
let orderString = order.serialize(with: schemaProvider)
res += " "
res += orderString
}
if let conflictClause = conflictClauseMaybe {
let conflictClauseString = conflictClause.serialize()
let conflictClauseString = conflictClause.serialize(with: schemaProvider)
res += " "
res += conflictClauseString
}
Expand All @@ -28,15 +28,15 @@ extension ColumnConstraint: Serializable {
case .notNull(let conflictClauseMaybe):
var res = "NOT NULL"
if let conflictClause = conflictClauseMaybe {
let conflictClauseString = conflictClause.serialize()
let conflictClauseString = conflictClause.serialize(with: schemaProvider)
res += " "
res += conflictClauseString
}
return res
case .unique(let conflictClauseMaybe):
var res = "UNIQUE"
if let conflictClause = conflictClauseMaybe {
let conflictClauseString = conflictClause.serialize()
let conflictClauseString = conflictClause.serialize(with: schemaProvider)
res += " "
res += conflictClauseString
}
Expand Down
10 changes: 9 additions & 1 deletion Sources/SQLiteORM/Expression.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

protocol Expression: Any {
public protocol Expression: Serializable {

}

Expand All @@ -11,3 +11,11 @@ extension Int: Expression {
extension Bool: Expression {

}

extension String: Expression {

}

extension KeyPath: Expression {

}
66 changes: 0 additions & 66 deletions Sources/SQLiteORM/SelectConstraints.swift

This file was deleted.

29 changes: 29 additions & 0 deletions Sources/SQLiteORM/Serializable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

public protocol Serializable {
func serialize(with schemaProvider: SchemaProvider) throws -> String
}

extension Int: Serializable {
public func serialize(with schemaProvider: SchemaProvider) throws -> String {
return self.description
}
}

extension Bool: Serializable {
public func serialize(with schemaProvider: SchemaProvider) throws -> String {
return self ? "1" : "0"
}
}

extension String: Serializable {
public func serialize(with schemaProvider: SchemaProvider) throws -> String {
return "'\(self)'"
}
}

extension KeyPath: Serializable {
public func serialize(with schemaProvider: SchemaProvider) throws -> String {
return try schemaProvider.columnNameWithTable(keyPath: self)
}
}
5 changes: 0 additions & 5 deletions Sources/SQLiteORM/StatementSerializator.swift

This file was deleted.

31 changes: 27 additions & 4 deletions Sources/SQLiteORM/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public protocol Initializable {
init()
}

public protocol SchemaProvider {
func columnNameWithTable<T, F>(keyPath: KeyPath<T, F>) throws -> String
}

public class Storage: NSObject {
private let tables: [AnyTable]
private let inMemory: Bool
Expand All @@ -22,7 +26,10 @@ public class Storage: NSObject {
}

convenience init(filename: String, apiProvider: SQLiteApiProvider, tables: [AnyTable]) throws {
try self.init(filename: filename, apiProvider: apiProvider, connection: ConnectionHolderImpl(filename: filename, apiProvider: apiProvider), tables: tables)
try self.init(filename: filename,
apiProvider: apiProvider,
connection: ConnectionHolderImpl(filename: filename, apiProvider: apiProvider),
tables: tables)
}

public convenience init(filename: String, tables: AnyTable...) throws {
Expand Down Expand Up @@ -185,7 +192,7 @@ public class Storage: NSObject {
var sql = "CREATE TABLE '\(name)' ("
let columnsCount = columns.count
for (columnIndex, column) in columns.enumerated() {
let columnString = column.serialize()
let columnString = column.serialize(with: self)
sql += "\(columnString)"
if columnIndex < columnsCount - 1 {
sql += ", "
Expand Down Expand Up @@ -333,11 +340,15 @@ public class Storage: NSObject {
return res
}

public func getAll<T>(_ constraints: SelectConstraintBuilder...) throws -> [T] where T: Initializable {
public func getAll<T>(_ constraints: SelectConstraint...) throws -> [T] where T: Initializable {
guard let anyTable = self.tables.first(where: { $0.type == T.self }) else {
throw Error.typeIsNotMapped
}
let sql = "SELECT * FROM \(anyTable.name)"
var sql = "SELECT * FROM \(anyTable.name)"
for constraint in constraints {
let constraintsString = try constraint.serialize(with: self)
sql += " \(constraintsString)"
}
let connectionRef = try ConnectionRef(connection: self.connection)
let statement = try connectionRef.prepare(sql: sql)
let table = anyTable as! Table<T>
Expand Down Expand Up @@ -592,6 +603,18 @@ public class Storage: NSObject {
}
}

extension Storage: SchemaProvider {
public func columnNameWithTable<T, F>(keyPath: KeyPath<T, F>) throws -> String {
guard let anyTable = self.tables.first(where: { $0.type == T.self }) else {
throw Error.typeIsNotMapped
}
guard let column = anyTable.columns.first(where: { $0.keyPath == keyPath }) else {
throw Error.columnNotFound
}
return "\(anyTable.name).\(column.name)"
}
}

extension Storage {

public func beginTransaction() throws {
Expand Down
Loading

0 comments on commit 6f51363

Please sign in to comment.