Skip to content

Commit

Permalink
Some DBModuleTests succeeding
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Sep 27, 2023
1 parent a54dec3 commit 312076f
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 282 deletions.
26 changes: 17 additions & 9 deletions DBModule/Sources/DBModule/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ class DatabaseManager: NSObject, MinisqlDBProtocol {
domain: "ArgumentError", code: 1,
userInfo: [NSLocalizedDescriptionKey: "Query or arguments are nil"])
}

let bindings = ValueUtil.toBindingsArray(args)

let statement = try db.prepare(query)

var rows: [Statement.Element] = []
Expand All @@ -92,21 +93,17 @@ class TransactionManager: NSObject, MinisqlTxProtocol {
}

private func begin() throws {
print("BEGIN TX")
savepointName = "Savepoint\(UUID().uuidString)"
if let savepointName = savepointName {
try database.run("SAVEPOINT '\(savepointName)'")
}
}

func commit() throws {
print("COMMIT TX")
if let savepointName = savepointName {
print("RELEASING SAVEPOINT")
try database.run("RELEASE '\(savepointName)'")
}
savepointName = nil
print("CHANGES '\(database.changes)'")
}

func rollback() throws {
Expand All @@ -131,7 +128,16 @@ class TransactionManager: NSObject, MinisqlTxProtocol {
try begin()
}

try statement.run(bindings)
do {
try statement.run(bindings)
} catch {
switch error {
case let SQLite.Result.error(message, _, _):
throw message
default:
throw error
}
}
return QueryResult(changes: database.changes)
}

Expand Down Expand Up @@ -238,7 +244,6 @@ class ValueArrayHandler: NSObject, MinisqlValuesProtocol {

public func get(_ index: Int) -> MinisqlValue? {
guard index < values.count else {
print("Error: Index out of bounds while trying to get value.")
return nil
}
return values[index]
Expand All @@ -250,20 +255,23 @@ class ValueArrayHandler: NSObject, MinisqlValuesProtocol {

func set(_ index: Int, value: MinisqlValue?) {
guard index < values.count else {
print("Error: Index out of bounds while trying to set value.")
return
}

guard let value = value else {
print("Error: Attempted to set nil value.")
return
}

values[index] = value
}
}

extension Date {
static var currentTimeStamp: Int64 {
return Int64(Date().timeIntervalSince1970 * 1000)
}
}

extension String: LocalizedError {
public var errorDescription: String? { return self }
}
247 changes: 124 additions & 123 deletions DBModule/Sources/DBModule/ValueUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,137 +9,138 @@ import Foundation
import Internalsdk
import SQLite


public class ValueUtil {
// Define the types constants
public static let TYPE_BYTES = Int(MinisqlValueTypeBytes)
public static let TYPE_STRING = Int(MinisqlValueTypeString)
public static let TYPE_INT = Int(MinisqlValueTypeInt)
public static let TYPE_BOOL = Int(MinisqlValueTypeBool)

public static func makeValue(from anyValue: Any) -> MinisqlValue {
let value: MinisqlValue!

switch anyValue {
case is String:
value = MinisqlNewValueString(anyValue as! String)
case is Int:
value = MinisqlNewValueInt(anyValue as! Int)
case is Bool:
value = MinisqlNewValueBool(anyValue as! Bool)
case is UInt8:
let blob = anyValue as! SQLite.Blob
let data = Data(blob.bytes)
value = MinisqlNewValueBytes(data)
default:
fatalError("Unsupported type \(type(of: anyValue)) with value: \(anyValue)")
}
return value
// Define the types constants
public static let TYPE_BYTES = Int(MinisqlValueTypeBytes)
public static let TYPE_STRING = Int(MinisqlValueTypeString)
public static let TYPE_INT = Int(MinisqlValueTypeInt)
public static let TYPE_BOOL = Int(MinisqlValueTypeBool)

public static func makeValue(from anyValue: Any) -> MinisqlValue {
let value: MinisqlValue!

switch anyValue {
case is String:
value = MinisqlNewValueString(anyValue as! String)
case is Int:
value = MinisqlNewValueInt(anyValue as! Int)
case is Bool:
value = MinisqlNewValueBool(anyValue as! Bool)
case is UInt8:
let blob = anyValue as! SQLite.Blob
let data = Data(blob.bytes)
value = MinisqlNewValueBytes(data)
default:
fatalError("Unsupported type \(type(of: anyValue)) with value: \(anyValue)")
}

public static func convertFromMinisqlValue(from internalsdkValue: MinisqlValue) -> Any? {
switch internalsdkValue.type {
case TYPE_STRING:
return internalsdkValue.string()
case TYPE_INT:
return Int(internalsdkValue.int_() as Int)
case TYPE_BYTES:
return internalsdkValue.bytes()!
case TYPE_BOOL:
return internalsdkValue.bool_()
default:
fatalError("Unsupported type")
}
return value
}

public static func convertFromMinisqlValue(from internalsdkValue: MinisqlValue) -> Any? {
switch internalsdkValue.type {
case TYPE_STRING:
return internalsdkValue.string()
case TYPE_INT:
return Int(internalsdkValue.int_() as Int)
case TYPE_BYTES:
return internalsdkValue.bytes()!
case TYPE_BOOL:
return internalsdkValue.bool_()
default:
fatalError("Unsupported type")
}

public static func toBindingsArray(_ args: MinisqlValuesProtocol) -> [Binding?] {
var bindings = [Binding?]()
for i in 0..<args.len() {
guard let arg = args.get(i) else {
bindings.append(nil)
continue
}
switch arg.type {
case TYPE_STRING:
bindings.append(arg.string())
case TYPE_INT:
bindings.append(arg.int_())
case TYPE_BOOL:
bindings.append(arg.bool_())
case TYPE_BYTES:
if let bytes = arg.bytes() {
let byteArray = [UInt8](bytes)
bindings.append(Blob(bytes: byteArray))
} else {
bindings.append(nil)
}

default:
bindings.append(nil)
}
}

public static func toBindingsArray(_ args: MinisqlValuesProtocol) -> [Binding?] {
var bindings = [Binding?]()
for i in 0..<args.len() {
guard let arg = args.get(i) else {
bindings.append(nil)
continue
}
switch arg.type {
case TYPE_STRING:
bindings.append(arg.string())
case TYPE_INT:
bindings.append(arg.int_())
case TYPE_BOOL:
bindings.append(arg.bool_())
case TYPE_BYTES:
if let bytes = arg.bytes() {
let byteArray = [UInt8](bytes)
bindings.append(Blob(bytes: byteArray))
} else {
bindings.append(nil)
}
return bindings

default:
bindings.append(nil)
}
}

static func setValueFromBinding(binding: Binding, value: MinisqlValue) {
switch binding.bindingType {
case "String":
value.setString(binding as? String)
case "Int64":
value.setInt(Int(binding as! Int64))
case "Bool":
value.setBool(Bool(binding as! Bool))
case "Blob":
let blob = binding as! Blob
let data = Data(blob.bytes)
value.setBytes(data)
default:
fatalError("Unsupported SQLite.Binding type: \(binding.bindingType)")
}
return bindings
}

static func setValueFromBinding(binding: Binding, value: MinisqlValue) {
switch binding.bindingType {
case "String":
value.setString(binding as? String)
case "Int64":
value.setInt(Int(binding as! Int64))
case "Bool":
value.setBool(Bool(binding as! Bool))
case "Blob":
let blob = binding as! Blob
let data = Data(blob.bytes)
value.setBytes(data)
default:
fatalError("Unsupported SQLite.Binding type: \(binding.bindingType)")
}

public static func convertToMinisqlValue(_ anyValue: Any) -> MinisqlValue? {
switch anyValue {
case is String:
return MinisqlNewValueString(anyValue as! String)
case is Int:
return MinisqlNewValueInt(anyValue as! Int)
case is Bool:
return MinisqlNewValueBool(anyValue as! Bool)
case is [Any]: // For arrays
if let jsonData = try? JSONSerialization.data(withJSONObject: anyValue, options: []),
let jsonString = String(data: jsonData, encoding: .utf8) {
return MinisqlNewValueString(jsonString)
}
case is [String: Any]: // For dictionaries
if let jsonData = try? JSONSerialization.data(withJSONObject: anyValue, options: []),
let jsonString = String(data: jsonData, encoding: .utf8) {
return MinisqlNewValueString(jsonString)
}
default:
return MinisqlNewValueString("\(anyValue)")
}
return nil
}

public static func convertToMinisqlValue(_ anyValue: Any) -> MinisqlValue? {
switch anyValue {
case is String:
return MinisqlNewValueString(anyValue as! String)
case is Int:
return MinisqlNewValueInt(anyValue as! Int)
case is Bool:
return MinisqlNewValueBool(anyValue as! Bool)
case is [Any]: // For arrays
if let jsonData = try? JSONSerialization.data(withJSONObject: anyValue, options: []),
let jsonString = String(data: jsonData, encoding: .utf8)
{
return MinisqlNewValueString(jsonString)
}
case is [String: Any]: // For dictionaries
if let jsonData = try? JSONSerialization.data(withJSONObject: anyValue, options: []),
let jsonString = String(data: jsonData, encoding: .utf8)
{
return MinisqlNewValueString(jsonString)
}
default:
return MinisqlNewValueString("\(anyValue)")
}

return nil
}

}
extension Binding {
var bindingType: String {
switch self {
case is Int64:
return "Int64"
case is Double:
return "Double"
case is String:
return "String"
case is Bool:
return "Bool"
case is Blob:
return "Blob"
case is NSNumber:
return "NSNumber"
default:
return "Unknown"
}
var bindingType: String {
switch self {
case is Int64:
return "Int64"
case is Double:
return "Double"
case is String:
return "String"
case is Bool:
return "Bool"
case is Blob:
return "Blob"
case is NSNumber:
return "NSNumber"
default:
return "Unknown"
}
}
}
Loading

0 comments on commit 312076f

Please sign in to comment.