Statistics for MySQL  1.1
 All Classes Files Functions Variables Typedefs Macros Pages
rownumber.c
Go to the documentation of this file.
1 /* rownumber.cc (Row number) */
2 
3 /***********************************************************************
4 * This code is part of Statistics for MySQL.
5 *
6 * Copyright (C) 2011 Heinrich Schuchardt (xypron.glpk@gmx.de)
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 ***********************************************************************/
20 
30 #include "sqlstat.h"
31 
36  long long count;
37 };
38 
39 
51 my_bool rownumber_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
52  struct rownumber_storage *data;
53 
54  if (args->arg_count != 0) {
55  strcpy(message,"rownumber() requires no argument");
56  return 1;
57  }
58 
59  data = (struct rownumber_storage *) malloc( sizeof(struct rownumber_storage));
60  if (data == NULL) {
61  strcpy(message,"Couldn't allocate memory");
62  return 1;
63  }
64  data->count = 0;
65 
66  initid->maybe_null = 0;
67  initid->decimals = NOT_FIXED_DEC;
68  initid->max_length = 13 + initid->decimals;
69  initid->ptr = (char *) data;
70  initid->const_item = 0;
71  return 0;
72 }
73 
84 long long rownumber(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
85  struct rownumber_storage *data;
86  long long ret;
87  if (initid->ptr) {
88  data = (struct rownumber_storage *) initid->ptr;
89  ret = ++data->count;
90  } else {
91  ret = 0;
92  *error = 1;
93  }
94  return ret;
95 }
96 
104 void rownumber_deinit(UDF_INIT *initid) {
105  if (initid->ptr) {
106  free(initid->ptr);
107  }
108  return;
109 }
Definition of functions for UDFs and plugins.
my_bool rownumber_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
Definition: rownumber.c:51
#define NOT_FIXED_DEC
Maximum number of digits in double As defined in mysql/sql_string.h.
Definition: sqlstat.h:77
long long rownumber(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve row number.
Definition: rownumber.c:84
long long count
Counter.
Definition: rownumber.c:36
Storage for rownumber function.
Definition: rownumber.c:35
void rownumber_deinit(UDF_INIT *initid)
Called after last access to function.
Definition: rownumber.c:104