针对luasql库不支持执行存储过程,需要针对C的代码进行修改
文件名: ls_mysql.c
/*
** Connects to a data source.
** param: one string for each connection parameter, said
** datasource, username, password, host and port.
*/
static int env_connect (lua_State *L) {
const char *sourcename = luaL_checkstring(L, 2);
const char *username = luaL_optstring(L, 3, NULL);
const char *password = luaL_optstring(L, 4, NULL);
const char *host = luaL_optstring(L, 5, NULL);
const int port = luaL_optint(L, 6, 0);
MYSQL *conn;
/* ==================== begin =================
** xxxxx
** 添加连接数据库的flag参数
** 用于扩充调用mysql的存储过程.
** 参数可选
*/
size_t client_flag = 0 ;
if(lua_gettop(L) == 7)
{
client_flag = (size_t)luaL_checknumber(L, 7);
}
getenvironment(L); /* validade environment */
/* Try to init the connection object. */
conn = mysql_init(NULL);
if (conn == NULL)
return luasql_faildirect(L, "error connecting: Out of memory.");
if (!mysql_real_connect(conn, host, username, password,
sourcename, port, NULL, client_flag))
{
char error_msg[100];
strncpy (error_msg, mysql_error(conn), 99);
mysql_close (conn); /* Close conn if connect failed */
return luasql_failmsg (L, "error connecting to database. MySQL: ", error_msg);
}
return create_connection(L, 1, conn);
}
默认情况下client_flag直接固定为0, 需要把参数开放出来给lua层,可选参数,默认为0
针对需要执行存储过程,则需要添加 CLIENT_MULTI_STATEMENTS 参数.
针对C的定义如下:
mysql/mysql_com.h:#define CLIENT_MULTI_STATEMENTS (1UL << 16) /* Enable/disable multi-stmt support */
http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html
Flag Name | Flag Description |
CLIENT_COMPRESS |
Use compression protocol.(使用压缩协议。) |