Fix #23 optimized Statement::getColumn() by name

- fix Statement::getColumn(apName) provided by #45 but was not working
  instead of using #46 that conflicts with current master
- rework it by using a map of columns name as
  a cache populated the first time the method is called
- add corresponding Unit Test
This commit is contained in:
Sébastien Rombauts
2015-04-30 18:39:46 +02:00
parent d45ec996a3
commit 7fbfc29677
3 changed files with 72 additions and 15 deletions

View File

@@ -258,9 +258,9 @@ Column Statement::getColumn(const int aIndex)
// Return a copy of the column data specified by its column name starting at 0
// (use the Column copy-constructor)
Column Statement::getColumn(const char* aName)
Column Statement::getColumn(const char* apName)
{
int aIndex = -1;
int Index = -1;
if (false == mbOk)
{
@@ -268,18 +268,28 @@ Column Statement::getColumn(const char* aName)
}
else
{
for (int i = 0; i < mColumnCount; ++i) {
if (sqlite3_column_name(mStmtPtr, i) == aName)
break;
if (mColumnNames.empty())
{
for (int i = 0; i < mColumnCount; ++i)
{
const char* pName = sqlite3_column_name(mStmtPtr, i);
mColumnNames[pName] = i;
}
}
if ((aIndex < 0) || (aIndex >= mColumnCount)) {
const TColumnNames::const_iterator iIndex = mColumnNames.find(apName);
if (iIndex != mColumnNames.end())
{
Index = (*iIndex).second;
}
else
{
throw SQLite::Exception("Column index out of range");
}
}
// Share the Statement Object handle with the new Column created
return Column(mStmtPtr, aIndex);
return Column(mStmtPtr, Index);
}
// Test if the column is NULL