Commit baa362af authored by Steven Murray's avatar Steven Murray
Browse files

Added cta-catalogue-schema-create

parent bc9229fb
......@@ -139,3 +139,24 @@ target_link_libraries (cta-catalogue-delete-all-data
install (TARGETS cta-catalogue-delete-all-data DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-delete-all-data.1cta DESTINATION /usr/share/man/man1)
add_custom_command(OUTPUT oracle_catalogue_schema.cpp
COMMAND sed 's/^/\ \ \"/' oracle_catalogue_schema.sql | sed 's/$$/\"/' > oracle_catalogue_schema.cpp
DEPENDS oracle_catalogue_schema.sql)
add_custom_command(OUTPUT OracleCatalogueSchema.cpp
COMMAND sed '/CTA_SQL_SCHEMA/r oracle_catalogue_schema.cpp' ${CMAKE_CURRENT_SOURCE_DIR}/OracleCatalogueSchema.before_SQL.cpp > OracleCatalogueSchema.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/OracleCatalogueSchema.before_SQL.cpp oracle_catalogue_schema.cpp)
add_executable(cta-catalogue-schema-create
CreateSchemaCmd.cpp
CreateSchemaCmdLineArgs.cpp
CreateSchemaCmdMain.cpp
OracleCatalogueSchema.cpp
SqliteCatalogueSchema.cpp)
target_link_libraries (cta-catalogue-schema-create
ctacatalogue)
install (TARGETS cta-catalogue-schema-create DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-schema-create.1cta DESTINATION /usr/share/man/man1)
/*
* 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 "rdbms/ConnFactoryFactory.hpp"
#include "catalogue/CreateSchemaCmd.hpp"
#include "catalogue/CreateSchemaCmdLineArgs.hpp"
#include "catalogue/OracleCatalogueSchema.hpp"
#include "catalogue/SqliteCatalogueSchema.hpp"
#include "common/exception/Exception.hpp"
#include <iostream>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CreateSchemaCmd::CreateSchemaCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream):
CmdLineTool(inStream, outStream, errStream) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
CreateSchemaCmd::~CreateSchemaCmd() noexcept {
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
int CreateSchemaCmd::exceptionThrowingMain(const int argc, char *const *const argv) {
const CreateSchemaCmdLineArgs cmdLineArgs(argc, argv);
const auto login = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath);
auto factory = rdbms::ConnFactoryFactory::create(login);
auto conn = factory->create();
const bool ctaCatalogueTableExists = tableExists("CTA_CATALOGUE", *conn);
if(ctaCatalogueTableExists) {
std::cerr << "Cannot create the database schema because the CTA_CATALOGUE table already exists" << std::endl;
return 1;
}
switch(login.dbType) {
case rdbms::Login::DBTYPE_IN_MEMORY:
case rdbms::Login::DBTYPE_SQLITE:
{
SqliteCatalogueSchema schema;
conn->executeNonQueries(schema.sql);
}
break;
case rdbms::Login::DBTYPE_ORACLE:
{
OracleCatalogueSchema schema;
conn->executeNonQueries(schema.sql);
}
break;
case rdbms::Login::DBTYPE_NONE:
throw exception::Exception("Cannot create a catalogue without a database type");
default:
{
exception::Exception ex;
ex.getMessage() << "Unknown database type: value=" << login.dbType;
throw ex;
}
}
return 0;
}
//------------------------------------------------------------------------------
// tableExists
//------------------------------------------------------------------------------
bool CreateSchemaCmd::tableExists(const std::string tableName, rdbms::Conn &conn) const {
const auto names = conn.getTableNames();
for(auto &name : names) {
if(tableName == name) {
return true;
}
}
return false;
}
} // 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 "catalogue/CmdLineTool.hpp"
#include "rdbms/Conn.hpp"
namespace cta {
namespace catalogue {
/**
* Command-line tool for creating the catalogue schema.
*/
class CreateSchemaCmd: public CmdLineTool {
public:
/**
* Constructor.
*
* @param inStream Standard input stream.
* @param outStream Standard output stream.
* @param errStream Standard error stream.
*/
CreateSchemaCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream);
/**
* Destructor.
*/
~CreateSchemaCmd() noexcept;
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
int exceptionThrowingMain(const int argc, char *const *const argv);
private:
/**
* Returns true if the table with the specified name exists in the database
* schema of teh specified database connection.
*
* @param tableName The name of the database table.
* @param conn The database connection.
* @return True if the table exists.
*/
bool tableExists(const std::string tableName, rdbms::Conn &conn) const;
}; // class CreateSchemaCmd
} // 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/>.
*/
#include "catalogue/CreateSchemaCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include <ostream>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CreateSchemaCmdLineArgs::CreateSchemaCmdLineArgs(const int argc, const char *const *const argv) {
if(argc != 2) {
exception::Exception ex;
ex.getMessage() << "Wrong number of command-line arguments: excepted=1 actual=" << (argc - 1) << std::endl <<
std::endl;
printUsage(ex.getMessage());
throw ex;
}
dbConfigPath = argv[1];
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void CreateSchemaCmdLineArgs::printUsage(std::ostream &os) {
os <<
"Usage:" << std::endl <<
" cta-catalogue-schema-create databaseConnectionFile" << std::endl <<
"Where:" << std::endl <<
" databaseConnectionFile" << std::endl <<
" The path to the file containing the connection details of the CTA" << std::endl <<
" catalogue database" << std::endl;
}
} // 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/>.
*/
#include <string>
namespace cta {
namespace catalogue {
/**
* Structure to store the command-line arguments of the command-line tool
* named cta-catalogue-schema-create.
*/
struct CreateSchemaCmdLineArgs {
/**
* Path to the file containing the connection details of the catalogue
* database.
*/
std::string dbConfigPath;
/**
* Constructor that parses the specified command-line arguments.
*
* @param argc The number of command-line arguments including the name of the
* executable.
* @param argv The vector of command-line arguments.
*/
CreateSchemaCmdLineArgs(const int argc, const char *const *const argv);
/**
* Prints the usage message of the command-line tool.
*
* @param os The output stream to which the usage message is to be printed.
*/
static void printUsage(std::ostream &os);
};
} // 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/>.
*/
#include "catalogue/CreateSchemaCmd.hpp"
#include "common/exception/Exception.hpp"
#include <iostream>
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
static int exceptionThrowingMain(const int argc, char *const *const argv);
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char *const *const argv) {
using namespace cta;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::Exception &ex) {
errorMessage = ex.getMessage().str();
} catch(std::exception &se) {
errorMessage = se.what();
} catch(...) {
errorMessage = "An unknown exception was thrown";
}
// Reaching this point means the command has failed, an exception was throw
// and errorMessage has been set accordingly
std::cerr << "Aborting: " << errorMessage << std::endl;
return 1;
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
static int exceptionThrowingMain(const int argc, char *const *const argv) {
using namespace cta;
catalogue::CreateSchemaCmd cmd(std::cin, std::cout, std::cerr);
return cmd.exceptionThrowingMain(argc, argv);
}
/*
* 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/OracleCatalogueSchema.hpp"
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
OracleCatalogueSchema::OracleCatalogueSchema(): sql(
// CTA_SQL_SCHEMA - The contents of oracle_catalogue_schema.cpp go here
) {
}
} // 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/>.
*/
#include <string>
namespace cta {
namespace catalogue {
/**
* Structure containing the SQL to create the schema of the in memory CTA
* database.
*
* The CMakeLists.txt file of this directory instructs cmake to generate
* OracleCatalogueSchema.cpp from:
* - OracleCatalogueSchema.before_SQL.cpp
* - oracle_catalogue_schema.sql
*
* The OracleSchema.before_SQL.cpp file is not compilable and is therefore
* difficult for Integrated Developent Environments (IDEs) to handle.
*
* The purpose of this class is to help IDEs by isolating the "non-compilable"
* issues into a small cpp file.
*/
struct OracleCatalogueSchema {
/**
* Constructor.
*/
OracleCatalogueSchema();
/**
* The schema.
*/
const std::string sql;
};
} // 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/>.
.TH CTA-CATALOGUE-SCHEMA-CREATE 1CTA "August 2016" CTA CTA
.SH NAME
cta-catalogue-schema-create \- Creates the CTA catalogue schema
.SH SYNOPSIS
.BI "cta-catalogue-schema-create databaseConnectionFile"
.SH DESCRIPTION
\fBcta-catalogue-schema-create\fP is a command-line tool that creates the CTA
catalogue database schema.
.P
This command-line tool will abort if it sees a CTA_CATALOGUE database table
already present in the database schema.
.SH ARGUMENTS
.TP
\fBdatabaseConnectionFile
The path to the configuration file containing the connection details of the
CTA catalogue database.
.SH RETURN VALUE
Zero on success and non-zero on failure.
.SH EXAMPLES
cta-catalogue-schema-create /etc/cta/cta_catalogue_db.conf
.SH AUTHOR
\fBCTA\fP Team
......@@ -233,10 +233,12 @@ CERN Tape Archive:
Scripts and utilities to faciliate working with the CTA catalogue
%files -n cta-catalogueutils
%attr(0755,root,root) %{_bindir}/cta-catalogue-delete-all-data
%attr(0755,root,root) %{_bindir}/cta-catalogue-schema-create
%attr(0755,root,root) %{_bindir}/cta-catalogue-schema-lock
%attr(0755,root,root) %{_bindir}/cta-catalogue-schema-status
%attr(0755,root,root) %{_bindir}/cta-catalogue-schema-unlock
%attr(0644,root,bin) %doc /usr/share/man/man1/cta-catalogue-delete-all-data.1cta.gz
%attr(0644,root,bin) %doc /usr/share/man/man1/cta-catalogue-schema-create.1cta.gz
%attr(0644,root,bin) %doc /usr/share/man/man1/cta-catalogue-schema-lock.1cta.gz
%attr(0644,root,bin) %doc /usr/share/man/man1/cta-catalogue-schema-status.1cta.gz
%attr(0644,root,bin) %doc /usr/share/man/man1/cta-catalogue-schema-unlock.1cta.gz
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment