I have a ruby script which I run using the JRuby Interpreter. The script connects to a Sybase database using DBI and Sybase JDBC (jTDS3.jar and jconn3.jar)
My problem is that I have a select query that alters the column names of table. For example:
SELECT
t.TRANSACTION as 'business_transaction',
t.TRADE_CURRENCY as 'currency',
t.CURRENCY as 'settlement_currency'
...etc...
FROM
TRADE t
...etc...
My problem is when using the examples directly from the documentation
sth = dbh.execute(stmt)
printf "Number of rows: %d\n", rows.size
printf "Number of columns: %d\n", sth.column_names.size
sth.column_info.each_with_index do |info, i|
printf "--- Column %d (%s) ---\n", i, info["name"]
end
or simply
sth = dbh.execute(stmt)
rows = sth.fetch_all
col_names = sth.column_names
sth.finish
DBI::Utils::TableFormatter.ascii(col_names, rows)
Not ALL the names come out as I set them using the 'as' clause in the query. Some are the original field names and some are the names I have specified.
For example they will list like:
--- Column 0 (TRANSACTION) ---
--- Column 1 (TRADE_CURRENCY) ---
--- Column 2 (settlement_currency) ---
or
TRANSACTION
TRADE_CURRENCY
settlement_currency
When testing this in Squirrel SQL Client the columns are correctly named so is this a bug in DBI or the Sybase JDBC drivers? or am I doing something wrong?
Any help would be greatly appreciated
I would guess that it's a bug in DBI, since the JDBC drivers have presumably been put through the mill for years. You may want to contact DBI folks to see if they have a suggestion.
I can confirm it is the Sybase drivers. Using the jTDS (v1.2.5) from http://jtds.sourceforge.net/ I can get all the column names correctly defined in my query and can confirm that the original issue is NOT DBI
If anyone who is following this wondered how I got jtds working with DBI under jRuby please take a look at one of my former questions - it did take some time - and DBI is a little funny when specifying the URL use
dbi:Jdbc:jtds:sybase://<host>:<port>/<db>
Please note the capital J for Jdbc
I hope this saves someone a lot of time ;-)
Sybase 6.0 JDBC drivers has some "interesting" behavior dealing with aliases. The resultSet.findColumn method will fail on a table column name lookup if an alias is defined.
There are some properties you can set on the connect to change some of these behaviors or just use the JTDS drivers.
来源:https://stackoverflow.com/questions/1530329/jrruby-sybase-jdbc-and-dbi-fetching-column-name-with-the-as-clause-issue