Commit a89681df authored by Dr. Carsten Kemena's avatar Dr. Carsten Kemena
Browse files

adding sqlite interface

parent d4762822
Pipeline #104941 passed with stages
in 1 minute and 34 seconds
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.14)
# onec cmake 3 will be used
#set(CMAKE_CXX_STANDARD 11)
......@@ -70,7 +70,10 @@ if (WITH_UNIT_TEST)
else (WITH_UNIT_TEST)
FIND_PACKAGE(Boost 1.65 COMPONENTS system filesystem iostreams REQUIRED)
endif(WITH_UNIT_TEST)
INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIR})
find_package(SQLite3 REQUIRED)
INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIR} ${SQLite3_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
ADD_DEFINITIONS( "-DHAS_BOOST" )
......@@ -99,7 +102,7 @@ set(phylogenyCPP PhylogeneticTree.cpp fitch.cpp dollo.cpp)
PREPEND(phylogenyCPP "${CMAKE_CURRENT_SOURCE_DIR}/src/phylogeny" ${phylogenyCPP})
# utility module
set(utilityCPP algorithm.cpp TwoValues.cpp DSM.cpp stringHelpers.cpp properties.cpp utility.cpp Settings.cpp Input.cpp Output.cpp)
set(utilityCPP algorithm.cpp TwoValues.cpp DSM.cpp stringHelpers.cpp properties.cpp utility.cpp Settings.cpp Input.cpp Output.cpp SQLiteDB.cpp)
PREPEND(utilityCPP "${CMAKE_CURRENT_SOURCE_DIR}/src/utility" ${utilityCPP})
#draw module
......@@ -121,7 +124,7 @@ ENDIF(CURL_FOUND)
add_library(BioSeqDataLib SHARED ${SOURCE_FILES})
set_target_properties (BioSeqDataLib PROPERTIES VERSION ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION})
target_link_libraries(BioSeqDataLib ${Boost_LIBRARIES} ${CURL_LIBRARIES})
target_link_libraries(BioSeqDataLib ${Boost_LIBRARIES} ${CURL_LIBRARIES} ${SQLite3_LIBRARIES})
INSTALL(TARGETS BioSeqDataLib
......
......@@ -29,6 +29,14 @@ struct GFFRecord
std::string parent;
std::map<std::string, std::string> attributes;
GFFRecord()
{}
GFFRecord( GFFRecord && ) = default;
GFFRecord( GFFRecord & ) = default;
GFFRecord & operator= ( GFFRecord && ) = default;
GFFRecord & operator= ( GFFRecord & ) = default;
/**
* @brief Construct a new GFFRecord object
*
......
#include "SQLiteDB.hpp"
#include <numeric>
#include <stdexcept>
#include <iostream>
namespace BioSeqDataLib
{
#include "SQLiteDB.hpp"
namespace fs = boost::filesystem;
SQLiteDB::SQLiteDB() : db_(nullptr)
{
}
SQLiteDB::~SQLiteDB()
{
if (db_ != nullptr)
sqlite3_close(db_);
}
SQLiteDB::SQLiteDB(const fs::path &dbFile, int flags)
{
open(dbFile, flags);
}
void
SQLiteDB::open(const fs::path &dbFile, int flags)
{
int rc = sqlite3_open_v2(dbFile.c_str(), &db_, flags, nullptr);
if(rc)
{
std::string errorMessage = sqlite3_errmsg(db_);
sqlite3_close(db_);
db_ = nullptr;
throw std::runtime_error("SQLite error: Can't open database: " + errorMessage + "\n");
}
}
void
SQLiteDB::exec(const std::string &query)
{
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, query.c_str(), -1, &stmt, 0);
if (rc != SQLITE_OK)
throw std::runtime_error("SQLite error: " + std::string(sqlite3_errmsg(db_)));
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
throw std::runtime_error("SQLite error: " + std::string(sqlite3_errmsg(db_)));
sqlite3_finalize(stmt);
}
void
SQLiteDB::createTable(const std::string &tableName, const std::string &columns)
{
std::string sql = "CREATE TABLE " + tableName + "(" + columns + ");";
exec(sql);
}
void
SQLiteDB::insert(const std::string &tableName, const std::string &columns, const std::vector<std::string> &values)
{
std::string sql = "";
for (auto &elem : values)
{
sql = "INSERT INTO " + tableName + "(" + columns + ") VALUES (" + elem + ");";
exec(sql);
}
}
/*
void
SQLiteDB::select(const std::string &tableName, const std::string &command)
{
std::string sql = "SELECT " + command + " FROM " + tableName;
rc_ = sqlite3_exec(db_, sql.c_str(), callback, 0, &zErrMsg_);
if(rc_ != SQLITE_OK )
{
throwError("Can't create table:");
}
}*/
}
\ No newline at end of file
/*
* Domain2GO.hpp
*
* Created on: 27 Oct 2021
* Author: Carsten Kemena
* Email: c.kemena[@]uni-muenster.de
* Copyright: 2021
*
* This file is part of BioSeqDataLib.
*
* BioSeqDataLib 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.
*
* BioSeqDataLib 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.
*
* You should have received a copy of the GNU General Public License
* along with BioSeqDataLib. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file Domain2GO.hpp
* \brief File containing the Domain2GO class.
*/
#ifndef SQLITEDB_HPP_
#define SQLITEDB_HPP_
#include <map>
#include <vector>
#include <boost/filesystem/path.hpp>
#include <sqlite3.h>
namespace fs = boost::filesystem;
namespace BioSeqDataLib
{
class SQLiteDB
{
private:
sqlite3 *db_;
public:
/**
* \brief Standard constructor
*/
SQLiteDB();
/**
* \brief Opens a database file.
* @param dbFile The database to open
* @param flags The open flags for the database.
*/
SQLiteDB(const fs::path &dbFile, int flags=(SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
/**
* \brief Standard destructor
*/
virtual ~SQLiteDB();
/**
* \brief Opens a database file.
* @param dbFile The database to open
* @param flags The open flags for the database.
*/
void open(const fs::path &dbFile, int flags=(SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
void
startTransaction()
{
char *sErrMsg;
sqlite3_exec(db_, "BEGIN TRANSACTION", NULL, NULL, &sErrMsg);
}
void
endTransaction()
{
char *sErrMsg;
sqlite3_exec(db_, "END TRANSACTION", NULL, NULL, &sErrMsg);
}
/**
* \brief executes a query without a return query.
* @param query The query.
*/
void
exec(const std::string &query);
/**
* \brief Execute a query.
* @param query The query to execute.
* @param func The function to handle the result.
*/
template<typename F>
void
exec(const std::string &query, F func)
{
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, query.c_str(), -1, &stmt, 0);
if (rc)
throw std::runtime_error("SQLite error: " + std::string(sqlite3_errmsg(db_)));
do
{
rc = sqlite3_step(stmt);
switch( rc )
{
case SQLITE_ROW:
func(stmt);
break;
case SQLITE_DONE:
break;
default:
throw std::runtime_error("SQLite error: " + std::string(sqlite3_errmsg(db_)));
break;
}
}
while (rc==SQLITE_ROW);
sqlite3_finalize(stmt);
}
void
createTable(const std::string &tableName, const std::string &columns);
void
insert(const std::string &tableName, const std::string &columns, const std::vector<std::string> &values);
};
}
#endif
\ No newline at end of file
......@@ -31,7 +31,7 @@ SET(utility_tests_src ./utility/utility_tests.cpp)
SET(utility_tests_exe utility_tests)
ADD_EXECUTABLE(${utility_tests_exe} ${utility_tests_src})
target_link_libraries(${utility_tests_exe}
${Boost_LIBRARIES} BioSeqDataLib
${Boost_LIBRARIES} ${SQLite3_LIBRARIES} BioSeqDataLib
)
SET(phylogeny_tests_src ./phylogeny/phylogeny_tests.cpp)
......
......@@ -3,6 +3,7 @@
#include <boost/test/unit_test.hpp>
#include <cstdio>
#include <iostream>
......@@ -159,8 +160,8 @@ BOOST_AUTO_TEST_CASE( FASTAWriter_Test )
outFile.open("fasta-writer.fa");
BioSeqDataLib::FASTAWriter<BioSeqDataLib::Sequence<>> writer;
writer.write(set, outFile);
outFile.close();
std::remove("fasta-writer.fa");
}
......@@ -263,6 +264,10 @@ BOOST_AUTO_TEST_CASE( IndexingFASTAReader_Test2 )
BOOST_CHECK_EQUAL(setb[0].comment(), "(X65921) H.sapiens fau 1 gene");
BOOST_CHECK_EQUAL(setb[0].seq(), "ctaccattttccctctcgattctatatgtacactcgggacaagttctcctgatcgaaaacggcaaaactaaggccccaagtaggaatgccttagttttcggggttaacaatgattaacactgagcctcacacccacgcgatgccctcagctcctcgctcagcgctctcaccaacagccgtagcccgcagccccgctggacaccggttctccatccccgcagcgtagcccggaacatggtagctgccatctttacctgctacgccagccttctgtgcgcgcaactgtctggtcccgccccgtcctgcgcgagctgctgcccaggcaggttcgccggtgcgagcgtaaaggggcggagctaggactgccttgggcggtacaaatagcagggaaccgcgcggtcgctcagcagtgacgtgacacgcagcccacggtctgtactgacgcgccctcgcttcttcctctttctcgactccatcttcgcggtagctgggaccgccgttcaggtaagaatggggccttggctggatccgaagggcttgtagcaggttggctgcggggtcagaaggcgcggggggaaccgaagaacggggcctgctccg");
BOOST_CHECK_EQUAL(setb.size(), 1);
inFile.close();
indexFile.close();
std::remove("seqSet.index");
}
......
/*
* SQLiteDB_Test.hpp
*
* Created on: 29 Oct 2021
* Author: CarstenK
* Email: c.kemena[@]uni-muenster.de
* Copyright: 2021
*
* This file is part of BioSeqDataLib.
*
* BioSeqDataLib 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.
*
* BioSeqDataLib 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.
*
* You should have received a copy of the GNU General Public License
* along with BioSeqDataLib. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SQLITEDB_TEST_HPP_
#define SQLITEDB_TEST_HPP_
#include <cstdio>
#include <boost/test/unit_test.hpp>
#include "../../src/utility/SQLiteDB.hpp"
BOOST_AUTO_TEST_SUITE(SQLiteDB_Test)
BOOST_AUTO_TEST_CASE( DB_CREATE_Test)
{
BioSeqDataLib::SQLiteDB db;
db.open("test.db");
db.createTable("COMPANY", "ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL");
db.insert("COMPANY", "ID,NAME,AGE,ADDRESS,SALARY", {"1, 'Paul', 32, 'California', 20000.00", "2, 'Allen', 25, 'Texas', 15000.00",
"3, 'Teddy', 23, 'Norway', 20000.00","4, 'Mark', 25, 'Rich-Mond ', 65000.00"} );
//db.select("COMPANY", "*");
std::remove("test.db");
}
BOOST_AUTO_TEST_SUITE_END()
#endif /* IDENTITYYMATRIX_TEST_HPP_ */
......@@ -39,3 +39,4 @@
#include "DSM_Test.hpp"
#include "utility_Test.hpp"
#include "BitMask_Test.hpp"
#include "SQLiteDB_Test.hpp"
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