曾以为Windows版本的MySQL存在不能使用UDF的BUG诸提交了一个bug报告。不过
似乎发现是我搞错了,MySQL的技术支持人员给了非常完美的解答,同大家分享
一下。下边是原文回复 :)
Sorry this isn't a bug.
Below I pasted a sample I did sometime ago for another user:
Ok. Assuming you have VC++ and the source distribution and a server
running,
I will create a UDF that returns a name:
Note: the sample is ugly, but the purpose here is to show you how
to handle the UDF.
- Open the mysqld.dsw workspace.
- Add New project to the workspace
- Project name: my_udf
- Select Win32 Dynamic-Link Library
- Click OK
- Select An Empty DLL project
- Click Finish
- Click OK
- Add a new file called my_udf.cpp to the project:
#include
1<stdlib.h>
2#include <winsock.h>
3#include <mysql.h>
4
5extern "C" {
6char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
7char *error);
8}
9
10char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
11char *error)
12{
13char * me = "my name";
14
15return me;
16}
17
18\- Type Ctrl+N for to create a new file.
19\- Select text type
20\- File name: my_udf.def
21\- Edit the above file with the below contents:
22LIBRARY UDF_EXAMPLE
23DESCRIPTION 'Example Using UDF with VC++'
24VERSION 1.0
25EXPORTS
26my_name
27
28\- Right Click the my_udf project and select Settings
29\- Click the C/C++ tab
30\- Select General in the Category Combo
31\- Add the macro HAVE_DLOPEN to the PreProcessor definition
32\- Select Preprocessor in the Category Combo
33\- Add the include path to the text box: Additional Include directories
34e.g: ../include
35\- Press F7 for to build the DLL.
36
37\- Copy the my_udf.dll to the environment path directory:
38\winnt\system32 for example.
39
40\- Start the mysql client and issue:
41
42C:\mysql-udf\bin>mysql -uroot -p
43Enter password:
44Welcome to the MySQL monitor. Commands end with ; or \g.
45Your MySQL connection id is 2 to server version: 3.23.52-max-nt
46
47Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
48
49mysql> CREATE FUNCTION my_name RETURNS STRING SONAME "my_udf.dll";
50Query OK, 0 rows affected (0.08 sec)
51
52mysql> select my_name();
53
54mysql> drop function my_name;
55Query OK, 0 rows affected (0.00 sec)
56
57画蛇添足的作下简要中文说明。
58
59抱歉,这并不是一个bug。下面我粘贴一个以前为某个客户做的简例,假设你有了
60VC++,源码分发,并且有一个正常运行的MySQL服务器。
61
62我将创建一个UDF它将一个名字:
63注意:例子非常简陋,目的是让你了解该如何处理手头的UDF。
64
65\- 打开 mysqld.dsw 工作区。
66\- 添加新项目到这个工作区
67\- Project name: my_udf // 项目名称:my_udf
68\- 选择 Win32 Dynamic-Link Library // Win32动态连接库
69\- 点击 OK
70\- 选择 An Empty DLL project // 一个空DLL项目
71\- 点击 Finish
72\- 点击 OK
73\- 添加新文件 my_udf.cpp 到项目中:
74#include <stdlib.h>
75#include <winsock.h>
76#include <mysql.h>
77
78extern "C" {
79char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
80char *error);
81// 兼容C
82}
83
84char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
85char *error)
86{
87char * me = "my name";
88
89return me;
90// 调用此UDF将返回 my name
91}
92\- 按 Ctrl+N 来创建一个新文件。
93\- 选择 text 类型
94\- File name: my_udf.def file://文件名:my_udf.def
95\- 按照下边的内容编辑文件。
96LIBRARY UDF_EXAMPLE
97DESCRIPTION 'Example Using UDF with VC++'
98VERSION 1.0
99EXPORTS
100my_name
101
102\- 右击my_udf项目并选择Settings
103\- 点 C/C++ 选项卡
104\- 选择 General
105\- 添加宏 HAVE_DLOPE 到预处理器定义
106\- 选择 Preprocessor
107\- 添加头文件路径: Additional Include directories
108例如: ../include
109\- 按 F7 去编译成 DLL.
110
111\- 复制 my_udf.dll 到环境变量path定义过的目录
112比如 \winnt\system32 。
113
114\- 打开mysql客户端
115
116C:\mysql-udf\bin>mysql -uroot -p
117Enter password:
118Welcome to the MySQL monitor. Commands end with ; or \g.
119Your MySQL connection id is 2 to server version: 3.23.52-max-nt
120
121Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
122
123mysql> CREATE FUNCTION my_name RETURNS STRING SONAME "my_udf.dll";
124Query OK, 0 rows affected (0.08 sec)
125
126mysql> select my_name();
127
128mysql> drop function my_name;
129Query OK, 0 rows affected (0.00 sec)
130
131
132ok!欢迎大家来MySQL板交流UDF设计经验!我的电子邮件是HeartIcy@163.com,
133手机13706410308。同时,我们MySQL板准备开始系统化持续性翻译MySQL文档,
134希望大家多多支持共同完成这一项目。
135
136HeartIcy
1372003年5月17日于中国济南</mysql.h></winsock.h></stdlib.h></mysql.h></winsock.h></stdlib.h>