Windows上的MySQL UDF開發

來源:互聯網
上載者:User
mysql|window
曾以為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 <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 ystem32 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)

畫蛇添足的作下簡要中文說明。

抱歉,這並不是一個bug。下面我粘貼一個以前為某個客戶做的簡例,假設你有了
VC++,源碼分發,並且有一個正常啟動並執行MySQL伺服器。

我將建立一個UDF它將一個名字:
注意:例子非常簡陋,目的是讓你瞭解該如何處理手頭的UDF。

- 開啟 mysqld.dsw 工作區。
- 添加新項目到這個工作區
- Project name: my_udf            // 項目名稱:my_udf
- 選擇 Win32 Dynamic-Link Library // Win32動態串連庫
- 點擊 OK
- 選擇 An Empty DLL project       // 一個空DLL項目
- 點擊 Finish
- 點擊 OK
- 添加新檔案 my_udf.cpp 到項目中:
#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;
    // 調用此UDF將返回 my name
}
- 按 Ctrl+N 來建立一個新檔案。
- 選擇 text 類型
- File name: my_udf.def file://檔案名稱:my_udf.def
- 按照下邊的內容編輯檔案。
LIBRARY         UDF_EXAMPLE
DESCRIPTION             'Example Using UDF with VC++'
VERSION         1.0
EXPORTS
        my_name

- 右擊my_udf項目並選擇Settings
- 點 C/C++ 選項卡
- 選擇 General
- 添加宏 HAVE_DLOPE 到前置處理器定義
- 選擇 Preprocessor
- 添加標頭檔路徑: Additional Include directories
  例如: ../include
- 按 F7 去編譯成 DLL.

- 複製 my_udf.dll 到環境變數path定義過的目錄
  比如 \winnt ystem32 。

- 開啟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!歡迎大家來MySQL板交流UDF設計經驗!我的電子郵件是HeartIcy@163.com,
手機13706410308。同時,我們MySQL板準備開始系統化持久性翻譯MySQL文檔,
希望大家多多支援共同完成這一項目。

HeartIcy
2003年5月17日於中國濟南


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.