Skip to content

Commit 5161d22

Browse files
author
Neo Chen
committed
modified: README.md
modified: outfile.c modified: outfile.h
1 parent 4656f6a commit 5161d22

File tree

3 files changed

+26
-278
lines changed

3 files changed

+26
-278
lines changed

README.md

Lines changed: 17 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -25,135 +25,36 @@ Plugin Install and Uninstall
2525

2626
### Install
2727

28-
create function outfile_create returns string soname 'liboutfile.so';
29-
create function outfile_remove returns string soname 'liboutfile.so';
30-
create function outfile_read returns string soname 'liboutfile.so';
31-
create function outfile_write returns string soname 'liboutfile.so';
28+
create function out2file returns string soname 'liboutfile.so';
3229

3330
### Uninstall
3431

35-
drop function outfile_create;
36-
drop function outfile_remove;
37-
drop function outfile_read;
38-
drop function outfile_write;
32+
drop function out2file;
3933

4034
Testing
4135
-------
4236
### 创建管道
43-
mysql> create function outfile_create returns string soname 'liboutfile.so';
44-
Query OK, 0 rows affected (0.02 sec)
45-
46-
mysql> select outfile_create('/tmp/myoutfile');
47-
+----------------------------+
48-
| outfile_create('/tmp/myoutfile') |
49-
+----------------------------+
50-
| ture |
51-
+----------------------------+
37+
38+
mysql> create function out2file returns string soname 'liboutfile.so';
39+
Query OK, 0 rows affected (0.00 sec)
40+
41+
42+
mysql> select out2file('/tmp/myoutfile',"Helloworld!!!");
43+
+--------------------------------------------+
44+
| out2file('/tmp/myoutfile',"Helloworld!!!") |
45+
+--------------------------------------------+
46+
| true |
47+
+--------------------------------------------+
5248
1 row in set (0.00 sec)
49+
50+
5351

5452
查看管道是否创建
55-
56-
$ ls /tmp/myoutfile
57-
/tmp/myoutfile
58-
59-
覆盖测试,正确应该返回false
60-
61-
mysql> select outfile_create('/tmp/myoutfile');
62-
+----------------------------+
63-
| outfile_create('/tmp/myoutfile') |
64-
+----------------------------+
65-
| false |
66-
+----------------------------+
67-
1 row in set (0.00 sec)
6853

69-
### 删除管道
70-
mysql> select outfile_remove('/tmp/myoutfile');
71-
+----------------------------+
72-
| outfile_remove('/tmp/myoutfile') |
73-
+----------------------------+
74-
| true |
75-
+----------------------------+
76-
1 row in set (0.00 sec)
77-
78-
mysql> select outfile_remove('/tmp/my');
79-
+------------------------+
80-
| outfile_remove('/tmp/my') |
81-
+------------------------+
82-
| false |
83-
+------------------------+
84-
1 row in set (0.00 sec)
8554

86-
删除不存在的管道会提示 false
87-
88-
### 读管道
89-
90-
在一个终端窗口中运行
91-
mysql> select outfile_read('/tmp/myoutfile');
92-
+--------------------------+
93-
| outfile_read('/tmp/myoutfile') |
94-
+--------------------------+
95-
| Hello world |
96-
+--------------------------+
97-
1 row in set (7.85 sec)
98-
99-
在另一个终端窗口中运行
100-
mysql> select outfile_write('/tmp/myoutfile','Hello world !!!');
101-
+---------------------------------------------+
102-
| outfile_write('/tmp/myoutfile','Hello world !!!') |
103-
+---------------------------------------------+
104-
| true |
105-
+---------------------------------------------+
106-
1 row in set (0.00 sec)
107-
108-
或者
109-
110-
在命令行运行
111-
$ echo "Hello world" > /tmp/myoutfile
112-
113-
在SQL客户端中运行
114-
mysql> select outfile_read('/tmp/myoutfile');
115-
+--------------------------+
116-
| outfile_read('/tmp/myoutfile') |
117-
+--------------------------+
118-
| Hello world
119-
|
120-
+--------------------------+
121-
1 row in set (0.00 sec)
122-
注意上面echo会自动增加换行符,-n参数可以避免
123-
$ echo -n "Hello world" > /tmp/myoutfile
124-
125-
mysql> select outfile_read('/tmp/myoutfile');
126-
+--------------------------+
127-
| outfile_read('/tmp/myoutfile') |
128-
+--------------------------+
129-
| Hello world |
130-
+--------------------------+
131-
1 row in set (0.01 sec)
132-
133-
### 写管道
134-
mysql> select outfile_write('/tmp/myoutfile','Hello world !!!');
135-
+---------------------------------------------+
136-
| outfile_write('/tmp/myoutfile','Hello world !!!') |
137-
+---------------------------------------------+
138-
| true |
139-
+---------------------------------------------+
140-
1 row in set (0.00 sec)
141-
142-
$ cat /tmp/myoutfile
143-
Hello world !!!
55+
root@netkiller ~/mysql-outfile-plugin % cat /tmp/myoutfile
56+
Helloworld!!!
14457

145-
管道 /tmp/nooutfile 不存在会返回false
146-
mysql> select outfile_write('/tmp/nooutfile',concat(mobile,'\r\n')) from demo;
147-
+-------------------------------------------------+
148-
| outfile_write('/tmp/nooutfile',concat(mobile,'\r\n')) |
149-
+-------------------------------------------------+
150-
| false |
151-
| false |
152-
| false |
153-
| false |
154-
| false |
155-
+-------------------------------------------------+
156-
5 rows in set (0.01 sec)
15758

15859
# Donations
15960

outfile.c

Lines changed: 3 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -13,127 +13,9 @@ Author: netkiller<netkiller@msn.com>
1313

1414
#include "outfile.h"
1515

16-
/* ------------------------ outfile_create ----------------------------- */
17-
18-
my_bool outfile_create_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
19-
{
20-
21-
if (args->arg_count != 1)
22-
{
23-
strncpy(message,
24-
"one arguments must be supplied: outfile_create('<pipename>').",
25-
MYSQL_ERRMSG_SIZE);
26-
return 1;
27-
}
28-
args->arg_type[0]= STRING_RESULT;
29-
30-
return 0;
31-
}
32-
33-
char *outfile_create(UDF_INIT *initid, UDF_ARGS *args,
34-
__attribute__ ((unused)) char *result,
35-
unsigned long *length,
36-
__attribute__ ((unused)) char *is_null,
37-
__attribute__ ((unused)) char *error)
38-
{
39-
40-
char *status;
41-
if (create_outfile(args->args[0]) == 0)
42-
status = "ture";
43-
else
44-
status = "false";
45-
46-
*length = strlen(status);
47-
return ((char *)status);
48-
49-
}
50-
51-
void outfile_create_deinit(UDF_INIT *initid)
52-
{
53-
return;
54-
}
55-
56-
/* ------------------------ outfile_remove ----------------------------- */
57-
58-
my_bool outfile_remove_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
59-
{
60-
61-
if (args->arg_count != 1)
62-
{
63-
strncpy(message,"one arguments must be supplied: outfile_remove('<pipename>').", MYSQL_ERRMSG_SIZE);
64-
return 1;
65-
}
66-
67-
args->arg_type[0]= STRING_RESULT;
68-
69-
return 0;
70-
}
71-
72-
char *outfile_remove(UDF_INIT *initid, UDF_ARGS *args,
73-
__attribute__ ((unused)) char *result,
74-
unsigned long *length,
75-
__attribute__ ((unused)) char *is_null,
76-
__attribute__ ((unused)) char *error)
77-
{
78-
79-
char *data;
80-
if( remove_outfile(args->args[0]) == 0 )
81-
data = "true";
82-
else
83-
//asprintf(&data, "ARG0=%s, ARG1=%d", args->args[0], errno);
84-
data = "false";
85-
86-
*length = strlen(data);
87-
return ((char *)data);
88-
89-
}
90-
91-
void outfile_remove_deinit(UDF_INIT *initid)
92-
{
93-
return;
94-
}
95-
96-
/* ------------------------ outfile_read ----------------------------- */
97-
98-
my_bool outfile_read_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
99-
{
100-
101-
if (args->arg_count != 1)
102-
{
103-
strncpy(message, "one arguments must be supplied: outfile_read('<pipename>').", MYSQL_ERRMSG_SIZE);
104-
return 1;
105-
}
106-
107-
args->arg_type[0]= STRING_RESULT;
108-
109-
return 0;
110-
}
111-
112-
char *outfile_read(UDF_INIT *initid, UDF_ARGS *args,
113-
__attribute__ ((unused)) char *result,
114-
unsigned long *length,
115-
__attribute__ ((unused)) char *is_null,
116-
__attribute__ ((unused)) char *error)
117-
{
118-
119-
char *data;
120-
121-
data = read_outfile(args->args[0]);
122-
123-
//asprintf(&data, "ARG0=%s, ARG1=%d", args->args[0], args->args[1]);
124-
*length = strlen(data);
125-
return ((char *)data);
126-
127-
}
128-
129-
void outfile_read_deinit(UDF_INIT *initid)
130-
{
131-
return;
132-
}
133-
13416
/* ------------------------ outfile_write ----------------------------- */
13517

136-
my_bool outfile_write_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
18+
my_bool out2file_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
13719
{
13820
if (args->arg_count != 2)
13921
{
@@ -145,7 +27,7 @@ my_bool outfile_write_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
14527
return 0;
14628
}
14729

148-
char *outfile_write(UDF_INIT *initid, UDF_ARGS *args,
30+
char *out2file(UDF_INIT *initid, UDF_ARGS *args,
14931
__attribute__ ((unused)) char *result,
15032
unsigned long *length,
15133
__attribute__ ((unused)) char *is_null,
@@ -163,7 +45,7 @@ char *outfile_write(UDF_INIT *initid, UDF_ARGS *args,
16345
return ((char *)status);
16446
}
16547

166-
void outfile_write_deinit(UDF_INIT *initid)
48+
void out2file_deinit(UDF_INIT *initid)
16749
{
16850
return;
16951
}

outfile.h

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,22 @@
1-
my_bool outfile_remove_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
2-
char *outfile_remove(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
3-
void outfile_remove_deinit(UDF_INIT *initid);
4-
5-
my_bool outfile_read_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
6-
char *outfile_read(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
7-
void outfile_read_deinit(UDF_INIT *initid);
8-
9-
my_bool outfile_write_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
10-
char *outfile_write(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
11-
void outfile_write_deinit(UDF_INIT *initid);
1+
my_bool out2file_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
2+
char *out2file(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
3+
void out2file_deinit(UDF_INIT *initid);
124

135
char* concat(const char* str1, const char* str2)
146
{
157
char* res;
168
asprintf(&res, "%s%s", str1, str2);
179
return res;
1810
}
19-
20-
char * read_outfile(char * path)
21-
{
22-
int fd;
23-
char buf[8];
24-
char *result="";
25-
/* open, read, and display the message from the FIFO */
26-
fd = open(path, O_RDONLY);
27-
while( read(fd, buf, sizeof(buf)) > 0){
28-
result = concat(result,buf);
29-
memset(buf, 0, sizeof(buf));
30-
}
31-
//printf("Received: %s\n", buf);
32-
close(fd);
33-
34-
return result;
35-
}
36-
3711
int write_outfile(char * path, char * msg)
3812
{
3913

4014
FILE* file = fopen(path, "a+");
4115
if (file == 0) {
4216
return 0;
4317
}
44-
fputs(msg, file);
45-
fclose(file);
18+
msg = concat(msg, "\r\n");
19+
fputs(msg, file);
20+
fclose(file);
4621
return 1;
4722
}
48-
int create_outfile(char * path)
49-
{
50-
/* create the FIFO (named pipe) */
51-
return mkoutfile(path, 0660);
52-
}
53-
int remove_outfile(char *path)
54-
{
55-
/* remove the FIFO */
56-
return unlink(path);
57-
}

0 commit comments

Comments
 (0)