Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Passing a file into DBD-Oracle

by TJRandall (Sexton)
on Feb 11, 2011 at 19:39 UTC ( [id://887666]=perlquestion: print w/replies, xml ) Need Help??

TJRandall has asked for the wisdom of the Perl Monks concerning the following question:

I need to pass a physical file( or the contents of that file), that contains code to create an Oracle stored procedure into DBD-Oracle. So like SQL PLus, where I'd say  SQL> @package.spb, I have been trying to do all sorts of things with $DBH->prepare / do / etc. As of right now, this is not working:
# load file contents open (FILEDATA, "$sql"); while ($record = <FILEDATA>) { print $record; } close(FILEDATA); # prepare the stored procedure if ($sth = $DBH->prepare( $record )){ # execute the stored procedure if ($sth->execute()){ print $LOG "it made it: $HoH{$filename {'full_filename'} \n"; + $HoH{$filename}{'build_state'} = 'COMPILED'; } else { # update the hash, since we will not be loading this one print $LOG "couldnt excute: $HoH{$filename {'full_filename'} \n"; + $HoH{$filename}{'build_state'} = 'FAILED - COULD NOT EXECUTE'; + } } else { # update the hash, since we will not be loading this one print $LOG "couldnt prepare: $HoH{$filename {'full_filename'} \n"; + $HoH{$filename}{'build_state'} = 'FAILED - COULD NOT PREPARE'; } # preparing sql
When executed, I get: DBD::Oracle::db prepare failed: ORA-24373: invalid length specified for statement (DBD ERROR: OCIStmtPrepare) at C:\Tronweb\TW_AUTOMATED_BUILDS\scripts\release_cvs_to_dev.pl line 261. This file that I'm trying to prepare is small, and I've even set up my Oracle session as:
#Open an Oracle session $DBH = DBI->connect( "dbi:Oracle:$sid", $usr, $pwd ) or die "Unable to connect to $sid: $DBI::errstr"; $DBH->{RaiseError} = 1; $DBH->{LongReadLen} = 5242880; $DBH->{LongTruncOk} = 0;
Do you have any suggestions for this dilemma? THANK YOU for any advice / direction that you can provide!

Replies are listed 'Best First'.
Re: Passing a file into DBD-Oracle
by roboticus (Chancellor) on Feb 11, 2011 at 20:46 UTC

    TJRandall:

    Your file read loop is keeping only the last line of the file, is that what you want? You're trying to pass undef (the value of $record) to the prepare subroutine, which just won't work. If not, p Perhaps you'd do better with:

    open (FILEDATA, "$sql"); my $record = join("",<FILEDATA>); close(FILEDATA); print $record;

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

    Update: Struck out the incorrect statement (nice catch, bart!) and added the bit between them.

      You're kinda right when you say the loop will not keep the contents of the file, except it won't even contain the contents of the last line of the file, because $record will be undef after the entire file is read, and the loop is left.

      And the title of this question is misleading, he doesn't want Oracle to use the file name; instead he wants and needs to send the contents of the file.

      I'm not sure what happens if you ask Oracle to execute the contents of an entire file in one go. You'll like have to split into single statements, and it's not clear how you send the source code for a package or package body. The invocation command "/" which is put after the end of a package or package body actually means "repeat the last command". How do you do the same through DBI? I have no idea.

        Overall, what I'm trying to do is to automate Oracle code builds. I check out the files locally, and then I load up a hash of hashes to keep track of what I'm loading, the compiled state, etc. So one of the key:value pairs for a named piece of code is 'full_name':'c:\..\..\..'. The files that I am working on contain Oracle functions, procedures, packages, etc.

        I didn't mean to be mis-leading in the Subject line - I would either (1) pass the actual file into DBI (like SQL Plus) or (2) read in the file contents, and then prepare() that. I just can't seem to get either to work. I will work on the suggestion from roboticus (thank you very much!) It didn't work on the first pass, but I will focus on the  undef issue. Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://887666]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 18:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found