2

How can I read the table's name after having created one width Table(name)?

Example:

let tab = Table("myTable")
let name = ??

Actually, this example does not make sense, but im my use case the variable "tab" is a class property and the class does not save the table name in a string.

Bernd
  • 169
  • 2
  • 9

2 Answers2

0

i do not fully understand your example. Basically when you create the tables in a class as you say i would gather it would look something like this:

class tab {

func Table(vString:String)
    static let TABLE_NAME = vString     //setting Name of Table in the class    
    static let table = Table(TABLE_NAME)// 

    static var kundeID = Expression<Int64>("kundeID")   //Setting Fields of the Table
    static let lastname = Expression<String>("lastname")
    static let firstname = Expression<String>("firstname")
    //.... until you got all the fields

//Creating the table if it does not yet exist.
static func createTable() throws {
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
        throw DataAccessError.Datastore_Connection_Error
    }
    do {
        let _ = try DB.run( table.create(ifNotExists: true) {t in
            t.column(kundeID, primaryKey: true) //primary key
            t.column(lastname) //define same columns as above!
            t.column(firstname)
            // and so on
            })

    } catch _ {
        // Error throw if table already exists
    }

}

Thus you know what the table name actually is going to be since you define it with your call of the Function Table. I don't think you can actually do that any other way. Have a look at those Links: https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md http://masteringswift.blogspot.ch/2015/09/create-data-access-layer-with.html

These should help you get going.

  • The class has only defined a constant with the assigned value Table("name"). I was wondering whether there is a smarter way to the get table name. – Bernd Feb 23 '16 at 07:32
  • Can you explain to me what you mean by "get" the table name? Do you want to read from the Database what tables exist? – René Blaser Feb 23 '16 at 16:48
  • Usually the constant filled with Table(name) is suffient. But I have to check for table existence after app start and therefore the table name as string is needed (query to sqlite_master). I don't think that it is effient trying to create the table with ifNotExists each time a query runs. – Bernd Feb 25 '16 at 07:13
0

I use these extensions..

extension Table{
  var getName: String{
    let cadena:NSString = self.asSQL()  //"SELECT * FROM \"NombreDeTabla\""
    return cadena.substringWithRange(NSRange (location: 15, length: cadena.length-16))
  }
}

Same with Columns:

extension Expression{
  var getName: String{
    let cadena:NSString = self.asSQL() //\"NombreDeColumna\"
    return cadena.substringWithRange(NSRange (location: 1, length: cadena.length-2))
  }
}

It just makes a substring from .asSQL() which always gives the same expression

Carlos.V
  • 409
  • 6
  • 13