用于包装旧版 SQL Server 数据库的最佳 Ruby ORM?

发布于 2024-08-25 01:44:34 字数 485 浏览 6 评论 0原文

我找到了这个答案,听起来几乎正是我的答案我正在做。关于 datamapper 是否可以通过数据对象支持 SQL Server,我听到了不同的答案。基本上,我们有一个应用程序,它在 SQL Server 中使用一致结构的数据库、一致命名的表等。我们正在制作各种必须与之交互的工具和东西,其中一些是远程的,所以我决定我们需要创建一些通用的、简单的访问点来在 SQL Server 应用程序上执行读/写操作,因为它是 API都是 C# 和其他我鄙视的东西。

现在我的问题是,是否有人知道任何示例或项目,其中 ruby​​ ORM 本质上可以通过定义每个模型的 pkeys、fkeys、表名等约定来为另一个应用程序的遗留数据库创建模型。Sequel 是我唯一的 ORM。曾经使用过 SQL Server,但从未做过类似这样的事情。有什么建议吗?

I found this answer and it sounds like almost exactly what I'm doing. I have heard mixed answers about whether or not datamapper can support SQL Server through dataobjects. Basically, we have an app that uses a consistently structured database, consistently named tables, etc in SQL Server. We're making all kinds of tools and stuff that have to interact with it, some of them remotely and so I decided that we need to create some common, simple access point to do read/write operations on the SQL Server app since it's API is all C# and other things I despise.

Now my question is if anyone has any examples or projects they know of where a ruby ORM can essentially create models for another application's legacy database by defining the conventions of each model's pkeys, fkeys, table names, etc. Sequel is the only ORM I've used with SQL Server but never to do anything quite like this. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

找回味觉 2024-09-01 01:44:35

Sequel 不会自动创建模型,但您可以编写一个扩展,获取 Sequel::Database#tables 和 Sequel::Database#schema (对于每个表)的输出,并对键和表名称进行一些分析以进行猜测在协会。 Sequel 默认情况下不这样做的原因是不可能在所有情况下都正确执行。但如果您的数据库始终遵循某些约定,那么您当然可以使其正常工作。

Sequel doesn't automatically create models, but you could write an extension that took the output of Sequel::Database#tables and Sequel::Database#schema (for each table), and did some analysis of the keys and table names to guess at associations. The reason Sequel doesn't do this by default is that it isn't possible to do correctly in all cases. But if your database always follows certain conventions, you could certainly get it to work.

千秋岁 2024-09-01 01:44:35

我无法给你一个完整的例子,因为我才开始涉足将 Ruby 与 MS SQL Server 一起使用,但根据我的经验和你所说的,我建议使用 Sequel 的模型工具。

Sequel 确实支持并针对 MS SQL Server 进行了测试,但使用 Active Record 时情况似乎不太明确。我没有使用过 DataMapper,所以无法对此发表评论。据我所知,Ruby 唯一的 ORM 是 DataMapper、Sequel、Active Record 2 和 Active Relation/Active Record 3(新的 Rails 3 数据访问工具)。

MS SQL 的 Active Record 适配器,但它没有MRI 1.8.7 完全为我工作。 Microsoft 的 IronRuby 团队正在使用此适配器的一个版本来运行 IronRuby on Rails 和 MS SQL Server,因此您可以考虑尝试在 IronRuby 上组合 Active Record。 IronRuby 目前处于候选发布阶段,因此这应该可以工作,尽管目前肯定还有很多错误修复正在进行。

I can't give you a fully worked example, since I've only started dabbling in using Ruby with MS SQL Server, but based on my experiences and what you've said, I would suggest using the model facility of Sequel.

Sequel definitely supports and is tested against MS SQL Server, but the situation seems a bit less clear cut with Active Record. I haven't used DataMapper, so can't comment on that. As far as I know, the only ORMs for Ruby are DataMapper, Sequel, Active Record 2 and Active Relation/Active Record 3 (the new Rails 3 data access stuff).

There is an Active Record adapter for MS SQL, but it didn't work for me at all with MRI 1.8.7. The IronRuby team at Microsoft are using a version of this adapter to run IronRuby on Rails with MS SQL Server, so you might consider trying an Active Record on IronRuby combination. IronRuby is currently at Release Candidate stage, so this should work, although there is definitely still a lot of bug fixing going on at the moment.

断爱 2024-09-01 01:44:35

ActiveRecord 是一个应该能够执行您所描述的操作的选项。

所以像这样的事情应该是可能的:

class Order < ActiveRecord::Base
  establish_connection { #a hash describing your SQL Server connection attributes }
  set_table_name 'PRODUCT_ORDER' # or whatever
  set_primary_key :PROD_ORD_ID
  has_many :order_lines
end

class OrderLine < ActiveRecord::Base
  establish_connection { #as above }
  set_table_name 'ORDER_LINE'
  belongs_to :order
  has_many :products # etc...
end

order = Order.first
order.order_lines.each { |line| puts line }

ActiveRecord is one option that ought to be able to do what you're describing.

So stuff like this ought to be possible:

class Order < ActiveRecord::Base
  establish_connection { #a hash describing your SQL Server connection attributes }
  set_table_name 'PRODUCT_ORDER' # or whatever
  set_primary_key :PROD_ORD_ID
  has_many :order_lines
end

class OrderLine < ActiveRecord::Base
  establish_connection { #as above }
  set_table_name 'ORDER_LINE'
  belongs_to :order
  has_many :products # etc...
end

order = Order.first
order.order_lines.each { |line| puts line }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文