If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Registering C based user defined function in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-15-10, 15:35
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Registering C based user defined function in DB2

Hi,

I'm using DB2 8.2 UDB on a Linux platform and just wanted to double check how to register a C function in DB2.

I have a C file called test.c and after compiling the code, I get the 'test' executable file which I have moved to /home/db2inst/sqllib/function.

Next I have the SQL file that has this code --

CREATE FUNCTION test(startTime TIMESTAMP, endTime TIMESTAMP)
RETURNS double
LANGUAGE c
PARAMETER STYLE sql
NO SQL
NOT FENCED
DETERMINISTIC
RETURNS NULL ON NULL INPUT
NO EXTERNAL ACTION
EXTERNAL NAME 'test' @


If I create the SQL function AND move the 'test' executable to /home/db2inst/sqllib/function, is there anything else that I need to do to successfully call the function? Is creating the function the same as registering it? Or do I need to run another command to do that?

Thanks!!
Reply With Quote
  #2 (permalink)  
Old 04-15-10, 15:43
dbzTHEdinosaur dbzTHEdinosaur is offline
Registered User
 
Join Date: Jun 2007
Location: germany
Posts: 96
just a suggestion

I would never name any type of object or executable 'test'.
__________________
Dick Brenholtz, Ami in Deutschland
Reply With Quote
  #3 (permalink)  
Old 04-15-10, 15:48
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
actually, i was just giving an example.. that's not the real name of the file.. thanks for the suggestion in any case!
Reply With Quote
  #4 (permalink)  
Old 04-15-10, 16:22
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by db2user24 View Post
If I create the SQL function AND move the 'test' executable to /home/db2inst/sqllib/function, is there anything else that I need to do to successfully call the function? Is creating the function the same as registering it?
Yes. These two steps should be sufficient. I haven't worked with C UDFs for a while, but I have a vague memory of external names consisting of two parts: the object file name and the function name itself. In your case it would probably be 'test.so!testFunction'. Also, make sure test.so is executable by the fenced user.
Reply With Quote
  #5 (permalink)  
Old 04-15-10, 17:14
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Nick is right. Building an executable is not the right thing - you need a shared library. And you should specify the function name (with C linkage) to tell DB2 which function should be called. You may want to have a look at the samples in sqllib/samples/c/ and also the "bldrtn" script in that directory. (Although I wouldn't base anything serious on this script, it gives you a good indication on how to compile and link the library.)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 04-15-10, 18:02
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Thanks n_i and stolze.. I checked again and see the External name in the SQL file as --
EXTERNAL NAME 'c_time_diff!timediff_secs'

I looked at the C file and see timediff_secs as the function name in the C file and under /home/db2inst/sqllib/function I see the file named c_time_diff. Am I correct in assuming that c_time_diff is the shared library in this case?

Last edited by db2user24; 04-16-10 at 11:28.
Reply With Quote
  #7 (permalink)  
Old 04-16-10, 07:35
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Yes, that's correct. If you place the library under $HOME/sqllib/functions, you can just specify the base name. If you put the library somewhere else, you have to use the absolute path name so that DB2 knows where to find the lib.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 04-16-10, 08:14
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by db2user24 View Post
Am I correct in assuming that c_time_diff is the shared library in this case?
This is not something you should assume. If it was you who compiled and linked it, you should know precisely what it is. If it wasn't you, you can always run "file /home/nrg1/sqllib/function/c_time_diff" to verify.
Reply With Quote
  #9 (permalink)  
Old 04-16-10, 11:26
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
thank you n_i... I just ran the command that you suggested and I get this --

/home/db2inst/sqllib/function/c_time_diff: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On