From 8232f00cc48fa469532239f79585519cb79067bb Mon Sep 17 00:00:00 2001 From: Ben Thompson Date: Fri, 25 Apr 2014 13:31:35 -0400 Subject: [PATCH] Ensure consistent results when using limit and offset. --- lib/orm/source/xt/javascript/data.sql | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/orm/source/xt/javascript/data.sql b/lib/orm/source/xt/javascript/data.sql index 0bf7ff94f..857ee0e43 100644 --- a/lib/orm/source/xt/javascript/data.sql +++ b/lib/orm/source/xt/javascript/data.sql @@ -276,8 +276,8 @@ select xt.install_js('XT','Data','xtuple', $$ plv8.elog(ERROR, 'Attribute not found in object map: ' + param.attribute); } - identifiers.push(pertinentExtension.isChild || pertinentExtension.isExtension ? - "jt" + (joins.length - 1) : + identifiers.push(pertinentExtension.isChild || pertinentExtension.isExtension ? + "jt" + (joins.length - 1) : "t1"); identifiers.push(prop.attr.column); pgType = this.getPgTypeFromOrmType( @@ -402,8 +402,8 @@ select xt.install_js('XT','Data','xtuple', $$ plv8.elog(ERROR, 'Attribute not found in object map: ' + param.attribute[c]); } - identifiers.push(pertinentExtension.isChild || pertinentExtension.isExtension ? - "jt" + (joins.length - 1) : + identifiers.push(pertinentExtension.isChild || pertinentExtension.isExtension ? + "jt" + (joins.length - 1) : "t1"); identifiers.push(prop.attr.column); @@ -514,7 +514,7 @@ select xt.install_js('XT','Data','xtuple', $$ /* We might need to look at toOne if the client is asking for a toOne without specifying the path. Unfortunately, if they do specify the path, then sql2 will fail. So this does - work, although we're really sorting by the primary key of the toOne, whereas the + work, although we're really sorting by the primary key of the toOne, whereas the user probably wants us to sort by the natural key TODO */ orderByColumnIdentifiers.push(prop.attr ? prop.attr.column : prop.toOne.column); @@ -1907,6 +1907,13 @@ select xt.install_js('XT','Data','xtuple', $$ return ret; } + /* Because we query views of views, you can get inconsistent results */ + /* when doing limit and offest queries without an order by. Add a default. */ + if (limit && offset && !orderBy.length && !clause.orderByColumns) { + /* We only want this on sql1, not sql2's clause.orderBy. */ + clause.orderByColumns = XT.format('order by t1.%1$I', [pkeyColumn]); + } + /* Query the model. */ sql1 = XT.format(sql1, [tableNamespace.decamelize(), table.decamelize(), pkeyColumn]); sql1 = sql1.replace('{joins}', clause.joins) -- 2.39.2