Skip to content
Snippets Groups Projects
Commit 2e8a1d17 authored by Victor Kotlyar's avatar Victor Kotlyar Committed by Michael Davis
Browse files

Add getSchemaColumnNames to the catalogue schema

parent 8c44aad5
No related branches found
No related tags found
No related merge requests found
......@@ -36,6 +36,58 @@ CatalogueSchema::CatalogueSchema(const std::string &sqlSchema,
const std::string &sqlSchemaTrigger): sql(sqlSchema), sql_trigger(sqlSchemaTrigger) {
}
//------------------------------------------------------------------------------
// getSchemaColumns
//------------------------------------------------------------------------------
std::map<std::string, std::string> CatalogueSchema::getSchemaColumns(const std::string &table) const {
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|"
"VARCHAR2";
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
const std::string createTableSQL = "CREATE TABLE " + table + "\\(([a-zA-Z0-9_, \\)\\(]+)\\)";
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
const std::string columnSQL = "([a-zA-Z_]+) +(" + columnTypes + ")";
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;
}
//------------------------------------------------------------------------------
// getSchemaTableNames
//------------------------------------------------------------------------------
......
......@@ -65,6 +65,15 @@ struct CatalogueSchema {
*/
std::list<std::string> getSchemaTableNames() const;
/**
* Returns the names of all the column and their type as a map for the given
* table in the catalogue schema.
*
* @param table The table name to get the columns.
* @return The map of types by name of all the columns for the given table in the catalogue schema.
*/
std::map<std::string, std::string> getSchemaColumns(const std::string &table) const;
/**
* Returns the names of all the indexes in the catalogue schema.
*
......
......@@ -115,6 +115,15 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
const auto schemaTableNames = schema->getSchemaTableNames();
const auto dbTableNames = conn.getTableNames();
const VerifyStatus verifyTablesStatus = verifyTableNames(schemaTableNames, dbTableNames);
for (const auto &table: schemaTableNames) {
std::cerr << table << std::endl;
const auto columns = schema->getSchemaColumns(table);
for (const auto &column : columns) {
std::cerr << " " << column.first << " " << column.second << std::endl;
}
}
std::cerr << "Checking index names..." << std::endl;
const auto schemaIndexNames = schema->getSchemaIndexNames();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment