-
Notifications
You must be signed in to change notification settings - Fork 13
/
udf_colwidth.cc
132 lines (96 loc) · 2.7 KB
/
udf_colwidth.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
returns the length of the longest value in a set
input parameters:
data (string)
output:
length of longest value (int)
registering the function:
CREATE AGGREGATE FUNCTION colwidth RETURNS INTEGER SONAME 'udf_colwidth.so';
getting rid of the function:
DROP FUNCTION colwidth;
*/
#ifdef STANDARD
#include <stdio.h>
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong;
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#endif
#include <mysql.h>
#include <m_ctype.h>
#include <m_string.h>
#ifdef HAVE_DLOPEN
extern "C" {
my_bool colwidth_init( UDF_INIT* initid, UDF_ARGS* args, char* message );
void colwidth_deinit( UDF_INIT* initid );
void colwidth_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
void colwidth_add( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
long long colwidth( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error );
struct colwidth_data
{
long long maxlength;
};
my_bool colwidth_init( UDF_INIT* initid, UDF_ARGS* args, char* message )
{
struct colwidth_data* data = NULL;
if (args->arg_count != 1)
{
strcpy(message, "wrong number of arguments: colwidth() requires exactly one argument");
return 1;
}
if ( args->arg_type[0] != STRING_RESULT )
{
strcpy(message, "wrong argument type: colwidth() requires a string as parameter 1");
return 1;
}
data = new struct colwidth_data;
data->maxlength=0;
initid->maybe_null = 0;
initid->ptr = (char*)data;
return 0;
}
void colwidth_deinit( UDF_INIT* initid )
{
struct colwidth_data* data = (struct colwidth_data*)initid->ptr;
if (data != NULL)
{
data->maxlength=0;
delete data;
initid->ptr = NULL;
}
}
void colwidth_clear( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message ) {
struct colwidth_data* data = (struct colwidth_data*)initid->ptr;
data->maxlength=(long long) 0;
}
void colwidth_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message ) {
colwidth_clear( initid, args, is_null, message );
colwidth_add( initid, args, is_null, message );
}
void colwidth_add( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message )
{
struct colwidth_data* data = (struct colwidth_data*)initid->ptr;
if (args->args[0] && (args->lengths[0]>data->maxlength))
{
data->maxlength=args->lengths[0];
}
}
long long colwidth( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error )
{
struct colwidth_data* data = (struct colwidth_data*)initid->ptr;
*is_null=0;
if (data->maxlength)
{
return data->maxlength;
}
return 0;
}
}
#endif