<commit_before>/*
 * Data types:
 * 	TINYINT		-- short
 * 	SMALLINT	-- short
 * 	YEAR		-- unsigned short
 * 	NULL		-- unsigned short
 * 	INTEGER		-- long
 * 	MEDIUMINT	-- long
 * 	TIMESTAMP	-- long
 * 	BIGINT		-- double
 * 	DECIMAL		-- double
 * 	FLOAT		-- double
 * 	DOUBLE		-- double
 * 	DATE		-- char*
 * 	TIME		-- char*
 * 	DATETIME	-- char*
 * 	STRING		-- char*
 * 	BLOB		-- char*
 * 	SET		-- char*
 * 	ENUM		-- char*
 */

/* Standard Libraries  */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* For return/parameter Types  */
#include <dbi/dbi.h>

/* Database Specific Libraries  */
#include <mysql/mysql.h>


/* No Custom Functions */
static const char *custom_function_list[] = {NULL};

/* Reserved Words */
static const char *reserved_word_list[] = { "ACTION", "ADD", "AGGREGATE", "ALL", "ALTER", "AFTER", "AND", "AS", "ASC", "AVG", "AVG_ROW_LENGTH", "AUTO_INCREMENT", "BETWEEN", "BIGINT", "BIT", "BINARY", "BLOB", "BOOL", "BOTH", "BY", "CASCADE", "CASE", "CHAR", "CHARACTER", "CHANGE", "CHECK", "CHECKSUM", "COLUMN", "COLUMNS", "COMMENT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATA", "DATABASE", "DATABASES", "DATE", "DATETIME", "DAY", "DAY_HOUR", "DAY_MINUTE", "DAY_SECOND", "DAYOFMONTH", "DAYOFWEEK", "DAYOFYEAR", "DEC", "DECIMAL", "DEFAULT", "DELAYED", "DELAY_KEY_WRITE", "DELETE", "DESC", "DESCRIBE", "DISTINCT", "DISTINCTROW", "DOUBLE", "DROP", "END", "ELSE", "ESCAPE", "ESCAPED", "ENCLOSED", "ENUM", "EXPLAIN", "EXISTS", "FIELDS", "FILE", "FIRST", "FLOAT", "FLOAT4", "FLOAT8", "FLUSH", "FOREIGN", "FROM", "FOR", "FULL", "FUNCTION", "GLOBAL", "GRANT", "GRANTS", "GROUP", "HAVING", "HEAP", "HIGH_PRIORITY", "HOUR", "HOUR_MINUTE", "HOUR_SECOND", "HOSTS", "IDENTIFIED", "IGNORE", "IN", "INDEX", "INFILE", "INNER", "INSERT", "INSERT_ID", "INT", "INTEGER", "INTERVAL", "INT1", "INT2", "INT3", "INT4", "INT8", "INTO", "IF", "IS", "ISAM", "JOIN", "KEY", "KEYS", "KILL", "LAST_INSERT_ID", "LEADING", "LEFT", "LENGTH", "LIKE", "LINES", "LIMIT", "LOAD", "LOCAL", "LOCK", "LOGS", "LONG", "LONGBLOB", "LONGTEXT", "LOW_PRIORITY", "MAX", "MAX_ROWS", "MATCH", "MEDIUMBLOB", "MEDIUMTEXT", "MEDIUMINT", "MIDDLEINT", "MIN_ROWS", "MINUTE", "MINUTE_SECOND", "MODIFY", "MONTH", "MONTHNAME", "MYISAM", "NATURAL", "NUMERIC", "NO", "NOT", "NULL", "ON", "OPTIMIZE", "OPTION", "OPTIONALLY", "OR", "ORDER", "OUTER", "OUTFILE", "PACK_KEYS", "PARTIAL", "PASSWORD", "PRECISION", "PRIMARY", "PROCEDURE", "PROCESS", "PROCESSLIST", "PRIVILEGES", "READ", "REAL", "REFERENCES", "RELOAD", "REGEXP", "RENAME", "REPLACE", "RESTRICT", "RETURNS", "REVOKE", "RLIKE", "ROW", "ROWS", "SECOND", "SELECT", "SET", "SHOW", "SHUTDOWN", "SMALLINT", "SONAME", "SQL_BIG_TABLES", "SQL_BIG_SELECTS", "SQL_LOW_PRIORITY_UPDATES", "SQL_LOG_OFF", "SQL_LOG_UPDATE", "SQL_SELECT_LIMIT", "SQL_SMALL_RESULT", "SQL_BIG_RESULT", "SQL_WARNINGS", "STRAIGHT_JOIN", "STARTING", "STATUS", "STRING", "TABLE", "TABLES", "TEMPORARY", "TERMINATED", "TEXT", "THEN", "TIME", "TIMESTAMP", "TINYBLOB", "TINYTEXT", "TINYINT", "TRAILING", "TO", "TYPE", "USE", "USING", "UNIQUE", "UNLOCK", "UNSIGNED", "UPDATE", "USAGE", "VALUES", "VARCHAR", "VARIABLES", "VARYING", "VARBINARY", "WITH", "WRITE", "WHEN", "WHERE", "YEAR", "YEAR_MONTH", "ZEROFILL", NULL };

/* Information About The Plugin */
/* plugin name
 * plugin description
 * plugin author
 * plugin URL
 * plugin version
 * compile date
 */
dbi_info_t plugin_info = {
        "mysql",
        "Wrapper for libmysql-client for use with MySQL servers",
        "<NAME> <<EMAIL>>",
        "http://libdbi.sourceforge.net/plugins/lookup.php?name=mysql",
        "mysql v0.0.1",
        __DATE__
};


/*****************************************************************************/
/* FUNCTION PROTOTYPES                                                       */
/*****************************************************************************/

 
/* Internal Functions *\
\**********************/

unsigned short _map_type(enum enum_field_types mytype);
unsigned short _map_type_attributes(enum enum_field_types mytype);

char *_strcpy_safe(char *str1, const char *str2);

dbi_row_t *_row_new( MYSQL_ROW *myrow, int nfields );

/* DBI Plugin Standard Functions *\
\*********************************/

int dbd_connect( dbi_driver_t *driver );

int dbd_disconnect( dbi_driver_t *driver );

dbi_result_t *dbd_efficient_query( dbi_driver_t *driver, char *statement );
int dbd_errno( dbi_driver_t *driver );
char *dbd_errstr( dbi_driver_t *driver );

int dbd_fetch_field( dbi_result_t *result, const char *key, void **dest );
int dbd_fetch_field_raw( dbi_result_t *result, const char *key, void **dest);
int dbd_fetch_row( dbi_result_t *result );
int dbd_free_query( dbi_result_t *result);
	
const char **dbd_get_custom_functions_list();
const dbi_info_t *dbd_get_info();
const char **dbd_get_reserved_words_list();
int dbd_goto_row(dbi_result_t *result, unsigned int row);

int dbd_initialize( dbi_plugin_t *plugin );

dbi_result_t *dbd_list_dbs( dbi_driver_t *driver );
dbi_result_t *dbd_list_tables( dbi_driver_t *driver );
	
/* Not sure if these two do what they're supposed to */
unsigned int dbd_num_rows( dbi_result_t *result );
unsigned int dbd_num_rows_affected( dbi_result_t *result );

dbi_result_t *dbd_query( dbi_driver_t *driver, char *statement );

int dbd_select_db( dbi_driver_t *driver, char *database);



/*****************************************************************************/
/* _MAP_TYPE                                                                 */
/*****************************************************************************/
/*
 * Precondition: none.
 * Postcondition: none.
 * Returns: The DBI type associated with MySQL type mytype.
 */

unsigned short _map_type( enum enum_field_types mytype)
{
	unsigned short type = 0;
	
	switch(mytype){
		case FIELD_TYPE_YEAR:
		case FIELD_TYPE_NULL:
		case FIELD_TYPE_TINY:
		case FIELD_TYPE_SHORT:
		case FIELD_TYPE_LONG:
		case FIELD_TYPE_INT24:
		case FIELD_TYPE_TIMESTAMP:
			type = DBI_TYPE_INTEGER;
		break;
		
		case FIELD_TYPE_FLOAT:
		case FIELD_TYPE_DOUBLE:
		case FIELD_TYPE_DECIMAL:
		case FIELD_TYPE_LONGLONG: /* Unforturnately, C can only see longlong's as doubles*/
			type = DBI_TYPE_DECIMAL;
		break;
		
		case FIELD_TYPE_DATE:
		case FIELD_TYPE_NEWDATE:
		case FIELD_TYPE_TIME:
		case FIELD_TYPE_DATETIME:
		case FIELD_TYPE_STRING:
		case FIELD_TYPE_VAR_STRING:
		case FIELD_TYPE_BLOB:
		case FIELD_TYPE_TINY_BLOB:
		case FIELD_TYPE_MEDIUM_BLOB:
		case FIELD_TYPE_LONG_BLOB:
		case FIELD_TYPE_SET:
		case FIELD_TYPE_ENUM:
			type = DBI_TYPE_STRING;
		break;

	}

	return type;
}

/*****************************************************************************/
/* _MAP_TYPE_ATTRIBUTES                                                      */
/*****************************************************************************/
/*
 * Precondition: none.
 * Postcondition: none.
 * Returns: The DBI type attributes associated with MySQL type mytype.
 */

unsigned short _map_type_attributes( enum enum_field_types mytype )
{
	unsigned short attb=0;

	switch(mytype){
		case FIELD_TYPE_YEAR:
		case FIELD_TYPE_NULL:
			attb |= DBI_INTEGER_UNSIGNED;
		case FIELD_TYPE_TINY:
		case FIELD_TYPE_SHORT:
			attb |= DBI_INTEGER_SIZE4;
		break;

		case FIELD_TYPE_LONG:
		case FIELD_TYPE_INT24:
		case FIELD_TYPE_TIMESTAMP:
			attb |= DBI_INTEGER_SIZE8;
		break;

		case FIELD_TYPE_FLOAT:
			attb |= DBI_DECIMAL_SIZE4;
		break;
		
		case FIELD_TYPE_DOUBLE:
		case FIELD_TYPE_DECIMAL:
		case FIELD_TYPE_LONGLONG: /* Unforturnately, C can only see longlong's as doubles*/
			attb |= DBI_DECIMAL_SIZE8;
		break;
		
		/* Add Decimal Sizes Later */
		default:
	}

	return attb;
}

/*****************************************************************************/
/* _STRCPY_SAFE                                                              */
/*****************************************************************************/
/*
 * Precondtion: str2 != NULL
 * Postcondition: str1 is freed if not null
 * Returns: A newly allocated copy of str2<commit_msg>Updated type LONGLONG.<commit_after>/*
 * Data types:
 * 	TINYINT		-- short
 * 	SMALLINT	-- short
 * 	YEAR		-- unsigned short
 * 	NULL		-- unsigned short
 * 	INTEGER		-- long
 * 	MEDIUMINT	-- long
 * 	TIMESTAMP	-- long
 * 	BIGINT		-- double
 * 	DECIMAL		-- double
 * 	FLOAT		-- double
 * 	DOUBLE		-- double
 * 	DATE		-- char*
 * 	TIME		-- char*
 * 	DATETIME	-- char*
 * 	STRING		-- char*
 * 	BLOB		-- char*
 * 	SET		-- char*
 * 	ENUM		-- char*
 */

/* Standard Libraries  */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* For return/parameter Types  */
#include <dbi/dbi.h>

/* Database Specific Libraries  */
#include <mysql/mysql.h>


/* No Custom Functions */
static const char *custom_function_list[] = {NULL};

/* Reserved Words */
static const char *reserved_word_list[] = { "ACTION", "ADD", "AGGREGATE", "ALL", "ALTER", "AFTER", "AND", "AS", "ASC", "AVG", "AVG_ROW_LENGTH", "AUTO_INCREMENT", "BETWEEN", "BIGINT", "BIT", "BINARY", "BLOB", "BOOL", "BOTH", "BY", "CASCADE", "CASE", "CHAR", "CHARACTER", "CHANGE", "CHECK", "CHECKSUM", "COLUMN", "COLUMNS", "COMMENT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATA", "DATABASE", "DATABASES", "DATE", "DATETIME", "DAY", "DAY_HOUR", "DAY_MINUTE", "DAY_SECOND", "DAYOFMONTH", "DAYOFWEEK", "DAYOFYEAR", "DEC", "DECIMAL", "DEFAULT", "DELAYED", "DELAY_KEY_WRITE", "DELETE", "DESC", "DESCRIBE", "DISTINCT", "DISTINCTROW", "DOUBLE", "DROP", "END", "ELSE", "ESCAPE", "ESCAPED", "ENCLOSED", "ENUM", "EXPLAIN", "EXISTS", "FIELDS", "FILE", "FIRST", "FLOAT", "FLOAT4", "FLOAT8", "FLUSH", "FOREIGN", "FROM", "FOR", "FULL", "FUNCTION", "GLOBAL", "GRANT", "GRANTS", "GROUP", "HAVING", "HEAP", "HIGH_PRIORITY", "HOUR", "HOUR_MINUTE", "HOUR_SECOND", "HOSTS", "IDENTIFIED", "IGNORE", "IN", "INDEX", "INFILE", "INNER", "INSERT", "INSERT_ID", "INT", "INTEGER", "INTERVAL", "INT1", "INT2", "INT3", "INT4", "INT8", "INTO", "IF", "IS", "ISAM", "JOIN", "KEY", "KEYS", "KILL", "LAST_INSERT_ID", "LEADING", "LEFT", "LENGTH", "LIKE", "LINES", "LIMIT", "LOAD", "LOCAL", "LOCK", "LOGS", "LONG", "LONGBLOB", "LONGTEXT", "LOW_PRIORITY", "MAX", "MAX_ROWS", "MATCH", "MEDIUMBLOB", "MEDIUMTEXT", "MEDIUMINT", "MIDDLEINT", "MIN_ROWS", "MINUTE", "MINUTE_SECOND", "MODIFY", "MONTH", "MONTHNAME", "MYISAM", "NATURAL", "NUMERIC", "NO", "NOT", "NULL", "ON", "OPTIMIZE", "OPTION", "OPTIONALLY", "OR", "ORDER", "OUTER", "OUTFILE", "PACK_KEYS", "PARTIAL", "PASSWORD", "PRECISION", "PRIMARY", "PROCEDURE", "PROCESS", "PROCESSLIST", "PRIVILEGES", "READ", "REAL", "REFERENCES", "RELOAD", "REGEXP", "RENAME", "REPLACE", "RESTRICT", "RETURNS", "REVOKE", "RLIKE", "ROW", "ROWS", "SECOND", "SELECT", "SET", "SHOW", "SHUTDOWN", "SMALLINT", "SONAME", "SQL_BIG_TABLES", "SQL_BIG_SELECTS", "SQL_LOW_PRIORITY_UPDATES", "SQL_LOG_OFF", "SQL_LOG_UPDATE", "SQL_SELECT_LIMIT", "SQL_SMALL_RESULT", "SQL_BIG_RESULT", "SQL_WARNINGS", "STRAIGHT_JOIN", "STARTING", "STATUS", "STRING", "TABLE", "TABLES", "TEMPORARY", "TERMINATED", "TEXT", "THEN", "TIME", "TIMESTAMP", "TINYBLOB", "TINYTEXT", "TINYINT", "TRAILING", "TO", "TYPE", "USE", "USING", "UNIQUE", "UNLOCK", "UNSIGNED", "UPDATE", "USAGE", "VALUES", "VARCHAR", "VARIABLES", "VARYING", "VARBINARY", "WITH", "WRITE", "WHEN", "WHERE", "YEAR", "YEAR_MONTH", "ZEROFILL", NULL };

/* Information About The Plugin */
/* plugin name
 * plugin description
 * plugin author
 * plugin URL
 * plugin version
 * compile date
 */
dbi_info_t plugin_info = {
        "mysql",
        "Wrapper for libmysql-client for use with MySQL servers",
        "<NAME> <<EMAIL>>",
        "http://libdbi.sourceforge.net/plugins/lookup.php?name=mysql",
        "mysql v0.0.1",
        __DATE__
};


/*****************************************************************************/
/* FUNCTION PROTOTYPES                                                       */
/*****************************************************************************/


/* Internal Functions *\
\**********************/

unsigned short _map_type(enum enum_field_types mytype);
unsigned short _map_type_attributes(enum enum_field_types mytype);

char *_strcpy_safe(char *str1, const char *str2);

dbi_row_t *_row_new( MYSQL_ROW *myrow, int nfields );

/* DBI Plugin Standard Functions *\
\*********************************/

int dbd_connect( dbi_driver_t *driver );

int dbd_disconnect( dbi_driver_t *driver );

dbi_result_t *dbd_efficient_query( dbi_driver_t *driver, char *statement );
int dbd_errno( dbi_driver_t *driver );
char *dbd_errstr( dbi_driver_t *driver );

int dbd_fetch_field( dbi_result_t *result, const char *key, void **dest );
int dbd_fetch_field_raw( dbi_result_t *result, const char *key, void **dest);
int dbd_fetch_row( dbi_result_t *result );
int dbd_free_query( dbi_result_t *result);
	
const char **dbd_get_custom_functions_list();
const dbi_info_t *dbd_get_info();
const char **dbd_get_reserved_words_list();
int dbd_goto_row(dbi_result_t *result, unsigned int row);

int dbd_initialize( dbi_plugin_t *plugin );

dbi_result_t *dbd_list_dbs( dbi_driver_t *driver );
dbi_result_t *dbd_list_tables( dbi_driver_t *driver );
	
/* Not sure if these two do what they're supposed to */
unsigned int dbd_num_rows( dbi_result_t *result );
unsigned int dbd_num_rows_affected( dbi_result_t *result );

dbi_result_t *dbd_query( dbi_driver_t *driver, char *statement );

int dbd_select_db( dbi_driver_t *driver, char *database);



/*****************************************************************************/
/* _MAP_TYPE                                                                 */
/*****************************************************************************/
/*
 * Precondition: none.
 * Postcondition: none.
 * Returns: The DBI type associated with MySQL type mytype.
 */

unsigned short _map_type( enum enum_field_types mytype)
{
	unsigned short type = 0;
	
	switch(mytype){
		case FIELD_TYPE_YEAR:
		case FIELD_TYPE_NULL:
		case FIELD_TYPE_TINY:
		case FIELD_TYPE_SHORT:
		case FIELD_TYPE_LONG:
		case FIELD_TYPE_INT24:
		case FIELD_TYPE_TIMESTAMP:
		case FIELD_TYPE_LONGLONG: 
			type = DBI_TYPE_INTEGER;
		break;
		
		case FIELD_TYPE_FLOAT:
		case FIELD_TYPE_DOUBLE:
		case FIELD_TYPE_DECIMAL:
			type = DBI_TYPE_DECIMAL;
		break;
		
		case FIELD_TYPE_DATE:
		case FIELD_TYPE_NEWDATE:
		case FIELD_TYPE_TIME:
		case FIELD_TYPE_DATETIME:
		case FIELD_TYPE_STRING:
		case FIELD_TYPE_VAR_STRING:
		case FIELD_TYPE_BLOB:
		case FIELD_TYPE_TINY_BLOB:
		case FIELD_TYPE_MEDIUM_BLOB:
		case FIELD_TYPE_LONG_BLOB:
		case FIELD_TYPE_SET:
		case FIELD_TYPE_ENUM:
			type = DBI_TYPE_STRING;
		break;

	}

	return type;
}

/*****************************************************************************/
/* _MAP_TYPE_ATTRIBUTES                                                      */
/*****************************************************************************/
/*
 * Precondition: none.
 * Postcondition: none.
 * Returns: The DBI type attributes associated with MySQL type mytype.
 */

unsigned short _map_type_attributes( enum enum_field_types mytype )
{
	unsigned short attb=0;

	switch(mytype){
		case FIELD_TYPE_YEAR:
		case FIELD_TYPE_NULL:
			attb |= DBI_INTEGER_UNSIGNED;
		case FIELD_TYPE_TINY:
		case FIELD_TYPE_SHORT:
			attb |= DBI_INTEGER_SIZE3;
		break;

		case FIELD_TYPE_LONG:
		case FIELD_TYPE_INT24:
		case FIELD_TYPE_TIMESTAMP:
			attb |= DBI_INTEGER_SIZE4;
		break;

		case FIELD_TYPE_LONGLONG:
			attb |= DBI_INTEGER_SIZE5;
		break;

		case FIELD_TYPE_FLOAT:
			attb |= DBI_DECIMAL_SIZE4;
		break;
		
		case FIELD_TYPE_DOUBLE:
		case FIELD_TYPE_DECIMAL:
			attb |= DBI_DECIMAL_SIZE8;
		break;
		
		/* Add Decimal Sizes Later */
		default:
	}

	return attb;
}

/*****************************************************************************/
/* _STRCPY_SAFE                                                              */
/*****************************************************************************/
/*
 * Precondtion: str2 != NULL
 * Postcondition: str1 is freed if not null
 * Returns: A newly allocated copy of str2
{'fraction_non_alphanumeric': 0.23023328149300157, 'fraction_numerical': 0.0031726283048211508, 'mean_word_length': 4.589707927677329, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1173712', 'n_tokens_mistral': 6006, 'n_tokens_neox': 5724, 'n_words': 1308}