How to UpLoad the Diffent type of files Through Oracle Pl/Sql...?
orabitsDec 22 2005 — edited Dec 22 2005Hi TOM,
I want one reusable component to be developed, My requirement is as follows...
We get .CSV,.XML,.DOC.PDF files on the N/w drive. One Batch Job will be running every night and this job should upload all the files present in that N/w drive to the database (Each File should be stored in one row with unique ID) and after loading of each file is done , move the uploaded file to the backup directory.
Note : Average file size is 10-15 MB.
Loading of files in database should be done only using Oracle procedure/function.
Already return this process in Pro*C. But I don't know How to write in Oravle Pl/Sl...?
Functionality of this FUNCTION(In Pro*C):
===================================
*****set_item_blob
*****
*****Input:
*****======
*****1. Path of the file in Windows folder which contains the part number's content
*****2. Item number in the T_ITEM table
*****3. Revision level in the T_ITEM table
*****Processing:
*****===========
*****1. Get the BLOB pointer for the ITM_BLOB column,
***** for the corresponding item number and revision level
*****2. Update the ITM_BLOB column with the content of the input file
***** ( using the EXEC SQL LOB WRITE function )
*****Output:
*****=======
*****1. ITM_BLOB column updated with the content of input file
*********************************************************************************************/
int set_item_blob (char chr_item_number,int revision_level,char file_path)
{
exec sql begin declare section;
OCIBlobLocator *blob_locator;
varchar vc_item_number[12];
long file_length=0;
varchar alert_message [500+1];
int plsql_err_code = 0;
exec sql end declare section;
FILE *fp_input_file;
char *blob_buffer;
EXEC SQL VAR blob_buffer IS RAW(BUFFER_LENGTH);
EXEC SQL ALLOCATE :blob_locator;
memset ( vc_item_number.arr, '\0', 12 );
strcpy ( vc_item_number.arr, chr_item_number );
vc_item_number.len = strlen ( vc_item_number.arr );
fp_input_file = fopen( file_path, "rb" );
if( fp_input_file == NULL)
{
sprintf ( alert_message.arr, "ngetupld BLOB upload failed for item_number = [%s], rev_level = [%d]. Failure in opening the file to be uploaded [%s]", chr_item_number, revision_level , file_path );
alert_message.len = strlen ( alert_message.arr );
EXEC SQL EXECUTE
BEGIN
P_INSERT_INTO_INFO_MESSAGES('AL',:alert_message,'BLB',NULL,NULL,:plsql_err_code);
END;
END-EXEC;
exec sql commit;
return 1;
}
else
{
(void) fseek(fp_input_file, 0L, SEEK_END) ;
file_length = (unsigned int)ftell(fp_input_file) ;
(void) fseek(fp_input_file, 0L, SEEK_SET) ;
if ( file_length > BUFFER_LENGTH )
{
sprintf ( alert_message.arr, "ngetupld BLOB upload failed for item_number = [%s], rev_level = [%d]. Length of the file to be uploaded(%ld) is more than the supported length(%ld)", chr_item_number, revision_level , file_length, BUFFER_LENGTH );
alert_message.len = strlen ( alert_message.arr );
EXEC SQL EXECUTE
BEGIN
P_INSERT_INTO_INFO_MESSAGES('AL',:alert_message,'BLB',NULL,NULL,:plsql_err_code);
END;
END-EXEC;
exec sql commit;
return 1;
}
EXEC SQL
UPDATE T_ITEM
SET ITM_BLOB = EMPTY_BLOB()
WHERE ITM_NUMBER = :vc_item_number
AND ITM_REVISION_LEVEL = :revision_level
RETURNING ITM_BLOB INTO :blob_locator;
if ( sqlca.sqlcode != 0 )
{
sprintf ( alert_message.arr, "ngetupld BLOB upload failed for item_number = [%s], rev_level = [%d]. SQL error %d occured while trying to get the BLOB locator", chr_item_number, revision_level , sqlca.sqlcode );
alert_message.len = strlen ( alert_message.arr );
EXEC SQL EXECUTE
BEGIN
P_INSERT_INTO_INFO_MESSAGES('AL',:alert_message,'BLB',NULL,NULL,:plsql_err_code);
END;
END-EXEC;
exec sql commit;
return 1;
}
blob_buffer=(char *)malloc(BUFFER_LENGTH); // Dynamic Memory Allocation for Itm_Blob
fread((void *)blob_buffer, (size_t)BUFFER_LENGTH, (size_t)1, fp_input_file);
EXEC SQL LOB WRITE ONE :file_length FROM :blob_buffer INTO :blob_locator;
if ( sqlca.sqlcode != 0 )
{
sprintf ( alert_message.arr, "ngetupld BLOB upload failed for item_number = [%s], rev_level = [%d]. SQL error %d occured while trying to update the BLOB content", chr_item_number, revision_level , sqlca.sqlcode );
alert_message.len = strlen ( alert_message.arr );
EXEC SQL EXECUTE
BEGIN
P_INSERT_INTO_INFO_MESSAGES('AL',:alert_message,'BLB',NULL,NULL,:plsql_err_code);
END;
END-EXEC;
exec sql commit;
return 1;
}
exec sql commit;
}
fclose(fp_input_file);
free(blob_buffer);
return 0;
}
Can Possible to do in Oacle Pl/Sql...?