From: Michael Farr Date: April 16 1999 6:41am Subject: balance solution List-Archive: http://lists.mysql.com/mysql/1935 Message-Id: <199904160643.QAA27433@lion.cs.latrobe.edu.au> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Wohoo, it works! I thought I should sent my result in, thanks for the help. At the moment it only works for floats, but that is ok. I do have one more problem however. I do not want to put the shared object file "balance.so" in /usr/lib, I assume there is an environmental variable owned by the mysql process where I can change the search path, does anyone know what it is called (I have tried LD_LIBRARY and LD_LIBRARY_PATH). oh yeah, if there are any mistakes in there that may cause run time errors feel free to fix it. /** Syntax for the new commands are: ** ** CREATE FUNCTION balance RETURNS FLOAT SONAME "balance.so"; ** The functions can be deleted by: ** ** DROP FUNCTION balance ** */ #include #include #include "mysql.h" extern "C" { my_bool balance_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void balance_deinit(UDF_INIT *initid); float balance(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error); } my_bool balance_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count != 1 || (args->arg_type[0] != REAL_RESULT)) { strcpy(message,"Wrong arguments to balance; Use the source"); return 1; } initid->ptr = new char[80] ; *initid->ptr = 0; return 0; } void balance_deinit(UDF_INIT *initid) { delete initid->ptr; } float balance(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { //initid->ptr double real_val = *((double*) args->args[0]) + atof(initid->ptr); sprintf(initid->ptr, "%f", real_val ); //thanks to Richard Tresider for that line!! return real_val; } example useage: gcc -shared -o balance.so balance.cpp cp balance.so /usr/lib mysql test CREATE TABLE acctest (debit FLOAT); INSERT INTO acctest (debit) VALUES (40); INSERT INTO acctest (debit) VALUES (-100); INSERT INTO acctest (debit) VALUES (20); SELECT debit, balance(debit) from acctest