終于發(fā)現(xiàn)了篇不錯(cuò)的Mysql開(kāi)發(fā)的文章
曾以為Windows版本的MySQL存在不能使用UDF的BUG諸提交了一個(gè)bug報(bào)告。不過(guò)
似乎發(fā)現(xiàn)是我搞錯(cuò)了,MySQL的技術(shù)支持人員給了非常完美的解答,同大家分享
一下。下邊是原文回復(fù) :)
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 <stdlib.h>
#include <winsock.h>
#include <mysql.h>
extern "C" {
char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
char *error);
}
char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
char *error)
{
char * me = "my name";
return me;
}
- Type Ctrl+N for to create a new file.
- Select text type
- File name: my_udf.def
- Edit the above file with the below contents:
LIBRARY UDF_EXAMPLE
DESCRIPTION 'Example Using UDF with VC++'
VERSION 1.0
EXPORTS
my_name
- Right Click the my_udf project and select Settings
- Click the C/C++ tab
- Select General in the Category Combo
- Add the macro HAVE_DLOPEN to the PreProcessor definition
- Select Preprocessor in the Category Combo
- Add the include path to the text box: Additional Include directories
e.g: ../include
- Press F7 for to build the DLL.
- Copy the my_udf.dll to the environment path directory:
\winnt\system32 for example.
- Start the mysql client and issue:
C:\mysql-udf\bin>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 3.23.52-max-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> CREATE FUNCTION my_name RETURNS STRING SONAME "my_udf.dll";
Query OK, 0 rows affected (0.08 sec)
mysql> select my_name();
mysql> drop function my_name;
Query OK, 0 rows affected (0.00 sec)
畫蛇添足的作下簡(jiǎn)要中文說(shuō)明。
抱歉,這并不是一個(gè)bug。下面我粘貼一個(gè)以前為某個(gè)客戶做的簡(jiǎn)例,假設(shè)你有了
VC++,源碼分發(fā),并且有一個(gè)正常運(yùn)行的MySQL服務(wù)器。
我將創(chuàng)建一個(gè)UDF它將一個(gè)名字:
注意:例子非常簡(jiǎn)陋,目的是讓你了解該如何處理手頭的UDF。
- 打開(kāi) mysqld.dsw 工作區(qū)。
- 添加新項(xiàng)目到這個(gè)工作區(qū)
- Project name: my_udf // 項(xiàng)目名稱:my_udf
- 選擇 Win32 Dynamic-Link Library // Win32動(dòng)態(tài)連接庫(kù)
- 點(diǎn)擊 OK
- 選擇 An Empty DLL project // 一個(gè)空DLL項(xiàng)目
- 點(diǎn)擊 Finish
- 點(diǎn)擊 OK
- 添加新文件 my_udf.cpp 到項(xiàng)目中:
#include <stdlib.h>
#include <winsock.h>
#include <mysql.h>
extern "C" {
char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
char *error);
// 兼容C
}
char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
char *error)
{
char * me = "my name";
return me;
// 調(diào)用此UDF將返回 my name
}
- 按 Ctrl+N 來(lái)創(chuàng)建一個(gè)新文件。
- 選擇 text 類型
- File name: my_udf.def file://文件名:my_udf.def
- 按照下邊的內(nèi)容編輯文件。
LIBRARY UDF_EXAMPLE
DESCRIPTION 'Example Using UDF with VC++'
VERSION 1.0
EXPORTS
my_name
- 右擊my_udf項(xiàng)目并選擇Settings
- 點(diǎn) C/C++ 選項(xiàng)卡
- 選擇 General
- 添加宏 HAVE_DLOPE 到預(yù)處理器定義
- 選擇 Preprocessor
- 添加頭文件路徑: Additional Include directories
例如: ../include
- 按 F7 去編譯成 DLL.
- 復(fù)制 my_udf.dll 到環(huán)境變量path定義過(guò)的目錄
比如 \winnt\system32 。
- 打開(kāi)mysql客戶端
C:\mysql-udf\bin>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 3.23.52-max-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> CREATE FUNCTION my_name RETURNS STRING SONAME "my_udf.dll";
Query OK, 0 rows affected (0.08 sec)
mysql> select my_name();
mysql> drop function my_name;
Query OK, 0 rows affected (0.00 sec)
ok!歡迎大家來(lái)MySQL板交流UDF設(shè)計(jì)經(jīng)驗(yàn)!我的電子郵件是HeartIcy@163.com,
手機(jī)13706410308。同時(shí),我們MySQL板準(zhǔn)備開(kāi)始系統(tǒng)化持續(xù)性翻譯MySQL文檔,
希望大家多多支持共同完成這一項(xiàng)目。
HeartIcy
2003年5月17日于中國(guó)濟(jì)南