furniture.cpp 2.32 KB
#include "furniture.h"

#include "func_rount.h"

namespace {
	const char* STR_UID		=	"uid";
	const char* STR_ID			=	"furniture_id";
	const char* STR_X			=	"x";
	const char* STR_Y			=	"y";
}

furniture_t::furniture_t( el::lib_mysql_if* db )
	 : el::lib_mysql_table_route10x10(db, g_gen_db_name("USER"), "t_furniture")
{
}

int furniture_t::update( USER_ID uid, uint32_t id, uint32_t x, uint32_t y )
{
	GEN_SQLSTR(this->sqlstr, "insert into %s (%s, %s, %s, %s)"
		"values(%" PRIu64 ",%u,%u,%u) on duplicate key update %s=%u, %s=%u",
		this->get_table_name(uid),
		STR_UID, STR_ID, STR_X, STR_Y,
		uid, id, x, y,
		STR_X, x, STR_Y, y);
	return this->exec_update_sqls(this->sqlstr);
}

int furniture_t::get( USER_ID uid, db_msg::load_user_msg_res* out )
{
	GEN_SQLSTR(this->sqlstr,"select %s,%s,%s from %s where %s=%" PRIu64,
		STR_ID, STR_X, STR_Y, 
		this->get_table_name(uid),
		STR_UID, uid);

	PB_STD_QUERY_WHILE_BEGIN(this->sqlstr, out, add_furnitures);

	uint32_t t_id = 0;
	uint32_t t_x = 0;
	uint32_t t_y = 0;

	GET_FIELD_UINT64(t_id);
	member->set_id(t_id);

	GET_FIELD_UINT64(t_x);
	member->set_x(t_x);

	GET_FIELD_UINT64(t_y);
	member->set_y(t_y);
	PB_STD_QUERY_WHILE_END();
}

int furniture_t::get_type_cnt( USER_ID uid, uint32_t& type_cnt )
{
	GEN_SQLSTR(this->sqlstr,"select COUNT(*) from %s where %s=%" PRIu64,
		this->get_table_name(uid), STR_UID, uid);

	STD_QUERY_ONE_BEGIN(this->sqlstr, el::ERR_DB::KEY_INEXIST);

	uint32_t v = 0;
	GET_FIELD_UINT64(v);
	type_cnt = v;

	STD_QUERY_ONE_END();
}

int furniture_t::get_home( USER_ID uid, share_msg::user_home_t* out )
{
	GEN_SQLSTR(this->sqlstr,"select %s,%s,%s from %s where (x <> 0 or y <> 0 )and %s=%" PRIu64,
		STR_ID, STR_X, STR_Y, 
		this->get_table_name(uid),
		STR_UID, uid);

	PB_STD_QUERY_WHILE_BEGIN(this->sqlstr, out, add_furniture);

	uint32_t t_id = 0;
	uint32_t t_x = 0;
	uint32_t t_y = 0;

	GET_FIELD_UINT64(t_id);
	member->set_id(t_id);

	GET_FIELD_UINT64(t_x);
	member->set_x(t_x);

	GET_FIELD_UINT64(t_y);
	member->set_y(t_y);
	PB_STD_QUERY_WHILE_END();
}

//////////////////////////////////////////////////////////////////////////
//
int Cfunc_route::on_update_furniture_msg(USER_ID uid, google::protobuf::Message* msg)
{
	auto in = (db_msg::update_furniture_msg*)msg;

	auto p = in->mutable_furniture();
	this->ret = this->furniture.update(uid, p->id(), p->x(), p->y());

	return this->ret;
}