Statistics for MySQL  0.9
rownumber.c
Go to the documentation of this file.
00001 /* rownumber.cc (Row number) */
00002 
00003 /***********************************************************************
00004 *  This code is part of Statistics for MySQL.
00005 *
00006 *  Copyright (C) 2011 Heinrich Schuchardt (xypron.glpk@gmx.de)
00007 *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");
00009 *  you may not use this file except in compliance with the License.
00010 *  You may obtain a copy of the License at
00011 *
00012 *      http://www.apache.org/licenses/LICENSE-2.0
00013 *
00014 *  Unless required by applicable law or agreed to in writing, software
00015 *  distributed under the License is distributed on an "AS IS" BASIS,
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017 *  See the License for the specific language governing permissions and
00018 *  limitations under the License.
00019 ***********************************************************************/
00020 
00030 #include "sqlstat.h"
00031 
00035 struct rownumber_storage {
00036   long long count; 
00037 };
00038 
00039 
00051 my_bool rownumber_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
00052   struct rownumber_storage *data;
00053 
00054   if (args->arg_count != 0) {
00055     strcpy(message,"rownumber() requires no argument");
00056     return 1;
00057   }
00058 
00059   data = (struct rownumber_storage *) malloc( sizeof(struct rownumber_storage));
00060   if (data == NULL) {
00061     strcpy(message,"Couldn't allocate memory");
00062     return 1;
00063   }  
00064   data->count = 0;
00065 
00066   initid->maybe_null = 0;
00067   initid->decimals   = NOT_FIXED_DEC;
00068   initid->max_length = 13 + initid->decimals;
00069   initid->ptr        = (char *) data;
00070   initid->const_item = 0;
00071   return 0;
00072 }
00073 
00084 long long rownumber(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
00085   struct rownumber_storage *data;
00086   long long ret;
00087   if (initid->ptr) {
00088     data = (struct rownumber_storage *) initid->ptr;
00089     ret = ++data->count;
00090   } else {
00091     ret = 0;
00092     *error = 1;
00093   }
00094   return ret;
00095 }
00096 
00104 void rownumber_deinit(UDF_INIT *initid) {
00105   if (initid->ptr) {
00106     free(initid->ptr);
00107   }
00108   return;
00109 }
 All Classes Files Functions Variables Typedefs Defines