void tag_database::execute(context &ctx, std::ostream &out, const tag *caller) const { const string &dsn = get_parameter(ctx, "dsn"); const string &database = get_parameter(ctx, "id"); if (dsn.empty()) { throw tag_database_exception( _("data source name (dsn) is not defined")); } string db_ptr = ctx.get_value(string("@SQLITE:DATABASE@") + database); if (!db_ptr.empty()) { tag_impl::execute(ctx, out, caller); return; } ctx.enter(); try { pool_ptr<sqlite_connection> c = database_pool::instance().acquire(dsn); if (!c->is_open()) c->open(dsn); // save the pointer to database for nested tags ctx.add_value(string("@SQLITE:DATABASE@") + get_parameter(ctx, "id"), to_string<sqlite3*>(c->get_ptr())); tag_impl::execute(ctx, out, caller); ctx.leave(); } catch (...) { ctx.leave(); throw; } }
void tag_delete::execute(context &ctx, std::ostream &out, const tag *caller) const { const string &collection = get_parameter(ctx, "collection"); if (collection.empty()) throw tag_delete_exception( _("query collection (collection) is not defined")); string db_ptr = ctx.get_value("@MONGODB:DATABASE@"); if (db_ptr.empty()) { throw tag_delete_exception( _("MongoDB database is not defined in the current context")); } database *db = (database*)from_string<void*>(db_ptr); ctx.enter(); try { BSONObjBuilder b; ctx.add_value("@MONGODB:PARAM@", to_string<BSONObjBuilder*>(&b)); // call inherited method tag_impl::execute(ctx, out, caller); db->remove(collection, b.obj()); } catch (...) { ctx.leave(); throw; } ctx.leave(); }
void tag_query::execute(context &ctx, std::ostream &out, const tag *caller) const { // obtain the current database of current context string db_ptr = ctx.get_value(string("@ODBC:DATABASE@") + get_parameter(ctx, "database")); if (db_ptr.empty()) { string id = get_parameter(ctx, "database"); if (id.empty()) { id = _("(default)"); } throw tag_query_exception( (format(_("database (id='{0}') is not defined in the current " "context")) % id).str()); } connection *db = (connection*)from_string<void*>(db_ptr); // intitialize the query query q(*db); query::parameters &p = q.prepare(get_parameter(ctx, "statement")); // process parameters vector<string> params = tokenize()(get_parameter(ctx, "parameters")); vector<string>::const_iterator j = params.begin(); for (query::parameters::iterator i = p.begin(); i != p.end(); ++i) { if (i->name.empty()) { // initialize unnamed parameter if (j != params.end()) { i->value = *j; ++j; } } else { // initialize named parameter i->value = ctx.get_value(i->name); } } // execute the query const query::fields &f = q.execute(); // for each row execute the subnodes while (q.next()) { ctx.enter(); try { for (query::fields::const_iterator i = f.begin(); i != f.end(); ++i) { ctx.add_value(i->name, i->get_value()); } // call inherited method tag_impl::execute(ctx, out, caller); } catch (...) { ctx.leave(); throw; } ctx.leave(); } }
void tag_usr::execute(context &ctx, std::ostream &out, const tag *caller) const { // create new context layer ctx.enter(); try { for (parameters::const_iterator i = params.begin(); i != params.end(); ++i) { // create and initialize the variable from parameter ostringstream s; (i->second)->execute(ctx, s, this); ctx.add_value(i->first, s.str()); } tag* t = user_tag; if (!t) { tag *p = parent; while (p && !(t = p->get_child("@" + name))) { p = p->get_parent(); } if (!t) throw tag_exception( (format(_("custom tag '{0}' is not defined at this scope")) % name).str()); } // execute custom tag ctx.push(this); try { static_cast<tag_tag*>(t)->execute(ctx, out, this); } catch (...) { ctx.pop(); throw; } ctx.pop(); } catch (...) { // free context layer created ctx.leave(); throw; } // free context layer created ctx.leave(); }
void tag_var::execute(context &ctx, std::ostream &out, const tag *caller) const { // create new context layer ctx.enter(); try { // create and initialize the variable strings vars = tokenize()(get_parameter(ctx, "name")); for (strings::const_iterator i = vars.begin(); i < vars.end(); ++i) { string p_type = get_parameter(ctx, "type"); ctx.add_value(*i, get_parameter(ctx, "value"), p_type.empty() ? "local" : p_type); } // call inherited method tag_impl::execute(ctx, out, caller); } catch (...) { // free context layer created ctx.leave(); throw; } // free context layer created ctx.leave(); }
void tag_task::execute(context &ctx, std::ostream &out, const tag *caller) const { const string &function = get_parameter(ctx, "function"); string server_ptr = ctx.get_value("@GEARMAN:server@"); if (server_ptr.empty()) throw tag_task_exception( _("gearman server is not defined in the current " "context")); server *gm = (server*)from_string<void*>(server_ptr); ctx.enter(); try { // cache protected output ostringstream s(ostringstream::out | ostringstream::binary); // call inherited method tag_impl::execute(ctx, s, caller); gm->add_task(function, s.str()); ctx.leave(); } catch (...) { ctx.leave(); throw; } }
void tag_query::execute(context &ctx, std::ostream &out, const tag *caller) const { // obtain the current database of current context string db_ptr = ctx.get_value(string("@PGSQL:DATABASE@") + get_parameter(ctx, "database")); if (db_ptr.empty()) { string id = get_parameter(ctx, "database"); if (id.empty()) { id = _("(default)"); } throw tag_query_exception( (format(_("database (id='{0}') is not defined in the current " "context")) % id).str()); } pqxx::connection *conn = (pqxx::connection*)from_string<void*>(db_ptr); bool need_to_free = false; pqxx::work *work; pqxx::result r; string work_ptr = ctx.get_value(string("@PGSQL:WORK@")); if (!work_ptr.empty()) { work = (pqxx::work*)from_string<void*>(work_ptr); } else { try { need_to_free = true; work = new pqxx::work(*conn); } catch(const std::exception &e) { throw tag_query_exception(e.what()); } } try { string self_ptr = to_string<const tag_query*>(this); string statement; strings params; parse(get_parameter(ctx, "statement"), statement, params); conn->unprepare(self_ptr); conn->prepare(self_ptr, statement); pqxx::prepare::invocation q = work->prepared(self_ptr); strings passed_params = tokenize()(get_parameter(ctx, "parameters")); strings::const_iterator j = passed_params.begin(); for (strings::const_iterator i = params.begin(); i != params.end(); ++i) { if (i->empty()) { q(*j); ++j; } else { string value = ctx.get_value(*i); if (value.empty()) q(); else q(value); } } r = q.exec(); if (need_to_free) { work->commit(); delete work; } } catch(const std::exception &e) { if (need_to_free) { work->abort(); delete work; } throw tag_query_exception(e.what()); } for (pqxx::result::const_iterator row = r.begin(); row != r.end(); ++row) { ctx.enter(); try { for (pqxx::result::tuple::const_iterator field = row->begin(); field != row->end(); ++field) { if (field.type() == 16) { // boolean try { if (field.as<bool>()) ctx.add_value(field.name(), "1"); else ctx.add_value(field.name(), "0"); } catch(...) { ctx.add_value(field.name(), ""); } } else ctx.add_value(field.name(), field.c_str()); } // call inherited method tag_impl::execute(ctx, out, caller); } catch (...) { ctx.leave(); throw; } ctx.leave(); } }