aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/base/database.cpp25
-rw-r--r--src/base/database.hpp9
-rw-r--r--test/base/database.cpp18
3 files changed, 52 insertions, 0 deletions
diff --git a/src/base/database.cpp b/src/base/database.cpp
index ec2e8493..ae097be4 100644
--- a/src/base/database.cpp
+++ b/src/base/database.cpp
@@ -102,6 +102,31 @@ namespace usdx
return result;
}
+ const bool StatDatabase::sqlite_table_contains_column(const std::string table, const std::string column)
+ {
+ std::string sqlStatement = "PRAGMA TABLE_INFO([";
+ sqlStatement += table;
+ sqlStatement += "]);";
+
+ sqlite3_stmt *sqliteStatement = sqlite_prepare(sqlStatement);
+ bool result = false;
+
+ int rc = sqlite3_step(sqliteStatement);
+ while (rc == SQLITE_ROW) {
+ const char *column_name = (const char*)sqlite3_column_blob(sqliteStatement, 1);
+
+ if (column == std::string(column_name)) {
+ result = true;
+ break;
+ }
+
+ rc = sqlite3_step(sqliteStatement);
+ }
+
+ sqlite3_finalize(sqliteStatement);
+ return result;
+ }
+
void StatDatabase::init(const std::string filename)
{
LOG4CXX_DEBUG(log, "Initializing Database: " << filename);
diff --git a/src/base/database.hpp b/src/base/database.hpp
index d5313609..c7bb00b8 100644
--- a/src/base/database.hpp
+++ b/src/base/database.hpp
@@ -80,6 +80,15 @@ namespace usdx
*/
const bool sqlite_table_exists(const std::string table);
+ /**
+ * Check if the given table has the given column by name.
+ *
+ * @param table Table to examine
+ * @param column Name of the column to check if exists
+ * @return true, if column exists in that table, false if not
+ */
+ const bool sqlite_table_contains_column(const std::string table, const std::string column);
+
// Singleton
StatDatabase(std::string filename);
diff --git a/test/base/database.cpp b/test/base/database.cpp
index fdf4a3dd..72b44094 100644
--- a/test/base/database.cpp
+++ b/test/base/database.cpp
@@ -43,6 +43,9 @@ namespace usdx
CPPUNIT_TEST(testFormatDate);
CPPUNIT_TEST(testTableExists);
CPPUNIT_TEST(testTableNotExists);
+ CPPUNIT_TEST(testTableColumnExists);
+ CPPUNIT_TEST(testTableColumnNotExists);
+ CPPUNIT_TEST(testTableColumnTableNotExists);
CPPUNIT_TEST_SUITE_END();
private:
StatDatabase *db;
@@ -102,6 +105,21 @@ namespace usdx
{
CPPUNIT_ASSERT( false == StatDatabase::get_instance()->sqlite_table_exists("abc") );
}
+
+ void testTableColumnExists()
+ {
+ CPPUNIT_ASSERT( true == StatDatabase::get_instance()->sqlite_table_contains_column("us_songs", "Title") );
+ }
+
+ void testTableColumnNotExists()
+ {
+ CPPUNIT_ASSERT( false == StatDatabase::get_instance()->sqlite_table_contains_column("us_songs", "title") );
+ }
+
+ void testTableColumnTableNotExists()
+ {
+ CPPUNIT_ASSERT( false == StatDatabase::get_instance()->sqlite_table_contains_column("abc", "Title") );
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(StatDatabaseTest);