"David Godsey" <[EMAIL PROTECTED]> wrote on 03/22/2006 01:21:07 PM:

> I'm in the process of writing my first UDF and would appreciate some 
help.
> 
> I am pulling data from a table like:
> 
>          SELECT payload_time,
>          SUBSTR(BINARY(frame_data),
>          FLOOR(foffset/8)+1,
>          CEIL((flength + (foffset %8 ))/8))
>          FROM RawMajorFrames
>          WHERE raw_major_frame_id=rfid
>          INTO ptime,fdata;
> 
> frame_data is type BLOB.  It is raw data collected. The substr will get
> the specific bytes I'm interested in.  What I need to do, is if the data
> is <= 8bytes, convert it to a BIGINT, so I can do some masking on the
> data.
> 
> So I am writing a UDF to do the job, but I am apparently unfamiliar with
> the Mysql data types and how I can convert them.
> 
> In a procedure.
> DECLARE fdata_bigint BIGINT UNSIGNED;
> SELECT BlobToInt(binary(fdata)) INTO fdata_bigint;
> 
> my_bool BlobToInt_init( UDF_INIT* initid, UDF_ARGS* args, char* message 
)
> {
>   if (args->arg_count != 1)
>   {
>     strcpy(message,"Wrong arguments to BlobToInt;  should be
> BlobToInt(blob)");
>     return 1;
>   }
>   return 0;
> }
> longlong BlobToInt( UDF_INIT* initid, UDF_ARGS* args, char* is_null, 
char
> *error )
> {
>    longlong tmplong =  *((longlong*)args->args[0]);
>    return tmplong;
> }
> 
> I guess I was just assuming I could just cast the data as the type I 
want,
> but that doesn't seem to work.  The function returns a 0.
> 
> Any help would be appreciated.
> 
> Accomplishing the impossible means only that the boss will add it to 
your
> regular duties.
> 
> David Godsey
> 

C is not my strongest language but aren't you getting a null-terminated 
string as args[0] ? What if you allocated a longlong and byte-copied the 
bytes from args[0] into your longlong? Maybe something like...

longlong BlobToInt( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char 
*error )
{
char idx, *cArg
   longlong tmplong, *plonglong ;
   plonglong = &tmplong;
   cArg = (args->args[0]);
   for(idx=0;idx<8;idx++) {
        plonglong[idx]=cArg[idx];
   }
   return tmplong;
}

Again, I strongly stress that C/C++ is not my best language (I don't use 
it nearly enough) but I think you can see what I was trying to do. Other 
options: memcpy(), strcpy(), strncpy() etc.... 

Shawn Green
Database Administrator
Unimin Corporation - Spruce Pine 



Reply via email to