List:General Discussion« Previous MessageNext Message »
From:Michael Widenius Date:April 16 1999 6:07am
Subject:Re: balance
View as plain text  
>>>>> "Michael" == Michael Farr <farrm@stripped>
> writes:

>> 
Michael> double balance(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
Michael> char *error)
Michael> {
Michael> float total = 0;
Michael> for(int i = 1; i<=indexId; i++)
Michael> total += account(i).debit;
Michael> }
>> 
Michael> If anyone can tell me how to access the data structures in MySQL
Michael> to get any
Michael> of those things out I should be able to finish this little problem.
>> 
Michael> Mike
>> 
>> Hi!
>> 
>> Sorry, you can't access the data structures in a UDF function.
>> 
>> You can however solve your problem this way:
>> 
>> Allocate a variable in your init function (you should save this in
>> UDF_INIT -> ptr)
>> 
>> For each call to the balance function, add the new value to the old
>> value and return the new value.
>> 
>> Regards,
>> Monty
>> 

UDF_INIT-> prt is a char* isn't it?  How can I store a double in a char*?  (
Michael> really hoping that isnt a stupid question )

Hi!

Try something like this:


my_bool balance(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
  initid->ptr=malloc(sizeof(double));
  if (! initid->ptr)
  {
    strcpy(message,"Not enough memory");
    return 1;
  }
  if (args->arg_count != 1)
  {
    strcpy(message,"Wrong number of arguments");
    return 1;
  }
  args->arg_type[0]=REAL_RESULT;	 /* Force result to double */
  (*(double*) initid->ptr)=0.0;
  return 0;
}

void balance_deinit(UDF_INIT *initid)
{
  if (initid->ptr)
    free(initid->ptr);
}


double balance(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
  double *old_value= (double*) initid->ptr;
  *old_value += *(double*) args->args[0]
  return *old_value;
}

Regards,
Monty

PS: The above code is not tested...
Thread
balanceMichael Farr15 Apr
  • balanceMichael Widenius15 Apr
    • Re: balanceMichael Farr15 Apr
      • Re: balanceMichael Widenius16 Apr
        • balance solutionMichael Farr16 Apr