/* * Copyright (c) 2017, brian * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifdef STANDARD /* STANDARD is defined, don't use any mysql functions */ #include #ifdef __WIN__ typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */ typedef __int64 longlong; #else typedef unsigned long long ulonglong; typedef long long longlong; #endif /*__WIN__*/ #else #include #include #if defined(MYSQL_SERVER) #include #else /* when compiled as standalone */ #include #endif #endif #include #include #ifdef HAVE_DLOPEN char *trim(char *str) { size_t len = 0; char *frontp = str; char *endp = NULL; if (str == NULL) { return NULL; } if (str[0] == '\0') { return str; } len = strlen(str); endp = str + len; /* Move the front and back pointers to address the first non-whitespace * characters from each end. */ while (isspace((unsigned char) *frontp)) { ++frontp; } if (endp != frontp) { while (isspace((unsigned char) *(--endp)) && endp != frontp) { } } if (str + len - 1 != endp) *(endp + 1) = '\0'; else if (frontp != str && endp == frontp) *str = '\0'; /* Shift the string so that it starts at str so that if it's dynamically * allocated, we can still free it on the returned pointer. Note the reuse * of endp to mean the front of the string buffer now. */ endp = str; if (frontp != str) { while (*frontp) { *endp++ = *frontp++; } *endp = '\0'; } return str; } my_bool ifempty_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void ifempty_deinit(UDF_INIT *initid); char* ifempty(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))); my_bool ifempty_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { int i; // if (args->arg_count == 0) { // strcpy(message, "Function requires at least 1 argument, (string [, string... ])"); // return 1; // } for (i = 0; i < args->arg_count; i++) { args->arg_type[i] = STRING_RESULT; } initid->maybe_null = 1; //can return null return 0; } char* ifempty(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))) { int i; char* Value; for (i = 0; i < args->arg_count; i++) { Value = args->args[i]; if (Value == NULL) continue; if (args->lengths[i] == 0) continue; if (strlen(trim(Value)) == 0) continue; *length = strlen(Value); return Value; } *is_null = 1; return '\0'; } #endif /* HAVE_DLOPEN */