pet.cpp 1.96 KB
#include "pet.h"

#include "func_rount.h"

namespace {
	const char* STR_UID		=	"uid";
	const char* STR_ID			=	"pet_id";
	const char* STR_VAL		=	"val";
	const char* STR_EXP		=	"exp";
	const char* STR_CNT		=	"cnt";
}

pet_t::pet_t( el::lib_mysql_if* db )
	 : el::lib_mysql_table_route10x10(db, g_gen_db_name("USER"), "t_pet")
{
}

int pet_t::update( USER_ID uid, uint32_t id, uint32_t val, uint32_t exp, uint32_t cnt)
{
	GEN_SQLSTR(this->sqlstr, "insert into %s (%s, %s, %s, %s, %s)"
		"values(%" PRIu64 ",%u,%u,%u,%u) on duplicate key update %s=%u, %s=%u, %s=%u",
		this->get_table_name(uid),
		STR_UID, STR_ID, STR_VAL, STR_EXP, STR_CNT,
		uid, id, val, exp, cnt,
		STR_VAL, val, STR_EXP, exp, STR_CNT, cnt);
	return this->exec_update_sqls(this->sqlstr);
}

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

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

	uint32_t t_id = 0;
	uint32_t t_val = 0;
	uint32_t t_exp = 0;
	uint32_t t_cnt = 0;

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

	GET_FIELD_UINT64(t_val);
//	member->set_val(t_val);

	GET_FIELD_UINT64(t_exp);
	member->set_exp(t_exp);

	GET_FIELD_UINT64(t_cnt);
	member->set_cnt(t_cnt);

	PB_STD_QUERY_WHILE_END();
}


int pet_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 Cfunc_route::on_update_pet_msg(USER_ID uid, google::protobuf::Message* msg)
{
	auto in = (db_msg::update_pet_msg*)msg;

	auto p = in->mutable_pet();
	this->ret = this->pet.update(uid, p->id(), 0,/*p->val(),*/ p->exp(), p->cnt());

	return this->ret;
}