Archived
1
0
Fork 0

arch/mysql.c: fixed the test and a overflow in init(). mysql needs alot more love.

This commit is contained in:
H Hautakoski 2010-08-26 21:37:47 +02:00 committed by Henrik Hautakoski
parent bc7492cb1d
commit 8cafb25c42
3 changed files with 63 additions and 74 deletions

View file

@ -23,7 +23,61 @@ static MYSQL* dbconn;
static unsigned long dbthread_id;
static char *dbtable = NULL;
int db_setup();
/*
* Database setup
*/
static int db_setup() {
int ret;
char stmt_create[] = "CREATE TABLE IF NOT EXISTS `%s` ("
"`Path` varchar(512) default NULL, "
"`Base` varchar(512) default NULL, "
"`Type` tinyint(1) default NULL, "
"`Status` tinyint(1) default NULL, "
"`Date` datetime default NULL) "
"ENGINE=MyISAM DEFAULT CHARSET=utf8";
char stmt_trunc[] = "TRUNCATE TABLE `%s`";
/* Allocate memory big enough for querys */
char *stmt = (char *) malloc((sizeof(char) * strlen(dbtable)) + strlen(stmt_create) - 1);
/* Create mysql query */
if (stmt == NULL || sprintf(stmt, stmt_create, dbtable) < 0) {
fprintf(stderr, "Error, create database sql\n");
return -1;
}
/* Run mysql query */
ret = mysql_query(dbconn, stmt);
/* Make sure query was successfull */
if (ret != 0) {
fprintf(stderr, "%s\n", mysql_error(dbconn));
goto clear;
}
/* Create mysql query */
if (sprintf(stmt, stmt_trunc, dbtable) < 0) {
fprintf(stderr, "Error, trunc sql\n");
goto clear;
}
/* Run mysql query */
ret = mysql_query(dbconn, stmt);
/* Make sure query was successfull */
if (ret != 0) {
fprintf(stderr, "%s\n", mysql_error(dbconn));
goto clear;
}
return 0;
clear:
/* Clean up */
free(stmt);
return -1;
}
/*
* Initialize database connection and connect to database
@ -33,7 +87,7 @@ int arch_db_init(char *host, char *username, char *password, char *database, cha
my_bool reconnect = 1;
/* Keep tablename for querys */
dbtable = malloc(sizeof(table));
dbtable = malloc(strlen(table) + 1);
strcpy(dbtable,table);
/* Init. database */
@ -82,58 +136,6 @@ void arch_db_close() {
mysql_library_end();
}
/*
* Database setup
*/
int db_setup() {
int ret;
char stmt_create[] = "CREATE TABLE IF NOT EXISTS `%s` ("
"`Path` varchar(512) default NULL, "
"`Base` varchar(512) default NULL, "
"`Type` tinyint(1) default NULL, "
"`Status` tinyint(1) default NULL, "
"`Date` datetime default NULL) "
"ENGINE=MyISAM DEFAULT CHARSET=utf8";
char stmt_trunc[] = "TRUNCATE TABLE `%s`";
/* Allocate memory big enough for querys */
char *stmt = (char *) malloc((sizeof(char) * strlen(dbtable)) + strlen(stmt_create) - 1);
/* Create mysql query */
if(sprintf(stmt, stmt_create, dbtable) < 0) {
fprintf(stderr, "Error, create database sql\n");
}
/* Run mysql query */
ret = mysql_query(dbconn, stmt);
/* Make sure query was successfull */
if(ret != 0) {
fprintf(stderr, "%s\n", mysql_error(dbconn));
return -1;
}
/* Create mysql query */
if(sprintf(stmt, stmt_trunc, dbtable) < 0) {
fprintf(stderr, "Error, trunc sql\n");
}
/* Run mysql query */
ret = mysql_query(dbconn, stmt);
/* Clean up */
free(stmt);
/* Make sure query was successfull */
if(ret != 0) {
fprintf(stderr, "%s\n", mysql_error(dbconn));
return -1;
}
return 0;
}
/*
* Insert into database
*/

View file

@ -39,7 +39,7 @@ indexer :
$(CC) $(CFLAGS) -D__DEBUG__ ../src/common/path.c ../src/fs/tree.c ../src/indexer.c t_indexer.c -o test_indexer
mysql :
$(CC) -D DB_DEBUG $(CFLAGS) ../src/mysql_db.c -L/usr/lib64/mysql \
$(CC) -D DB_DEBUG $(CFLAGS) ../src/arch/mysql.c -L/usr/lib64/mysql \
-lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto t_mysql.c -o test_mysql
queue :

View file

@ -1,33 +1,20 @@
#ifndef INOTIFY_DEBUG
#define INOTIFY_DEBUG
#endif
#include <stdio.h>
#include "../src/notify_db.h"
#include "../src/arch/db.h"
int main(int argc, char *argv[]) {
// Keep me safe
printf("Run!\n");
// Connect
notify_db_init("localhost", "warez", "elebobo", "filesystem", "filesystem");
// Truncate
//notify_db_truncate();
arch_db_init("localhost", "warez", "elebobo", "filesystem", "filesystem");
sleep(10);
// Insert
notify_db_insert("/this/is/my/path/to/", "myfile", 0);
arch_db_insert("/this/is/my/path/to/", "myfile", 0);
// Delete
//notify_db_delete("/this/is/my/path/to/", "myfile");
arch_db_delete("/this/is/my/path/to/", "myfile");
// Close
notify_db_close();
arch_db_close();
// Kill me
return 0;
}
}