SQLiteSchemaInserter.hpp 3.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * The CERN Tape Archive (CTA) project
 * Copyright © 2018 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 "rdbms/Login.hpp"
21
#include "rdbms/ConnPool.hpp"
22
23
24

namespace cta {
namespace catalogue {
25
26
27
28
29
30
31
32
33
  /**
   * This class is used to create a InMemory SQLiteSchema from .sql files 
   * located in allSchemasVersionPath (example : /catalogue/1.0/dbtype_catalogue_schema.sql)
   * 
   * @param schemaVersion the schema version to insert in order to look in the correct folder within allSchemasVersionPath
   * @param catalogueDbType the dbType of the schema to insert in the SQLiteSchema
   * @param allSchemasVersionPath the directory containing all the versions of the catalogue schema
   * @param sqliteConn the connection of the InMemory SQLite schema
   */
34
35
  class SQLiteSchemaInserter {
  public:
36
37
38
39
40
41
42
    /**
     * Constructor
     * @param schemaVersion the schema version to insert in order to look in the correct folder within allSchemasVersionPath
     * @param catalogueDbType the dbType of the schema to insert in the SQLiteSchema
     * @param allSchemasVersionPath the directory containing all the versions of the catalogue schema
     * @param sqliteConn the connection of the InMemory SQLite schema
     */
43
    SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allSchemasVersionPath,rdbms::Conn &sqliteConn);
44
45
46
47
    /**
     * Insert the schema corresponding to the version and the database type into the
     * InMemory SQLite database accessible via the one given in the constructor
     */
48
49
50
    void insert();    
    virtual ~SQLiteSchemaInserter();
  private:
51
52
    const std::string c_catalogueFileNameTrailer = "_catalogue_schema.sql";
    
53
54
    std::string m_schemaVersion;
    cta::rdbms::Login::DbType m_dbType;
55
    std::string m_allSchemasVersionPath;
56
    cta::rdbms::Conn & m_sqliteCatalogueConn;
57
58
59
60
    /**
     * Put the schema located in SCHEMA_VERSION/dbType_catalogue_schema.sql
     * @return the string containing the .sql statements for the creation of the schema
     */
61
    std::string readSchemaFromFile();
62
63
64
65
66
    /**
     * Separates the statements and put them in a std::list<std::string> 
     * @param schema the sql statements put all together
     * @return a list containing separated statements from the schema passed in parameter
     */
67
    std::list<std::string> getAllStatementsFromSchema(const std::string &schema);
68
69
70
71
72
    /**
     * Returns the string corresponding to the database type
     * This method is used to determine the correct .sql file to create the InMemory SQLite database
     * @return the string corresponding to the database type
     */
73
    std::string getDatabaseType();
74
75
76
77
    /**
     * Returns the path of the .sql file that will be executed to create the InMemory SQLite database
     * @return 
     */
78
    std::string getSchemaFilePath();
79
80
81
82
    /**
     * Execute all the statements passed in parameter against the InMemory SQLite database 
     * @param statements the statements to execute in the InMemory SQLite database
     */
83
    void executeStatements(const std::list<std::string> &statements);
84
85
86
87
88
  };
  
}}