<commit_before>#include <v8.h>
#include "js_common.h"

#ifdef windows
#	include <my_global.h>
#endif

#include <mysql.h>
#include <stdlib.h>
#include <string.h>

v8::Persistent<v8::FunctionTemplate> rest;

JS_METHOD(_mysql) {
	v8::HandleScope handle_scope;
	if (args.This()->InternalFieldCount() == 0) {
		return v8::ThrowException(JS_STR("Invalid call format. Use 'new MySQL()'"));
	}
	SAVE_VALUE(0, JS_BOOL(false));
	
	return args.This();
}

JS_METHOD(_connect) {
	v8::HandleScope handle_scope;
	if (args.Length() < 4) {
		return v8::ThrowException(JS_STR("Invalid call format. Use 'mysql.connect(host, user, pass, db)'"));
	}

	MYSQL *conn;
	
	v8::String::Utf8Value host(args[0]);
	v8::String::Utf8Value user(args[1]);
	v8::String::Utf8Value pass(args[2]);
	v8::String::Utf8Value db(args[3]);

	conn = mysql_init(NULL);

	if (!mysql_real_connect(conn, *host, *user, *pass, *db, 0, NULL, 0)) {
		return JS_BOOL(false);
	} else {
		mysql_query(conn, "SET NAMES 'utf8'");				 
		SAVE_PTR(0, conn);
		return args.This();
	}	
}

JS_METHOD(_query) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}		
	if (args.Length() < 1) {
		return v8::ThrowException(JS_STR("No query specified"));
	}
	v8::String::Utf8Value q(args[0]);
	
	MYSQL * conn = LOAD_PTR(0, MYSQL *);
	MYSQL_RES *res;
	int code = mysql_real_query(conn, *q, q.length());
	if (code != 0) { return JS_BOOL(false); }
	
	res = mysql_store_result(conn);
	
	if (res) {
		v8::Handle<v8::Value> resargs[] = { v8::External::New((void *) res) };
		return rest->GetFunction()->NewInstance(1, resargs);
	} else {
		if (mysql_field_count(conn)) {
			return JS_BOOL(false);
		} else {
			return JS_BOOL(true);
		}
	}
}

JS_METHOD(_error) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}

	MYSQL * conn = LOAD_PTR(0, MYSQL *);

	return JS_STR(mysql_error(conn));
}

JS_METHOD(_errno) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}
	
	MYSQL * conn = LOAD_PTR(0, MYSQL *);

	return JS_INT(mysql_errno(conn));
}

JS_METHOD(_affectedrows) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}
		
	MYSQL * conn = LOAD_PTR(0, MYSQL *)

	return JS_INT(mysql_affected_rows(conn));
}

JS_METHOD(_insertid) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}

	MYSQL * conn = LOAD_PTR(0, MYSQL *);

	return JS_INT(mysql_insert_id(conn));
}

JS_METHOD(_escape) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}
	if (args.Length() < 1) {
		return v8::ThrowException(JS_STR("Nothing to escape"));
	}
	v8::String::Utf8Value str(args[0]);
	
	int len = args[0]->ToString()->Utf8Length();
	char * result = (char *) malloc((2*len+1) * sizeof(char));
	
	MYSQL * conn = LOAD_PTR(0, MYSQL *);

	int length = mysql_real_escape_string(conn, result, *str, len);

	return JS_STR(result, length);
}

JS_METHOD(_qualify) {
	v8::HandleScope handle_scope;
	if (args.Length() < 1) {
		return v8::ThrowException(JS_STR("Nothing to qualify"));
	}

	int len = args[0]->ToString()->Utf8Length();
	v8::String::Utf8Value str(args[0]);
	
	char * result = (char *) malloc((len+3) * sizeof(char));
	strcpy(result+1, *str);
	result[0] = '`';
	result[len+1] = '`';
	result[len+2] = '\0';
	
	return JS_STR(result);
}

JS_METHOD(_result) {
	v8::HandleScope handle_scope;
	SAVE_VALUE(0, args[0]);
	return args.This();<commit_msg>strcpy -> strncpy<commit_after>#include <v8.h>
#include "js_common.h"

#ifdef windows
#	include <my_global.h>
#endif

#include <mysql.h>
#include <stdlib.h>
#include <string.h>

v8::Persistent<v8::FunctionTemplate> rest;

JS_METHOD(_mysql) {
	v8::HandleScope handle_scope;
	if (args.This()->InternalFieldCount() == 0) {
		return v8::ThrowException(JS_STR("Invalid call format. Use 'new MySQL()'"));
	}
	SAVE_VALUE(0, JS_BOOL(false));
	
	return args.This();
}

JS_METHOD(_connect) {
	v8::HandleScope handle_scope;
	if (args.Length() < 4) {
		return v8::ThrowException(JS_STR("Invalid call format. Use 'mysql.connect(host, user, pass, db)'"));
	}

	MYSQL *conn;
	
	v8::String::Utf8Value host(args[0]);
	v8::String::Utf8Value user(args[1]);
	v8::String::Utf8Value pass(args[2]);
	v8::String::Utf8Value db(args[3]);

	conn = mysql_init(NULL);

	if (!mysql_real_connect(conn, *host, *user, *pass, *db, 0, NULL, 0)) {
		return JS_BOOL(false);
	} else {
		mysql_query(conn, "SET NAMES 'utf8'");				 
		SAVE_PTR(0, conn);
		return args.This();
	}	
}

JS_METHOD(_query) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}		
	if (args.Length() < 1) {
		return v8::ThrowException(JS_STR("No query specified"));
	}
	v8::String::Utf8Value q(args[0]);
	
	MYSQL * conn = LOAD_PTR(0, MYSQL *);
	MYSQL_RES *res;
	int code = mysql_real_query(conn, *q, q.length());
	if (code != 0) { return JS_BOOL(false); }
	
	res = mysql_store_result(conn);
	
	if (res) {
		v8::Handle<v8::Value> resargs[] = { v8::External::New((void *) res) };
		return rest->GetFunction()->NewInstance(1, resargs);
	} else {
		if (mysql_field_count(conn)) {
			return JS_BOOL(false);
		} else {
			return JS_BOOL(true);
		}
	}
}

JS_METHOD(_error) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}

	MYSQL * conn = LOAD_PTR(0, MYSQL *);

	return JS_STR(mysql_error(conn));
}

JS_METHOD(_errno) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}
	
	MYSQL * conn = LOAD_PTR(0, MYSQL *);

	return JS_INT(mysql_errno(conn));
}

JS_METHOD(_affectedrows) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}
		
	MYSQL * conn = LOAD_PTR(0, MYSQL *)

	return JS_INT(mysql_affected_rows(conn));
}

JS_METHOD(_insertid) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}

	MYSQL * conn = LOAD_PTR(0, MYSQL *);

	return JS_INT(mysql_insert_id(conn));
}

JS_METHOD(_escape) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::Value> c = LOAD_VALUE(0);
	if (c->IsFalse()) {
		return v8::ThrowException(JS_STR("No connection established"));
	}
	if (args.Length() < 1) {
		return v8::ThrowException(JS_STR("Nothing to escape"));
	}
	v8::String::Utf8Value str(args[0]);
	
	int len = args[0]->ToString()->Utf8Length();
	char * result = (char *) malloc((2*len+1) * sizeof(char));
	
	MYSQL * conn = LOAD_PTR(0, MYSQL *);

	int length = mysql_real_escape_string(conn, result, *str, len);

	return JS_STR(result, length);
}

JS_METHOD(_qualify) {
	v8::HandleScope handle_scope;
	if (args.Length() < 1) {
		return v8::ThrowException(JS_STR("Nothing to qualify"));
	}

	int len = args[0]->ToString()->Utf8Length();
	v8::String::Utf8Value str(args[0]);
	
	char * result = (char *) malloc((len+3) * sizeof(char));
	strncpy(result+1, *str, len);
	result[0] = '`';
	result[len+1] = '`';
	result[len+2] = '\0';
	
	return JS_STR(result);
}

JS_METHOD(_result) {
	v8::HandleScope handle_scope;
	SAVE_VALUE(0, args[0]);
	return args.This();
{'fraction_non_alphanumeric': 0.18732466207600101, 'fraction_numerical': 0.024993624075490946, 'mean_word_length': 5.402448979591837, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 56, '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': '865710', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3118, 'n_words': 573}