http://www.mysql.com/doc/en/Adding_functions.html
요즘 PHP할 기회가 있어 올만에 놀러와 쓸때없는 글 올립니다......^^;
[UDF생성방법] - 환경설정 . LD_LABRARY_PATH를 path에 추가한다. shell)vi /etc/ld.so.conf - ld.so.conf file - /usr/local/mysql/lib/mysql/lib(mysql library디렉토리) shell)ldconfig (설정을 적용함)
간단하게 한글체크하는 예제를 만들어 봤습니다. ishangul.c 소스============================================================
#include <stdio.h> #include <stdlib.h> #include <mysql.h>
my_bool ishangul_init(UDF_INIT* initid, UDF_ARGS *args, char* message) { if(args->arg_count != 1) { strcpy(message, "ishangul requires one arguement"); return 1; }
initid->max_length = 1; return 0; }
long long ishangul(UDF_INIT* initid, UDF_ARGS* args, char* message) { if(((unsigned char)* args->args[0])>=0xa1 && ((unsigned char)* args->args[0])<=0xfe) { return 1; } else { return 0; } }
===========================================================================
- 컴파일하기 // mysql library path를 잡은 경우 . shell)gcc -shared -o ishangul.so ishangul.c // myslq library path를 잡지 않은경우(mysql을 /usr/local/mysql에 컴파일했을경우) . shell)gcc -shared -o ishangul.so ishangul.c -I/usr/local/mysql/include/mysql -L/usr/local/mysql/lib/mysql/lib -lmysqlclient
ishangul.so를 공유라이브러리폴더로 복사 . shell)cp ishangul.so /usr/lib;
- mysql 접속 - shell)mysql -uroot xxxxxx
- 등록 - mysql>create function ishangul returns integer soname "ishangul.so";
- 확인 - mysql>use mysql; mysql>select * from func; +----------+-----+-------------+----------+ | name | ret | dl | type | +----------+-----+-------------+----------+ | ishangul | 0 | ishangul.so | function | +----------+-----+-------------+----------+ 1 rows in set (0.00 sec)
- 테스트 - mysql> select ishangul('한'); +----------------+ | ishangul('한') | +----------------+ | 1 | +----------------+ 1 row in set (0.00 sec)
mysql> select ishangul('H'); +---------------+ | ishangul('H') | +---------------+ | 0 | +---------------+ 1 row in set (0.00 sec)
더 자세한 정보는..아래 링크를 참조하세요. http://www.mysql.com/doc/en/Adding_functions.html
|