This repository has been archived on 2024-01-15. You can view files and clone it, but cannot push or open issues or pull requests.
vn-mysql/udfs/multimax.cc

77 lines
1.5 KiB
C++

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
#define NAME "multimax"
#define N_ARGS 2
#define ARG_MAX 0
#define ARG_VALUE 1
#define DATA(ptr) ((Data *) ptr)
extern "C" {
#include "value.cc"
typedef struct {
Value max;
Value value;
}
Data;
void free_data(UDF_INIT *initid) {
Data* data = (Data *) initid->ptr;
value_set_null(&data->max);
value_set_null(&data->value);
}
long long multimax(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
Value * value = &DATA(initid->ptr)->value;
if (value->is_null) {
*is_null = 1;
return 0;
}
return value->val.i;
}
void multimax_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
Data *data = (Data *) initid->ptr;
Value max;
value_from_arg(&max, args, ARG_MAX);
if (value_compare(&max, &data->max) > 0) {
value_copy_arg(&data->max, args, ARG_MAX);
value_copy_arg(&data->value, args, ARG_VALUE);
}
}
void multimax_clear(UDF_INIT *initid, char *is_null, char *error) {
free_data(initid);
}
bool multimax_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
if (args->arg_count == N_ARGS) {
Data *data = DATA(malloc(sizeof(Data)));
value_init(&data->max);
value_init(&data->value);
args->arg_type[ARG_VALUE] = INT_RESULT;
initid->ptr = (char *) data;
initid->maybe_null = 1;
initid->const_item = 0;
return 0;
} else {
sprintf(message, "%s must have %d parameters", NAME, N_ARGS);
return 1;
}
}
void multimax_deinit(UDF_INIT *initid) {
free_data(initid);
free(initid->ptr);
}
}