📄 oraperl.pm
字号:
Example: while (($deptno, $dname, $loc) = &ora_fetch($csr)) { warn "Truncated!!!" if $ora_errno == 1406; # do something with the data } warn $ora_errstr if $ora_errno;This function is equivalent to the OCI ofetch function.=item * ora_close &ora_close($csr)If an SQL statement is no longer required (for example, all the dataselected has been processed, or no more rows are to be inserted) thenthe statement identifier should be released. This is done by callingthe &ora_close() function with the statement identifier as its onlyparameter.This function is equivalent to the OCI oclose function.B<DBD:> Since $csr is a Perl5 reference the statement/cursor is nowautomatically closed if $csr is overwritten or goes out of scope.=item * ora_do &ora_do($lda, $statement)Not all SQL statements return data or contain substitutionvariables. In these cases the &ora_do() function may beused as an alternative to &ora_open() and &ora_close().This function takes two parameters, a login identifier andthe statement to be executed.Example: &ora_do($lda, 'drop table employee');This function is roughly equivalent to &ora_close( &ora_open($lda, $statement) )B<DBD:> oraperl v2 used to return the string 'OK' to indicatesuccess with a zero numeric value. The Oraperl emulation nowuses the string '0E0' to achieve the same effect since it doesnot cause any C<-w> warnings when used in a numeric context.=item * ora_logoff &ora_logoff($lda)When the program no longer needs to access a given database, the loginidentifier should be released using the &ora_logoff() function.This function is equivalent to the OCI ologoff function.B<DBD:> Since $lda is a Perl5 reference the database login identifieris now automatically released if $lda is overwritten or goes out of scope.=back=head2 Ancillary FunctionsAdditional functions available are: &ora_titles(),&ora_lengths(), &ora_types(), &ora_autocommit(),&ora_commit(), &ora_rollback() and &ora_version().The first three are of most use within a program whichallows statements to be entered interactively. See, forexample, the sample program sql which is supplied withOraperl and may have been installed at your site.=over 2=item * ora_titles @titles = &ora_titles($csr)A program may determine the field titles of an executedquery by calling &ora_titles(). This function takes asingle parameter, a statement identifier (obtained from&ora_open()) indicating the query for which the titles arerequired. The titles are returned as an array of strings,one for each column.Titles are truncated to the length of the field, as reportedby the &ora_lengths() function.B<DBD:> oraperl v2.2 actually changed the behaviour such that thetitles were not truncated unless an optional second parameter wastrue. This was not reflected in the oraperl manual. The Oraperlemulation adopts the non truncating behaviour and doesn't support thetruncate parameter.=item * ora_lengths @lengths = &ora_lengths($csr)A program may determine the length of each of the fieldsreturned by a query by calling the &ora_lengths() function.This function takes a single parameter, a statementidentifier (obtained from &ora_open()) indicating the queryfor which the lengths are required. The lengths arereturned as an array of integers, one for each column.=item * ora_types @types = &ora_types($csr)A program may determine the type of each of the fields returned by aquery by calling the &ora_types() function. This function takes asingle parameter, a statement identifier (obtained from &ora_open())indicating the query for which the lengths are required. The types arereturned as an array of integers, one for each field.These types are defined in your OCI documentation. The correctinterpretation for Oracle v6 is given in the file oraperl.ph.=item * ora_autocommit &ora_autocommit($lda, $on_or_off)Autocommit mode (in which each transaction is committed immediately,without waiting for an explicit commit) may be enabled or disabledusing &ora_autocommit(). This function takes two parameters, a loginidentifier (obtained from &ora_login()) and a true/false valueindicating whether autocommit is to be enabled (non-zero) or disabled(zero). By default, autocommit is off.Note that autocommit can only be set per login, not per statement. Ifyou need to control autocommit by statement (for example, to allowdeletions to be rolled back, but insertions to be committedimmediately) you should make multiple calls to &ora_login() and use aseparate login identifier for each statement.=item * ora_commit, ora_rollback &ora_commit($lda) &ora_rollback($lda)Modifications to a database may be committed or rolled back using the&ora_commit() and &ora_rollback() functions. These functions take asingle parameter, a login identifier obtained from &ora_login().Transactions which have been committed (either explicitly by a call to&ora_commit() or implicitly through the use of &ora_autocommit())cannot be subsequently rolled back.Note that commit and rollback can only be used per login, not perstatement. If you need to commit or rollback by statement you shouldmake multiple calls to &ora_login() and use a separate login identifierfor each statement.=item * ora_version &ora_version()The &ora_version() function prints the version number andcopyright information concerning Oraperl. It also printsthe values of various compilation time options. It does notreturn any value, and should not normally be used in aprogram.Example: perl -MOraperl -e 'ora_version()' This is Oraperl, version 2, patch level 0. Debugging is available, including the -D flag. Default fetch row cache size is 5. Empty bind values are replaced by a space. Perl is copyright by Larry Wall; type oraperl -v for details. Additions for oraperl: Copyright 1991, 1992, Kevin Stock. Oraperl may be distributed under the same conditions as Perl.This function is the equivalent of Perl's C<-v> flag.B<DBD:> The Oraperl emulation printout is similar but not identical.=back=head1 VARIABLESSix special variables are provided, $ora_cache, $ora_long,$ora_trunc, $ora_errno, $ora_errstr and $ora_verno.=head2 Customisation VariablesThese variables are used to dictate the behaviour of Oraperlunder certain conditions.=over 2=item * $ora_cacheThe $ora_cache variable determines the default cache size used by the&ora_open() function for SELECT statements if an explicit cache size isnot given.It is initialised to the default value reported by &ora_version() butmay be set within a program to apply to all subsequent calls to&ora_open(). Cursors which are already open are not affected. Asdistributed, the default value is five, but may have been altered atyour installation.As a special case, assigning zero to $ora_cache resets it to thedefault value. Attempting to set $ora_cache to a negative value resultsin a warning.=item * $ora_longNormally, Oraperl interrogates the database to determine the length ofeach field and allocates buffer space accordingly. This is notpossible for fields of type LONG or LONGRAW. To allocate spaceaccording to the maximum possible length (65535 bytes) would obviouslybe extremely wasteful of memory.Therefore, when &ora_open() determines that a field is a LONG type, itallocates the amount of space indicated by the $ora_long variable. Thisis initially set to 80 (for compatibility with Oracle products) but maybe set within a program to whatever size is required.$ora_long is only used when fetching data, not when inserting it.=item * $ora_truncSince Oraperl cannot determine exactly the maximum length of a LONGfield, it is possible that the length indicated by $ora_long is notsufficient to store the data fetched. In such a case, the optionalsecond parameter to &ora_fetch() indicates whether the truncationshould be allowed or should provoke an error.If this second parameter is not specified, the value of $ora_trunc isused as a default. This only applies to LONG and LONGRAW data types.Truncation of a field of any other type is always considered an error(principally because it indicates a bug in Oraperl).=back=head2 Status VariablesThese variables report information about error conditions or aboutOraperl itself. They may only be read; a fatal error occurs if aprogram attempts to change them.=over 2=item * $ora_errno$ora_errno contains the Oracle error code provoked by the last functioncall.There are two cases of particular interest concerning &ora_fetch(). Ifa LONG or LONGRAW field is truncated (and truncation is allowed) then&ora_fetch() will complete successfully but $ora_errno will be set to1406 to indicate the truncation. When &ora_fetch() fails, $ora_errnowill be set to zero if this was due to the end of data or an error codeif it was due to an actual error.=item * $ora_errstrThe $ora_errstr variable contains the Oracle error messagecorresponding to the current value of $ora_errno.=item * $ora_vernoThe $ora_verno variable contains the version number of Oraperl in theform v.ppp where v is the major version number and ppp is thepatchlevel. For example, in Oraperl version 3, patch level 142,$ora_verno would contain the value 3.142 (more or less, allowing forfloating point error).=back=head1 SUBSTITUTION VARIABLESOraperl allows an SQL statement to contain substitution variables.These consist of a colon followed by a number. For example, a programwhich added records to a telephone list might use the following call to&ora_open(): $csr = &ora_open($csr, "insert into telno values(:1, :2)");The two names :1 and :2 are called substitution variables. Thefunction &ora_bind() is used to assign values to these variables. Forexample, the following statements would add two new people to thelist: &ora_bind($csr, "Annette", "472-8836"); &ora_bind($csr, "Brian", "937-1823");Note that the substitution variables must be assigned consecutivelybeginning from 1 for each SQL statement, as &ora_bind() assigns itsparameters in this order. Named substitution variables (for example,:NAME, :TELNO) are not permitted.B<DBD:> Substitution variables are now bound as type 1 (VARCHAR2)and not type 5 (STRING) by default. This can alter the behaviour ofSQL code which compares a char field with a substitution variable.See the String Comparison section in the Datatypes chapter of theOracle OCI manual for more details.You can work around this by using DBD::Oracle's ability to specifythe Oracle type to be used on a per field basis: $char_attrib = { ora_type => 5 }; # 5 = STRING (ala oraperl2.4) $csr = ora_open($dbh, "select foo from bar where x=:1 and y=:2"); $csr->bind_param(1, $value_x, $char_attrib); $csr->bind_param(2, $value_y, $char_attrib); ora_bind($csr); # bind with no parameters since we've done bind_param()'s=head1 DEBUGGINGB<DBD:> The Oraperl $ora_debug variable is not supported. Howeverdetailed debugging can be enabled at any time by executing $h->debug(2);where $h is either a $lda or a $csr. If debugging is enabled on an$lda then it is automatically passed on to any cursors returned by&ora_open().=head1 EXAMPLE format STDOUT_TOP = Name Phone ==== ===== . format STDOUT = @<<<<<<<<<< @>>>>>>>>>> $name, $phone . die "You should use oraperl, not perl\n" unless defined &ora_login; $ora_debug = shift if $ARGV[0] =~ /^\-#/; $lda = &ora_login('t', 'kstock', 'kstock') || die $ora_errstr; $csr = &ora_open($lda, 'select * from telno order by name') || die $ora_errstr; $nfields = &ora_fetch($csr); print "Query will return $nfields fields\n\n"; while (($name, $phone) = &ora_fetch($csr)) { write; } warn $ora_errstr if $ora_errno; die "fetch error: $ora_errstr" if $ora_errno; do ora_close($csr) || die "can't close cursor"; do ora_logoff($lda) || die "can't log off Oracle";=head1 NOTESIn keeping with the philosophy of Perl, there is no pre-defined limitto the number of simultaneous logins or SQL statements which may beactive, nor to the number of data fields which may be returned by aquery. The only limits are those imposed by the amount of memoryavailable, or by Oracle.=head1 WARNINGSThe Oraperl emulation software shares no code with the originaloraperl. It is built on top of the new Perl5 DBI and DBD::Oraclemodules. These modules are still evolving. (One of the goals ofthe Oraperl emulation software is to allow useful work to be donewith the DBI and DBD::Oracle modules whilst insulating users fromthe ongoing changes in their interfaces.)It is quite possible, indeed probable, that some differences inbehaviour will exist. These are probably confined to error handling.B<All> differences in behaviour which are not documented here should bereported to to dbi-users@perl.org.=head1 SEE ALSO=over 2=item Oracle DocumentationSQL Language Reference Manual.Programmer's Guide to the Oracle Call Interfaces.=item BooksProgramming Perl by Larry Wall and Randal Schwartz.Learning Perl by Randal Schwartz.=item Manual Pagesperl(1)=back=head1 AUTHOROriginal Oraperl 2.4 code and documentationby Kevin Stock <kstock@auspex.fr>.DBI and Oraperl emulation using DBD::Oracle by Tim Bunce.=head1 MAINTAINER As of DBD::Oracle release 1.17 in February 2006 The Pythian Group, Inc.(L<http://www.pythian.com>) are taking the lead in maintaining DBD::Oracle withmy assistance and gratitude.=head1 COPYRIGHTCopyright (c) 1994-2006 Tim Bunce. Ireland.The DBD::Oracle module is free open source software; you canredistribute it and/or modify it under the same terms as Perl 5.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -