mysql서버를 이용하여 통계서버를 구축한다던가 램DB를 비스무레한 흉내를 내고 싶다면 유용할 것 같습니다. 공유메모리에 소팅된 데이터를 빠른 속도로 입력할 수 있습니다.
아래 소스는 데이터를 파일로 생성한 뒤, db에 넣는 작업을 하지만,
좀만 연구하시면 공유메모리에 있는 내용을 mysql 서버로 바로 입력이 가능합니다.
"MYSQL" 잘 만들었군요~!
#define HOSTNAME "localhost"
#define DBUSER "jhko"
#define DBPASS "jhko"
#define DBNAME "test"
#define IMPORT_VERSION "3.5"
#include "client_priv.h"
#include "mysql_version.h"
static void db_error_with_table(MYSQL *mysql, char *table);
static void db_error(MYSQL *mysql);
static char *field_escape(char *to,const char *from,uint length);
static char *add_load_option(char *ptr,const char *object, const char *statement);
static my_bool lock_tables=0,ignore_errors=0,opt_delete=0;
static my_bool replace=0,silent=0,ignore=0,opt_compress=0, opt_low_priority= 0, tty_password= 0;
static uint opt_local_file=0;
static MYSQL mysql_connection;
static char *opt_password=0, *current_user=0,
*current_host=0, *current_db=0, *fields_terminated=0,
*lines_terminated=0, *enclosed=0, *opt_enclosed=0,
*escaped=0, *opt_columns=0,
*default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
static uint opt_mysql_port= 3306, opt_protocol= 0;
static my_string opt_mysql_unix_port=0;
static longlong opt_ignore_lines= -1;
static CHARSET_INFO *charset_info= &my_charset_latin1;
#include <sslopt-vars.h>
#ifdef HAVE_SMEM
static char *shared_memory_base_name=0;
#endif
static int get_options()
{
return(0);
}
static int write_to_table(char *tablename, const char *hard_path, MYSQL *sock)
{
char sql_statement[FN_REFLEN*16+256], *end;
if (opt_delete)
{
sprintf(sql_statement, "DELETE FROM %s", tablename);
if (mysql_query(sock, sql_statement))
{
db_error_with_table(sock, tablename);
return 1;
}
}
sprintf(sql_statement, "LOAD DATA %s %s INFILE '%s'", \
opt_low_priority ? "LOW_PRIORITY" : "", \
opt_local_file ? "LOCAL" : "", hard_path);
end= strend(sql_statement);
if (replace) end= strmov(end, " REPLACE");
if (ignore) end= strmov(end, " IGNORE");
end= strmov(strmov(end, " INTO TABLE "), tablename);
if (fields_terminated || enclosed || opt_enclosed || escaped) end= strmov(end, " FIELDS");
end= add_load_option(end, fields_terminated, " TERMINATED BY");
end= add_load_option(end, enclosed, " ENCLOSED BY");
end= add_load_option(end, opt_enclosed, " OPTIONALLY ENCLOSED BY");
end= add_load_option(end, escaped, " ESCAPED BY");
end= add_load_option(end, lines_terminated, " LINES TERMINATED BY");
if (opt_ignore_lines >= 0)
end= strmov(longlong10_to_str(opt_ignore_lines, strmov(end, " IGNORE "),10), " LINES");
if (opt_columns) end= strmov(strmov(strmov(end, " ("), opt_columns), ")");
*end= '\0';
if (mysql_query(sock, sql_statement))
{
db_error_with_table(sock, tablename);
return 1;
}
if (!silent)
{
if (mysql_info(sock)) /* If NULL-pointer, print nothing */
{
fprintf(stdout, "%s.%s: %s\n", current_db, tablename, mysql_info(sock));
}
}
return 0;
}
static void lock_table(MYSQL *sock, int tablecount, char **raw_tablename)
{
DYNAMIC_STRING query;
int i;
char tablename[FN_REFLEN];
init_dynamic_string(&query, "LOCK TABLES ", 256, 1024);
for (i=0 ; i < tablecount ; i++)
{
fn_format(tablename, raw_tablename[i], "", "", 1 | 2);
dynstr_append(&query, tablename);
dynstr_append(&query, " WRITE,");
}
if (mysql_real_query(sock, query.str, query.length-1)) db_error(sock);
}
static MYSQL *db_connect(char *host, char *database, char *user, char *passwd)
{
MYSQL *sock;
mysql_init(&mysql_connection);
if (opt_compress) mysql_options(&mysql_connection,MYSQL_OPT_COMPRESS,NullS);
if (opt_local_file) mysql_options(&mysql_connection,MYSQL_OPT_LOCAL_INFILE, (char*) &opt_local_file);
#ifdef HAVE_OPENSSL
if (opt_use_ssl)
mysql_ssl_set(&mysql_connection, opt_ssl_key, opt_ssl_cert, opt_ssl_ca, opt_ssl_capath, opt_ssl_cipher);
#endif
if (opt_protocol)
mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
#ifdef HAVE_SMEM
if (shared_memory_base_name)
mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
#endif
if (!(sock= mysql_real_connect(&mysql_connection,host,user,passwd,
database,opt_mysql_port,opt_mysql_unix_port, 0)))
{
ignore_errors=0; /* NO RETURN FROM db_error */
db_error(&mysql_connection);
}
mysql_connection.reconnect= 0;
if (mysql_select_db(sock, database))
{
ignore_errors=0;
db_error(&mysql_connection);
}
return sock;
}
static void db_disconnect(char *host, MYSQL *sock)
{
mysql_close(sock);
}
static void safe_exit(int error, MYSQL *sock)
{
if (ignore_errors) return;
if (sock) mysql_close(sock);
exit(error);
}
static void db_error_with_table(MYSQL *mysql, char *table)
{
my_printf_error(0,"Error: %s, when using table: %s",
MYF(0), mysql_error(mysql), table);
safe_exit(1, mysql);
}
static void db_error(MYSQL *mysql)
{
my_printf_error(0,"Error: %s", MYF(0), mysql_error(mysql));
safe_exit(1, mysql);
}
static char *add_load_option(char *ptr, const char *object, const char *statement)
{
if (object)
{
if (object[0] == '0' && (object[1] == 'x' || object[1] == 'X'))
{
ptr= strxmov(ptr," ",statement," ",object,NullS);
}
else
{
ptr= strxmov(ptr," ",statement," '",NullS);
ptr= field_escape(ptr,object,(uint) strlen(object));
*ptr++= '\'';
}
}
return ptr;
}
/*
** Allow the user to specify field terminator strings like:
** "'", "\", "\\" (escaped backslash), "\t" (tab), "\n" (newline)
** This is done by doubleing ' and add a end -\ if needed to avoid
** syntax errors from the SQL parser.
*/
static char *field_escape(char *to,const char *from,uint length)
{
const char *end;
uint end_backslashes=0;
for (end= from+length; from != end; from++)
{
*to++= *from;
if (*from == '\\')
end_backslashes^=1; /* find odd number of backslashes */
else
{
if (*from == '\'' && !end_backslashes)
*to++= *from; /* We want a dublicate of "'" for MySQL */
end_backslashes=0;
}
}
/* Add missing backslashes if user has specified odd number of backs.*/
if (end_backslashes)
*to++= '\\';
return to;
}
int rdb_insert(char *tablename, const char *filename)
{
int exitcode=0, error=0;
MYSQL *sock=0;
my_init();
/**************************
* mysql 접속정보
**************************/
current_host = HOSTNAME;
current_db = DBNAME;
current_user = DBUSER;
opt_password= DBPASS;
/******************************************************
* 필드 구분, 라인 구분을 default로 사용하지 않을 때,
* get_options 함수를 수정해서 사용.
******************************************************/
if (get_options())
{
return(1);
}
if (!(sock= db_connect(current_host, current_db, current_user, opt_password)))
{
return(1); /* purecov: deadcode */
}
if (mysql_query(sock, "set @@character_set_database=binary;"))
{
db_error(sock); /* We shall countinue here, if --force was given */
return(1);
}
if (lock_tables) lock_table(sock, 1, &tablename);
if ((error=write_to_table(tablename, filename, sock)))
if (exitcode == 0) exitcode = error;
db_disconnect(current_host, sock);
my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
#ifdef HAVE_SMEM
my_free(shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
#endif
my_end(0);
return(exitcode);
}
int main()
{
const char *filename = "/tmp/jhko.txt" ;
char tablename[] = "jhko" ;
rdb_insert(tablename, filename);
}
client_priv.h

