mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-08-20 08:36:05 +08:00
Incorrect NOLOCK position in queries where there is no table alias (#8783)
"WITH(NOLOCK)" is positioned before the where clause if there is no alias for the table.
This commit is contained in:
parent
441c6da145
commit
e243e8e547
@ -42,7 +42,7 @@ namespace Orchard.Data {
|
|||||||
|
|
||||||
return _shellSettings.DataTablePrefix + "_" + tableName;
|
return _shellSettings.DataTablePrefix + "_" + tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// based on https://stackoverflow.com/a/39518098/2669614
|
// based on https://stackoverflow.com/a/39518098/2669614
|
||||||
public override SqlString OnPrepareStatement(SqlString sql) {
|
public override SqlString OnPrepareStatement(SqlString sql) {
|
||||||
// only work on select queries
|
// only work on select queries
|
||||||
@ -167,7 +167,7 @@ namespace Orchard.Data {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// rebuild query
|
// rebuild query
|
||||||
for(int i = 0; i < affectedCaptures.Count; i++) {
|
for (int i = 0; i < affectedCaptures.Count; i++) {
|
||||||
var inner = affectedCaptures[i];
|
var inner = affectedCaptures[i];
|
||||||
for (int j = i + 1; j < affectedCaptures.Count; j++) {
|
for (int j = i + 1; j < affectedCaptures.Count; j++) {
|
||||||
var outer = affectedCaptures[j];
|
var outer = affectedCaptures[j];
|
||||||
@ -203,7 +203,7 @@ namespace Orchard.Data {
|
|||||||
public bool IsAltered { get; set; }
|
public bool IsAltered { get; set; }
|
||||||
|
|
||||||
public void AddNoLockHints() {
|
public void AddNoLockHints() {
|
||||||
Value = AddNoLockHints(Value, TableNames);
|
Value = AddNoLockHints(Value, TableNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string AddNoLockHints(string query, IEnumerable<string> tableNames) {
|
private string AddNoLockHints(string query, IEnumerable<string> tableNames) {
|
||||||
@ -240,13 +240,19 @@ namespace Orchard.Data {
|
|||||||
if (tableIndex == fromIndex + 1
|
if (tableIndex == fromIndex + 1
|
||||||
|| parts[tableIndex - 1].Equals(",")) {
|
|| parts[tableIndex - 1].Equals(",")) {
|
||||||
|
|
||||||
parts.Insert(tableIndex + 2, "WITH(NOLOCK)");
|
if (parts[tableIndex + 1].Equals("where", StringComparison.OrdinalIgnoreCase)) {
|
||||||
|
// There is no alias in the query, so we add "WITH(NOLOCK)" immediately after table name but before the "where" clause.
|
||||||
|
parts.Insert(tableIndex + 1, "WITH(NOLOCK)");
|
||||||
|
} else {
|
||||||
|
// We add "WITH(NOLOCK)" after the table alias.
|
||||||
|
parts.Insert(tableIndex + 2, "WITH(NOLOCK)");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// probably doing a join, so edit the next "on" and make it
|
// probably doing a join, so edit the next "on" and make it
|
||||||
// "WITH (NOLOCK) on"
|
// "WITH (NOLOCK) on"
|
||||||
for (int i = tableIndex + 1; i < whereIndex; i++) {
|
for (int i = tableIndex + 1; i < whereIndex; i++) {
|
||||||
if (parts[i].Trim().Equals("WITH(NOLOCK)", StringComparison.OrdinalIgnoreCase)) {
|
if (parts[i].Trim().Equals("WITH(NOLOCK)", StringComparison.OrdinalIgnoreCase)) {
|
||||||
// we processed this table anme already
|
// we processed this table name already.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (parts[i].Trim().Equals("on", StringComparison.OrdinalIgnoreCase)) {
|
if (parts[i].Trim().Equals("on", StringComparison.OrdinalIgnoreCase)) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user