Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How to deal with database specific queries

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1 2
Go to End
Author Topic: 32937 Views
  • xeraa
    Avatar
    Google Summer of Code Hacker
    58 Posts

    Re: How to deal with database specific queries Link to this post

    After working on persistence issues and doing some documentation, I'm finally back to this thread ;-).

    I tried to document the syntactic basics Brian is referring to here: http://doc.silverstripe.com/doku.php?id=sql-queries - comments are of course very welcome.

    Now to the thing to be used instead of the original switch: Would the following be what you intend? So you can (optionally) use version numbers and there's the option "other", so you won't have to duplicate queries (I wouldn't use compliant as that is sometimes not true, even if it is working across two databases).

    <?php

    DB::switched_query(array(
    "mysql" => array(
    4.0.2 => "<query1>",
    5.0.3 => "<query2>",
    ),
    "pgsql" => "<query3>",
    "mssql" => "<query4>"
    ));

    DB::switched_query(array(
    "mssql" => array(
    2000 => "<query1>",
    2005 => "<query2>",
    ),
    "other" => "<query3>"
    ));

    DB::switched_query(array(
    "pgsql" => "<query1>",
    "other" => "<query2>"
    ));

    ?>

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: How to deal with database specific queries Link to this post

    hey cool documentation, very comprehensive!

    i like the implementation of version-numbers, its unobtrusive for developers, but available if needed. how about optionally accepting query-objects?

    DB::switched_query(array(
    "mysql" => array(
    4.0.2 => $mysqlQueryObj,
    5.0.3 => "<query2>",
    ),
    "default" => "<query3>"
    ));


    also, i think "default" is more accepted as a name for the fallback-query than "other" - its not really important, but easier to recognize IMO.

  • Sigurd
    Avatar
    Forum Moderator
    628 Posts

    Re: How to deal with database specific queries Link to this post

    Cool, well done!
    I agree with Ingo; version numbers are nice, and 'default' is a good self-explanatory phrase.

  • Anonymous user
    Avatar
    22 Posts

    Re: How to deal with database specific queries Link to this post

    Yes, I agree with Ingo, and this looks good, but please ensure the following cases are covered (referring to the first example in this thread):

    if mysql = 4.0.1 we should throw an error (NOTE: I'm pointing out the implication in the example above that 4.0.2 is implied as a minimum version number needed. If we want to indicate that "we don't care" about version number for a particular query, we should probably have a "default" query that can be specified on a database-specific level. That means mysql would have 4.0.2, 5.0.3, and default queries as array elements.)

    if mysql = 5.0.1 we should use the query specified in the 4.0.2 array element.

    cheers,

    Brian

  • xeraa
    Avatar
    Google Summer of Code Hacker
    58 Posts

    Re: How to deal with database specific queries Link to this post

    To finish this off, I just did the documentation for this at http://doc.silverstripe.com/doku.php?id=database-support - hope it's how you expected it...

    32937 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.