sybase from ruby: JDBC connection to Sybase - ruby

I need to connect to a fairly old Sybase database from Ruby.
I ended up using jRuby, only because I couldn't get any manner of connecting to Sybase from MRI to work; If there's a way that actually works and isn't crazy to set up a Sybase connection for an old-ish version of Sybase, that'd be great. But, I beat my head against the wall on that one for a while, I think there is not.
So I wound up in jRuby, in order to use JDBC. I do have a JDBC.jar file that works to connect to Sybase.
I know I can write raw JDBC code in jRuby, just as I would in Java, using the JDBC API. But the JDBC API is such a bear to work with, among other things being very poor at escaping/injection-protecting (or maybe I just don't understand how to use it right, see 'difficult to work with', at least for me).
Are there Ruby 'wrapper' libraries that will work in jRuby as a wrapper for an arbitrary JDBC? If I could get it to work with ActiveRecord, that might be cool, but not neccesarily actually required.
I had been using the Ruby 'DBI' package, which was actually working great. But the Ruby DBI package seems to be unmaintained and is becoming increasingly difficult for me to use; there are a bunch of pull requests (https://github.com/erikh/ruby-dbi/issues) related to making it work under 1.9.3, silencing deprecation warnings, etc. Which are not being attended to by committers. I emailed the github project owner to ask if there was any chance of them being attended to, and received a one-word answer "No", with no explanation. Not really sure what's going on.
So, what are my options for connecting to Sybase from Ruby, with jRuby JDBC or other?
(Per demand below, I'll add that I use RHEL 5 as an OS. But if the answer is "once you get FreeTDS installed, you can do X, Y, and Z like this", I can probably manage to get FreeTDS installed myself. In the past, I've gotten stumped even AFTER getting FreeTDS installed on the host).

I used JTDS jar and activerecord-jdbc-adapter gem for my Rails 3/Sybase ASE 15 project.
The only problem I had is limit and offset for Sybase are not really supported. I end up writing my own visitors to make limit and offset work.
You may want to take a look here: https://github.com/arkadiyk/ar-sybase-jdbc-adapter

This question seems to be abandoned, but I'd like to log some success. I'd like to start with stating that this works on windows and linux.
I am currently using MSSQL v7 and MSSQL v8 from jruby with the JDBC driver distributables I downloaded from microsoft.
The catch is, JDBC 3.0 and 4.0 drivers are unable to connect to MSSQL v7. I was very lucky to find out JDBC 2.0 actually worked against it.
I'm not sure what the OP's concern was about SQL injection, but these drivers are capable of creating Prepared Statements (for partitioned SQL) and Callable Statements (for SPs) which can be used to escape parameters safely.
About the ugliness, yes you're right. That's why a bit of code is needed:
class Java::ComMicrosoftSqlserverJdbc::SQLServerResultSet
def to_a
#I used meta_data here to convert resultset to array of hashes
end
end
and to get multiple results from a single select statement:
yap= connection.create_statement
rowing= yap.execute sql
row_the_boat= []
while rowing
row_the_boat<< yap.get_result_set.to_a
rowing= yap.get_more_results
end

I have used FreeTDS and TinyTDS on Linux via Sequel to get to some of our MSSQL and Sybase DBMs.
It wasn't as elegant as using MySQL or Postgres, but was doable. And, Sequel is a very good tool for non-Rails DB access. It's very powerful and flexible.

I put together a simple example of using FreeTDS/TinyTDS/Sequel with SyBase (we have Sybase ASE 15.3)
See the horizon-tds-example GitHub/repo for a Dockerfile
#<!-- language: rb -->
require 'dotenv/load'
require 'sequel'
require 'tiny_tds'
require 'awesome_print'
opts = {
adapter: 'tinytds',
login_timeout: 5,
timeout: 10,
tds_version: '50', #42 or 50
host: ENV['APP_HZ_HOST'],
port: ENV['APP_HZ_PORT'],
database: ENV['APP_HZ_DATABASE'],
username: ENV['APP_HZ_USER'],
password: ENV['APP_HZ_PASSWORD']
}
bib_id = Integer( ARGV[0] || 0 )
tag_num = ARGV[1] || ''
sql = "SELECT * FROM [bib] WHERE bib#=? AND tag=?"
DB = Sequel.connect opts
dataset = DB[sql, bib_id, tag_num]
dataset.each do |row|
ap row
end

Related

How to connect to Oracle through LDAP with Ruby 2.7?

My application used to query an Oracle database through a classical OCI connection string such as 'user/password#server/database'.
But the server architecture has changed so that connections should now go through LDAP for security reasons. The URL looks like this :
jdbc:oracle:thin:#ldap://intranet.oid-01.dama.ch:3063/EDWHPD,cn=OracleContext,dc=emea,dc=dama,dc=ch
I don't know how to handle this, and it raises several questions for me:
Would the old DBI gem be relevant here?
Should I use the Net-LDAP gem, and what would it bring to help establishing the connection to oracle?
Do you know a tutorial explaining how to handle this type of database access?
Thank you for your help!

Does ruby sequel support the JSON datatype which Mysql 5.7 introduced?

It does not appear the ruby based sequel toolkit (currently at v4.32.0 I believe) supports the JSON datatype which mysql introduced with v5.7. Can somebody confirm that is true and suggests workarounds for migrations and queries? Also whether there are plans to introduce it.
Sequel doesn't currently support MySQL's JSON type.
There aren't any near-future plans to implement support for it, though something like a mysql_json extension should be fairly easy to support on the MySQL adapter. The mysql2 adapter would be trickier as it does all of its own typecasting.

How do I do a SQLite to PostgreSQL migration?

I have a problem with migrating my SQLite3 database to PostgreSQL. How and what do I need to do?
I am searching the internet, but find only migrations from MySQL to PostgreSQL.
Can anyone help me?
I need to convert my SQLite database to PostgreSQL database for Heroku cloud hosting.
You don't want to try to do a binary conversion.
Instead, rely on exporting the data, then importing it, or use the query language of both and using selects and inserts.
I HIGHLY recommend you look at Sequel. It's a great ORM, that makes switching between DBMs very easy.
Read through the opening page and you'll get the idea. Follow that by reading through the cheat sheet and the rest of the documentation and you'll quickly see how easy and flexible it is to use.
Read about migrations in Sequel. They're akin to migrations in Rails, and make it very easy to develop a schema and maintain it across various systems.
Sequel makes it easy to open and read the SQLite3 table, and concurrently open a PostgreSQL database and write to it. For instance, this is a slightly modified version of the first two lines of the "cheat sheet":
SQLITE_DB = Sequel.sqlite('my_blog.db')
PGSQL_DB = Sequel.connect('postgres://user:password#localhost/my_db')
Base all your subsequent interactions with either database using SQLITE_DB and PGSQL_DB and you'll be on your way to porting the data.
The author of Sequel is very responsive and is a big fan of PostgreSQL, so the ORM has great integration with all its features.

accessing a database from ruby using an odbc driver (Remedy)

I have the Remedy odbc driver installed on my machine. I am able to pull down data from Remedy via excel just fine specifying this driver in my connection.
Is there any way I could use this driver from within Ruby / Ruby on rails to connect to and read from the DB?
Many thanks!
I have not found a way to use the Remedy ODBC driver from Ruby. Also, I am suspecting that using the driver which seems to be quite antiquated and is already throwing some arcane errors when used from Excel might not be simple to use.
I was able to connect through R via RODBC and thus an R script that would pull the data for you and write to CSV might be a solution.
What I am actually using - which to my mind is the simplest way of achieving this - is talking directly to the Remedy SQL backend. All that you will have to do is parse the data upon pulling it from DB which is very easy to set up (priorities, service types, statuses are stored as integers and not actual names and timestamps are in epoch format).

Connecting Ruby to MSSQL server without DBI

Is there a way to connect ruby to mssql without using DBI? Is there any native Ruby TDS lib that handles that?
FreeTDS comes to mind, however I have not used it.
Java TDS to MSSQL.
Ruby ODBC or Java ODBC
SOAP using TDS endpoints.
The MSSQL DBI is by far the better out of the lot of them. However I guess if you are running Linux and trying to connect to MSSQL, then try "FreeTDS". There are heaps of tut's and sites to show how to setup and configure for all flavours of Linux.
I've published an example application how to connect to an MS SQL Server with ruby, you can check it here.
It mask use of activerecord-sqlserver-adapter to allow ORM.

Resources