CatalogueSchema.cpp 8.56 KB
Newer Older
1
/*
2
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2015-2021 CERN
4
5
6
7
 * @license        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.
8
 *
9
10
11
12
 *                 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.
13
 *
14
15
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18
 */

#include "catalogue/CatalogueSchema.hpp"
19
20
21
#include "common/utils/Regex.hpp"
#include "common/utils/utils.hpp"
#include "common/exception/Exception.hpp"
22
23
24
25
26
27
28
29
30
31

namespace cta {
namespace catalogue {

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CatalogueSchema::CatalogueSchema(const std::string &sqlSchema): sql(sqlSchema) {
}

32
33
34
//------------------------------------------------------------------------------
// getSchemaColumns
//------------------------------------------------------------------------------
35
std::map<std::string, std::string> CatalogueSchema::getSchemaColumns(const std::string &tableName) const {
36
37
38
39
40
41
42
43
  std::map<std::string, std::string> schemaColumnNames;
  std::string::size_type searchPos = 0;
  std::string::size_type findResult = std::string::npos;
  const std::string columnTypes = 
    "NUMERIC|"
    "INTEGER|"
    "CHAR|"
    "VARCHAR|"
44
45
46
47
48
    "VARCHAR2|"
    "BLOB|"
    "BYTEA|"
    "VARBINARY|"
    "RAW";
49
50
51
52
53
54
55
56
57
  
  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
58
        const std::string createTableSQL = "CREATE[a-zA-Z ]+TABLE " + tableName + "[ ]*\\(([a-zA-Z0-9_, '\\)\\(]+)\\)";
59
60
61
62
63
64
65
66
67
68
69
70
        cta::utils::Regex tableSqlRegex(createTableSQL.c_str());
        auto tableSql = tableSqlRegex.exec(sqlStmt);
        if (2 == tableSql.size()) {
          tableSql[1] += ","; // hack for parsing 
          // we use the same logic here as for trailing ';'
          std::string::size_type searchPosComma = 0;
          std::string::size_type findResultComma = std::string::npos;
          while(std::string::npos != (findResultComma = tableSql[1].find(',', searchPosComma))) {
            const std::string::size_type stmtLenComma = findResultComma - searchPosComma;
            const std::string sqlStmtComma = utils::trimString(tableSql[1].substr(searchPosComma, stmtLenComma));
            searchPosComma = findResultComma + 1;
            if(0 < sqlStmtComma.size()) { // Ignore empty statements
71
              const std::string columnSQL = "([a-zA-Z_0-9]+) +(" + columnTypes + ")";        
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
              cta::utils::Regex columnSqlRegex(columnSQL.c_str());
              auto columnSql = columnSqlRegex.exec(sqlStmtComma);
              if (3 == columnSql.size()) {
                schemaColumnNames.insert(std::make_pair(columnSql[1], columnSql[2]));
              }
            }
          }
        }
      }
    }
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
  return schemaColumnNames;
}

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//------------------------------------------------------------------------------
// 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
103
        cta::utils::Regex tableNamesRegex("CREATE[a-zA-Z ]+TABLE ([a-zA-Z_0-9]+)");
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        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;
}

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//------------------------------------------------------------------------------
// getSchemaSequenceNames
//------------------------------------------------------------------------------
std::list<std::string> CatalogueSchema::getSchemaSequenceNames() const {  
  std::list<std::string> schemaSequences;
  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 tableSequencesRegex("CREATE SEQUENCE ([a-zA-Z_]+)");
        auto sequenceName = tableSequencesRegex.exec(sqlStmt);
        if (2 == sequenceName.size()) {
          schemaSequences.push_back(sequenceName[1].c_str());
        }
      }
    }
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
  return schemaSequences;
}

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//------------------------------------------------------------------------------
// getSchemaVersion
//------------------------------------------------------------------------------
std::map<std::string, uint64_t> CatalogueSchema::getSchemaVersion() const {
  try {
    std::map<std::string, uint64_t> schemaVersion;
    cta::utils::Regex schemaVersionRegex(
      "INSERT INTO CTA_CATALOGUE\\("
      "  SCHEMA_VERSION_MAJOR,"
      "  SCHEMA_VERSION_MINOR\\)"
      "VALUES\\("
      "  ([[:digit:]]+),"
      "  ([[:digit:]]+)\\);"
    );
    auto version = schemaVersionRegex.exec(sql);
    if (3 == version.size()) {
      schemaVersion.insert(std::make_pair("SCHEMA_VERSION_MAJOR", cta::utils::toUint64(version[1].c_str())));
      schemaVersion.insert(std::make_pair("SCHEMA_VERSION_MINOR", cta::utils::toUint64(version[2].c_str())));
    } else {
      exception::Exception ex;
      ex.getMessage() << "Could not find SCHEMA_VERSION";
      throw ex; 
    }
    return schemaVersion;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

201
202
} // namespace catalogue
} // namespace cta