<commit_before>/*
 * libdbi - database independent abstraction layer for C.
 * Copyright (C) 2001-2002, <NAME> and <NAME>.
 * http://libdbi.sourceforge.net
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * dbd_msql.c: mSQL database support 
 * Copyright (C) 2003, <NAME> <<EMAIL>>
 * http://libdbi-drivers.sourceforge.net
 * 
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#define _GNU_SOURCE /* we need asprintf */

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

#include <dbi/dbi.h>
#include <dbi/dbi-dev.h>
#include <dbi/dbd.h>


#include <msql.h>
#include "dbd_msql.h"


static const dbi_info_t driver_info = {
	"msql",
	"Mini SQL (mSQL) database support",
	"<NAME> <<EMAIL>>",
	"libdbi-drivers.sourceforge.net", 
	"dbd_msql v" VERSION,
	__DATE__
};

static const char *custom_functions[] = {NULL}; 
static const char *reserved_words[] = MSQL_RESERVED_WORDS;

void _translate_msql_type(int fieldtype, unsigned short *type, unsigned int *attribs);
void _get_field_info(dbi_result_t *result);
void _get_row_data(dbi_result_t *result, dbi_row_t *row, unsigned long long rowidx);


void dbd_register_driver(const dbi_info_t **_driver_info, const char ***_custom_functions, 
			 const char ***_reserved_words) 
{
        *_driver_info = &driver_info;
	*_custom_functions = custom_functions;
	*_reserved_words = reserved_words;
}

int dbd_initialize(dbi_driver_t *driver) 
{
        return 0;
}

int dbd_connect(dbi_conn_t *conn) 
{
        int connection;
	const char *host = dbi_conn_get_option(conn, "host");
	const char *dbname = dbi_conn_get_option(conn, "dbname");
	
	if(( connection = msqlConnect( (char *)host )) > 0){
		conn->connection = (void *)connection;
		if( dbname ) {
			msqlSelectDB(connection, (char *)dbname);
			conn->current_db = strdup(dbname);
		}
		return 0;
	}
	return -1;
}

int dbd_disconnect(dbi_conn_t *conn) 
{
        if (conn->connection) msqlClose((int)conn->connection);
	return 0;
}


int dbd_fetch_row(dbi_result_t *result, unsigned long long rownum) 
{
        dbi_row_t *row = NULL;
  
	if (result->result_state == NOTHING_RETURNED) return -1;
	
	if (result->result_state == ROWS_RETURNED) {
		row = _dbd_row_allocate(result->numfields); 
		_get_row_data(result, row, rownum);
		_dbd_row_finalize(result, row, rownum);
	}
	
	return 1; 
}


int dbd_free_query(dbi_result_t *result) 
{
        if (result->result_handle) msqlFreeResult((m_result *)result->result_handle);
	return 0;
}


int dbd_goto_row(dbi_result_t *result, unsigned long long row) 
{
        msqlDataSeek((m_result *)result->result_handle, row);
	return 1;
}


int dbd_get_socket(dbi_conn_t *conn)
{
        if(! conn->connection) return -1;
	return (int)conn->connection;
}


dbi_result_t *dbd_list_dbs(dbi_conn_t *conn, const char *pattern) 
{
	dbi_result_t *result = NULL;
	m_result *dbs;

	dbs = msqlListDBs((int)conn->connection);
  
	if( dbs != NULL ) {
		result = _dbd_result_create(conn, dbs, (dbs ? msqlNumRows(dbs) : 0), 0);
		_dbd_result_set_numfields(result, msqlNumFields(((m_result *)result->result_handle)));
		_get_field_info(result);
		
	}
	return result;
}

/* This function needs some work, to function like the other drivers */
dbi_result_t *dbd_list_tables(dbi_conn_t *conn, const char *db, const char *pattern) 
{
	dbi_result_t *result = NULL;
	m_result *tables;
	
	tables = msqlListTables((int)conn->connection);
	
	if( tables != NULL ) {
		result = _dbd_result_create(conn, (void *)tables, (tables ? msqlNumRows(tables) : 0), 0);
		_dbd_result_set_numfields(result, msqlNumFields(((m_result *)result->result_handle)));
		_get_field_info(result);
		
	}
	return result;
}


int dbd_quote_string(dbi_driver_t *driver, const char *orig, char *dest) 
{
	unsigned int len;
	strcpy(dest, "'");
	const char *escaped = "\'\"\\";<commit_msg>fixed swapped lines in dbd_quote_string<commit_after>/*
 * libdbi - database independent abstraction layer for C.
 * Copyright (C) 2001-2002, <NAME> and <NAME>.
 * http://libdbi.sourceforge.net
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * dbd_msql.c: mSQL database support 
 * Copyright (C) 2003, <NAME> <<EMAIL>>
 * http://libdbi-drivers.sourceforge.net
 * 
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#define _GNU_SOURCE /* we need asprintf */

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

#include <dbi/dbi.h>
#include <dbi/dbi-dev.h>
#include <dbi/dbd.h>


#include <msql.h>
#include "dbd_msql.h"


static const dbi_info_t driver_info = {
	"msql",
	"Mini SQL (mSQL) database support",
	"<NAME> <<EMAIL>>",
	"libdbi-drivers.sourceforge.net", 
	"dbd_msql v" VERSION,
	__DATE__
};

static const char *custom_functions[] = {NULL}; 
static const char *reserved_words[] = MSQL_RESERVED_WORDS;

void _translate_msql_type(int fieldtype, unsigned short *type, unsigned int *attribs);
void _get_field_info(dbi_result_t *result);
void _get_row_data(dbi_result_t *result, dbi_row_t *row, unsigned long long rowidx);


void dbd_register_driver(const dbi_info_t **_driver_info, const char ***_custom_functions, 
			 const char ***_reserved_words) 
{
        *_driver_info = &driver_info;
	*_custom_functions = custom_functions;
	*_reserved_words = reserved_words;
}

int dbd_initialize(dbi_driver_t *driver) 
{
        return 0;
}

int dbd_connect(dbi_conn_t *conn) 
{
        int connection;
	const char *host = dbi_conn_get_option(conn, "host");
	const char *dbname = dbi_conn_get_option(conn, "dbname");
	
	if(( connection = msqlConnect( (char *)host )) > 0){
		conn->connection = (void *)connection;
		if( dbname ) {
			msqlSelectDB(connection, (char *)dbname);
			conn->current_db = strdup(dbname);
		}
		return 0;
	}
	return -1;
}

int dbd_disconnect(dbi_conn_t *conn) 
{
        if (conn->connection) msqlClose((int)conn->connection);
	return 0;
}


int dbd_fetch_row(dbi_result_t *result, unsigned long long rownum) 
{
        dbi_row_t *row = NULL;
  
	if (result->result_state == NOTHING_RETURNED) return -1;
	
	if (result->result_state == ROWS_RETURNED) {
		row = _dbd_row_allocate(result->numfields); 
		_get_row_data(result, row, rownum);
		_dbd_row_finalize(result, row, rownum);
	}
	
	return 1; 
}


int dbd_free_query(dbi_result_t *result) 
{
        if (result->result_handle) msqlFreeResult((m_result *)result->result_handle);
	return 0;
}


int dbd_goto_row(dbi_result_t *result, unsigned long long row) 
{
        msqlDataSeek((m_result *)result->result_handle, row);
	return 1;
}


int dbd_get_socket(dbi_conn_t *conn)
{
        if(! conn->connection) return -1;
	return (int)conn->connection;
}


dbi_result_t *dbd_list_dbs(dbi_conn_t *conn, const char *pattern) 
{
	dbi_result_t *result = NULL;
	m_result *dbs;

	dbs = msqlListDBs((int)conn->connection);
  
	if( dbs != NULL ) {
		result = _dbd_result_create(conn, dbs, (dbs ? msqlNumRows(dbs) : 0), 0);
		_dbd_result_set_numfields(result, msqlNumFields(((m_result *)result->result_handle)));
		_get_field_info(result);
		
	}
	return result;
}

/* This function needs some work, to function like the other drivers */
dbi_result_t *dbd_list_tables(dbi_conn_t *conn, const char *db, const char *pattern) 
{
	dbi_result_t *result = NULL;
	m_result *tables;
	
	tables = msqlListTables((int)conn->connection);
	
	if( tables != NULL ) {
		result = _dbd_result_create(conn, (void *)tables, (tables ? msqlNumRows(tables) : 0), 0);
		_dbd_result_set_numfields(result, msqlNumFields(((m_result *)result->result_handle)));
		_get_field_info(result);
		
	}
	return result;
}


int dbd_quote_string(dbi_driver_t *driver, const char *orig, char *dest) 
{
	unsigned int len;
	const char *escaped = "\'\"\\";
	strcpy(dest, "'");
{'fraction_non_alphanumeric': 0.12085466280881371, 'fraction_numerical': 0.009347874471399956, 'mean_word_length': 4.45658773527626, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 69, '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': '6682830', 'n_tokens_mistral': 3425, 'n_tokens_neox': 3228, 'n_words': 854}