Last Updated: February 25, 2016
·
825
· winash

Brogram - MySQL UDF to check if STR1 contains all words from STR2

Should have done this in app code, but where is the fun in that

#ifdef STANDARD
   /* STANDARD is defined, don't use any mysql functions */
   #include <stdlib.h>
   #include <stdio.h>
   #include <string.h>
   #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 <my_global.h>
   #include <my_sys.h>
   #if defined(MYSQL_SERVER)
   #include <m_string.h>/* To get strmov() */
   #else
   /* when compiled as standalone */
   #include <string.h>
   #define strmov(a,b) stpcpy(a,b)
   #define bzero(a,b) memset(a,0,b)
   #define memcpy_fixed(a,b,c) memcpy(a,b,c)
   #endif
   #endif
   #include <mysql.h>
   #include <ctype.h>

   #ifdef HAVE_DLOPEN

   #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_SOLARIS_STYLE_GETHOST)
   static pthread_mutex_t LOCK_hostname;
   #endif

char *trim(char *str);
my_bool str_match_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
double str_match(UDF_INIT *initid, UDF_ARGS *args,
                __attribute__ ((unused)) char *result,
               unsigned long *length,
                __attribute__ ((unused)) char *is_null,
                __attribute__ ((unused)) char *error);
void str_match_deinit(UDF_INIT *initid);



my_bool str_match_init(UDF_INIT *initid, UDF_ARGS *args, char *message){

if (args->arg_count != 2)
  {
    strncpy(message,
            "two arguments must be supplied: str_match('text','matching string').",
            MYSQL_ERRMSG_SIZE);
    return 1;
  }

  args->arg_type[0] = STRING_RESULT;
  args->arg_type[1] = STRING_RESULT;

  return 0;

}



double str_match(UDF_INIT *initid, UDF_ARGS *args,
                __attribute__ ((unused)) char *result,
               unsigned long *length,
                __attribute__ ((unused)) char *is_null,
                __attribute__ ((unused)) char *error){

//Split the second string using strtok
 char * pch;
 int match = 1;
 char *trimm;
 pch = strtok (args->args[1]," ");
 while (pch != NULL)
 {
   //pch contains the token, trim it and match with large string
   trimm = trim(pch);
   if((trimm!=0 && pch!= 0) &&NULL == strstr(args->args[0],trimm)){
       match = 0;
       break;
   }

   pch = strtok (NULL, " ");
 }
 if(match == 1)
    return 1;
 else
    return 0;

}


char *trim(char *str)
{
    size_t len = 0;
    char *frontp = str - 1;
    char *endp = NULL;

    if( str == NULL )
            return NULL;

    if( str[0] == '\0' )
            return str;

    len = strlen(str);
    endp = str + len;

    while( isspace(*(++frontp)) );
    while( isspace(*(--endp)) && endp != frontp );

    if( str + len - 1 != endp )
            *(endp + 1) = '\0';
    else if( frontp != str &&  endp == frontp )
            *str = '\0';

    endp = str;
    if( frontp != str )
    {
            while( *frontp ) *endp++ = *frontp++;
            *endp = '\0';
    }


    return str;
}



void str_match_deinit(UDF_INIT *initid)
{

}

   #endif /* HAVE_DLOPEN */

More here http://dev.mysql.com/doc/refman/5.1/en/adding-functions.html