Skip to content

Commit 13654a1

Browse files
committed
added sqlitestorage, refs #12
1 parent efa6881 commit 13654a1

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

source/annotatorlib/include/AnnotatorLib/Storage/MySQLStorage.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ANNOTATORLIB_API MySQLStorage : public AbstractStorage {
115115
* @brief createTables
116116
* creates tables in mysql database if they not exist
117117
*/
118-
void createTables();
118+
virtual void createTables();
119119
Poco::Data::Statement getStatement();
120120
};
121121
/************************************************************/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 Annotator Team
2+
#ifndef ANNOTATOR_ANNOTATORLIB_STORAGE_SQLITESTORAGE_H
3+
#define ANNOTATOR_ANNOTATORLIB_STORAGE_SQLITESTORAGE_H
4+
5+
/************************************************************
6+
MySQLStorage class header
7+
************************************************************/
8+
#include <AnnotatorLib/Storage/AbstractStorage.h>
9+
#include <AnnotatorLib/Storage/Pkg_Storage.h>
10+
#include <AnnotatorLib/annotatorlib_api.h>
11+
#include <AnnotatorLib/Storage/MySQLStorage.h>
12+
#include <QJsonObject>
13+
14+
#include <Poco/Data/SessionPool.h>
15+
16+
namespace AnnotatorLib {
17+
namespace Storage {
18+
19+
/************************************************************/
20+
/**
21+
* @brief The SQLiteStorage class
22+
*/
23+
class ANNOTATORLIB_API SQLiteStorage : public MySQLStorage {
24+
25+
public:
26+
~SQLiteStorage();
27+
28+
bool open();
29+
30+
/**
31+
* @brief setPath
32+
* @param path
33+
* Path should be like:
34+
* "annotator.db";
35+
*/
36+
void setPath(std::string path) override;
37+
38+
StorageType getType() override;
39+
40+
void createTables() override;
41+
};
42+
/************************************************************/
43+
/* External declarations (package visibility) */
44+
/************************************************************/
45+
46+
/* Inline functions */
47+
48+
} // of namespace Storage
49+
} // of namespace AnnotatorLib
50+
51+
/************************************************************
52+
End of SQLiteStorage class header
53+
************************************************************/
54+
55+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2016 Annotator Team
2+
#define Annotator_AnnotatorLib_Storage_SQLiteStorage_BODY
3+
4+
/************************************************************
5+
SQLiteStorage class body
6+
************************************************************/
7+
8+
// include associated header file
9+
#include <AnnotatorLib/Loader/SQLiteLoader.h>
10+
#include <AnnotatorLib/Saver/SQLiteSaver.h>
11+
#include <AnnotatorLib/Storage/SQLiteStorage.h>
12+
13+
#include <Poco/Data/SQLite/Connector.h>
14+
#include <Poco/Data/RecordSet.h>
15+
#include <Poco/Data/Session.h>
16+
#include <Poco/Data/Statement.h>
17+
18+
using Poco::Data::Session;
19+
using Poco::Data::Statement;
20+
using namespace Poco::Data::Keywords;
21+
22+
namespace AnnotatorLib {
23+
namespace Storage {
24+
25+
void SQLiteStorage::setPath(std::string path) { this->path = path; }
26+
27+
StorageType SQLiteStorage::getType() { return AnnotatorLib::StorageType::SQLITE; }
28+
29+
SQLiteStorage::~SQLiteStorage() {
30+
if (pool) delete pool;
31+
}
32+
33+
bool SQLiteStorage::open() {
34+
Poco::Data::SQLite::Connector::registerConnector();
35+
pool = new Poco::Data::SessionPool("SQLite", this->path);
36+
createTables();
37+
38+
AnnotatorLib::Loader::SQLiteLoader loader;
39+
loader.setPath(this->path);
40+
loader.loadSession(this);
41+
42+
this->_open = true;
43+
return _open;
44+
}
45+
46+
void SQLiteStorage::createTables() {
47+
Poco::Data::Statement statement = getStatement();
48+
49+
statement = getStatement();
50+
51+
statement << "PRAGMA encoding = \"UTF-8\";";
52+
statement.execute();
53+
54+
statement << "CREATE TABLE IF NOT EXISTS `annotations` ( \
55+
`id` char(16) NOT NULL, \
56+
`next` char(16) NOT NULL, \
57+
`previous` char(16) NOT NULL, \
58+
`object` char(16) NOT NULL, \
59+
`frame` char(16) NOT NULL, \
60+
`x` float NOT NULL default 0, \
61+
`y` float NOT NULL default 0, \
62+
`width` float NOT NULL default 1, \
63+
`height` float NOT NULL default 1, \
64+
`type` char(16) NOT NULL, \
65+
PRIMARY KEY (`id`) \
66+
) DEFAULT CHARSET=utf8;";
67+
statement.execute();
68+
69+
statement = getStatement();
70+
71+
statement << "CREATE TABLE IF NOT EXISTS `classes` ("
72+
<< "`id` char(16) NOT NULL, "
73+
<< "`name` varchar(256) NOT NULL,"
74+
<< "PRIMARY KEY (`id`)"
75+
<< ") DEFAULT CHARSET=utf8;";
76+
statement.execute();
77+
78+
statement = getStatement();
79+
80+
statement << "CREATE TABLE IF NOT EXISTS `objects` ("
81+
<< "`id` char(16) NOT NULL, "
82+
<< "`name` varchar(256) NOT NULL,"
83+
<< "`class` char(16) NOT NULL,"
84+
<< "PRIMARY KEY (`id`)"
85+
<< ") DEFAULT CHARSET=utf8;";
86+
statement.execute();
87+
}
88+
89+
// static attributes (if any)
90+
91+
} // of namespace Storage
92+
} // of namespace AnnotatorLib
93+
94+
/************************************************************
95+
End of SQLiteStorage class body
96+
************************************************************/

source/annotatorlib/source/Storage/StorageFactory.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// include associated header file
1111
#include "AnnotatorLib/Storage/JSONStorage.h"
1212
#include "AnnotatorLib/Storage/MySQLStorage.h"
13+
#include "AnnotatorLib/Storage/SQLiteStorage.h"
1314
#include "AnnotatorLib/Storage/StorageFactory.h"
1415
#include "AnnotatorLib/Storage/XMLStorage.h"
1516

@@ -38,6 +39,7 @@ shared_ptr<AbstractStorage> StorageFactory::createStorage(
3839
case StorageType::MYSQL:
3940
return std::make_shared<MySQLStorage>();
4041
case StorageType::SQLITE:
42+
return std::make_shared<SQLiteStorage>();
4143
default:
4244
return nullptr;
4345
}

0 commit comments

Comments
 (0)