diff --git a/catalogue/CMakeLists.txt b/catalogue/CMakeLists.txt index 7c236312617e8744adccdb2a444de0df52468769..0d6f49eaa6c4ab7a3853bd5039e3e887f5d1f863 100644 --- a/catalogue/CMakeLists.txt +++ b/catalogue/CMakeLists.txt @@ -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) diff --git a/catalogue/CreateSchemaCmd.cpp b/catalogue/CreateSchemaCmd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70bfa823f8a4f9875d28aec1c69ef9e848753a17 --- /dev/null +++ b/catalogue/CreateSchemaCmd.cpp @@ -0,0 +1,101 @@ +/* + * 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 diff --git a/catalogue/CreateSchemaCmd.hpp b/catalogue/CreateSchemaCmd.hpp new file mode 100644 index 0000000000000000000000000000000000000000..9c8e0e9b006a49e7ff587d424a66c557e5dd60ea --- /dev/null +++ b/catalogue/CreateSchemaCmd.hpp @@ -0,0 +1,71 @@ +/* + * 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 diff --git a/catalogue/CreateSchemaCmdLineArgs.cpp b/catalogue/CreateSchemaCmdLineArgs.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5275bdabd54e955aae796f6c5e00e81f0496c9b --- /dev/null +++ b/catalogue/CreateSchemaCmdLineArgs.cpp @@ -0,0 +1,55 @@ +/* + * 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 diff --git a/catalogue/CreateSchemaCmdLineArgs.hpp b/catalogue/CreateSchemaCmdLineArgs.hpp new file mode 100644 index 0000000000000000000000000000000000000000..be5386b3f5bd3e30dc80115737341f343b973249 --- /dev/null +++ b/catalogue/CreateSchemaCmdLineArgs.hpp @@ -0,0 +1,53 @@ +/* + * 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 diff --git a/catalogue/CreateSchemaCmdMain.cpp b/catalogue/CreateSchemaCmdMain.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2af06a92fc27dcb96d5bb7220e11989bca10ff5e --- /dev/null +++ b/catalogue/CreateSchemaCmdMain.cpp @@ -0,0 +1,67 @@ +/* + * 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); +} diff --git a/catalogue/OracleCatalogueSchema.before_SQL.cpp b/catalogue/OracleCatalogueSchema.before_SQL.cpp new file mode 100644 index 0000000000000000000000000000000000000000..156425287535aabde740a90c93fbcaf7c96572d4 --- /dev/null +++ b/catalogue/OracleCatalogueSchema.before_SQL.cpp @@ -0,0 +1,33 @@ +/* + * 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 diff --git a/catalogue/OracleCatalogueSchema.hpp b/catalogue/OracleCatalogueSchema.hpp new file mode 100644 index 0000000000000000000000000000000000000000..063eb3386b90ea5aa88bf7f70290cca5483b855e --- /dev/null +++ b/catalogue/OracleCatalogueSchema.hpp @@ -0,0 +1,52 @@ +/* + * 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 diff --git a/catalogue/cta-catalogue-schema-create.1cta b/catalogue/cta-catalogue-schema-create.1cta new file mode 100644 index 0000000000000000000000000000000000000000..5fc9af32959ef035718609b3efbf95f031b9ce7c --- /dev/null +++ b/catalogue/cta-catalogue-schema-create.1cta @@ -0,0 +1,39 @@ +.\" 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 diff --git a/cta.spec.in b/cta.spec.in index dbb65245f786b98e48a5fb46fc38a23cceeb3c9d..549ec86555c218b9f8eb01961aa5975e827b5d9b 100644 --- a/cta.spec.in +++ b/cta.spec.in @@ -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