<commit_before>#include <stdlib.h>
#include <sys/select.h>

#include "luapgsql.h"

/** private helper functions **/

/* server NOTICE message handler */
static void pg_notice(void *arg, const char *message) {
#ifdef DEBUG
	fprintf(stderr, "debug.notice [%s]\n", message);
#endif
}

/* check and return pointer */
static void *luaL_checkpointer(lua_State* L, int i) {
	luaL_checktype(L, i, LUA_TLIGHTUSERDATA);
	return lua_touserdata(L, i);
}

/* check and return connection object parameter */
static con_t *luaL_checkconn(lua_State* L, int i) {
	luaL_checkudata(L, i, TYPE_CONNECTION);
   	return lua_toconn(L, i);
}

/* check and return result object parameter */
static rs_t *luaL_checkresult(lua_State* L, int i) {
	luaL_checkudata(L, i, TYPE_RESULT);
   	return lua_toresult(L, i);
}

/* push a connection object on the stack */
static void lua_pushconn(lua_State *L, PGconn *con) {
	con_t *p = lua_newconn(L);
	luaL_getmetatable(L, TYPE_CONNECTION);
	lua_setmetatable(L, -2);
	p->ptr = con;
	p->open = 1;
#ifdef DEBUG
	fprintf(stderr, "debug.lua_pushconn ptr [%p]\n", (void *)p->ptr);
	fprintf(stderr, "debug.lua_pushconn open [%d]\n", p->open);
#endif
}

/* push a result object on the stack */
static void lua_pushresult(lua_State *L, PGresult *rs) {
	rs_t *p = lua_newresult(L);
	luaL_getmetatable(L, TYPE_RESULT);
	lua_setmetatable(L, -2);
	p->ptr = rs;
	p->open = 1;
	p->row = 0;
#ifdef DEBUG
	fprintf(stderr, "debug.lua_pushresult ptr [%p]\n", (void *)p->ptr);
	fprintf(stderr, "debug.lua_pushresult open [%d]\n", p->open);
	fprintf(stderr, "debug.lua_pushresult row [%d]\n", p->row);
#endif
}

/* get one value from PGresult and push it onto the Lua stack */
static void lua_pushpgdata(lua_State *L, PGresult *rs, int row, int col) {
	const char *val;
	double temp;
	/* grab the value */
	if (PQgetisnull(rs, row, col)) {
		/* tasty NULLs - take that PHP! */
		lua_pushnil(L);
	} else {
		val = PQgetvalue(rs, row, col);
		switch (PQftype(rs, col)) {
		case BOOLOID:
			/* map postgresql default bool format */
			if (val[0] == 't') {
				lua_pushboolean(L, 1);
			} else {
				lua_pushboolean(L, 0);
			}
			break;
		case INT2OID:
		case INT4OID:
		case INT8OID:
		case FLOAT4OID:
		case FLOAT8OID:
		case NUMERICOID:
			/* convert using Lua string -> number conversion for reliability */
			lua_pushstring(L, val);
			temp = lua_tonumber(L, -1);
			lua_pop(L, 1);
			lua_pushnumber(L, temp);
			break;
		default:
			/* it's all just a string after that */
			lua_pushstring(L, val);
			break;
		}
	}
}

/* push a table onto the stack containing a row of data */
static void lua_pushpgrow(lua_State *L, PGresult *rs, int row) {
	int col;
	int cols = PQnfields(rs);
	lua_createtable(L, cols, cols);
	for (col = 0; col < cols; col++) {
		/* grab the data */
		lua_pushpgdata(L, rs, row, col);
		/* give us an indexed ... */
		lua_pushvalue(L, -1);
		lua_rawseti(L, -3, col + 1);
		/* ... and assoc array */
		lua_pushstring(L, PQfname(rs, col));
		lua_pushvalue(L, -2);
		lua_settable(L, -4);
		lua_pop(L, 1);
	}
}<commit_msg>cleaned up comments<commit_after>#include <stdlib.h>
#include <sys/select.h>

#include "luapgsql.h"

/** private helper functions **/

/* server NOTICE message handler */
static void pg_notice(void *arg, const char *message) {
#ifdef DEBUG
	fprintf(stderr, "debug.notice [%s]\n", message);
#endif
}

/* check and return pointer */
static void *luaL_checkpointer(lua_State* L, int i) {
	luaL_checktype(L, i, LUA_TLIGHTUSERDATA);
	return lua_touserdata(L, i);
}

/* check and return connection object parameter */
static con_t *luaL_checkconn(lua_State* L, int i) {
	luaL_checkudata(L, i, TYPE_CONNECTION);
   	return lua_toconn(L, i);
}

/* check and return result object parameter */
static rs_t *luaL_checkresult(lua_State* L, int i) {
	luaL_checkudata(L, i, TYPE_RESULT);
   	return lua_toresult(L, i);
}

/* push a connection object on the stack */
static void lua_pushconn(lua_State *L, PGconn *con) {
	con_t *p = lua_newconn(L);
	luaL_getmetatable(L, TYPE_CONNECTION);
	lua_setmetatable(L, -2);
	p->ptr = con;
	p->open = 1;
#ifdef DEBUG
	fprintf(stderr, "debug.lua_pushconn ptr [%p]\n", (void *)p->ptr);
	fprintf(stderr, "debug.lua_pushconn open [%d]\n", p->open);
#endif
}

/* push a result object on the stack */
static void lua_pushresult(lua_State *L, PGresult *rs) {
	rs_t *p = lua_newresult(L);
	luaL_getmetatable(L, TYPE_RESULT);
	lua_setmetatable(L, -2);
	p->ptr = rs;
	p->open = 1;
	p->row = 0;
#ifdef DEBUG
	fprintf(stderr, "debug.lua_pushresult ptr [%p]\n", (void *)p->ptr);
	fprintf(stderr, "debug.lua_pushresult open [%d]\n", p->open);
	fprintf(stderr, "debug.lua_pushresult row [%d]\n", p->row);
#endif
}

/* get one value from PGresult and push it onto the Lua stack */
static void lua_pushpgdata(lua_State *L, PGresult *rs, int row, int col) {
	const char *val;
	double temp;
	/* grab the value */
	if (PQgetisnull(rs, row, col)) {
		/* tasty NULLs - take that PHP! */
		lua_pushnil(L);
	} else {
		val = PQgetvalue(rs, row, col);
		switch (PQftype(rs, col)) {
		case BOOLOID:
			/* map postgresql default bool format */
			if (val[0] == 't') {
				lua_pushboolean(L, 1);
			} else {
				lua_pushboolean(L, 0);
			}
			break;
		case INT2OID:
		case INT4OID:
		case INT8OID:
		case FLOAT4OID:
		case FLOAT8OID:
		case NUMERICOID:
			/* convert using Lua string -> number conversion for reliability */
			lua_pushstring(L, val);
			temp = lua_tonumber(L, -1);
			lua_pop(L, 1);
			lua_pushnumber(L, temp);
			break;
		default:
			/* it's all just a string after that */
			lua_pushstring(L, val);
			break;
		}
	}
}

/* push a table onto the stack containing a row of data from PGresult */
static void lua_pushpgrow(lua_State *L, PGresult *rs, int row) {
	int col;
	int cols = PQnfields(rs);
	lua_createtable(L, cols, cols);
	for (col = 0; col < cols; col++) {
		/* grab the data */
		lua_pushpgdata(L, rs, row, col);
		/* give us an indexed ... */
		lua_pushvalue(L, -1);
		lua_rawseti(L, -3, col + 1);
		/* ... and assoc array */
		lua_pushstring(L, PQfname(rs, col));
		lua_pushvalue(L, -2);
		lua_settable(L, -4);
		lua_pop(L, 1);
	}
}
{'fraction_non_alphanumeric': 0.15288713910761154, 'fraction_numerical': 0.007217847769028871, 'mean_word_length': 3.8273950910530483, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, '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': '2909730', 'n_tokens_mistral': 2567, 'n_tokens_neox': 2391, 'n_words': 662}