The general usage format for both Sybase::DBlib and Sybase::CTlib is this:
The DBPROCESS or CS_CONNECTION that is opened with the call to new() is automatically closed when the $dbh goes out of scope:
Package global attributes can be set using the %Att hash table in either modules. The %Att variable is not exported, so it must be fully qualified:
NOTE: setting an attribute via the %Att variable does NOT change the status of currently allocated database handles.
In this version, the available attributes for a $dbh are set when the $dbh is created. You can't add arbitrary attributes during the life of the $dbh . This has been done to implement a stricter behavior and to catch attribute errors.
It is possible to add your own attributes to a $dbh at creation time. The Sybase::BCP module adds two attributes to the normal Sybase::DBlib attribute set by passing an additional attribute variable to the Sybase::DBlib new() call:
Please see the discussion on Special handling of DATETIME, MONEY & NUMERIC/DECIMAL values below for details.
You should if possible have the Sybase Open Client documentation available when writing Sybperl programs.
The API calls that have been implemented use the same calling sequence as their C equivalents, with a couple of exceptions, detailed below.
Please see also Common Sybase::DBlib and Sybase::CTlib routines below.
List of API calls
Standard Routines:
Both forms of the call are identical.
This call can be used multiple times if connecting to multiple servers with different username/password combinations is required, for example.
The additional attributes parameter allows you to define application specific attributes that you wish to associate with the $dbh .
When the results row is a COMPUTE row, the ComputeID field of the DBPROCESS is set:
dbnextrow() can also return a hash keyed on the column name:
See also dbclropt() and dbisopt() below.
The second argument to dbsafestr() (normally DBSINGLE, DBDOUBLE or DBBOTH) has been replaced with a literal ' or " (meaning DBSINGLE or DBDOUBLE, respectively). Omitting this argument means DBBOTH.
The calling sequence is a little different from the C version, and logging is off by default:
$dbh_2 and $colnum are the DBPROCESS and column number of a currently active query. Example:
It is necessary to call BCP_SETL(TRUE) before opening the connection with which one wants to run a BCP IN operation.
See EXAMPLES, below.
In the second form you pass an array reference instead of passing the LIST, which makes processing a little bit faster on wide tables.
The routines which in the C version take pointers to arguments (in order to return values) return these values in an array instead:
NOTE: All floating point types (MONEY, FLOAT, REAL, DECIMAL, etc.) are converted to FLOAT before being sent to the RPC.
NOTE: This call executes both dbrpcsend() and dbsqlok(). You can call $dbh ->dbresults direcly after calling $dbh ->dbrpcsend.
NOTE: These routines have not been thouroughly tested!
If you provide a second parameter it is taken as a procedure to call for each row. The callback is called with the values of the row as parameters.
If you provide a third parameter, this is used in the call to dbnextrow() to retrieve associative arrays rather than 'normal' arrays for each row, and store them in the returned array. To pass the third parameter without passing the &rowcallback value you should pass the special value undef as second parameter:
See also eg/sql.pl for an example.
Contributed by Gisle Aas.
NOTE: This routine loads all the data into memory. It should not be run with a query that returns a large number of rows. To avoid the risk of overflowing memory, you can limit the number of rows that the query returns by setting the 'MaxRows' field of the $dbh attribute field:
This value is not set by default.
and to set a connection specific attribute you would code
NOTE!!! Global attribute setting changes do not affect existing connections, and changing an attribute inside a ct_fetch() does not change the behaviour of the data retrieval during that ct_fetch() loop.
The following attributes are currently defined:
The following message handler differentiates the SHOWPLAN or STATICSTICS messages from other messages:
This could then be used like this:
Et voila!
The sybperl 1.0xx man page is included in this package in pod/sybperl-1.0xx.man
Sybase::Sybperl is layered on top of the Sybase::DBlib package, and could therefore suffer a small performance penalty.
Additional attributes can be set by passing a hash reference.
Return values: CS_SUCCEED, CS_FAIL or CS_CANCELED (the operation was canceled).
NOTE: ct_execute() is equivalent to calling ct_command() followed by ct_send().
NOTE: You should only need to call ct_command()/ct_send() directly if you want to do RPCs or cursor operations. For straight queries you should use ct_execute() or ct_sql() instead.
NOTE: You only need to call ct_send() directly if you've used ct_command() to set up your SQL query.
The commonly used values for $res_type are CS_ROW_RESULT, CS_CMD_DONE, CS_CMD_SUCCEED, CS_COMPUTE_RESULT, CS_CMD_FAIL. The full list of values is on page 3-203 OpenClient reference manual.
See also the description of ct_fetchable() below.
The $status value takes the following values: CS_SUCCEED, CS_END_RESULTS, CS_FAIL, CS_CANCELED.
You could use it like this:
The STATUS field is a bitmask which can be tested for the following values: CS_CANBENULL, CS_HIDDEN, CS_IDENTITY, CS_KEY, CS_VERSION_KEY, CS_TIMESTAMP and CS_UPDATEABLE. See table 3-46 of the Open Client Client Library Reference Manual for a description of each of these values.
An empty array is returned if there is no data to fetch.
Values for $action: CS_SET, CS_GET, CS_CLEAR
Values for $option: see p.3-170 of the OpenClient reference manual
Values for $param: When setting an option, $param can be a integer or a string. When retrieving an option, $param is set and returned. When clearing an option, $param is ignored.
Value for $type: CS_INT_TYPE if $param is of integer type, CS_CHAR_TYPE if $param is a string
See eg/ct_cursor.pl for an example.
The hash should be used like this:
Note that ct_param() converts all parameter types to either CS_CHAR_TYPE, CS_FLOAT_TYPE, CS_DATETIME_TYPE, CS_MONEY_TYPE or CS_INT_TYPE.
See eg/ct_param.pl for an example.
The attributes values are copied from $dbh to $dbh2 .
$property is one of the properties that is settable via ct_config() (see your OpenClient man page on ct_config() for a complete list).
$value is the input value if $action is <B>CS_GET</B>, and the output value if $action is <B>CS_GET</B>.
$ret is the return status of the ct_config() call.
Example:
If the $doAssoc parameter is CS_TRUE, then each row is a reference to an associative array (keyed on the column names) rather than a normal array (see ct_fetch(), above).
If you provide a second parameter it is taken as a procedure to call for each row. The callback is called with the values of the row as parameters.
This routine is very usefull to send SQL commands to the server that do not return rows, such as:
Examples can be found in eg/ct_sql.pl.
NOTE: This routine loads all the data into memory. Memory consumption can therefore become quite important for a query that returns a large number of rows, unless the MaxRows attribute has been set.
Two additional attributes are set after calling ct_sql(): ROW_COUNT holds the number of rows affected by the command, and RC holds the return code of the last call to ct_execute().
and to set a connection specific attribute you would code
NOTE!!! Global attribute setting changes do not affect existing connections, and changing an attribute inside a ct_fetch() does not change the behaviour of the data retrieval during that ct_fetch() loop.
The following attributes are currently defined:
The traces are pretty obscure, but they can be useful when trying to find out what is really going on inside the program.
For the TRACE_* flags to be available in your scripts, you must load the Sybase::??lib module with the following syntax:
This tells the autoloading mechanism to import all the default symbols, plus all the trace symbols.
The Sybase::CTlib and Sybase::DBlib modules include special features to handle DATETIME, MONEY, and NUMERIC/DECIMAL (CTlib only) values in their native formats correctly. What this means is that when you retrieve a date using ct_fetch() or dbnextrow() it is not converted to a string, but kept in the internal format used by the Sybase libraries. You can then manipulate this date as you see fit, and in particular 'crack' the date into it's components.
The same is true for MONEY (and for CTlib NUMERIC values), which otherwise are converted to floating point values, and hence are subject to loss of precision in certain situations. Here they are stored as MONEY values, and by using operator overloading we can give you intuitive access to the cs_calc()/dbmnyxxx() routines.
This feature has been implemented by creating new classes in both Sybase::DBlib and Sybase::CTlib: Sybase::DBlib::DateTime, Sybase::DBlib::Money, Sybase::CTlib::DateTime, Sybase::CTlib::Money and Sybase::CTlib::Numeric (hereafter referred to as DateTime, Money and Numeric). All the examples below use the CTlib module. The syntax is identical for the DBlib module, except that the Numeric class does not exist. To create data items of these types you call:
The DateTime class defines the following methods:
NOTE: Not implemented in DBlib.
will print the date string correctly, and
will do a comparison of the two dates, not the two strings.
crack executes cs_dt_crack()/dbdatecrack() on the date value, and returns the following list:
Compare this with the value returned by the standard Perl function localtime():
In addition, the values returned for the week_day can change depending on the locale that has been set.
Please see the discussion on cs_dt_crack() or dbdatecrack() in the Open Client / Open Server Common Libraries Reference Manual, chap. 2.
The Money and Numeric classes define these methods
CAVEAT! You must call the set method to assign a value to a Money/Numeric data item. If you use
then $mny will loose its special Money or Numeric behavior and become a normal Perl data item.
When a new Numeric data item is created, the SCALE and PRECISION values are determined by the initialization. If the data item is created as part of a SELECT statement, then the SCALE and PRECISION values will be those of the retrieved item. If the item is created via the newnumeric method (either explicitly or implicitly) the SCALE and PRECISION are deduced from the initializing value. For example, $num = $dbh ->newnumeric(11.111) will produce an item with a SCALE of 3 and a PRECISION of 5. This is totally transparent to the user.
Tim Bunce & Andreas Koenig - for all the work on MakeMaker
Dave Bowen & Amy Lin for help with Sybase::CTlib.
Jeffrey Wong for the Sybase::DBlib DBMONEY routines.
Numerous folks have contributed ideas and bug fixes for which they have my undying thanks :-)
The sybperl mailing list <sybperl-l@trln.lib.unc.edu> is the best place to ask questions.