2010-07-17 14 views
5

मैं mysql के लिए एक कस्टम उचित उपयोगकर्ता परिभाषित फ़ंक्शन लिखने की कोशिश कर रहा हूं, इसलिए मैंने अपना खुद का निर्माण करने के लिए उदाहरण के रूप में http://www.mysqludf.org/index.php से str_ucwords फ़ंक्शन लिया।उचित केस mysql udf

my_bool str_ucwords_init(UDF_INIT *initid, UDF_ARGS *args, char *message) 
{ 
    /* make sure user has provided exactly one string argument */ 
    if (args->arg_count != 1 || args->arg_type[0] != STRING_RESULT || args->args[0] == NULL) 
    { 
     strcpy(message,"str_ucwords requires one string argument"); 
     return 1; 
    } 

    /* str_ucwords() will not be returning null */ 
    initid->maybe_null=0; 

    return 0; 
} 

char *str_ucwords(UDF_INIT *initid, UDF_ARGS *args, 
    char *result, unsigned long *res_length, 
    char *null_value, char *error) 
{ 
    int i; 
    int new_word = 0; 

    // copy the argument string into result 
    strncpy(result, args->args[0], args->lengths[0]); 

    *res_length = args->lengths[0]; 

    // capitalize the first character of each word in the string 
    for (i = 0; i < *res_length; i++) 
    { 
     if (my_isalpha(&my_charset_latin1, result[i])) 
     { 
      if (!new_word) 
      { 
       new_word = 1; 
       result[i] = my_toupper(&my_charset_latin1, result[i]); 
      } 
     } 
     else 
     { 
      new_word = 0; 
     } 
    } 

    return result; 
} 

यह ठीक काम करता है अगर मैं select str_ucwords("test string"); कोशिश लेकिन अगर मैं select str_ucwords(name) from name; की तरह एक डेटाबेस से क्षेत्रों का चयन करने के लिए प्रयास करें मैं कुछ भी नहीं लौटे मिलता है।

मैं इस फ़ंक्शन को कैसे बदलूं ताकि यह मुझे डेटाबेस पर फ़ील्ड से डेटा खींच सके?

मैंने पहले से ही init फ़ंक्शन से args->arg_type[0] != STRING_RESULT को हटाने का प्रयास किया है।

उत्तर

2

मुद्दा है, तर्क के प्रकार नहीं है, यह है कि यह NULL जब str_ucwords_init कहा जाता है (क्योंकि str_ucwords_init किसी भी पंक्तियों पुन: प्राप्त करने से पहले कहा जाता है) है। क्षेत्रों के साथ काम करने के लिए str_ucwords पाने के लिए आपको जब वास्तविक तर्क रिक्त है str_ucwords में _init समारोह में 1 से initid->maybe_null की स्थापना, और 1 के लिए *null_value की स्थापना (शून्य पर और result, हालांकि कि आवश्यक नहीं हो सकता) द्वारा शून्य मानों का समर्थन करने के लिए है । lib_mysqludf_str की

my_bool str_ucwords_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { 
    unsigned long res_length; 

    /* make sure user has provided exactly one argument */ 
    if (args->arg_count != 1) { 
     snprintf(message, MYSQL_ERRMSG_SIZE, "wrong number of arguments: str_ucwords requires one string argument, got %d arguments", args->arg_count); 
     return 1; 
    } 
    /* make sure user has provided a string argument */ 
    if (args->arg_type[0] != STRING_RESULT) { 
     snprintf(message, MYSQL_ERRMSG_SIZE, "str_ucwords requires one string argument (expected type %d, got type %d)", STRING_RESULT, args->arg_type[0]); 
     return 1; 
    } 

    res_length = args->lengths[0]; 

    if (SIZE_MAX < res_length) { 
     snprintf(message, MYSQL_ERRMSG_SIZE, "res_length (%lu) cannot be greater than SIZE_MAX (%zu)", res_length, (size_t) (SIZE_MAX)); 
     return 1; 
    } 

    initid->ptr = NULL; 

    if (res_length > 255) { 
     char *tmp = (char *) malloc((size_t) res_length); /* This is a safe cast because res_length <= SIZE_MAX. */ 
     if (tmp == NULL) { 
      snprintf(message, MYSQL_ERRMSG_SIZE, "malloc() failed to allocate %zu bytes of memory", (size_t) res_length); 
      return 1; 
     } 
     initid->ptr = tmp; 
    } 

    initid->maybe_null = 1; 
    initid->max_length = res_length; 
    return 0; 
} 

char *str_ucwords(UDF_INIT *initid, UDF_ARGS *args, 
      char *result, unsigned long *res_length, 
      char *null_value, char *error) 
{ 
    int i; 
    int new_word = 1; 

    if (args->args[0] == NULL) { 
     result = NULL; 
     *res_length = 0; 
     *null_value = 1; 
    } else { 
     if (initid->ptr != NULL) { 
      result = initid->ptr; 
     } 

     // copy the argument string into result 
     memcpy(result, args->args[0], args->lengths[0]); 
     *res_length = args->lengths[0]; 

     // capitalize the first character of each word in the string 
     for (i = 0; i < *res_length; i++) { 
      if (my_isalpha(&my_charset_latin1, result[i])) { 
       if (new_word) { 
        new_word = 0; 
        result[i] = my_toupper(&my_charset_latin1, result[i]); 
       } else { 
        result[i] = my_tolower(&my_charset_latin1, result[i]); 
       } 
      } else { 
       new_word = 1; 
      } 
     } 
    } 
    return result; 
} 

बाद में विज्ञप्ति जिसका मतलब है कि वे भी तालिका स्तंभ साथ काम करना चाहिए परिवर्तन के बिना कार्यों में शून्य मानों का समर्थन करना चाहिए।

संबंधित मुद्दे