diff --git a/libretroshare/src/util/retrodb.cc b/libretroshare/src/util/retrodb.cc index a183a6952..b6633c3a5 100644 --- a/libretroshare/src/util/retrodb.cc +++ b/libretroshare/src/util/retrodb.cc @@ -557,6 +557,43 @@ bool RetroDb::sqlUpdate(const std::string &tableName, std::string whereClause, c return execSQL_bind(sqlQuery, paramBindings); } +bool RetroDb::tableExists(const std::string &tableName) +{ + if (!isOpen()) { + return false; + } + + std::string sqlQuery = "PRAGMA table_info(" + tableName + ");"; + + bool result = false; + sqlite3_stmt* stmt = NULL; + + int rc = sqlite3_prepare_v2(mDb, sqlQuery.c_str(), sqlQuery.length(), &stmt, NULL); + if (rc == SQLITE_OK) { + rc = sqlite3_step(stmt); + switch (rc) { + case SQLITE_ROW: + result = true; + break; + case SQLITE_DONE: + break; + default: + std::cerr << "RetroDb::tableExists(): Error executing statement (code: " << rc << ")" + << std::endl; + return false; + } + } else { + std::cerr << "RetroDb::tableExists(): Error preparing statement\n"; + std::cerr << "Error code: " << sqlite3_errmsg(mDb) + << std::endl; + } + + if (stmt) { + sqlite3_finalize(stmt); + } + + return result; +} /********************** RetroCursor ************************/ diff --git a/libretroshare/src/util/retrodb.h b/libretroshare/src/util/retrodb.h index 3898dec7b..8b79c0017 100644 --- a/libretroshare/src/util/retrodb.h +++ b/libretroshare/src/util/retrodb.h @@ -38,8 +38,6 @@ #include "contentvalue.h" - - class RetroCursor; /*! @@ -90,8 +88,6 @@ public: /* modifying db */ public: - - /*! * Start transaction * @return true/false @@ -171,22 +167,13 @@ public: */ void vacuum(); - /*! - * Build the "VALUE" part of an insertiong sql query - * @param parameter contains place holder query - * @param paramBindings + * Check if table exist in database + * @param tableName table to check + * @return true/false */ - void buildInsertQueryValue(const std::map keyMap, const ContentValue& cv, - std::string& parameter, std::list& paramBindings); + bool tableExists(const std::string& tableName); - /*! - * Build the "VALUE" part of an insertiong sql query - * @param parameter contains place holder query - * @param paramBindings - */ - void buildUpdateQueryValue(const std::map keyMap, const ContentValue& cv, - std::string& parameter, std::list& paramBindings); public: static const int OPEN_READONLY; @@ -197,6 +184,22 @@ private: bool execSQL_bind(const std::string &query, std::list& blobs); + /*! + * Build the "VALUE" part of an insertiong sql query + * @param parameter contains place holder query + * @param paramBindings + */ + void buildInsertQueryValue(const std::map keyMap, const ContentValue& cv, + std::string& parameter, std::list& paramBindings); + + /*! + * Build the "VALUE" part of an insertiong sql query + * @param parameter contains place holder query + * @param paramBindings + */ + void buildUpdateQueryValue(const std::map keyMap, const ContentValue& cv, + std::string& parameter, std::list& paramBindings); + private: sqlite3* mDb;