Http服务器-第七步对lua添加SQL支持

C++封装一层,然后给lua用,

SQLInterface.cpp



#include "mysql/mysql.h"


#pragma comment(lib,"mysql/libmysql.lib")



class SQLInterface
{


public:
	SQLInterface()
	{
		this->res = nullptr;
		this->mysql = nullptr;
		cout << __FUNCTION__ << endl;
	}
	~SQLInterface()
	{
		cout << __FUNCTION__ << endl;
	}
	bool connect(string user, string pwd, string host, string db, int port)
	{
		mysql = new MYSQL;

		MYSQL_RES *result;
		MYSQL_ROW sql_row;
		int res;
		mysql_init(mysql);
		//	if (!mysql)return false;
		if (!mysql_real_connect(mysql, host.c_str(), user.c_str(), pwd.c_str(), db.c_str(), port, NULL, 0))
		{
			return false;
		}
		return true;
	}

	bool exec(string sql)
	{
		if (!mysql)return false;

		int res11 = mysql_query(this->mysql, sql.c_str());//查询
		if (!res11)
		{
			MYSQL_RES * result = mysql_store_result(mysql);
			if (result)
			{
				this->res = result;
			}
		}
		else
		{
			cout << "query sql failed!" << endl;
		}

		return true;
	}

	int pushParams(lua_State*l, int count)
	{
		if (!mysql)return 0;
		int ret = 0;

		MYSQL_ROW row;
		while (row = mysql_fetch_row(res))//获取具体的数据
		{
			int i = count;

			while (i)
			{
				char *str = row[--i];

				lua_pushstring(l, str);
				++ret;
				cout << str << " ";
			}
			cout << endl;  //  << "     " << sql_row[2] << endl;
		}


		cout << endl; cout << endl; cout << endl;
		return ret;
	}
	void release()
	{
		if (res)mysql_free_result(res);
		if (mysql)mysql_close(mysql);
	}
private:
	MYSQL *mysql  ;
	MYSQL_RES *res ;

};






int SQL_Script_Bridge(lua_State *l)
{
	int top = lua_gettop(l);

	const char * host = lua_tostring(l, top - 6);
	int port = lua_tonumber(l, top - 5);
	const char * user = lua_tostring(l, top - 4);
	const char * pswd = lua_tostring(l, top - 3);
	const char * table = lua_tostring(l, top - 2);
	const char *  sql = lua_tostring(l, top - 1);
	int count = lua_tonumber(l, top);

	cout << sql << count << endl;


	SQLInterface s;
	s.connect(user, pswd, host, table, port);
	s.exec(sql);
	int n = s.pushParams(l, count);

	return n;

}




 
int SQL_Script_create(lua_State*l)
{

	void * user = lua_newuserdata(l, sizeof SQLInterface);
	
	SQLInterface* ins = (SQLInterface*)user;
	ins->SQLInterface::SQLInterface();

	return 1;
}


 
int SQL_Script_connect(lua_State*l)
{

	int top = lua_gettop(l);

	void *  instance = lua_touserdata(l, top - 5);
	const char * host = lua_tostring(l, top - 4);
	int port = lua_tonumber(l, top - 3);
	const char * user = lua_tostring(l, top - 2);
	const char * pswd = lua_tostring(l, top - 1);
	const char * table = lua_tostring(l, top);


	SQLInterface* ins = (SQLInterface*)instance;
	bool ret = ins->connect(user, pswd, host, table, port);

	lua_pushboolean(l,ret);

	return 1;
}



int SQL_Script_execute(lua_State*l)
{

	int top = lua_gettop(l);

	void *  instance = lua_touserdata(l, top - 2);
	const char * sql = lua_tostring(l, top - 1);
	int  row_count = lua_tonumber(l, top);

	SQLInterface* ins = (SQLInterface*)instance;

	ins->exec(sql);
	int n = ins->pushParams(l, row_count);

	return n;
}

int SQL_Script_release(lua_State*l)
{

	int top = lua_gettop(l);

	void *  instance = lua_touserdata(l, top);

	SQLInterface* ins = (SQLInterface*)instance;

	
	
	return 0;
}

 

sql.lua,还可以添加一些参数 错误处理 来保证健壮性



local t={};

t.config=require("sql_config");
 

--exec the sql 
--return the res table which is one by one 
function t:exec(sql,row_count)
    local c=t.config;

    local t={ SQL_Script_Bridge(c.host , c.port , c.user , c.pwd , c.db , sql,row_count)};
 
 
    return t;

 
 
end


function t:create()
    return  SQL_Script_create();

end

function t:connect(ins)
    local c=t.config;

    local bool = SQL_Script_connect(ins,c.host , c.port , c.user , c.pwd , c.db);
 

end


function t:release(ins)
    SQL_Script_release(ins);
end

function t:execute(ins,sql,col_count)
  local t={ SQL_Script_execute(ins, sql,col_count)};
 
  return t;
end


return t;

 

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/747086

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值