Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

ambition: hard-coded MySQL syntax in WhereProcessor methods

#96

The SQL generated by the WhereProcessor has MySQL syntax baked in. So far, I've noticed backquotes to escape column names and the REGEX operator.

The methods in ActiveRecord::ConnectionAdapters::Quoting should take care of the quoting variations, but there's nothing native in ActiveRecord to handle db-specific regex operators.

Reported by Matthew King · August 30th, 2007 @ 08:01 AM

State: resolved
Milestone: none
Assigned to: Chris Wanstrath Chris Wanstrath

Activity

  1. Matthew King
    Matthew King
    • Title changed from Hard-coded MySQL syntax in WhereProcessor methods to ambition: hard-coded MySQL syntax in WhereProcessor methods

    August 30th, 2007 @ 08:19 AM

  2. Chris Wanstrath
    Chris Wanstrath
    • State changed from new to open

    I thought RLIKE was MySQL-specific and REGEXP was SQL standard. Am I getting it backwards?

    Yes, Ambition was written for MySQL and ActiveRecord. But I don't want it to stay that way. Any help on general compatibility would be great.

    So, backticks are a no-go? I don't like them that much, anyway.

    August 30th, 2007 @ 10:56 AM

  3. Matthew King
    Matthew King

    Here's a patch that adds a quote_column_name facade for the ActiveRecord connection to Processor, which is then used in WhereProcessor#translate.

    The patch has no tests, because I can't figure out a way to test without using a live ActiveRecord connnection.

    I will gladly add some tests, if someone can give me a clue on how to mock or stub or prestidigitate an AR connection, so that I can access the adapter-specific quoting methods.

    August 30th, 2007 @ 10:59 AM

  4. Matthew King
    Matthew King

    Regarding REGEXP and SQL standards, I dunno. I haven't been using postgres long, and I have to look everything up anyway. Speaking of which, the PostgreSQL manual has a table showing regex operators:

    http://www.postgresql.org/docs/8...

    It should be easy to add a clumsy helper method that picks a regex operator based on the AR::Base.connection adapter, but that seems ugly.

    August 30th, 2007 @ 11:27 AM

  5. Chris Wanstrath
    Chris Wanstrath

    I think having database-specific translators maybe the only way. Since that requires some architecture re-thinking, I may take that on later tonight sometime. Suck.

    I added the column_name thing. I need to improve to_sql generation -- right now it's not matching the actual SQL called. Note to self.

    http://projects.require.errthebl...

    August 30th, 2007 @ 12:29 PM

  6. Matthew King
    Matthew King

    Re: database-specific translators for regexp

    How bad of an idea is this?

    Monkeypatch each ActiveRecord adapter with regexp-producing methods, then call them in WhereProcessor#process_match3 using:

    ActiveRecord::Base.connection.regexp(value)
    

    or a suitable helper method.

    August 30th, 2007 @ 06:59 PM

  7. Matthew King
    Matthew King

    I'm uploading a meaty patch (with tests!) that implements the idea in my last comment. I.e., it monkeypatches the Abstract, MySQL, and PostgreSQL adapters with a method named ambition_regexp. It also adds a method named adapter_regexp to Processor and slightly smartens the rescue clauses in the quoting methods. The results are not as ugly as I had feared.

    In where_test.rb, I added contexts for MySQL, PostgreSQL, and an adapter that doesn't get monkeypatched (so I can test the Abstract adapter). It turns out that you can fake a new PostgreSQL connection by passing it strings for args, and you can fake a MySQL connection by overriding its connect method and passing strings for args. This appears to be sufficient to test the helper methods in these classes, namely the quoting.

    August 31st, 2007 @ 10:06 AM

  8. Chris Wanstrath
    Chris Wanstrath

    Do you have AIM or hang out on IRC? I'm doing some refactoring today and want to talk to you about this patch / concept.

    September 1st, 2007 @ 03:17 PM

  9. Matthew King
    Matthew King

    I usually don't do the chatty things, but I can on weekdays. On weekends, my real boss requests that I keep computer work to a minimum.

    September 1st, 2007 @ 05:12 PM

  10. Chris Wanstrath
    Chris Wanstrath
    • State changed from open to resolved

    I took a slightly different approach, but the idea is the same. Should be fixed now:

    http://projects.require.errthebl...

    The statement method opens the door for future DB-specific stuffs, too (one hopes).

    September 2nd, 2007 @ 12:38 PM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.