Skip to content

Commit

Permalink
use different limit / offset method when joins are being used
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Aug 10, 2024
1 parent e790270 commit 4aa69ce
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/db/utils/SqlUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,40 @@ class SqlUtils {
if (query != null) {
sql += '\nWHERE (${Query.queryExprToSql(query, values, table.name)})';
}
if (limit != null) {
sql += ' LIMIT ' + limit;
}
if (offset != null) {
sql += ' OFFSET ' + offset;

var hasJoins = (relationships != null);
if (hasJoins) {
var tableDef = databaseSchema.findTable(table.name);
if (tableDef != null) {
var primaryKeyColumns = tableDef.findPrimaryKeyColumns();
if (primaryKeyColumns != null && primaryKeyColumns.length > 0) {
if (limit != null || offset != null) {
if (query == null) {
sql += '\nWHERE ';
} else {
sql += ' AND\n';
}
var tableName = table.name;
var primaryKeyName = primaryKeyColumns[0].name;
sql += '$tableName.$primaryKeyName IN (\n';
sql += ' SELECT $primaryKeyName from $tableName\n';
if (limit != null) {
sql += ' LIMIT $limit\n';
}
if (offset != null) {
sql += ' OFFSET $offset\n';
}
sql += ')';
}
}
}
} else {
if (limit != null) {
sql += ' LIMIT ' + limit;
}
if (offset != null) {
sql += ' OFFSET ' + offset;
}
}
sql += ';';
return sql;
Expand Down

0 comments on commit 4aa69ce

Please sign in to comment.