List:General Discussion« Previous MessageNext Message »
From:Michael Farr Date:April 16 1999 6:41am
Subject:balance solution
View as plain text  
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
"" 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:
** The functions can be deleted by:
** DROP FUNCTION balance

#include <stdio.h>
#include <stdlib.h>
#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.cpp
cp /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
balanceMichael Farr15 Apr
  • balanceMichael Widenius15 Apr
    • Re: balanceMichael Farr15 Apr
      • Re: balanceMichael Widenius16 Apr
        • balance solutionMichael Farr16 Apr