|
| 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 | + ************************************************************/ |
0 commit comments