Commit a9cd7cc9 authored by Steven Murray's avatar Steven Murray
Browse files

Removed Conn::executeNonQueries()

parent a9eeac9c
......@@ -23,6 +23,7 @@
#include "catalogue/PostgresCatalogueSchema.hpp"
#include "catalogue/SqliteCatalogueSchema.hpp"
#include "common/exception/Exception.hpp"
#include "common/utils/utils.hpp"
#include "rdbms/ConnPool.hpp"
#include "rdbms/Login.hpp"
#include "rdbms/AutocommitMode.hpp"
......@@ -71,19 +72,19 @@ int CreateSchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
case rdbms::Login::DBTYPE_SQLITE:
{
SqliteCatalogueSchema schema;
conn.executeNonQueries(schema.sql);
executeNonQueries(conn, schema.sql);
}
break;
case rdbms::Login::DBTYPE_POSTGRESQL:
{
PostgresCatalogueSchema schema;
conn.executeNonQueries(schema.sql);
executeNonQueries(conn, schema.sql);
}
break;
case rdbms::Login::DBTYPE_MYSQL:
{
MysqlCatalogueSchema schema;
conn.executeNonQueries(schema.sql);
executeNonQueries(conn, schema.sql);
// execute triggers
auto triggers = schema.triggers();
......@@ -95,7 +96,7 @@ int CreateSchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
case rdbms::Login::DBTYPE_ORACLE:
{
OracleCatalogueSchema schema;
conn.executeNonQueries(schema.sql);
executeNonQueries(conn, schema.sql);
}
break;
case rdbms::Login::DBTYPE_NONE:
......@@ -131,5 +132,29 @@ void CreateSchemaCmd::printUsage(std::ostream &os) {
CreateSchemaCmdLineArgs::printUsage(os);
}
//------------------------------------------------------------------------------
// executeNonQueries
//------------------------------------------------------------------------------
void CreateSchemaCmd::executeNonQueries(rdbms::Conn &conn, 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))) {
// Calculate the length of the current statement without the trailing ';'
const std::string::size_type stmtLen = findResult - searchPos;
const std::string sqlStmt = utils::trimString(sqlStmts.substr(searchPos, stmtLen));
searchPos = findResult + 1;
if(0 < sqlStmt.size()) { // Ignore empty statements
conn.executeNonQuery(sqlStmt);
}
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
} // namespace catalogue
} // namespace cta
......@@ -72,6 +72,19 @@ private:
*/
bool tableExists(const std::string tableName, rdbms::Conn &conn) const;
/**
* Parses the specified string of multiple SQL statements separated by
* semicolons and calls executeNonQuery() for each statement found.
*
* Please note that this method does not support statements that themselves
* contain one more semicolons.
*
* @param conn The database connection.
* @param sqlStmts Multiple SQL statements separated by semicolons.
* Statements that themselves contain one more semicolons are not supported.
*/
void executeNonQueries(rdbms::Conn &conn, const std::string &sqlStmts);
}; // class CreateSchemaCmd
} // namespace catalogue
......
......@@ -18,6 +18,7 @@
#include "catalogue/SqliteCatalogueSchema.hpp"
#include "catalogue/SchemaCreatingSqliteCatalogue.hpp"
#include "common/utils/utils.hpp"
namespace cta {
namespace catalogue {
......@@ -45,7 +46,31 @@ void SchemaCreatingSqliteCatalogue::createCatalogueSchema() {
try {
const SqliteCatalogueSchema schema;
auto conn = m_connPool.getConn();
conn.executeNonQueries(schema.sql);
executeNonQueries(conn, schema.sql);
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
//------------------------------------------------------------------------------
// executeNonQueries
//------------------------------------------------------------------------------
void SchemaCreatingSqliteCatalogue::executeNonQueries(rdbms::Conn &conn, 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))) {
// Calculate the length of the current statement without the trailing ';'
const std::string::size_type stmtLen = findResult - searchPos;
const std::string sqlStmt = utils::trimString(sqlStmts.substr(searchPos, stmtLen));
searchPos = findResult + 1;
if(0 < sqlStmt.size()) { // Ignore empty statements
conn.executeNonQuery(sqlStmt);
}
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
......
......@@ -61,6 +61,19 @@ private:
*/
void createCatalogueSchema();
/**
* Parses the specified string of multiple SQL statements separated by
* semicolons and calls executeNonQuery() for each statement found.
*
* Please note that this method does not support statements that themselves
* contain one more semicolons.
*
* @param conn The database connection.
* @param sqlStmts Multiple SQL statements separated by semicolons.
* Statements that themselves contain one more semicolons are not supported.
*/
void executeNonQueries(rdbms::Conn &conn, const std::string &sqlStmts);
}; // class SchemaCreatingSqliteCatalogue
} // namespace catalogue
......
......@@ -115,30 +115,6 @@ Stmt Conn::createStmt(const std::string &sql) {
}
}
//------------------------------------------------------------------------------
// 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))) {
// Calculate the length of the current statement without the trailing ';'
const std::string::size_type stmtLen = findResult - searchPos;
const std::string sqlStmt = utils::trimString(sqlStmts.substr(searchPos, stmtLen));
searchPos = findResult + 1;
if(0 < sqlStmt.size()) { // Ignore empty statements
executeNonQuery(sqlStmt);
}
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
//------------------------------------------------------------------------------
// executeNonQuery
//------------------------------------------------------------------------------
......
......@@ -117,17 +117,6 @@ public:
*/
Stmt createStmt(const std::string &sql);
/**
* 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.
*
* @param sqlStmts The SQL statements to be executed.
*/
void executeNonQueries(const std::string &sqlStmts);
/**
* Executes the statement.
*
......
......@@ -166,26 +166,6 @@ TEST_P(cta_rdbms_ConnTest, DISABLED_createTableInMemoryDatabase_executeNonQuery_
}
}
TEST_P(cta_rdbms_ConnTest, createTableInMemoryDatabase_executeNonQueries) {
using namespace cta::rdbms;
const std::string sql = "CREATE TABLE POOLED_STMT_TEST(ID INTEGER);";
// First in-memory database
{
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t maxNbConns = 1;
ConnPool connPool(login, maxNbConns);
auto conn = connPool.getConn();
ASSERT_TRUE(conn.getTableNames().empty());
conn.executeNonQueries(sql);
ASSERT_EQ(1, conn.getTableNames().size());
}
}
TEST_P(cta_rdbms_ConnTest, createSameTableInTwoSeparateInMemoryDatabases_executeNonQuery) {
using namespace cta::rdbms;
......@@ -220,38 +200,4 @@ TEST_P(cta_rdbms_ConnTest, createSameTableInTwoSeparateInMemoryDatabases_execute
}
}
TEST_P(cta_rdbms_ConnTest, createSameTableInTwoSeparateInMemoryDatabases_executeNonQueries) {
using namespace cta::rdbms;
const std::string sql = "CREATE TABLE POOLED_STMT_TEST(ID INTEGER);";
// First in-memory database
{
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t maxNbConns = 1;
ConnPool connPool(login, maxNbConns);
auto conn = connPool.getConn();
ASSERT_TRUE(conn.getTableNames().empty());
conn.executeNonQueries(sql);
ASSERT_EQ(1, conn.getTableNames().size());
}
// Second in-memory database
{
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t maxNbConns = 1;
ConnPool connPool(login, maxNbConns);
auto conn = connPool.getConn();
ASSERT_TRUE(conn.getTableNames().empty());
conn.executeNonQueries(sql);
ASSERT_EQ(1, conn.getTableNames().size());
}
}
} // namespace unitTests
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment