Created
April 17, 2012 18:41
-
-
Save tron1point0/2408110 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef STANDARD | |
#include <string.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#else | |
#include <my_global.h> | |
#include <my_sys.h> | |
#endif | |
#include <mysql.h> | |
#include <m_ctype.h> | |
#include <m_string.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#ifdef HAVE_DLOPEN | |
#define LIBVERSION "lib_mysqludf_fwrite version 0.1.0" | |
#define WRITE_ERROR "Error writing to file." | |
my_bool lib_mysqludf_fwrite_info_init(UDF_INIT *initid, UDF_ARGS *args, char *message){ | |
return 1; | |
} | |
char* lib_mysqludf_fwrite_info(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error){ | |
strcpy(result,LIBVERSION); | |
*length = strlen(LIBVERSION); | |
return result; | |
} | |
my_bool udf_fwrite_init(UDF_INIT *initid, UDF_ARGS *args, char *message){ | |
unsigned int i = 0; | |
if(args->arg_count >= 2){ | |
for (i = 0; i < args->arg_count; i++) { | |
args->arg_type[i] = STRING_RESULT; | |
} | |
return 0; | |
} else { | |
strcpy(message,"Usage: fwrite(filename,string1,[string2,...,stringN]"); | |
return 1; | |
} | |
} | |
char* udf_fwrite(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error){ | |
FILE *file; | |
unsigned int i = 0; | |
*length = 0; | |
result = malloc(1); | |
if ((file = fopen(args->args[0],"w")) != NULL) { | |
for (i = 1; i < args->arg_count; i++) { | |
if (fwrite(args->args[i], 1, args->lengths[i], file) != args->lengths[i]) { | |
*length = strlen(WRITE_ERROR); | |
result = realloc(result,*length); | |
strncpy(result,WRITE_ERROR,*length); | |
fclose(file); | |
return result; | |
} else { | |
result = realloc(result, *length + args->lengths[i] + 1); | |
strncpy(result + *length,args->args[i],args->lengths[i]); | |
*length += args->lengths[i] + 1; | |
} | |
} | |
fclose(file); | |
} | |
if (!(*result) || result == NULL) { | |
*is_null = 1; | |
} else { | |
result[*length] = 0x00; | |
} | |
return result; | |
} | |
#endif /* HAVE_DLOPEN */ |
also:
$ sudo cp lib_mysqludf_fwrite.so /usr/lib/mysql/plugin/
$ sudo /etc/rc.d/mysqld restart
:: Stopping MySQL Server [DONE]
:: Starting MySQL Server [DONE]
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> create function udf_fwrite returns string soname 'lib_mysqludf_fwrite.so';
Query OK, 0 rows affected (0.00 sec)
mysql> select udf_fwrite('/tmp/test','abc');
+-------------------------------+
| udf_fwrite('/tmp/test','abc') |
+-------------------------------+
| abc |
+-------------------------------+
LIBDIR=/usr/lib
INCLUDE=/usr/include
MYSQL=$(INCLUDE)/mysql
PLUGINDIR=/usr/lib/mysql/plugin
CC=/usr/bin/gcc
objects=$(patsubst %.c,%.so,$(wildcard *.c))
$(PLUGINDIR)/%.so: %.so
install -d $(dir $@)
install -t $(dir $@) $<
%.so: %.c
$(CC) -Wall -I$(MYSQL) -I. -shared -fPIC $< -o $@
all: $(objects)
clean:
rm $(PWD)/*.so || true
install: $(patsubst %,$(PLUGINDIR)/%,$(objects))
mysql -u root < install.sql
DROP FUNCTION IF EXISTS lib_mysqludf_fwrite_info;
CREATE FUNCTION lib_mysqludf_fwrite_info RETURNS string SONAME 'lib_mysqludf_fwrite.so';
DROP FUNCTION IF EXISTS udf_fwrite;
CREATE FUNCTION udf_fwrite RETURNS string SONAME 'lib_mysqludf_fwrite.so';
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for my own benefit: