Skip to content

Support for Microsoft SQL Server style "SELECT TOP 5..." version of the limit clause - #37

Closed
swieton wants to merge 4 commits into
j4mie:masterfrom
swieton:mssql
Closed

swieton wants to merge 4 commits into
j4mie:masterfrom
swieton:mssql

Conversation

@swieton

@swieton swieton commented Jun 6, 2011

Copy link
Copy Markdown

This quick patch will allow Idiorm to generate correct limit clauses for SQL Server. If it detects the driver is "mssql" or "sqlsrv", or alternately the "limit_clause_style" configuration option is set, it will generate a statement like "SELECT TOP 5 * FROM widgets" instead of the more traditional "SELECT * FROM widgets LIMIT 5".

This is by no means a complete implementation of a SQL generator for MSSQL - but it's a start and allows basic functionality. Notably missing is support for the "OFFSET" clause, which is not handled at all (that is, it will generate invalid SQL and the driver will throw an exception in that case).

Also included are updates to the test framework that uses a different mock for the DummyPDO class. By using a new class instead of inheriting from the PDO base class, it's easy to fake things like the driver it claims to be using. This required a little bit more code and less flexibility in the mock, but it does allow us to test the different SQL generation for MSSQL (which includes the differing quote character which does not appear to have been tested previously.

@jameslporter

Copy link
Copy Markdown

I was about to write a patch for this very thing! I reviewed swieton's changes and they look good, please pull!

Comment thread idiorm.php

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think even the sql srv driver needs top_N style...eg some paris:

$debt = Model::factory('Debt')->find_one(653178);
$accounts = $debt->account()->find_many();

the error:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client
10.0][SQL Server]Incorrect syntax near 'LIMIT'.' in C:\Users\jporter\Desktop\agency_import2\idiorm\idiorm.php:966
Stack trace:
#0 C:\Users\jporter\Desktop\agency_import2\idiorm\idiorm.php(966): PDOStatement->execute(Array)
#1 C:\Users\jporter\Desktop\agency_import2\idiorm\idiorm.php(362): ORM->_run()
#2 C:\Users\jporter\Desktop\agency_import2\paris\paris.php(115): ORM->find_one(653178)
#3 C:\Users\jporter\Desktop\agency_import2\take2.php(13): ORMWrapper->find_one(653178)
#4 {main}
thrown in C:\Users\jporter\Desktop\agency_import2\idiorm\idiorm.php on line 966
PS C:\Users\jporter\Desktop\agency_import2>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Jim,

I'm glad to hear my work is useful to someone!

What's the issue you're having? Is it simply that we need another
option in that switch statement? Or is it something more significant
missing?

Thanks!
Mike

On Mon, Dec 5, 2011 at 10:22 AM, Jim Porter
reply@reply.github.com
wrote:

@@ -232,6 +249,20 @@
         }

         /**

  •         * Returns a constant for the "limit clause style" for MS Sql Server
  •         * versus grown up databases.
  •         */
  •        protected static function _detect_limit_clause_style() {
  •            switch(self::$_db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
  •                case 'sqlsrv':

I think even the sql srv driver needs top_N style...eg some paris:

$debt = Model::factory('Debt')->find_one(653178);
$accounts = $debt->account()->find_many();

the error:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client
10.0][SQL Server]Incorrect syntax near 'LIMIT'.' in C:\Users\jporter\Desktop\agency_import2\idiorm\idiorm.php:966
Stack trace:
#0 C:\Users\jporter\Desktop\agency_import2\idiorm\idiorm.php(966): PDOStatement->execute(Array)
#1 C:\Users\jporter\Desktop\agency_import2\idiorm\idiorm.php(362): ORM->_run()
#2 C:\Users\jporter\Desktop\agency_import2\paris\paris.php(115): ORM->find_one(653178)
#3 C:\Users\jporter\Desktop\agency_import2\take2.php(13): ORMWrapper->find_one(653178)
#4 {main}
 thrown in C:\Users\jporter\Desktop\agency_import2\idiorm\idiorm.php on line 966
PS C:\Users\jporter\Desktop\agency_import2>


Reply to this email directly or view it on GitHub:
https://github.com/j4mie/idiorm/pull/37/files#r269955

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I got it working once I merged your branch in. Now I'm just trying to figure out how I can get the types of relationships I need.

debt has one account
account has no debt_id

so in Debt extends Model function account(){ return $this->has_one('Account'); }

But every way I try it won't create the correct query. I need for the debt object to fetch an account where debt->account_id == account->id

I've been fiddling with various ways of tricking paris into using a different foreign key.

So recap:

$debt = Model::factory('Debt')->find_one(653178);
//i has a debt!! it has properties!
$debt->account()->find_one();
// no worky, it's trying to find an account with "debt_id" column on account table.

@schmod

schmod commented May 18, 2012

Copy link
Copy Markdown

+1 This looks good, and should be merged.

@ghost ghost assigned treffynnon Nov 9, 2012
@treffynnon

Copy link
Copy Markdown
Collaborator

I am really not keen on including database specific switches into the core library. It seems like this patch is necessary with no cross compatible way of handling query limiting.

I don't run MS SQL so can you guys who are please feedback on this patch and the comments in the diff? I will remove it from the 1.2.0 milestone for now.

Have you tested this with real tables @schmod ?

@treffynnon

Copy link
Copy Markdown
Collaborator

This pull request has now slipped again to the 1.4.0 release. Please can someone running MS SQL comment on this patch.

This patch will be closed (without merge) in 2013 if there are no comments or further requests for this feature.

@treffynnon

Copy link
Copy Markdown
Collaborator

Thank you for the pull request, but unfortunately in this case I am going to be closing it without merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants