SybSQL provides high level interfaces between Ruby and Sybase server. This class includes methods that connect to a server, and methods that send commands to a server, and methods that process the results.
Parameters:
Details for key-value pair
| Key | Value |
|---|---|
| 'S' | The name of the server to connect to. (required) |
| 'U' | The name used to log into the server.(required) |
| 'P' | The password used to log into the server. (the default is "" ) |
| 'lang' | A locale name which is registered in $SYBASE/locales/locales.dat |
| 'timeout' | The timeout value(seconds) for a server response. (the default is 0 , which represents an infinite timeout period.) |
| 'login-timeout' | The login timeout value(seconds) |
| 'appname' | The application name used when logging into the server |
| 'hostname' | The host machine name used when logging into the server |
| 'async' | In case of multiple threads application,
you must set this to true. (the default is false) See also SybContext.create. |
Examples:
query = SybSQL.new({'S'=>'SYBSRV01','U'=>'sa','P'=>'XXXXXX',
'lang'=>'ja_JP.ujis'} )
class MyContext < SybSQLContext
def srvmsgCB( con,msghash)
print "My server-callback\n"
end
end
query = SybSQL.new({'S'=>'SYBSRV01','U'=>'sa','P'=>'XXXXXX'}, MyContext)
If the force parameter is true, the connection is closed whether or not results are pending, and without notifying the server.
Parameters:
Examples:
query.sql('select * from sysservers')
raise "ERROR: select ..." unless (query.cmd_done? )
| cmd | SybCommand object |
| st | SybConstant::CS_SUCCEED or SybConstant::CS_ROW_FAIL |
| col | The array of column names |
| row | The array of row datas |
| true | The row's value is added to the result that the results method returns. |
| nil | The row's value is not added to the result that the results method returns. (When large number of rows are retrieved, this is effective in order to save memory) |
| false | Stop to iterates |
Examples:
query.sql("select title_id, notes from titles"){
|cm,s,c,r|
note = r[1]
# get first sentence
if( note =~ /^([^\.]*)\./io ) then
print r[0],"\t",$1,"\n"
end
nil # Not use a SybResult object
}
raise "ERROR: use DB" unless query.sql_norow('use master')
The following table lists correspondence with SyBase datatype and
Ruby datatype that this module handle.
| SyBase datatype | Ruby datatype |
|---|---|
| TINYINT | Integer |
| SMALLINT | Integer |
| INT | Integer |
| REAL | Float |
| FLOAT | Float |
| Others | String |
The parameter type indicates the type of result. the following table lists the possible value of type.
| CS_CMD_SUCCEED | The success of a command that returns no data |
| CS_CMD_DONE | The results of a command have been completely processed. |
| CS_CMD_FAIL | The server encountered an error while executing a command |
| CS_ROW_RESULT | Regular row results |
| CS_PARAM_RESULT | Parameter results |
| CS_STATUS_RESULT | Stored procedure return status results. |
| CS_COMPUTE_RESULT | Compute row results |
### Prints out two RowResults. #####
query.sql("select * from table1\n select * from table2")
raise "Failed" unless (query.cmd_done? )
raise "No results in table1" unless (res = query.nth_results(0, CS_ROW_RESULT) )
res.rows.each {
|r| print " #{r.join('|')}\n"
}
raise "No results in table2" unless (res = query.nth_results(1, CS_ROW_RESULT) )
res.rows.each {
|r| print " #{r.join('|')}\n"
}
This method is equivalent to
nth_results(0, SybConstant::CS_ROW_RESULT)
This method is equivalent to
nth_results(0, SybConstant::CS_PARAM_RESULT)
Parameters:
In initial condition, Data transfer size is set to 1024 bytes.
if the lg parameter is FALSE, then the server does not
update log
("select into/bulkcopy option" is necessary for the database
beforehand.)
Parameters:
| rowid | index of row data(start at 0) |
| row | The array of row datas |
| clmid | The column number (start at 0) |
| clm | The array of column names |
| data | String : A chunk of Image/Text data nil : It retrieved the last chunk of data false : Failed |
If error has occurred, then it raise a RuntimeError exception.
Examples
Retrieves image data from the au_pix table
query=SybSQL.new( {'S'=>'SYBASE', 'U'=>'sa', 'P'=>'XXXXXX'} )
raise "ERROR: use pubs2" unless( query.sql_norow("use pubs2") )
# Enlarges CS_OPT_TEXTSIZE
# (The maximum data size which the server should return)
unless( query.connection.setopt(CS_OPT_TEXTSIZE, 1024 * 1024)) then
$stderr.print("ERROR: setopt(CS_OPT_TEXTSIZE)\n");
end
file = File.open("486-29-1786.ras","w") # open the file
query.image_transize(8192) # set buffer size for transfer
imgsize = 0
sql = "select au_id ,pic from au_pix where au_id = '486-29-1786'"
query.sql_getimage(sql, 2){
|rid,r,cid,clm, data|
if( data.kind_of?(String) )then
file.write(data)
imgsize += data.length
elsif (data.nil?) then
print "End of data\n"
file.close
end
}
print "Size=#{imgsize}\n"
Parameters:
Parameters:
If error has occurred, then it raise a RuntimeError exception.
Parameters:
Examples
Inserts new Image data row to the au_pix table in the pubs2
database.
query=SybSQL.new( {'S'=>'SYBASE', 'U'=>'sa', 'P'=>'XXXXXX'} )
# Enable bulkcopy into the database
raise "ERROR: use master" unless( query.sql_norow("use master") )
query.sql("exec sp_dboption pubs2,'select into/bulkcopy',true")
raise "ERROR: sp_dboption" unless (query.cmd_done? )
# Check the return status.
raise "ERROR: sp_dboption" if( query.top_status_result != 0 )
# checkpoint
raise "ERROR: use pubs2" unless( query.sql_norow("use pubs2") )
raise "ERROR: checkpoint" unless( query.sql_norow("checkpoint") )
# Insert new row
sql = "insert au_pix (au_id,format_type ) values ('my-image-1', 'JPG')"
raise "ERROR: insert" unless( query.sql_norow(sql) )
# Update Image column to NULL. -- (required)
sql = 'update au_pix set pic=NULL where au_id = "my-image-1"'
raise "ERROR update" unless( query.sql_norow(sql) )
query.image_transize( 2048 ) # set buffer size for transfer
query.image_log( false ) # with no log
# Retrieves SybIODesc
sql = 'select au_id,pic from au_pix where au_id = "my-image-1"'
query.sql_iodesc(sql,2)
iodesc = query.top_row_result.nthrow(0,1)
raise "Cannot fetch IODESC" unless( iodesc.kind_of?(SybIODesc) )
# Send contents of my-image-1.jpg file to the server.
query.send_imagefile(iodesc, "my-image-1.jpg")
print "Success\n"
Parameters:
| cmd | SybCommand object |
| st | SybConstant::CS_SUCCEED or SybConstant::CS_ROW_FAIL |
| col | The array of column names |
| row | The array of row datas |
| true | The row's value is added to the result that the results method returns. |
| nil | The row's value is not added to the result that the results method returns. (When large number of rows are retrieved, this is effective in order to save memory) |
| false | Stop to iterates |
Examples:
See, sample/cursor_disp.rb and sample/cursor_update.rb