In this blog, I would demonestrate how to write an external procedure in C and build as a Shared Object(*.so) and publish the same as an Oracle Library which can be called as PL/SQL Stored Procedure or Function in our programs.
I am defining a function in C to Reverse a String. The signature is given below.
This function taken a String argument and return the reverse of the String.
Following are the Steps to Define the Library.
Create an Header file to Include the Signature of the Function. I have named this file as OracleLibraryExample.h.
Create OracleLibraryExample.c File and define the Function reverseString.
Compile the *.c Files and Make it as Shared Object files(.so) files.
Login to Oracle Server machine through Unix Terminal.
export the ORACLE_HOME,ORACLE_SID,LDD_LIBRARY_PATH if it is not set already.
Create a new Schema say "Zenith" to create & test the Shared libraries.
Grant necessary privileges to "Zenith" such as CREATE PROCEDURE, EXECUTE PROCEDURE and CREATE LIBRARY etc.
Create a Library in this Schema.
Publish the Function and Create a Wrapper to Call the C routine.
Stop the Listener.
Edit the listener.ora file in the directory $ORACLE_HOME/network/admin.
Edit the tnsnames.ora files. Add the following.
Start the listener by executing lsnrctl start.
Start sqlplus and check whether library is working.
If the library is accessing through SQL Developer you might get the below error.
To rectify this error, you need to set an enironment variable in your local machine as shown below.
Now start the SQL Developer and execute the function, you will get the expected result.
Wow !!! Thats amazing..
I am defining a function in C to Reverse a String. The signature is given below.
- char *reverseString(char *);
This function taken a String argument and return the reverse of the String.
Following are the Steps to Define the Library.
- #ifndef _ORACLE_LIBRARY_EXAMPLE_
- #define _ORACLE_LIBRARY_EXAMPLE_
- extern char *reverseString(char *);
- #endif
- #include "OracleLibraryExample.h"
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- char *reverseString(char *text) {
- int length=strlen(text);
- int i=0;
- int i_idx;
- char *tmpStr=malloc(length*sizeof(char *));
- printf("Size of the String %d\n",length);
- printf("String :%s\n",text);
- for(i_idx=length-1;i_idx>=0;i_idx--) {
- *(tmpStr+i++)=text[i_idx];
- }
- *(tmpStr+i)='\0';
- return tmpStr;
- }
Compile the *.c Files and Make it as Shared Object files(.so) files.
- gcc -c -Wall -Werror -fpic OracleLibraryExample.c
- gcc -shared -o libOracleLibraryExample.so OracleLibraryExample.o
- This will generate a Shared Library (.so) file(libOracleLibraryExample.so)
- CREATE OR REPLACE LIBRARY lib_string_function_sample AS '/home/oracle/libTest/libOracleLibraryExample.so';
Publish the Function and Create a Wrapper to Call the C routine.
- CREATE OR REPLACE FUNCTION reverse_string(text varchar2)
- RETURN varchar2
- AUTHID DEFINER
- AS LANGUAGE C
- LIBRARY lib_string_function_sample
- NAME "reverseString"
- parameters(text string);
- /
- EXT_LISTENER =
- (DESCRIPTION_LIST =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = IPC)
- (KEY = EXTPROC1523)
- )
- )
- )
- )
- SID_LIST_EXT_LISTENER =
- (SID_LIST =
- (SID_DESC =
- (SID_NAME = PLSExtProc)
- (ORACLE_HOME = /opt/ora12c/product/12.1.0/dbhome_1 )
- (PROGRAM = extproc)
- (ENVS="EXTPROC_DLLS=ONLY:/home/oracle/libTest/libOraclePrintString.so:/home/oracle/libTest/libOracleLibraryExample.so,LD_LIBRARY_PATH=/opt/ora12c/product/12.1.0/dbhome_1/lib")
- )
- )
- Save & Exit
- EXTPROC_CONNECTION_DATA =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = IPC)
- (KEY = EXTPROC1523)
- )
- )
- (CONNECT_DATA =
- (SID = PLSExtProc)
- (PRESENTATION = RO)
- )
- )
Start the listener by executing lsnrctl start.
- export LD_LIBRARY_PATH=$LIBRARY_PATH:/home/oracle/libTest
- SQL> SELECT reverse_string('Hello Welcome to Oracle Library') FROM dual;
- REVERSE_STRING('HELLOWELCOMETOORACLELIBRARY')
- --------------------------------------------------------------------------------
- yrarbiL elcarO ot emocleW olleH
- ORA-28595: Extproc agent : Invalid DLL Path
- 28595. 00000 - "Extproc agent : Invalid DLL Path"
- *Cause: The path of DLL supplied for the extproc execution is invalid.
- *Action: Check if the DLL path is set properly using the EXTPROC_DLLS
- environment variable.
To rectify this error, you need to set an enironment variable in your local machine as shown below.
- SET EXTPROC_DLLS=ONLY:/home/oracle/libTest/libOraclePrintString.so:/home/oracle/libTest/libOracleLibraryExample.so
Now start the SQL Developer and execute the function, you will get the expected result.
Wow !!! Thats amazing..
No comments:
Post a Comment