SQL: Add HasColumn() to check if a result contains a named column.

This commit is contained in:
Daniel Vassdal 2015-03-02 03:06:55 +01:00 committed by Peter Powell
parent 4ec475ba10
commit d38f2ad213
4 changed files with 62 additions and 3 deletions

View File

@ -122,6 +122,15 @@ class SQL::Result : public classbase
* @param result A reference to the vector to store column names in.
*/
virtual void GetCols(std::vector<std::string>& result) = 0;
/**
* Check if there's a column with the specified name in the result
*
* @param the column name
* @param on success, this is the column index
* @returns true, or false if the column is not found
*/
virtual bool HasColumn(const std::string& column, size_t& index) = 0;
};
/** SQL::Error holds the error state of a request.

View File

@ -203,6 +203,19 @@ class MySQLresult : public SQL::Result
result.assign(colnames.begin(), colnames.end());
}
bool HasColumn(const std::string& column, size_t& index)
{
for (size_t i = 0; i < colnames.size(); ++i)
{
if (colnames[i] == column)
{
index = i;
return true;
}
}
return false;
}
SQL::Field GetValue(int row, int column)
{
if ((row >= 0) && (row < rows) && (column >= 0) && (column < (int)fieldlists[row].size()))

View File

@ -87,6 +87,16 @@ class PgSQLresult : public SQL::Result
PGresult* res;
int currentrow;
int rows;
std::vector<std::string> colnames;
void getColNames()
{
colnames.resize(PQnfields(res));
for(unsigned int i=0; i < colnames.size(); i++)
{
colnames[i] = PQfname(res, i);
}
}
public:
PgSQLresult(PGresult* result) : res(result), currentrow(0)
{
@ -107,11 +117,25 @@ class PgSQLresult : public SQL::Result
void GetCols(std::vector<std::string>& result) CXX11_OVERRIDE
{
result.resize(PQnfields(res));
for(unsigned int i=0; i < result.size(); i++)
if (colnames.empty())
getColNames();
result = colnames;
}
bool HasColumn(const std::string& column, size_t& index)
{
if (colnames.empty())
getColNames();
for (size_t i = 0; i < colnames.size(); ++i)
{
result[i] = PQfname(res, i);
if (colnames[i] == column)
{
index = i;
return true;
}
}
return false;
}
SQL::Field GetValue(int row, int column)

View File

@ -82,6 +82,19 @@ class SQLite3Result : public SQL::Result
{
result.assign(columns.begin(), columns.end());
}
bool HasColumn(const std::string& column, size_t& index)
{
for (size_t i = 0; i < columns.size(); ++i)
{
if (columns[i] == column)
{
index = i;
return true;
}
}
return false;
}
};
class SQLConn : public SQL::Provider