-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add change table support for sql server
- Loading branch information
Showing
7 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using FluentCommand.Extensions; | ||
using FluentCommand.Query.Generators; | ||
using FluentCommand.Reflection; | ||
|
||
namespace FluentCommand.Query; | ||
|
||
public class ChangeTableBuilder : StatementBuilder<ChangeTableBuilder> | ||
{ | ||
private TableExpression _fromTable; | ||
private QueryParameter _parameter; | ||
|
||
public ChangeTableBuilder(IQueryGenerator queryGenerator, List<QueryParameter> parameters) : base(queryGenerator, parameters) | ||
{ | ||
} | ||
|
||
public ChangeTableBuilder From( | ||
string tableName, | ||
string tableSchema = null, | ||
string tableAlias = null) | ||
{ | ||
_fromTable = new TableExpression(tableName, tableSchema, tableAlias); | ||
|
||
return this; | ||
} | ||
|
||
public ChangeTableBuilder From<TEntity>( | ||
string tableAlias = null) | ||
{ | ||
var typeAccessor = TypeAccessor.GetAccessor<TEntity>(); | ||
|
||
_fromTable = new TableExpression(typeAccessor.TableName, typeAccessor.TableSchema, tableAlias); | ||
|
||
return this; | ||
} | ||
|
||
public ChangeTableBuilder LastVersion(long lastVersion) | ||
{ | ||
var name = NextParameter(); | ||
_parameter = new QueryParameter(name, lastVersion, typeof(long)); | ||
|
||
Parameters.Add(_parameter); | ||
|
||
return this; | ||
} | ||
|
||
public override QueryStatement BuildStatement() | ||
{ | ||
if (_parameter == null) | ||
return new QueryStatement(QueryGenerator.TableExpression(_fromTable), Parameters); | ||
|
||
var table = QueryGenerator.TableExpression(new TableExpression(_fromTable.TableName, _fromTable.TableSchema)); | ||
|
||
var statement = $"CHANGETABLE (CHANGES {table}, {_parameter.Name})"; | ||
|
||
if (_fromTable.TableAlias.HasValue()) | ||
statement += $" AS [{_fromTable.TableAlias}]"; | ||
|
||
return new QueryStatement(statement, Parameters); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/FluentCommand.SqlServer/Query/ChangeTableBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace FluentCommand.Query; | ||
|
||
public static class ChangeTableBuilderExtensions | ||
{ | ||
public static SelectBuilder ChangeTable(this SelectBuilder selectBuilder, Action<ChangeTableBuilder> builder) | ||
{ | ||
if (selectBuilder is null) | ||
throw new ArgumentNullException(nameof(selectBuilder)); | ||
if (builder is null) | ||
throw new ArgumentNullException(nameof(builder)); | ||
|
||
var queryBuilder = selectBuilder as IQueryBuilder; | ||
|
||
var innerBuilder = new ChangeTableBuilder(queryBuilder.QueryGenerator, queryBuilder.Parameters); | ||
builder(innerBuilder); | ||
|
||
var statement = innerBuilder.BuildStatement(); | ||
selectBuilder.FromRaw(statement.Statement); | ||
|
||
return selectBuilder; | ||
|
||
} | ||
|
||
public static SelectEntityBuilder<TEntity> ChangeTable<TEntity>(this SelectEntityBuilder<TEntity> selectBuilder, Action<ChangeTableBuilder> builder) | ||
where TEntity : class | ||
{ | ||
if (selectBuilder is null) | ||
throw new ArgumentNullException(nameof(selectBuilder)); | ||
if (builder is null) | ||
throw new ArgumentNullException(nameof(builder)); | ||
|
||
var queryBuilder = selectBuilder as IQueryBuilder; | ||
|
||
var innerBuilder = new ChangeTableBuilder(queryBuilder.QueryGenerator, queryBuilder.Parameters); | ||
|
||
// preset table and schema | ||
innerBuilder.From<TEntity>(); | ||
|
||
builder(innerBuilder); | ||
|
||
var statement = innerBuilder.BuildStatement(); | ||
selectBuilder.FromRaw(statement.Statement); | ||
|
||
return selectBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...mmand.Tests/Query/Snapshots/SelectBuilderTest.SelectEntityChangeTableBuilder.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SELECT [t].[Id], [t].[EmailAddress], [t].[IsEmailAddressConfirmed], [t].[DisplayName], [t].[FirstName], [t].[LastName], [t].[PasswordHash], [t].[ResetHash], [t].[InviteHash], [t].[AccessFailedCount], [t].[LockoutEnabled], [t].[LockoutEnd], [t].[LastLogin], [t].[IsDeleted], [t].[Created], [t].[CreatedBy], [t].[Updated], [t].[UpdatedBy], [t].[RowVersion] | ||
FROM CHANGETABLE (CHANGES [User], @p0000) AS [c] | ||
INNER JOIN [User] AS [t] ON [c].[Id] = [t].[Id]; |