Installing the sqlclient_adapter plugin

Posted by Ceaser Larry on September 8, 2006

I’ve changed the sqlclient_adapter to work as a Rails plugin. I also bundled a modified version of RubyCLR so installation should be easier.

To install just open a command prompt and change the working directory to the root of your rails application and type.

ruby script\plugin install

Then add the following line to the config\enviroment.rb file

require File.expand_path("#{RAILS_ROOT}/vendor/plugins/sqlclient_adapter/init.rb")

just after

require File.join(File.dirname(__FILE__), 'boot')

Then changed the adapter in the database.yml file to “sqlclient”

The modifications I made to RubyCLR are related to the way it handles string marshaling. When strings are passed from the CLR to Ruby, RubyCLR converts them to basic single byte C strings using the StringToHGlobalAnsi marshaling function. This corrupts any multi-byte string you want to work with.

If you use it’s sibling StringToHGlobalUni, which doesn’t modify the string, you get a string with 2 bytes for every character. RubyCLR will not work at all with this behavior since it relies on strings to represents meta-data between the two environments.

I basically made a quick hack between the two and went with UTF8 encoding. But after thinking about it for a while, I think string conversation behavior should be a user-defined option for the current version of ruby, sort of like Ruby’s $KCODE variable.

Finally, there are two known issues. It still only works with Rails v1.0 and UTF8 strings are not being decoded as they are passed from Ruby to the CLR.

Comments Archive

  1. John Lam said 1 day later:

    I’m working on fixing all of the string-related metadata stuff, which is why RubyCLR is lying in twisted little pieces on my computer right now.

    The string encoding problem is a real hard one to figure out (and I’m no expert on this by any means). If someone passes a UTF-8 string to Ruby that isn’t ASCII, and you try to modify that string in Ruby you’ll get undefined behavior (probably a crash).

    I need to think about how to let you use CLR strings to represent data in addition to using Ruby strings. But that’s a project that’s a bit further down the road.

    Interface identity is still broken in RubyCLR (which you discovered in your other post). I had to do this now 3-week long refactoring project so that I can get a clean enough reflection API to fix interface identity correctly.

    Hang in there!

  2. Sam Smoot said about 1 month later:

    I’ve conquered the Unicode/UTF8 stuff in my mssqlclient adapter. It’s a C++/CLI bridge to ADO.NET based largely on John’s work in the Helpers, but minus lots of two-way stuff.

    I’d appreciate any opinions you have on it.

    I agree the 50MB is pretty excessive, but at this point I’ve decided I can live with it if I can produce some working code.

  3. Ceaser said about 1 month later:

    I plan on checking it out, either this week or the next. I’m pretty excited, hopefully I can say, “Yes, Ruby plays nicely with SQL Server”, when I’m asked.