31 using namespace PGSTD;
36 class AddItem :
public transactor<> {
40 AddItem(
Item* item) : transactor<>(
"AddItem"), m_item(item) {}
42 void operator()(argument_type &T) {
43 T.exec(
"INSERT INTO item (description, checked) values('" +
46 result R(T.exec(
"SELECT lastval()"));
47 R.front()[0].to(m_new_id);
58 class AssociateItemWithCategory :
public transactor<> {
62 AssociateItemWithCategory(
long id,
long cat_id) :
63 transactor<>(
"AssociateItemWithCategory"), m_id(id), m_cat_id(cat_id) {}
65 void operator()(argument_type &T) {
67 os <<
"INSERT INTO category_item (item, category) VALUES('" << m_id <<
"', '" << m_cat_id <<
"')";
75 class DeleteItem :
public transactor<> {
78 DeleteItem(
long id) : transactor<>(
"DeleteItem"), m_id(id) {}
80 void operator()(argument_type &T) {
82 os <<
"DELETE FROM item WHERE id='" << m_id <<
'\'';
90 class UpdateItemCheckedState :
public transactor<> {
94 : transactor<>(
"UpdateItemCheckedState"), m_items(items) {}
96 void operator()(argument_type &T) {
97 for (
ItemIter i = m_items.begin(); i != m_items.end(); ++i) {
99 os <<
"UPDATE item SET checked=" 100 << ((*i)->get_checked() ?
"true" :
"false")
101 <<
" WHERE id=" << (*i)->get_id();
110 class RemoveItemFromCategory :
public transactor<> {
114 RemoveItemFromCategory(
long id,
long cat_id) :
115 transactor<>(
"RemoveItemFromCategory"), m_id(id), m_cat_id(cat_id) {}
117 void operator()(argument_type &T) {
119 os <<
"DELETE FROM category_item WHERE item='" 120 << m_id <<
"' AND category='" << m_cat_id <<
'\'';
128 class SetItemFlag :
public transactor<> {
132 SetItemFlag(
long id,
bool flag) : transactor<>(
"SetItemFlag"), m_id(id), m_flag(flag) {}
134 void operator()(argument_type &T) {
136 os <<
"UPDATE item SET checked=" << (m_flag ?
"true" :
"false") <<
" WHERE id='" << m_id <<
'\'';
144 class SetCategoryFlag :
public transactor<> {
148 SetCategoryFlag(
long cat_id,
bool flag) : transactor<>(
"SetCategoryFlag"), m_cat_id(cat_id), m_flag(flag) {}
150 void operator()(argument_type &T) {
152 os <<
"UPDATE item SET checked=" << (m_flag ?
"true" :
"false") <<
" FROM category_item AS c WHERE item.id=c.item AND c.category='" << m_cat_id <<
"'";
160 class SetAllFlags :
public transactor<> {
163 SetAllFlags(
bool flag) : transactor<>(
"SetAllFlags"), m_flag(flag) {}
165 void operator()(argument_type &T) {
167 os <<
"UPDATE item SET checked=" << (m_flag ?
"true" :
"false");
175 class DeleteCategory :
public transactor<> {
178 DeleteCategory(
long id) : transactor<>(
"DeleteCategory"), m_id(id) {}
180 void operator()(argument_type &T) {
182 os <<
"DELETE FROM category WHERE id='" << m_id <<
'\'';
190 class NewCategory :
public transactor<> {
195 NewCategory(
string name,
long*
id) : transactor<>(
"NewCategory"), m_name(name), m_new_id(id) {}
197 void operator()(argument_type &T) {
198 T.exec(
"INSERT INTO category (NAME) values('" + T.esc(m_name) +
"')");
199 R = T.exec(
"SELECT lastval()");
203 R.front()[0].to(*m_new_id);
210 class AddCategoryItem :
public transactor<> {
215 AddCategoryItem(
Item* item,
long cat_id)
216 : transactor<>(
"AddCategoryItem"), m_item(item), m_cat_id(cat_id) {}
218 void operator()(argument_type &T) {
219 T.exec(
"INSERT INTO item (description, checked) values('" +
222 result R(T.exec(
"SELECT lastval()"));
223 result::reference ref = R.front();
224 m_new_id =ref[0].as(
long());
226 os <<
"INSERT INTO category_item (category, item) VALUES('" 227 << m_cat_id <<
"', '" << m_new_id <<
"')";
241 class CopyItemsToCategory :
public transactor<> {
246 CopyItemsToCategory(
long from_cat_id,
long to_cat_id,
item_choice choice)
247 : transactor<>(
"CopyItemsToCategory"),
248 m_from_cat_id(from_cat_id),
249 m_to_cat_id(to_cat_id),
250 m_item_choice(choice) {}
252 void operator()(argument_type &T) {
255 os <<
"INSERT INTO category_item (item, category) ";
258 if (m_from_cat_id >= 0) {
259 switch (m_item_choice) {
262 os <<
"SELECT ci.item AS item, '" << m_to_cat_id <<
"' AS category FROM category_item AS ci " 263 <<
"JOIN item i ON ci.item=i.id ";
266 os <<
"SELECT ci.item AS item, '" << m_to_cat_id <<
"' AS category FROM category_item AS ci ";
268 os <<
"WHERE ci.category='" << m_from_cat_id <<
"' ";
270 switch (m_item_choice) {
272 os <<
"AND i.checked=true ";
275 os <<
"AND i.checked=false ";
278 os <<
"AND ci.item NOT IN ";
280 os <<
"SELECT i.id AS item, '" << m_to_cat_id <<
"' FROM item AS i WHERE ";
281 switch (m_item_choice) {
283 os <<
"i.checked=true AND ";
286 os <<
"i.checked=false AND ";
289 os <<
"i.id NOT IN ";
293 os <<
"(SELECT c2.item FROM category_item AS c2 WHERE c2.category='" << m_to_cat_id <<
"')";
302 class GetNextItemId :
public transactor<> {
306 GetNextItemId(
long*
id) : transactor<>(
"GetNextItemId"), m_id(id) {}
308 void operator()(argument_type &T) {
309 R = T.exec(
"SELECT nextval('item_sequence')");
313 R.front()[0].to(*m_id);
320 class GetNextCategoryId :
public transactor<> {
324 GetNextCategoryId(
long* cat_id) : transactor<>(
"GetNextCategoryId"), m_cat_id(cat_id) {}
326 void operator()(argument_type &T) {
327 R = T.exec(
"SELECT nextval('category_sequence')");
331 R.front()[0].to(*m_cat_id);
341 class GetAllItems :
public transactor<nontransaction> {
347 : transactor<nontransaction>(
"GetAllItems"), m_items(items), m_item_choice(choice) {}
349 void operator()(argument_type &T) {
352 os <<
"SELECT id, description, checked FROM item";
353 switch (m_item_choice) {
355 os <<
" WHERE checked=true";
358 os <<
" WHERE checked=false";
361 os <<
" ORDER BY id";
362 R = T.exec(os.str());
367 for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
369 item->
set_id(c[0].as(
long()));
372 m_items->push_back(item);
380 class LoadModel :
public pqxx::transactor<pqxx::nontransaction> {
385 LoadModel(
KitModel& kitmodel) : pqxx::transactor<pqxx::nontransaction>(
"LoadModel"), m_kitmodel(kitmodel) {}
387 void operator()(argument_type &T) {
388 R = T.exec(
"SELECT i.id AS item_id, c.id AS category_id, i.description, c.name AS category, i.checked FROM item AS i FULL OUTER JOIN category_item AS ci ON i.id=ci.item FULL OUTER JOIN category AS c ON c.id=ci.category");
392 for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
395 if (!c[0].is_null()) {
396 long item_id = c[0].as(
long());
409 if (!c[1].is_null()) {
410 long cat_id = c[1].as(
long());
417 category->
set_name(c[3].as(
string()));
432 class SaveModel :
public transactor<> {
439 SaveModel(
KitModel* model) : transactor<>(
"SaveModel"), m_model(model), m_items(0) {}
443 m_items.push_back(&item);
449 m_categories.push_back(&category);
454 void operator()(argument_type &T) {
457 m_model->
foreach_item( sigc::mem_fun(*
this, &SaveModel::on_foreach_item) );
460 for (
ModelItemIter i = m_items.begin(); i != m_items.end(); ++i) {
465 os <<
"INSERT INTO item (id, description, checked) values(" 466 << item->
get_id() <<
", '" 468 << (item->
get_checked() ?
"true" :
"false") <<
")";
470 os <<
"DELETE FROM item WHERE id=" 473 os <<
"UPDATE item SET description='" 476 <<
" WHERE id=" << item->
get_id();
478 cout <<
"Query: " << os.str() << endl;
484 m_model->
foreach_category( sigc::mem_fun(*
this, &SaveModel::on_foreach_category) );
492 os <<
"INSERT INTO category (id, name) values(" 496 os <<
"DELETE FROM category WHERE id=" 499 os <<
"UPDATE category SET name='" 501 <<
"' WHERE id=" << cat->
get_id();
503 cout <<
"Query: " << os.str() << endl;
511 for (
ItemMapIter i2 = items->begin(); i2 != items->end(); i2++) {
514 os <<
"DELETE FROM category_item WHERE item=" 518 cout <<
"Query: " << os.str() << endl;
522 for (
ItemMapIter i2 = items->begin(); i2 != items->end(); i2++) {
526 os <<
"INSERT INTO category_item (item, category) values (" 529 cout <<
"Query: " << os.str() << endl;
552 class LoadCategory :
public transactor<nontransaction> {
558 : transactor<nontransaction>(
"LoadCategory"), m_category(category), m_item_choice(choice) {}
560 void operator()(argument_type &T) {
563 os <<
"SELECT id, description, checked FROM item AS i " 564 <<
"JOIN category_item AS ci ON i.id=ci.item " 565 <<
"WHERE ci.category = '" << m_category->
get_id() <<
"'";
567 switch (m_item_choice) {
569 os <<
" AND i.checked=true";
572 os <<
" AND i.checked=false";
576 os <<
" ORDER BY i.id";
577 R = T.exec(os.str());
582 for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
584 i->
set_id(c[0].as(
long()));
597 class GetCategories :
public transactor<nontransaction> {
602 : transactor<nontransaction>(
"GetCategories"), m_categories(categories) {}
604 void operator()(argument_type &T) {
605 m_result = T.exec(
"SELECT id, name FROM category ORDER BY id");
609 if (!m_result.empty()) {
610 for (result::const_iterator c = m_result.begin(); c != m_result.end(); ++c) {
612 cat->
set_id(c[0].as(
long()));
614 m_categories->push_back(cat);
630 KitListPgsqlDao::KitListPgsqlDao(
int verbose) :
KitListDao(verbose) {
647 KitListPgsqlDao::KitListPgsqlDao(
string dbname,
int verbose) :
KitListDao(verbose), m_dbname(dbname) {
666 KitListPgsqlDao::KitListPgsqlDao(
string dbname,
string user,
string port,
int verbose)
667 :
KitListDao(verbose), m_dbname(dbname), m_user(user), m_port(port) {
671 KitListPgsqlDao::~KitListPgsqlDao() {
672 delete m_db_connection;
674 cout <<
"Connection closed" << endl;
680 void KitListPgsqlDao::connect() {
683 if (m_dbname.size() > 0) cs +=
"dbname=" + m_dbname;
684 if (m_user.size() > 0) cs +=
" user=" + m_user;
685 if (m_port.size() > 0) cs +=
" port=" + m_port;
687 cout <<
"Connect string: \"" << cs <<
'"' << endl;
689 m_db_connection =
new connection(cs);
691 cout <<
"Connected to database: " << m_db_connection->dbname() << endl
692 <<
"Username: " << m_db_connection->username() << endl
693 <<
"Hostname: " << (m_db_connection->hostname() ? m_db_connection->hostname() :
"(null)") << endl
694 <<
"Socket: " << m_db_connection->sock() << endl
695 <<
"Backend PID: " << m_db_connection->backendpid() << endl
696 <<
"Port: " << m_db_connection->port() << endl
697 <<
"Backend version: " << m_db_connection->server_version() << endl
698 <<
"Protocol version: " << m_db_connection->protocol_version() << endl << endl;
711 m_db_connection->perform(LoadCategory(retval, choice));
725 GetAllItems g = GetAllItems(retval, choice);
726 m_db_connection->perform(g);
735 long KitListPgsqlDao::add_item(
const std::string name) {
738 m_db_connection->perform(AddItem(&item));
747 long KitListPgsqlDao::add_item(
const std::string name,
long cat_id) {
750 m_db_connection->perform(AddCategoryItem(&item, cat_id));
763 void KitListPgsqlDao::append_items_to_category(
long to_cat_id,
long from_cat_id,
item_choice choice) {
764 m_db_connection->perform(CopyItemsToCategory(from_cat_id, to_cat_id, choice));
774 void KitListPgsqlDao::associate_item_with_category(
long id,
long cat_id) {
775 m_db_connection->perform(AssociateItemWithCategory(
id, cat_id));
784 m_db_connection->perform(GetCategories(&retval));
793 long KitListPgsqlDao::new_category(
const std::string name) {
795 m_db_connection->perform(NewCategory(name, &retval));
804 void KitListPgsqlDao::delete_item(
long id) {
805 m_db_connection->perform(DeleteItem(
id));
812 void KitListPgsqlDao::update_item_checked_state(
ItemContainer& items) {
813 m_db_connection->perform(UpdateItemCheckedState(items));
823 void KitListPgsqlDao::remove_item_from_category(
long id,
long cat_id) {
824 m_db_connection->perform(RemoveItemFromCategory(
id, cat_id));
831 long KitListPgsqlDao::get_next_item_id() {
833 m_db_connection->perform(GetNextItemId(&retval));
841 long KitListPgsqlDao::get_next_category_id() {
843 m_db_connection->perform(GetNextCategoryId(&retval));
851 void KitListPgsqlDao::delete_category(
long id) {
852 m_db_connection->perform(DeleteCategory(
id));
860 void KitListPgsqlDao::set_item_flag(
long id) {
861 m_db_connection->perform(SetItemFlag(
id,
true));
868 void KitListPgsqlDao::unset_item_flag(
long id) {
869 m_db_connection->perform(SetItemFlag(
id,
false));
876 void KitListPgsqlDao::set_category_flag(
long id) {
877 m_db_connection->perform(SetCategoryFlag(
id,
true));
884 void KitListPgsqlDao::unset_category_flag(
long id) {
885 m_db_connection->perform(SetCategoryFlag(
id,
false));
892 void KitListPgsqlDao::set_all_flags() {
893 m_db_connection->perform(SetAllFlags(
true));
900 void KitListPgsqlDao::unset_all_flags() {
901 m_db_connection->perform(SetAllFlags(
false));
912 KitModel* KitListPgsqlDao::get_model() {
914 m_db_connection->perform(LoadModel(*retval));
926 void KitListPgsqlDao::save_model(
KitModel* model) {
927 m_db_connection->perform(SaveModel(model));
930 #endif //HAVE_LIBPQXX
ModelItemContainer::iterator ModelItemIter
Represents a Category combined with GuiState attributes.
ItemContainer::iterator ItemIter
virtual void add_item(Item *item)
Associates the passed item with this Category.
void foreach_category(const SlotForeachCategory &slot)
Calls the provided slot for each category in the map.
ItemMap::iterator ItemMapIter
virtual void add_category(ModelCategory *category)
Add a category to the model.
virtual ModelItem * find_item(long id)
Finds an item by it's unique ID.
std::vector< Item * > ItemContainer
virtual ModelCategory * find_category(long id)
Finds a Category by it's unique ID.
virtual void set_checked(bool checked)
void set_name(const std::string name)
std::vector< ModelItem * > ModelItemContainer
Defines the methods that an implementation of this class must implement.
std::string get_description()
Represents an Item combined with GuiState attributes.
std::vector< ModelCategory * > ModelCategoryContainer
Holds a rich graph of objects representing the application's data model.
virtual ItemMap * get_removed_children()
Returns a list of items that have been removed from the Category.
void foreach_item(const SlotForeachModelItem &slot)
Calls the provided slot for each item in the map.
virtual void set_checked(bool checked)
ModelCategoryContainer::iterator ModelCategoryIter
virtual void add_item(Item *)
Adds the passed item to this ModelCategory.
std::vector< Category * > CategoryContainer
virtual void add_item(ModelItem *item)
Adds an item to the model.
virtual void purge()
Purges deleted categories and items from the model.
virtual void reset()
Resets all contained objects to their default states.
std::map< long, ModelCategory * > CategoryMap
void set_description(const std::string description)
std::map< long, ModelItem * > ItemMap
virtual ItemMap * get_added_children()
Returns a list of items that have been added to the Category.