Last active
December 11, 2015 04:08
-
-
Save ibizaman/4542411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Undefined symbols: | |
| "bool& ResultSetHelper::get<bool&>(std::shared_ptr<sql::ResultSet>, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from: | |
| std::enable_if<(2)!=(3ul), void>::type DBTable<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool>::populateFromDB<2>(std::shared_ptr<sql::ResultSet>) in DBTableTest.o | |
| "int& ResultSetHelper::get<int&>(std::shared_ptr<sql::ResultSet>, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from: | |
| std::enable_if<(0)!=(3ul), void>::type DBTable<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool>::populateFromDB<0>(std::shared_ptr<sql::ResultSet>) in DBTableTest.o | |
| "std::basic_string<char, std::char_traits<char>, std::allocator<char> >& ResultSetHelper::get<std::basic_string<char, std::char_traits<char>, std::allocator<char> >&>(std::shared_ptr<sql::ResultSet>, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from: | |
| std::enable_if<(1)!=(3ul), void>::type DBTable<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool>::populateFromDB<1>(std::shared_ptr<sql::ResultSet>) in DBTableTest.o | |
| ld: symbol(s) not found |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef DBTABLE_H | |
| #define DBTABLE_H | |
| #include <array> | |
| #include <memory> | |
| #include <sstream> | |
| #include <string> | |
| #include <cppconn/sqlstring.h> | |
| #include <cppconn/resultset.h> | |
| namespace ResultSetHelper { | |
| template<typename T> T get(std::shared_ptr<sql::ResultSet>, const std::string&); | |
| } | |
| template<typename... Columns> | |
| class DBTable | |
| { | |
| public: | |
| typedef std::array<std::string, sizeof...(Columns)> Names; | |
| typedef std::tuple<Columns...> Values; | |
| DBTable(const std::string&, Names, std::shared_ptr<sql::ResultSet> = nullptr); | |
| private: | |
| template<int I> | |
| typename std::enable_if<I == sizeof...(Columns), void>::type | |
| populateFromDB(std::shared_ptr<sql::ResultSet>); | |
| template<int I> | |
| typename std::enable_if<I != sizeof...(Columns), void>::type | |
| populateFromDB(std::shared_ptr<sql::ResultSet>); | |
| std::string _table; | |
| Names _columnNames; | |
| Values _columns; | |
| bool _populatedFromDB; | |
| }; | |
| #include "DBTable.hpp" | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template<typename... Columns> | |
| DBTable<Columns...>::DBTable(const std::string& table, Names columnNames, std::shared_ptr<sql::ResultSet> db) | |
| : _table(table), | |
| _columnNames(columnNames) | |
| { | |
| if (db == nullptr) { | |
| _populatedFromDB = false; | |
| } else { | |
| _populatedFromDB = true; | |
| populateFromDB<0>(db); | |
| } | |
| } | |
| template<typename... Columns> | |
| template<int I> | |
| typename std::enable_if<I == sizeof...(Columns), void>::type | |
| DBTable<Columns...>::populateFromDB(std::shared_ptr<sql::ResultSet> db) | |
| { | |
| (void)db; | |
| } | |
| template<typename... Columns> | |
| template<int I> | |
| typename std::enable_if<I != sizeof...(Columns), void>::type | |
| DBTable<Columns...>::populateFromDB(std::shared_ptr<sql::ResultSet> db) | |
| { | |
| std::get<I>(_columns) = ResultSetHelper::get<decltype(std::get<I>(_columns))>(db, std::get<I>(_columnNames)); | |
| populateFromDB<I+1>(db); | |
| } | |
| namespace ResultSetHelper { | |
| template<> | |
| std::istream* get<std::istream*>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getBlob(column); | |
| } | |
| template<> | |
| bool get<bool>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getBoolean(column); | |
| } | |
| template<> | |
| long double get<long double>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getDouble(column); | |
| } | |
| template<> | |
| int32_t get<int32_t>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getInt(column); | |
| } | |
| template<> | |
| uint32_t get<uint32_t>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getUInt(column); | |
| } | |
| template<> | |
| int64_t get<int64_t>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getInt64(column); | |
| } | |
| template<> | |
| uint64_t get<uint64_t>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getUInt64(column); | |
| } | |
| template<> | |
| sql::SQLString get<sql::SQLString>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getString(column); | |
| } | |
| template<> | |
| std::string get<std::string>(std::shared_ptr<sql::ResultSet> db, const std::string& column) | |
| { | |
| return db->getString(column).asStdString(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <gtest/gtest.h> | |
| #include <gmock/gmock.h> | |
| #include "database/DBTable.hxx" | |
| #include <memory> | |
| #include <cppconn/resultset.h> | |
| #include "test/mock/ResultSet.h" | |
| class DBTableImpl : public DBTable<int,std::string,bool> | |
| { | |
| public: | |
| DBTableImpl(std::shared_ptr<sql::ResultSet> db = nullptr) | |
| : DBTable("table", {{"id","name","checked"}}, db) | |
| { | |
| } | |
| }; | |
| TEST( DBTableTest, fromDatabase ) | |
| { | |
| mock::ResultSet result; | |
| DBTableImpl table(std::shared_ptr<sql::ResultSet>(&result)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment