Skip to content

Instantly share code, notes, and snippets.

@zigorou
Created December 18, 2011 15:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zigorou/1493687 to your computer and use it in GitHub Desktop.
Save zigorou/1493687 to your computer and use it in GitHub Desktop.
Building MySQL UDF on MacOSX
#ifdef STANDARD
/* STANDARD is defined, don't use any mysql functions */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong;/* Microsofts 64 bit types */
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#if defined(MYSQL_SERVER)
#include <m_string.h>/* To get strmov() */
#else
/* when compiled as standalone */
#include <string.h>
#define strmov(a,b) stpcpy(a,b)
#define bzero(a,b) memset(a,0,b)
#define memcpy_fixed(a,b,c) memcpy(a,b,c)
#endif
#endif
#include <mysql.h>
#include <ctype.h>
#ifdef HAVE_DLOPEN
#if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_SOLARIS_STYLE_GETHOST)
static pthread_mutex_t LOCK_hostname;
#endif
//
// User #includes go here
//
#include <lib_mysqludf_calc_distance.h>
//
// init, deinit and actual function prototypes here
//
//
// init function
//
my_bool calc_distance_udf_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
if(!(args->arg_count == 4)) {
strcpy(message, "Expected four arguments (lat, lon, locate_lat, locate_lon)");
return 1;
}
args->arg_type[0] = REAL_RESULT;
args->arg_type[1] = REAL_RESULT;
args->arg_type[2] = REAL_RESULT;
args->arg_type[3] = REAL_RESULT;
return 0;
}
//
// deinit function
//
void calc_distance_udf_deinit(UDF_INIT *initid __attribute__((unused))) {}
//
// Actual function
//
double calc_distance_udf(
UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
char* is_null __attribute__((unused)), char* error __attribute__((unused))
) {
double lat = *((double *)(args->args[0]));
double lon = *((double *)(args->args[1]));
double locate_lat = *((double *)(args->args[2]));
double locate_lon = *((double *)(args->args[3]));
lat = lat * M_PI / 180;
lon = lon * M_PI / 180;
locate_lat = locate_lat * M_PI / 180;
locate_lon = locate_lon * M_PI / 180;
return 3959 * acos(cos(lat) * cos(locate_lat) * cos(locate_lon - lon) + sin(lat) * sin(locate_lat));
}
#endif /* HAVE_DLOPEN */
#ifndef __LIB_MYSQLUDF_CALC_DISTANCE
#define __LIB_MYSQLUDF_CALC_DISTANCE 1
#include <mysql.h>
my_bool calc_distance_udf_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void calc_distance_udf_deinit(UDF_INIT *initid __attribute__((unused)));
double calc_distance_udf(
UDF_INIT* initid,
UDF_ARGS* args __attribute__((unused)),
char* is_null __attribute__((unused)),
char* error __attribute__((unused))
);
#endif
MYSQL_BUILD_DIR =
MYSQL_PLUGIN_DIR =
CC := gcc
CPPFLAGS := -Iinclude -I$(MYSQL_BUILD_DIR)/include -dynamiclib
LIB_FILE := lib_mysqludf_calc_distance.dylib
VPATH := src include
all: lib_mysqludf_calc_distance.dylib
lib_mysqludf_calc_distance.dylib: src/lib_mysqludf_calc_distance.c include/lib_mysqludf_calc_distance.h
$(CC) $(CPPFLAGS) -o $@ src/lib_mysqludf_calc_distance.c
install: lib_mysqludf_calc_distance.dylib
install -m 755 lib_mysqludf_calc_distance.dylib $(MYSQL_PLUGIN_DIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment