0

I am continuing the conversion of a number of my apps from SQLite.sift to GRDB.swift. I converted my structs to add Codable, FetchableRecord so as to have them work better with GRDB. This was suggested in a reply to my first post on this subject.

struct FigureList: Codable, FetchableRecord
{
    let figID: Int64
    var notes: String?
    var theDescription: String
    var obrienNum: String
    var manufNum: String?
    var catagory: String?
}

This is the piece of code I'm looking for help to redo so it will work with GRDB. My apps use this type of code to build arrays from the database table. Pretty standard process.

static func getFigureList() -> [FigureList]
{
    var theArray = [FigureList]()
    let theTable = Table(gTheCollection)

    let figID = Expression<Int64>("FigureID")
    let notes = Expression<String>("Notes")
    let theDescription = Expression<String>("theDescription")
    let obrienNum = Expression<String>("ObrienNum")
    let manufNum = Expression<String>("ManufNum")
    let theCatagory = Expression<String>("Category")

    do {
        for figures in try Database.shared.databaseConnection!.prepare(theTable.order(obrienNum)) {

            theArray.append(FigureList(figID: figures[figID],
                                       notes: figures[notes],
                                       theDescription: figures[theDescription],
                                       obrienNum: figures[obrienNum],
                                       manufNum: figures[manufNum],
                                       catagory: figures[theCatagory]))
        }
    } catch {
        print("Fetching figure list failed: \(error)")
    }
    return theArray
}

This is what I have come up with so far. It doesn't produce any warnings or errors but then again I'm pretty sure it's not total correct. Again, thanks in advance for any help.

static func getFigList() -> [FigureList]
{
    var theArray = [FigureList]()
    let theTable = gTheCollection
    
    let figID = Column("FigureID")
    let notes = Column("Notes")
    let theDescription = Column("theDescription")
    let obrienNum = Column("ObrienNum")
    let manufNum = Column("ManufNum")
    let theCatagory = Column("Category")
    
    do {
        try Database.shared.databaseConnection!.read { db in
            let figures = try Row.fetchOne(db, sql: "SELECT * FROM = ? ORDER BY ObrienNum", arguments: [theTable])
            
            theArray.append(FigureList.init(figID: figures![figID],
                                            notes: figures![notes],
                                            theDescription: figures![theDescription],
                                            obrienNum: figures![obrienNum],
                                            manufNum: figures![manufNum],
                                            catagory: figures![theCatagory]))
        }
    } catch {
        print("Fetching figure list failed: \(error)")
    }
    
    return theArray
}
Quailcreek
  • 125
  • 2
  • 9

1 Answers1

1

I converted my structs to add Codable, FetchableRecord so as to have them work better with GRDB.

Good. Now it's very simple:

static func getFigList() throws -> [FigureList] {
    return try Database.shared.databaseConnection!.read { db in
        // SELECT * FROM FigureList ORDER BY ObrienNum
        return FigureList.order(Column("ObrienNum")).fetchAll(db)
    }
}
Gwendal Roué
  • 3,949
  • 15
  • 34
  • 1
    I know it is not recommended to send a link to the documentation in a StackOverflow answer. Yet it is pretty obvious you did not check the GRDB doc enough. Uses cases like yours are covered with great detail in the doc. Put it on your side! – Gwendal Roué Jan 06 '21 at 09:56