<commit_before>#include "dbd_mysql.h"

int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query);

/*
 * connection,err = DBD.MySQl.New(dbname, user, password, host, port)
 */
static int connection_new(lua_State *L) {
    int n = lua_gettop(L);

    connection_t *conn = NULL;

    const char *host = NULL;
    const char *user = NULL;
    const char *password = NULL;
    const char *db = NULL;
    int port = 0;

    const char *unix_socket = NULL; /* TODO always NULL */
    int client_flag = 0; /* TODO always 0, set flags from options table */

    /* db, user, password, host, port */
    switch (n) {
    case 5:
	if (lua_isnil(L, 5) == 0) 
	    port = luaL_checkint(L, 5);
    case 4: 
	if (lua_isnil(L, 4) == 0) 
	    host = luaL_checkstring(L, 4);
    case 3:
	if (lua_isnil(L, 3) == 0) 
	    password = luaL_checkstring(L, 3);
    case 2:
	if (lua_isnil(L, 2) == 0) 
	    user = luaL_checkstring(L, 2);
    case 1:
	/*
	 * db is the only mandatory parameter
	 */
	db = luaL_checkstring(L, 1);
    }

    conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t));

    conn->mysql = mysql_init(NULL);

    if (!mysql_real_connect(conn->mysql, host, user, password, db, port, unix_socket, client_flag)) {
	lua_pushnil(L);
	lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, mysql_error(conn->mysql));
	return 2;
    }

    /*
     * by default turn off autocommit
     */
    mysql_autocommit(conn->mysql, 0);

    luaL_getmetatable(L, DBD_MYSQL_CONNECTION);
    lua_setmetatable(L, -2);

    return 1;
}

/*
 * success = connection:autocommit(on)
 */
static int connection_autocommit(lua_State *L) {
    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION);
    int on = lua_toboolean(L, 2); 
    int err = 0;

    if (conn->mysql) {
	err = mysql_autocommit(conn->mysql, on);
    }

    lua_pushboolean(L, !err);
    return 1;<commit_msg>mysql supports unix socket<commit_after>#include "dbd_mysql.h"

int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query);

/*
 * connection,err = DBD.MySQl.New(dbname, user, password, host, port)
 */
static int connection_new(lua_State *L) {
    int n = lua_gettop(L);

    connection_t *conn = NULL;

    const char *host = NULL;
    const char *user = NULL;
    const char *password = NULL;
    const char *db = NULL;
    int port = 0;

    const char *unix_socket = NULL; /* TODO always NULL */
    int client_flag = 0; /* TODO always 0, set flags from options table */

    /* db, user, password, host, port */
    switch (n) {
    case 5:
	if (lua_isnil(L, 5) == 0) 
	    port = luaL_checkint(L, 5);
    case 4: 
	if (lua_isnil(L, 4) == 0) 
        host = luaL_checkstring(L, 4);
        if (host[0] == '/') {
            unix_socket = host;
            host        = NULL;
        };
    case 3:
	if (lua_isnil(L, 3) == 0) 
	    password = luaL_checkstring(L, 3);
    case 2:
	if (lua_isnil(L, 2) == 0) 
	    user = luaL_checkstring(L, 2);
    case 1:
	/*
	 * db is the only mandatory parameter
	 */
	db = luaL_checkstring(L, 1);
    }

    conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t));

    conn->mysql = mysql_init(NULL);

    if (!mysql_real_connect(conn->mysql, host, user, password, db, port, unix_socket, client_flag)) {
	lua_pushnil(L);
	lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, mysql_error(conn->mysql));
	return 2;
    }

    /*
     * by default turn off autocommit
     */
    mysql_autocommit(conn->mysql, 0);

    luaL_getmetatable(L, DBD_MYSQL_CONNECTION);
    lua_setmetatable(L, -2);

    return 1;
        
}

/*
 * success = connection:autocommit(on)
 */
static int connection_autocommit(lua_State *L) {
    connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_MYSQL_CONNECTION);
    int on = lua_toboolean(L, 2); 
    int err = 0;

    if (conn->mysql) {
	err = mysql_autocommit(conn->mysql, on);
    }

    lua_pushboolean(L, !err);
    return 1;
{'fraction_non_alphanumeric': 0.1359026369168357, 'fraction_numerical': 0.014959432048681541, 'mean_word_length': 2.992914979757085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6473878', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1458, 'n_words': 366}