Skip to content
Snippets Groups Projects
Commit 21fd1064 authored by Victor Kotlyar's avatar Victor Kotlyar Committed by Michael Davis
Browse files

Add table names DB verification against schema for sqlite.

parent ed63a0b4
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,15 @@
#pragma once
#include "common/utils/Regex.hpp"
#include "common/utils/utils.hpp"
#include "common/exception/Exception.hpp"
#include <string>
#include <list>
// TODO
#include <iostream>
namespace cta {
namespace catalogue {
......@@ -48,6 +56,56 @@ struct SqliteCatalogueSchema {
* The schema.
*/
const std::string sql;
// TODO
std::list<std::string> getSchemaTableNames() const {
std::list<std::string> schemaTables;
std::string::size_type searchPos = 0;
std::string::size_type findResult = std::string::npos;
try {
while(std::string::npos != (findResult = sql.find(';', searchPos))) {
// Calculate the length of the current statement without the trailing ';'
const std::string::size_type stmtLen = findResult - searchPos;
const std::string sqlStmt = utils::trimString(sql.substr(searchPos, stmtLen));
searchPos = findResult + 1;
if(0 < sqlStmt.size()) { // Ignore empty statements
cta::utils::Regex tableNamesRegex("CREATE TABLE ([a-zA-Z_]+)");
auto tableName = tableNamesRegex.exec(sqlStmt);
if (2 == tableName.size()) {
schemaTables.push_back(tableName[1].c_str());
}
}
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
return schemaTables;
}
// TODO
std::list<std::string> getSchemaIndexNames() {
std::list<std::string> schemaIndices;
std::string::size_type searchPos = 0;
std::string::size_type findResult = std::string::npos;
try {
while(std::string::npos != (findResult = sql.find(';', searchPos))) {
// Calculate the length of the current statement without the trailing ';'
const std::string::size_type stmtLen = findResult - searchPos;
const std::string sqlStmt = utils::trimString(sql.substr(searchPos, stmtLen));
searchPos = findResult + 1;
if(0 < sqlStmt.size()) { // Ignore empty statements
cta::utils::Regex tableIndicesRegex("CREATE INDEX ([a-zA-Z_]+)");
auto indexName = tableIndicesRegex.exec(sqlStmt);
if (2 == indexName.size()) {
schemaIndices.push_back(indexName[1].c_str());
}
}
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
return schemaIndices;
}
};
} // namespace catalogue
......
......@@ -27,6 +27,8 @@
#include "rdbms/Login.hpp"
#include "rdbms/AutocommitMode.hpp"
#include <algorithm>
namespace cta {
namespace catalogue {
......@@ -71,7 +73,15 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
case rdbms::Login::DBTYPE_SQLITE:
{
// TODO
//SqliteCatalogueSchema schema;
SqliteCatalogueSchema schema;
const auto schemaTableNames = schema.getSchemaTableNames();
const auto dbTableNames = conn.getTableNames();
std::cerr << "error code: "<< static_cast<int>(verifyTableNames(schemaTableNames, dbTableNames)) << std::endl;
const auto schemaIndexNames = schema.getSchemaIndexNames();
for (auto index: schemaIndexNames) {
std::cout << index << std::endl;
}
//conn.executeNonQueries(schema.sql);
}
break;
......@@ -135,5 +145,32 @@ void VerifySchemaCmd::printUsage(std::ostream &os) {
VerifySchemaCmdLineArgs::printUsage(os);
}
//------------------------------------------------------------------------------
// verifyTableNames
//------------------------------------------------------------------------------
VerifySchemaCmd::VerifyStatus VerifySchemaCmd::verifyTableNames(const std::list<std::string> &schemaTableNames,
const std::list<std::string> &dbTableNames) const {
VerifyStatus status = VerifyStatus::UNKNOWN;
// check for schema tables
for(auto &tableName : schemaTableNames) {
const bool schemaTableIsNotInDb = dbTableNames.end() == std::find(dbTableNames.begin(), dbTableNames.end(), tableName);
if (schemaTableIsNotInDb) {
std::cerr << "ERROR: the schema table " << tableName << " not found in the DB" << std::endl;
status = VerifyStatus::ERROR;
}
}
// check for extra tables in DB
for(auto &tableName : dbTableNames) {
const bool dbTableIsNotInSchema = schemaTableNames.end() == std::find(schemaTableNames.begin(), schemaTableNames.end(), tableName);
if (dbTableIsNotInSchema) {
std::cerr << "WARNING: the database table " << tableName << " not found in the schema" << std::endl;
if ( VerifyStatus::ERROR != status) {
status = VerifyStatus::WARNING;
}
}
}
return status;
}
} // namespace catalogue
} // namespace cta
......@@ -43,6 +43,8 @@ public:
* Destructor.
*/
~VerifySchemaCmd() noexcept;
enum class VerifyStatus { OK, WARNING, ERROR, UNKNOWN };
private:
......@@ -71,6 +73,15 @@ private:
* @return True if the table exists.
*/
bool tableExists(const std::string tableName, rdbms::Conn &conn) const;
/**
* TODO
* @param
* @param
* @return
*/
VerifyStatus verifyTableNames(const std::list<std::string> &schemaTableNames,
const std::list<std::string> &dbTableNames) const;
}; // class VerifySchemaCmd
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment