Added support for With No Lock

This commit is contained in:
2023-09-11 13:29:01 +02:00
parent 9364a45561
commit 8519ca3a13
8 changed files with 243 additions and 27 deletions

View File

@@ -43,6 +43,9 @@ namespace DynamORM.Builders
/// <summary>Gets table alias.</summary>
string Alias { get; }
/// <summary>Gets table no lock status.</summary>
bool NoLock { get; }
/// <summary>Gets table schema.</summary>
Dictionary<string, DynamicSchemaColumn> Schema { get; }
}

View File

@@ -86,12 +86,14 @@ namespace DynamORM.Builders.Implementation
/// <param name="name">The name of table.</param>
/// <param name="alias">The table alias.</param>
/// <param name="owner">The table owner.</param>
public TableInfo(DynamicDatabase db, string name, string alias = null, string owner = null)
/// <param name="nolock">The table should be used with nolock.</param>
public TableInfo(DynamicDatabase db, string name, string alias = null, string owner = null, bool nolock = false)
: this()
{
Name = name;
Alias = alias;
Owner = owner;
NoLock = nolock;
if (!name.ContainsAny(StringExtensions.InvalidMemberChars))
Schema = db.GetSchema(name, owner: owner);
@@ -104,7 +106,8 @@ namespace DynamORM.Builders.Implementation
/// <param name="type">The type which can be mapped to database.</param>
/// <param name="alias">The table alias.</param>
/// <param name="owner">The table owner.</param>
public TableInfo(DynamicDatabase db, Type type, string alias = null, string owner = null)
/// <param name="nolock">The table should be used with nolock.</param>
public TableInfo(DynamicDatabase db, Type type, string alias = null, string owner = null, bool nolock = false)
: this()
{
DynamicTypeMap mapper = DynamicMapperCache.GetMapper(type);
@@ -114,6 +117,7 @@ namespace DynamORM.Builders.Implementation
Owner = (mapper.Table != null) ? mapper.Table.Owner : owner;
Alias = alias;
NoLock = nolock;
Schema = db.GetSchema(type);
}
@@ -127,6 +131,9 @@ namespace DynamORM.Builders.Implementation
/// <summary>Gets or sets table alias.</summary>
public string Alias { get; internal set; }
/// <summary>Gets or sets table alias.</summary>
public bool NoLock { get; internal set; }
/// <summary>Gets or sets table schema.</summary>
public Dictionary<string, DynamicSchemaColumn> Schema { get; internal set; }
@@ -235,6 +242,7 @@ namespace DynamORM.Builders.Implementation
Database.AddToCache(this);
SupportSchema = (db.Options & DynamicDatabaseOptions.SupportSchema) == DynamicDatabaseOptions.SupportSchema;
SupportNoLock = (db.Options & DynamicDatabaseOptions.SupportNoLock) == DynamicDatabaseOptions.SupportNoLock;
}
/// <summary>Initializes a new instance of the <see cref="DynamicQueryBuilder"/> class.</summary>
@@ -283,6 +291,9 @@ namespace DynamORM.Builders.Implementation
/// <summary>Gets a value indicating whether database supports standard schema.</summary>
public bool SupportSchema { get; private set; }
/// <summary>Gets a value indicating whether database supports with no lock syntax.</summary>
public bool SupportNoLock { get; private set; }
/// <summary>
/// Generates the text this command will execute against the underlying database.
/// </summary>
@@ -725,6 +736,15 @@ namespace DynamORM.Builders.Implementation
item = item.Validated("Alias"); // Intercepting null and empty aliases
return string.Format("{0} AS {1}", parent, item);
case "NOLOCK":
if (!SupportNoLock)
return parent;
if (node.Arguments != null && node.Arguments.Length > 1)
throw new ArgumentException("NOLOCK method expects no arguments.");
return string.Format("{0} {1}", parent, "WITH(NOLOCK)");
case "COUNT":
if (node.Arguments != null && node.Arguments.Length > 1)
throw new ArgumentException("COUNT method expects one or none argument: " + node.Arguments.Sketch());

View File

@@ -346,6 +346,7 @@ namespace DynamORM.Builders.Implementation
string owner = null;
string main = null;
string alias = null;
bool nolock = false;
Type type = null;
while (true)
@@ -369,6 +370,20 @@ namespace DynamORM.Builders.Implementation
node = node.Host;
continue;
}
// Support for the NoLock() virtual method...
if (node is DynamicParser.Node.Method && ((DynamicParser.Node.Method)node).Name.ToUpper() == "NOLOCK")
{
object[] args = ((DynamicParser.Node.Method)node).Arguments;
if (args != null && args.Length > 0)
throw new ArgumentNullException("arg", "NoLock() doesn't support arguments.");
nolock = true;
node = node.Host;
continue;
}
/*if (node is DynamicParser.Node.Method && ((DynamicParser.Node.Method)node).Name.ToUpper() == "subquery")
{
@@ -439,7 +454,7 @@ namespace DynamORM.Builders.Implementation
}
if (!string.IsNullOrEmpty(main))
tableInfo = type == null ? new TableInfo(Database, main, alias, owner) : new TableInfo(Database, type, alias, owner);
tableInfo = type == null ? new TableInfo(Database, main, alias, owner, nolock) : new TableInfo(Database, type, alias, owner, nolock);
else
throw new ArgumentException(string.Format("Specification #{0} is invalid: {1}", index, result));
}
@@ -461,6 +476,9 @@ namespace DynamORM.Builders.Implementation
if (!string.IsNullOrEmpty(tableInfo.Alias))
sb.AppendFormat(" AS {0}", tableInfo.Alias);
if (SupportNoLock && tableInfo.NoLock)
sb.AppendFormat(" WITH(NOLOCK)");
_from = string.IsNullOrEmpty(_from) ? sb.ToString() : string.Format("{0}, {1}", _from, sb.ToString());
}
@@ -530,6 +548,7 @@ namespace DynamORM.Builders.Implementation
string owner = null;
string alias = null;
string condition = null;
bool nolock = false;
Type tableType = null;
// If the expression resolves to a string...
@@ -607,6 +626,20 @@ namespace DynamORM.Builders.Implementation
continue;
}
// Support for the NoLock() virtual method...
if (node is DynamicParser.Node.Method && ((DynamicParser.Node.Method)node).Name.ToUpper() == "NOLOCK")
{
object[] args = ((DynamicParser.Node.Method)node).Arguments;
if (args != null && args.Length > 0)
throw new ArgumentNullException("arg", "NoLock() doesn't support arguments.");
nolock = true;
node = node.Host;
continue;
}
// Support for table specifications...
if (node is DynamicParser.Node.GetMember)
{
@@ -718,7 +751,7 @@ namespace DynamORM.Builders.Implementation
if (justAddTables)
{
if (!string.IsNullOrEmpty(main))
tableInfo = tableType == null ? new TableInfo(Database, main, alias, owner) : new TableInfo(Database, tableType, alias, owner);
tableInfo = tableType == null ? new TableInfo(Database, main, alias, owner, nolock) : new TableInfo(Database, tableType, alias, owner, nolock);
else
throw new ArgumentException(string.Format("Specification #{0} is invalid: {1}", index, result));
@@ -746,6 +779,9 @@ namespace DynamORM.Builders.Implementation
if (!string.IsNullOrEmpty(tableInfo.Alias))
sb.AppendFormat(" AS {0}", tableInfo.Alias);
if (SupportNoLock && tableInfo.NoLock)
sb.AppendFormat(" WITH(NOLOCK)");
if (!string.IsNullOrEmpty(condition))
sb.AppendFormat(" ON {0}", condition);

View File

@@ -59,6 +59,9 @@ namespace DynamORM
/// <summary>Database support stored procedures (EXEC procedure ...).</summary>
SupportStoredProcedures = 0x00000100,
/// <summary>Database support with no lock syntax.</summary>
SupportNoLock = 0x00001000,
/// <summary>Debug option allowing to enable command dumps by default.</summary>
DumpCommands = 0x01000000,
}