Ejemplo n.º 1
0
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();
	}
}
Ejemplo n.º 2
0
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();
}
Ejemplo n.º 3
0
void tag_set::execute(context &ctx, std::ostream &out,
  const tag *caller) const {
	string name = get_parameter(ctx, "name");
	if (name.empty())
		throw tag_storage_exception(
		  _("key name (name) is not defined"));
	// obtain the current storage of current context by ugly hack
	string db_ptr = ctx.get_value(string("@BDB:DATABASE@") +
	  get_parameter(ctx, "storage"));
	if (db_ptr.empty()) {
		string id = get_parameter(ctx, "storage");
		if (id.empty()) {
			id = _("(default)");
		}
		throw tag_set_exception(
		  (format(_("storage (id='{0}') is not defined in the current context")) %
		    id).str());
	}
	bdb_database *db = (bdb_database*)from_string<void*>(db_ptr);
	// cache protected output
	ostringstream s(ostringstream::out | ostringstream::binary);
	// call inherited method
	tag_impl::execute(ctx, s, caller);
	// set the data
	db->set(name, s.str());
}
Ejemplo n.º 4
0
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;
	}

}
Ejemplo n.º 5
0
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;
    }
}
Ejemplo n.º 6
0
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();
	}

}