Created
July 31, 2017 09:13
-
-
Save kimgea/3ebb5c0151042f254eec56e64953e13d to your computer and use it in GitHub Desktop.
Fast and dirty code to show structure for a way to query on objects. Needs improvment for working with const and puting stuff in lists if chosen, and probably more.
This file contains 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 "stdafx.h" | |
#include <string> | |
class collection {}; | |
template<typename query_set_base_t> | |
struct query_set_base | |
{ | |
query_set_base_t & select(const std::string & q) { return *static_cast<query_set_base_t*>(this); } | |
query_set_base_t & filter(const std::string & q) { return *static_cast<query_set_base_t*>(this); } | |
query_set_base_t & order_by(const std::string & q) { return *static_cast<query_set_base_t*>(this); } | |
}; | |
class query_set : public query_set_base<query_set> {}; | |
template<typename query_set_t> | |
class query_manager_base | |
{ | |
protected: | |
query_set_t _query; | |
public: | |
// from standard query set | |
query_set_t & select(const std::string & q) { return _query.select(q); } | |
query_set_t & filter(const std::string & q) { return _query.filter(q); } | |
query_set_t & order_by(const std::string & q) { return _query.order_by(q); } | |
// standard query manager functions | |
void update() {}; | |
void insert() {}; | |
}; | |
typedef query_manager_base<query_set> query_manager; | |
template<typename query_manager_t> | |
struct model_base // Table | |
{ | |
query_manager_t objects; | |
void update() { objects.update(); } | |
void insert() { objects.insert(); } | |
}; | |
typedef model_base<query_manager> model; | |
////////////////////////////////////////////////////////////////////// | |
// Custom query managers and querysets | |
// Custom query set | |
struct custom_query_set : public query_set_base<custom_query_set> | |
{ | |
custom_query_set & get_special_users() { return *this; } | |
}; | |
// custom query manager | |
struct custom_query_manager : public query_manager_base<custom_query_set> | |
{ | |
// From custom query set | |
custom_query_set & get_special_users() { return _query.get_special_users(); } | |
// Custom query manager functions | |
void user(const std::string & client_no, collection & c) {} | |
void update() {} // Custom update. like setting timestamps.. | |
}; | |
////////////////////////////////////////////////////////////////////// | |
// models / tables | |
class user : public model {}; | |
class customer : public model_base<custom_query_manager> {}; | |
int main() | |
{ | |
user c; // Has only out of the box functionality | |
c.objects.select("UserNo = '1'"); | |
collection col; | |
customer cdc; // Has CUSTOM queryset and query manager functions | |
cdc.objects.user("1", col); // CUSTOM query function | |
cdc.objects. | |
select(""). | |
get_special_users(). // CUSTOM query for case_debt_collection | |
filter(""). | |
order_by(""); | |
cdc.update(); // Calls CUSTOM update | |
cdc.insert(); // calls standard insert | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment