Skip to content

Commit

Permalink
Workspace directory existence and DB table / index check added
Browse files Browse the repository at this point in the history
  • Loading branch information
intjftw committed Jun 28, 2018
1 parent 288ad6d commit 5b2454a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 21 deletions.
26 changes: 18 additions & 8 deletions parser/src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ po::options_description commandLineArguments()
* provided then empty string returns which indicates the problem.
* @return The path of the project directory.
*/
std::string prepareProjectDir(const po::variables_map& vm_)
std::string prepareProjectDir(const po::variables_map& vm_, bool create_)
{
const std::string projDir
= vm_["workspace"].as<std::string>() + '/'
Expand Down Expand Up @@ -121,9 +121,9 @@ std::string prepareProjectDir(const po::variables_map& vm_)
return std::string();
}
}
else
else if(!isNewProject && create_ || isNewProject && !create_)
{
LOG(error) << projDir << " already exists. Use -f for reparsing!";
LOG(error) << "Database and working directory are inconsistent. Use -f for reparsing!";
return std::string();
}

Expand Down Expand Up @@ -203,16 +203,20 @@ int main(int argc, char* argv[])
return 1;
}

//--- Check if database exists ---//

bool dbExists = cc::util::databaseExists(vm["database"].as<std::string>());

//--- Check workspace and project directory ---//

std::string projDir = prepareProjectDir(vm);
std::string projDir = prepareProjectDir(vm, !dbExists);
if (projDir.empty())
return 1;

//--- Create and init database ---//

std::shared_ptr<odb::database> db = cc::util::createDatabase(
vm["database"].as<std::string>());
std::shared_ptr<odb::database> db = cc::util::connectDatabase(
vm["database"].as<std::string>(), !dbExists);

if (!db)
{
Expand All @@ -224,7 +228,10 @@ int main(int argc, char* argv[])
if (vm.count("force"))
cc::util::removeTables(db, SQL_DIR);

cc::util::createTables(db, SQL_DIR);
if(vm.count("force") || !dbExists)
{
cc::util::createTables(db, SQL_DIR);
}

//--- Start parsers ---//

Expand All @@ -241,7 +248,10 @@ int main(int argc, char* argv[])

//--- Add indexes to the database ---//

cc::util::createIndexes(db, SQL_DIR);
if(vm.count("force") || !dbExists)
{
cc::util::createIndexes(db, SQL_DIR);
}

//--- Create project config file ---//

Expand Down
2 changes: 1 addition & 1 deletion plugins/cpp/test/src/cpppropertiesservicetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CppPropertiesServiceTest : public ::testing::Test
{
public:
CppPropertiesServiceTest() :
_db(cc::util::createDatabase(dbConnectionString)),
_db(cc::util::connectDatabase(dbConnectionString, true)),
_transaction(_db),
_cppservice(new CppServiceHandler(
_db,
Expand Down
2 changes: 1 addition & 1 deletion plugins/cpp/test/src/cppreferenceservicetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CppReferenceServiceTest : public ::testing::Test
{
public:
CppReferenceServiceTest() :
_db(cc::util::createDatabase(dbConnectionString)),
_db(cc::util::connectDatabase(dbConnectionString, true)),
_transaction(_db),
_cppservice( new CppServiceHandler(
_db,
Expand Down
5 changes: 4 additions & 1 deletion util/include/util/dbutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>

#include <odb/database.hxx>
#include <odb/sqlite/exceptions.hxx>

#ifdef DATABASE_PGSQL
# define SQL_ILIKE "ILIKE"
Expand All @@ -24,7 +25,9 @@ namespace cc
namespace util
{

std::shared_ptr<odb::database> createDatabase(const std::string& connStr_);
std::shared_ptr<odb::database> connectDatabase(const std::string& connStr_, bool create_);

bool databaseExists(const std::string&connStr_);

/**
* This function adds indexes to the database. These indexes are added from the
Expand Down
89 changes: 80 additions & 9 deletions util/src/dbutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ namespace util
*/
static std::map<std::string, std::shared_ptr<odb::database>> databasePool;

std::shared_ptr<odb::database> createDatabase(const std::string& connStr_)
std::shared_ptr<odb::database> connectDatabase(const std::string& connStr_, bool create_)
{
databaseExists(connStr_);
auto iter = databasePool.find(connStr_);
if (iter != databasePool.end())
{
Expand All @@ -284,12 +285,6 @@ std::shared_ptr<odb::database> createDatabase(const std::string& connStr_)

std::shared_ptr<odb::database> db;

#ifdef DATABASE_MYSQL
if (database == "mysql")
db.reset(new odb::mysql::database(odbOpts.size(), cStyleOptions),
[](odb::database*){});
#endif

#ifdef DATABASE_SQLITE
if (database == "sqlite")
{
Expand All @@ -298,7 +293,7 @@ std::shared_ptr<odb::database> createDatabase(const std::string& connStr_)
optionsSize,
cStyleOptions,
false,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
SQLITE_OPEN_READWRITE | (create_ ? SQLITE_OPEN_CREATE : 0),
true,
"",
std::make_unique<odb::sqlite::single_connection_factory>());
Expand All @@ -324,7 +319,10 @@ std::shared_ptr<odb::database> createDatabase(const std::string& connStr_)
updateConnectionString(connStr_, "database", "postgres");
std::string dbName = connStrComponent(connStr_, "database");

createPsqlDatbase(defaultPsqlConnStr, dbName);
if(create_)
{
createPsqlDatbase(defaultPsqlConnStr, dbName);
}

int size = odbOpts.size();
db.reset(new odb::pgsql::database(size, cStyleOptions),
Expand Down Expand Up @@ -354,6 +352,79 @@ std::shared_ptr<odb::database> createDatabase(const std::string& connStr_)
return db;
}

bool databaseExists(const std::string& connStr_)
{
std::size_t colonPos = connStr_.find(':');
if (colonPos == std::string::npos)
return false;

std::string database = connStr_.substr(0, colonPos);
std::string options = connStr_.substr(colonPos + 1);

std::vector<std::string> odbOpts = createOdbOptions(options);
char** cStyleOptions = createCStyleOptions(odbOpts);

std::shared_ptr<odb::database> db;

#ifdef DATABASE_SQLITE
if (database == "sqlite")
{
int optionsSize = odbOpts.size();
try
{
auto sqliteDB = new odb::sqlite::database(
optionsSize,
cStyleOptions,
false,
SQLITE_OPEN_READWRITE,
true,
"",
std::make_unique<odb::sqlite::single_connection_factory>());
}
catch(odb::database_exception& e)
{
return false;
}

return true;
}

return false;
#endif

#ifdef DATABASE_PGSQL
if (database == "pgsql")
{
std::string defaultPsqlConnStr =
updateConnectionString(connStr_, "database", "postgres");
std::string dbName = connStrComponent(connStr_, "database");

std::size_t colonPos = connStr_.find(':');
std::string database = connStr_.substr(0, colonPos);
std::string options = connStr_.substr(colonPos + 1);

std::vector<std::string> odbOpts = createOdbOptions(options);
char** cStyleOptions = createCStyleOptions(odbOpts);

int size = odbOpts.size();
odb::pgsql::database db(size, cStyleOptions);

odb::connection_ptr connection = db.connection();

if (!connection->execute(
"SELECT 1 FROM pg_database WHERE datname = '" + database + "'"))
{
return false;
}

return true;
}

return false;

#endif
}

void createTables(
std::shared_ptr<odb::database> db_,
const std::string& sqlDir_)
Expand Down
2 changes: 1 addition & 1 deletion webserver/include/webserver/pluginhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ inline void registerPluginSimple(
"database",
dbName);

std::shared_ptr<odb::database> db = util::createDatabase(connStr);
std::shared_ptr<odb::database> db = util::connectDatabase(connStr, true);

if (!db)
{
Expand Down

0 comments on commit 5b2454a

Please sign in to comment.