stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.67k stars 1.56k forks source link

Expressions as argument #947

Closed karjaubayev closed 3 years ago

karjaubayev commented 5 years ago

Build info: Xcode 10.2 Swift 5.1 CocoaPods

I am very confused with Expression type. I wanted to write helper function to my Models, but I can't pass Expression as arguments.

struct Category {
let id: Int
let name: String
}
protocol SQLTable {
    static var tableName: String { get }
    static var expressions: [Expressible?] { get }
}

Here in protocol I couldn't assign following types Expression<Any>, Expression<Value>, Expression<Void>

extension Category: SQLTable {
    static var expressions: [Expressible?] {
        let id = Expression<Int64>("id")
        let name = Expression<String>("name")
        return [id, name]
    }

    static var tableName: String {
        return String(describing: self)
    }

}
class DatabaseManager {
static let sharedInstance = DatabaseManager()

let db: SQLite.Connection = {
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        return try! Connection("\(path)/db.sqlite3")
    }()
}
extension DatabaseManager {
    func createTable(table: Table, expressions: [Expressible]) {
        try sharedInstance.db.run(table.create { t in 
            for expression in expressions {
                t.column(expression)
        }) 
    }
}

Then in line "t.column" I can't pass expression as it's Expressible type

nathanfallet commented 3 years ago

Please ask on StackOverflow for this kind of questions (we are tracking bugs on GitHub)