Skip to content
Snippets Groups Projects
Commit 74bb8d4c authored by Steven Murray's avatar Steven Murray
Browse files

Added method Conn::executeNonQueries()

parent a6dd5063
No related branches found
No related tags found
No related merge requests found
......@@ -29,24 +29,23 @@ namespace catalogue {
//------------------------------------------------------------------------------
SchemaCreatingSqliteCatalogue::SchemaCreatingSqliteCatalogue(const std::string &filename, const uint64_t nbConns):
SqliteCatalogue(filename, nbConns) {
createCatalogueSchema();
try {
createCatalogueSchema();
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
//------------------------------------------------------------------------------
// createCatalogueSchema
//------------------------------------------------------------------------------
void SchemaCreatingSqliteCatalogue::createCatalogueSchema() {
const InMemoryCatalogueSchema schema;
std::string::size_type searchPos = 0;
std::string::size_type findResult = std::string::npos;
auto conn = m_connPool.getConn();
while(std::string::npos != (findResult = schema.sql.find(';', searchPos))) {
const std::string::size_type length = findResult - searchPos + 1;
const std::string sql = schema.sql.substr(searchPos, length);
searchPos = findResult + 1;
auto stmt = conn->createStmt(sql, rdbms::Stmt::AutocommitMode::ON);
stmt->executeNonQuery();
try {
const InMemoryCatalogueSchema schema;
auto conn = m_connPool.getConn();
conn->executeNonQueries(schema.sql);
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
......
......@@ -16,10 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Conn.hpp"
#include "common/exception/Exception.hpp"
#include "rdbms/Conn.hpp"
#include <memory>
#include <string>
namespace cta {
namespace rdbms {
......@@ -30,6 +30,26 @@ namespace rdbms {
Conn::~Conn() throw() {
}
//------------------------------------------------------------------------------
// executeNonQueries
//------------------------------------------------------------------------------
void Conn::executeNonQueries(const std::string &sqlStmts) {
try {
std::string::size_type searchPos = 0;
std::string::size_type findResult = std::string::npos;
while(std::string::npos != (findResult = sqlStmts.find(';', searchPos))) {
const std::string::size_type length = findResult - searchPos + 1;
const std::string sqlStmt = sqlStmts.substr(searchPos, length);
searchPos = findResult + 1;
executeNonQuery(sqlStmt, rdbms::Stmt::AutocommitMode::ON);
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
//------------------------------------------------------------------------------
// executeNonQuery
//------------------------------------------------------------------------------
......
......@@ -51,7 +51,20 @@ public:
virtual std::unique_ptr<Stmt> createStmt(const std::string &sql, const Stmt::AutocommitMode autocommitMode) = 0;
/**
* Convenience function implemented in Conn around Conn::createStmt(),
* Convenience method that parses the specified string of multiple SQL
* statements and calls executeNonQuery() for each individual statement found.
*
* Please note that each statement should be a non-query terminated by a
* semicolon and that each individual statement will be executed with
* autocommit ON.
*
* @param sqlStmts The SQL statements to be executed.
* @param autocommitMode The autocommit mode of the statement.
*/
void executeNonQueries(const std::string &sqlStmts);
/**
* Convenience method that wraps Conn::createStmt() followed by
* Stmt::executeNonQuery().
*
* @param sql The SQL statement.
......
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