querybuilder icon indicating copy to clipboard operation
querybuilder copied to clipboard

[Table] with nolock for each join tables

Open BalajiDharmaraj opened this issue 3 years ago • 1 comments

How can i add table hint for each join? var query = new Query().FromRaw("Users with (nolock)") .Join("Authors", "Authors.Id", "Posts.AuthorId") .Where("Id", 1) .Where("Status", "Active");

will be:

SELECT * FROM Users with (nolock) INNER JOIN [Authors] ON [Authors].[Id] = [Posts].[AuthorId] WHERE [Id] = 1 AND [Status] = 'Active'

but I want it to be: SELECT * FROM Users with (nolock) INNER JOIN [Authors] with (nolock) ON [Authors].[Id] = [Posts].[AuthorId] WHERE [Id] = 1 AND [Status] = 'Active'

BalajiDharmaraj avatar Apr 07 '21 06:04 BalajiDharmaraj

I had the same issue and tackled it by implementing my own compiler.

class NoLockCompiler : SqlServerCompiler
{
    public override string CompileTableExpression(SqlResult ctx, AbstractFrom from)
    {
        return base.CompileTableExpression(ctx, from) + " WITH (NOLOCK)";
    }
}

/*
var q = new Query("foo")
    .Join("bar as baz", "a", "b");
    
SELECT * FROM [foo] WITH (NOLOCK) INNER JOIN [bar] as [baz] WITH (NOLOCK) ON [a] = [b]
*/

It might be nice if SqlServerCompiler had a boolean property to enable this behavior out of the box.

asherber avatar Jun 22 '21 23:06 asherber