Skip to content

Commit

Permalink
Merge branch 'main' into hishamco/PdfMediaFileTextProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco authored Nov 7, 2024
2 parents e395d19 + d08a99d commit 39d2333
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public SqlGrammar() : base(false)

// Create Index.
orderList.Rule = MakePlusRule(orderList, comma, orderMember);
orderMember.Rule = Id + orderDirOptional;
orderMember.Rule = Id + (orderDirOptional | "(" + functionArguments + ")");
orderDirOptional.Rule = Empty | "ASC" | "DESC";

// Select stmt.
Expand Down
27 changes: 22 additions & 5 deletions src/OrchardCore.Modules/OrchardCore.Queries/Sql/SqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,37 @@ private void EvaluateOrderClause(ParseTreeNode parseTreeNode)
var idList = parseTreeNode.ChildNodes[2];

_modes.Push(FormattingModes.SelectClause);

for (var i = 0; i < idList.ChildNodes.Count; i++)
{
var id = idList.ChildNodes[i].ChildNodes[0];

if (i > 0)
{
_builder.Append(", ");
}

var id = idList.ChildNodes[i].ChildNodes[0];

// RANDOM() is a special case where we need to use the dialect's random function.
if (id.ChildNodes[0].Token != null && id.ChildNodes[0].Token.ValueString.Equals("RANDOM", StringComparison.OrdinalIgnoreCase))
{
var funArgs = idList.ChildNodes[i].ChildNodes[1].ChildNodes[0];

// "RANDOM" + {funArgs} + no arguments?
if (funArgs.Term.Name == "funArgs" && funArgs.ChildNodes.Count == 0)
{
_builder.Append(_dialect.RandomOrderByClause);

continue;
}
}

EvaluateId(id);

if (idList.ChildNodes[i].ChildNodes[1].ChildNodes.Count > 0)
var orderDirOpt = idList.ChildNodes[i].ChildNodes[1].ChildNodes[0];

if (orderDirOpt.Term.Name == "orderDirOpt" && orderDirOpt.ChildNodes.Count > 0)
{
_builder.Append(' ').Append(idList.ChildNodes[i].ChildNodes[1].ChildNodes[0].Term.Name);
_builder.Append(' ').Append(orderDirOpt.ChildNodes[0].Term.Name);
}
}

Expand Down Expand Up @@ -805,7 +822,7 @@ private void EvaluateOverClauseOptional(ParseTreeNode overClauseOpt)
var orderMember = orderList.ChildNodes[i];
var id = orderMember.ChildNodes[0];
EvaluateSelectId(id);
var orderDirOpt = orderMember.ChildNodes[1];
var orderDirOpt = orderMember.ChildNodes[1].ChildNodes[0];
if (orderDirOpt.ChildNodes.Count > 0)
{
_builder.Append(' ').Append(orderDirOpt.ChildNodes[0].Term.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public Query CreateQuery(ILuceneQueryService builder, LuceneQueryContext context
break;
case "lt":
lt = element.Value;
nodeKind = gt.GetValueKind();
nodeKind = lt.GetValueKind();
break;
case "lte":
lt = element.Value;
nodeKind = gt.GetValueKind();
nodeKind = lt.GetValueKind();
includeUpper = true;
break;
case "boost":
Expand Down
6 changes: 6 additions & 0 deletions src/docs/reference/modules/Queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ The SQL parser is also able to convert some specific functions to the intended d
| `year(_date_)` | Returns the years part of a date. |
| `now()` | Returns current date time (utc). |

Order By clauses can also use the `random()` function (case insensitive) to order results randomly, e.g.,

```
SELECT * FROM ContentItemIndex ORDER BY random()
```

## Scripting

The following JavaScript functions are available with this module.
Expand Down
15 changes: 15 additions & 0 deletions test/OrchardCore.Tests/Orchard.Queries/SqlParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,19 @@ public void ShouldParseSubquery(string sql, string expectedSql)
Assert.True(result);
Assert.Equal(expectedSql, FormatSql(rawQuery));
}

[Theory]
[InlineData("select a order by RANDOM()", "SELECT [a] ORDER BY newid();")]
[InlineData("select a order by random()", "SELECT [a] ORDER BY newid();")]
[InlineData("select a order by RANDOM", "SELECT [a] ORDER BY [RANDOM];")]
[InlineData("select a order by random", "SELECT [a] ORDER BY [random];")]
public void ShouldOrderByRandom(string sql, string expectedSql)
{
// Arrange & Act
var result = SqlParser.TryParse(sql, _schema, _defaultDialect, _defaultTablePrefix, null, out var rawQuery, out var errors);

// Assert
Assert.True(result);
Assert.Equal(expectedSql, FormatSql(rawQuery));
}
}

0 comments on commit 39d2333

Please sign in to comment.