One more C tip with Oracle on Linux ....

Coloque aqui tutoriais (por enquanto, sobre qualquer assunto relacionado a Oracle) e apostilas.
Post Reply
kim_kaphwan
Rank: Estagiário Pleno
Rank: Estagiário Pleno
Posts: 6
Joined: Fri, 04 Jun 2004 8:54 am
Location: sp

You can create c-functions libraries and use them within Oracle. The procedure is very simple. In the example, the library has only one function that converts a lowercase string:

lib.c:

Select all

 
#include <stdlib.h> 
#include <string.h> 
                                                                                 
char *converte( char *text ) { 
          int i; 
    for (i=0; text[i]!='\0'; i++) { 
           text[i]=tolower(text[i]); 
    } 
    return text; 
} 
to compile, we use The GCC:

Select all

gcc -c lib.c
The GCC will generate the lib.o object. Then we just create the .so with the command:

Select all

make -f $ORACLE_HOME/rdbms/demo/demo_rdbms.mk extproc_callback \ 
          SHARED_LIBNAME=lib.so \ 
            OBJS=lib.o
We will then have a LInux lib called lib.so. We need to move it to a path where Oracle finds you, for example $ oracle_home / lib / or $ oracle_home / bin /

The library is ready, now we only need to show Oracle as there. In SQLPlus we create Bilbioteca like this:

Select all

sql> CREATE OR REPLACE LIBRARY lib AS '/oracle/9.2.0/lib/lib.so'  
sql> / 
Replace the path /oracle/9.2.0/lib/ for the full path from where you put your lib. To create a function to access the functions of our lib, we use the following SQL:

Select all

       
sql> CREATE OR REPLACE FUNCTION "CONVERTE" (cstr VARCHAR2) 
2  return varchar2 as external 
3  library lib 
4  name "converte" 
5  language c 
6  parameters  (cstr string); 
The name converts is the name of the function. In case we declare to as a function that receives a varchar2 converted into string C (cstr) and returns a varchar2. We used Bilitheca created previously LIB and through the Name Parameter "Converts", we defined that we want to use the converts from that library.
The Language Parameter Defined the language in which this function was written and parameters parameters indicates what type of data oracle must send to it, in the case a string C (cstr).

To use our new function, we can make a simple test within SQLPlus:
SQL without the function:

Select all

SQL> select status from v$session where status='ACTIVE'; 
 
STATUS 
-------- 
ACTIVE 
ACTIVE 
ACTIVE 
ACTIVE 
ACTIVE 
ACTIVE 
ACTIVE 
ACTIVE 
ACTIVE 
ACTIVE 
ACTIVE 
 
11 rows selected. 
 
SQL com a função:  
 
SQL> select converte(status) from v$session where status='ACTIVE'; 
 
CONVERTE(STATUS) 
 
active 
active 
active 
active 
active 
active 
active 
active 
active 
active 
active 
 
11 rows selected. 
Well .. That's it ..: D
User avatar
adrianoturbo
Moderador
Moderador
Posts: 393
Joined: Thu, 20 Mar 2008 4:09 pm
Location: Brasília

Brother, do you have the library to connect the seat to C?
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 13 guests