Define function in C and call by SQL : Define Function « Function « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Function » Define Function 
Define function in C and call by SQL
 
/* Use the function */  
CREATE FUNCTION FormatName
RETURNS STRING
SONAME 'C:\\MySQL\\lib\\MySQLFunction.dll';

/* Def file */
LIBRARY "MySQLFunction"
EXPORTS
   FormatName

/* .c file */
#include <string>

#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>

using namespace std;

char* FormatName(UDF_INIT *initid, UDF_ARGS *args,
                 char *result, unsigned long *length,
                 char *is_null, char *error)
{
   // Retrieve the Name parameter from the args parameter
   char* fullName = new char[args->lengths[0]];
   fullName = args->args[0];

   // Convert from char* to string
   string strName = fullName;

   // Trim whitespace from end of string
   int i = strName.length();
   while (strName[i== ' ' || strName[i== '\0'
       i--;
   string trimName = strName.substr(0, i + 1);

   // Arrange in 'LastName, FirstName' format
   int spaceIndex = trimName.find_first_of(" ");
   string firstName = trimName.substr(0, spaceIndex);
   string lastName = trimName.substr(spaceIndex + 1);
   string formattedName = lastName + ", " + firstName;

   // Convert back to char* and set the length argument
   char* fmtName = new char[];
   formattedName.copy(result, string::npos);
   *length = static_cast<unsigned long>(i + 2);

   return result;
}

           
         
  
Related examples in the same category
1.Arithmetic Functions
2.Comparison Functions,Tests, Branching
3.Calculating, Formatting, and Transformation Functions
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.