Skip to content
Snippets Groups Projects
Commit 8b8b7b2e authored by Victor Kotlyar's avatar Victor Kotlyar
Browse files

Move genereic schema functions to base CatalogueSchema class.

parent 0244a057
Branches
Tags
No related merge requests found
......@@ -27,6 +27,7 @@ set (CATALOGUE_LIB_SRC_FILES
Catalogue.cpp
CatalogueFactory.cpp
CatalogueFactoryFactory.cpp
CatalogueSchema.cpp
ChecksumTypeMismatch.cpp
ChecksumValueMismatch.cpp
CmdLineTool.cpp
......@@ -247,6 +248,7 @@ add_executable(cta-catalogue-schema-verify
VerifySchemaCmd.cpp
VerifySchemaCmdLineArgs.cpp
VerifySchemaCmdMain.cpp
CatalogueSchema.cpp
OracleCatalogueSchema.cpp
SqliteCatalogueSchema.cpp
PostgresCatalogueSchema.cpp
......
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catalogue/CatalogueSchema.hpp"
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CatalogueSchema::CatalogueSchema(const std::string &sqlSchema): sql(sqlSchema) {
}
//------------------------------------------------------------------------------
// getSchemaTableNames
//------------------------------------------------------------------------------
std::list<std::string> CatalogueSchema::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;
}
//------------------------------------------------------------------------------
// getSchemaIndexNames
//------------------------------------------------------------------------------
std::list<std::string> CatalogueSchema::getSchemaIndexNames() const {
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
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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 {
/**
* Structure containing the common schema procedures of the CTA catalogue
* database.
*/
struct CatalogueSchema {
/**
* Constructor.
*/
CatalogueSchema(const std::string &sqlSchema);
/**
* The schema.
*/
const std::string sql;
/**
* TODO
*
* @return
*/
std::list<std::string> getSchemaTableNames() const;
/**
* TODO
*
* @return
*/
std::list<std::string> getSchemaIndexNames() const;
};
} // namespace catalogue
} // namespace cta
......@@ -24,7 +24,7 @@ namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
OracleCatalogueSchema::OracleCatalogueSchema(): sql(
OracleCatalogueSchema::OracleCatalogueSchema(): CatalogueSchema(
// CTA_SQL_SCHEMA - The contents of oracle_catalogue_schema.cpp go here
) {
}
......
......@@ -18,6 +18,8 @@
#pragma once
#include "CatalogueSchema.hpp"
#include <string>
namespace cta {
......@@ -38,16 +40,11 @@ namespace catalogue {
* The purpose of this class is to help IDEs by isolating the "non-compilable"
* issues into a small cpp file.
*/
struct OracleCatalogueSchema {
struct OracleCatalogueSchema: public CatalogueSchema {
/**
* Constructor.
*/
OracleCatalogueSchema();
/**
* The schema.
*/
const std::string sql;
};
} // namespace catalogue
......
......@@ -24,7 +24,7 @@ namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
SqliteCatalogueSchema::SqliteCatalogueSchema(): sql(
SqliteCatalogueSchema::SqliteCatalogueSchema(): CatalogueSchema(
// CTA_SQL_SCHEMA - The contents of sqlite_catalogue_schema.cpp go here
) {
}
......
......@@ -22,6 +22,8 @@
#include "common/utils/utils.hpp"
#include "common/exception/Exception.hpp"
#include "CatalogueSchema.hpp"
#include <string>
#include <list>
......@@ -46,66 +48,11 @@ namespace catalogue {
* The purpose of this class is to help IDEs by isolating the "non-compilable"
* issues into a small cpp file.
*/
struct SqliteCatalogueSchema {
struct SqliteCatalogueSchema: public CatalogueSchema {
/**
* Constructor.
*/
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
......
......@@ -106,17 +106,19 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
}
break;
case rdbms::Login::DBTYPE_ORACLE:
{/*
{
// TODO
OracleCatalogueSchema schema;
std::cerr << "Checking table names..." << std::endl;
const auto schemaTableNames = schema.getSchemaTableNames();
const auto dbTableNames = conn.getTableNames();
std::cerr << "error code: "<< static_cast<int>(verifyTableNames(schemaTableNames, dbTableNames)) << std::endl;
std::cerr << "Checking index names..." << std::endl;
const auto schemaIndexNames = schema.getSchemaIndexNames();
const auto dbIndexNames = conn.getIndexNames();
std::cerr << "error code: "<< static_cast<int>(verifyIndexNames(schemaIndexNames, dbIndexNames)) << std::endl;
*/}
}
break;
case rdbms::Login::DBTYPE_NONE:
throw exception::Exception("Cannot verify a catalogue without a database type");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment