Skip to content

Commit

Permalink
DBBrowserDB: Use SQL grammar for getting fields of table
Browse files Browse the repository at this point in the history
When reading in the DB layout use our SQL grammar parser to determine
the fields of a table. This simplifies the code a bit and might also
give more detailed information in some situations.
  • Loading branch information
MKleusberg committed Jun 4, 2013
1 parent b07f0a5 commit 2463116
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/sqlitedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,26 +876,25 @@ void DBBrowserDB::updateSchema( )
objectMap::Iterator it;
for ( it = objMap.begin(); it != objMap.end(); ++it )
{
if((*it).gettype() == "table" || (*it).gettype() == "view")
// Use our SQL parser to generate the field list for tables. For views we currently have to fall back to the
// pragma SQLite offers.
if((*it).gettype() == "table")
{
sqlb::Table t((*it).getname());
(*it).fldmap = t.parseSQL((*it).getsql()).fields();
} else if((*it).gettype() == "view") {
statement = QString("PRAGMA TABLE_INFO(`%1`);").arg((*it).getname());
logSQL(statement, kLogMsg_App);
err=sqlite3_prepare_v2(_db,statement.toUtf8(),statement.length(),
&vm, &tail);
if (err == SQLITE_OK){
(*it).fldmap.clear();
while ( sqlite3_step(vm) == SQLITE_ROW ){
if (sqlite3_column_count(vm)==6)
{
QString val_name = QString::fromUtf8((const char *)sqlite3_column_text(vm, 1));
QString val_type = QString::fromUtf8((const char *)sqlite3_column_text(vm, 2));
bool val_nn = sqlite3_column_int(vm, 3) == 1;
QString val_default = QString::fromUtf8((const char *)sqlite3_column_text(vm, 4));
int val_pk = sqlite3_column_int(vm, 5);
if(val_pk == 1)
val_type.append(QString(" PRIMARY KEY"));

sqlb::FieldPtr f(new sqlb::Field(val_name, val_type, val_nn, val_default));
sqlb::FieldPtr f(new sqlb::Field(val_name, val_type));
(*it).addField(f);
}
}
Expand Down

0 comments on commit 2463116

Please sign in to comment.