我需要从 Ruby 连接到一个相当旧的 Sybase 数据库。
我最终使用了 jRuby,只是因为我无法通过任何方式从 MRI 连接到 Sybase 来工作;如果有一种方法确实可行,并且不会疯狂地为旧版本的 Sybase 设置 Sybase 连接,那就太好了。但是,我把头撞在墙上了一段时间,我认为没有。
所以我最终选择了 jRuby,以便使用 JDBC。我确实有一个可用于连接到 Sybase 的 JDBC.jar 文件。
我知道我可以使用 JDBC API 在 jRuby 中编写原始 JDBC 代码,就像在 Java 中一样。但是 JDBC API 很难使用,除此之外,它在转义/注入保护方面非常差(或者也许我只是不明白如何正确使用它,请参阅“难以使用”,至少对于我)。
是否有 Ruby“包装器”库可以在 jRuby 中作为任意 JDBC 的包装器工作?如果我能让它与 ActiveRecord 一起工作,那可能很酷,但实际上不一定是必需的。
我一直在使用 Ruby 'DBI' 包,它实际上运行得很好。但 Ruby DBI 包似乎没有维护,对我来说越来越难以使用;有一堆与使其在 1.9.3 下工作、消除弃用警告等相关的拉取请求 (https://github.com/erikh/ruby-dbi/issues)。提交者没有关注这些请求。我给 github 项目所有者发了一封电子邮件,询问他们是否有机会得到关注,但收到的答案是“否”,没有任何解释。不太确定发生了什么事。
那么,使用 jRuby JDBC 或其他方式从 Ruby 连接到 Sybase 有哪些选项?
(根据下面的要求,我会补充说我使用 RHEL 5 作为操作系统。但如果答案是“一旦安装了 FreeTDS,您就可以像这样执行 X、Y 和 Z”,我可能可以设法获得 FreeTDS过去,即使在主机上安装了 FreeTDS 之后,我也被难住了)。
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).
发布评论
评论(4)
我在 Rails 3/Sybase ASE 15 项目中使用了 JTDS jar 和
activerecord-jdbc-adapter
gem。我遇到的唯一问题是 Sybase 的限制和偏移量并没有得到真正的支持。我最终写了我自己的访客来进行限制和抵消工作。
您可能想看看这里: https://github.com/arkadiyk/ar- sybase-jdbc-适配器
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
这个问题似乎被放弃了,但我想记录一些成功。首先我想声明这适用于 Windows 和 Linux。
我目前正在使用 jruby 的 MSSQL v7 和 MSSQL v8 以及从 microsoft 下载的 JDBC 驱动程序发行版。
问题是,JDBC 3.0 和 4.0 驱动程序无法连接到 MSSQL v7。我很幸运地发现 JDBC 2.0 确实有效反对它。
我不确定OP对SQL注入的担忧是什么,但这些驱动程序能够创建准备好的语句(用于分区 SQL)和 可调用语句(对于 SP),可用于 安全地转义参数。
关于丑陋,是的,你是对的。这就是为什么需要一些代码:
并从单个 select 语句获取多个结果:
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:
and to get multiple results from a single select statement:
我用过FreeTDS 和 TinyTDS 通过 Sequel 以获取我们的一些 MSSQL 和 Sybase DBM。
它不像使用 MySQL 或 Postgres 那样优雅,但也是可行的。而且,Sequel 是一个非常好的非 Rails DB 访问工具。它非常强大且灵活。
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.
我整理了一个将 FreeTDS/TinyTDS/Sequel 与 SyBase 一起使用的简单示例(我们有 Sybase ASE 15.3)
请参阅
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