One function to rule them all

I finish the initial implementation of the function that will process database command objects for SQLite databases.It is large somewhat complex and untested so I thought I would share.

DatabaseResultSet Execute(DatabaseCommand& databaseCommand)
{
DatabaseResultSet databaseResultSet;

const char* tail=NULL;
sqlite3_stmt* statement=NULL;
size_t row=0;

rc = sqlite3_prepare_v2(
mDatabase, /* Database handle */
databaseCommand.GetQuery().c_str(), /* SQL statement, UTF-8 encoded */
databaseCommand.GetQuery().length(),/* Maximum length of zSql in bytes. */
&statement, /* OUT: Statement handle */
&tail /* OUT: Pointer to unused portion of zSql */
);
if(rc!=SQLITE_OK)
{
return databaseResultSet; //no need to call finalize because prepare failed.
}


BOOST_FOREACH(DatabaseParameter parameter, databaseCommand.GetDatabaseParameters())
{
switch(parameter.GetType())
{
case DATABASE_DATATYPE_NULL:
break;
case DATABASE_DATATYPE_INTEGER:
sqlite3_bind_int(statement,sqlite3_bind_parameter_index(statement,parameter.GetName().c_str()),parameter.GetValue());
break;
case DATABASE_DATATYPE_TEXT:
sqlite3_bind_text(statement,sqlite3_bind_parameter_index(statement,parameter.GetName().c_str()),parameter.GetValue().c_str(),parameter.GetLength(),SQLITE_STATIC);
break;
case DATABASE_DATATYPE_DOUBLE:
sqlite3_bind_double(statement,sqlite3_bind_parameter_index(statement,parameter.GetName().c_str()),parameter.GetValue());
break;
case DATABASE_DATATYPE_BLOB:
break;
default:
break;
}
}

do
{
rc = sqlite3_step(statement);
}
while(rc==SQLITE_BUSY); //make sure this actually happens.

if(rc!=SQLITE_ROW && rc!=SQLITE_DONE)
{
goto JOBDONE;
}

for(int i=0;i(row,i,sqlite3_column_text(statement,i));
}

do
{
rc = sqlite3_step(statement);
}
while(rc==SQLITE_BUSY); //make sure this actually happens.
row++;
}


JOBDONE:
rc = sqlite3_finalize(statement);

return databaseResultSet;
}

Comments

Popular posts from this blog

VK9 - Milestone8 Completed

VK9 - Milestone13 Completed

VK9 - Milestone16 Completed