Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStovell committed May 5, 2020
1 parent 27c18f2 commit 93e0593
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DELETE FROM [dbo].[TestDocumentTbl] WITH (ROWLOCK) WHERE [Id] = @Id
DELETE FROM [RelatedDocument] WITH (ROWLOCK) WHERE [Id] = @Id
DELETE FROM [OtherRelatedTable] WITH (ROWLOCK) WHERE [Id] = @Id
DELETE FROM [dbo].[RelatedDocument] WITH (ROWLOCK) WHERE [Id] = @Id
DELETE FROM [dbo].[OtherRelatedTable] WITH (ROWLOCK) WHERE [Id] = @Id

@Id=Doc-1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DELETE FROM [dbo].[TestDocumentTbl] WITH (ROWLOCK) WHERE [Id] = @Id
DELETE FROM [RelatedDocument] WITH (ROWLOCK) WHERE [Id] = @Id
DELETE FROM [dbo].[RelatedDocument] WITH (ROWLOCK) WHERE [Id] = @Id

@Id=Doc-1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ INSERT INTO [dbo].[TestDocumentTbl] ([Id], [AColumn], [JSON]) VALUES
(@0__Id, @0__AColumn, @0__JSON)
,(@1__Id, @1__AColumn, @1__JSON)
,(@2__Id, @2__AColumn, @2__JSON)
INSERT INTO [RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
INSERT INTO [dbo].[RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
(@0__Id, 'TestDocumentTbl', @relateddocument_0, 'OtherTbl')
,(@0__Id, 'TestDocumentTbl', @relateddocument_1, 'OtherTbl')
,(@2__Id, 'TestDocumentTbl', @relateddocument_2, 'OtherTbl')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
INSERT INTO [dbo].[TestDocumentTbl] ([Id], [AColumn], [JSON]) VALUES
(@Id, @AColumn, @JSON)
INSERT INTO [RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
INSERT INTO [dbo].[RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
(@Id, 'TestDocumentTbl', @relateddocument_0, 'OtherTbl')
,(@Id, 'TestDocumentTbl', @relateddocument_1, 'OtherTbl')
INSERT INTO [OtherRelatedTable] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
INSERT INTO [dbo].[OtherRelatedTable] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
(@Id, 'TestDocumentTbl', @otherrelatedtable_0, 'OtherTbl')
,(@Id, 'TestDocumentTbl', @otherrelatedtable_1, 'OtherTbl')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
INSERT INTO [dbo].[TestDocumentTbl] ([Id], [AColumn], [JSON]) VALUES
(@Id, @AColumn, @JSON)
INSERT INTO [RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
INSERT INTO [dbo].[RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
(@Id, 'TestDocumentTbl', @relateddocument_0, 'OtherTbl')
,(@Id, 'TestDocumentTbl', @relateddocument_1, 'OtherTbl')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
INSERT INTO [dbo].[TestDocumentTbl] ([Id], [AColumn], [JSON]) VALUES
(@Id, @AColumn, @JSON)
INSERT INTO [RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
INSERT INTO [dbo].[RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable]) VALUES
(@Id, 'TestDocumentTbl', @relateddocument_0, 'OtherTbl')

@Id=New-Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ DELETE FROM @references

INSERT INTO @references VALUES (@relateddocument_0, 'OtherTbl'), (@relateddocument_1, 'OtherTbl')

DELETE FROM [RelatedDocument] WHERE [Id] = @Id
DELETE FROM [dbo].[RelatedDocument] WHERE [Id] = @Id
AND [RelatedDocumentId] not in (SELECT Reference FROM @references)

INSERT INTO [RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable])
INSERT INTO [dbo].[RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable])
SELECT @Id, 'TestDocumentTbl', Reference, ReferenceTable FROM @references t
WHERE NOT EXISTS (SELECT null FROM [RelatedDocument] r WHERE r.[Id] = @Id AND r.[RelatedDocumentId] = t.Reference )
WHERE NOT EXISTS (SELECT null FROM [dbo].[RelatedDocument] r WHERE r.[Id] = @Id AND r.[RelatedDocumentId] = t.Reference )

DELETE FROM @references

INSERT INTO @references VALUES (@otherrelatedtable_0, 'OtherTbl'), (@otherrelatedtable_1, 'OtherTbl')

DELETE FROM [OtherRelatedTable] WHERE [Id] = @Id
DELETE FROM [dbo].[OtherRelatedTable] WHERE [Id] = @Id
AND [RelatedDocumentId] not in (SELECT Reference FROM @references)

INSERT INTO [OtherRelatedTable] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable])
INSERT INTO [dbo].[OtherRelatedTable] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable])
SELECT @Id, 'TestDocumentTbl', Reference, ReferenceTable FROM @references t
WHERE NOT EXISTS (SELECT null FROM [OtherRelatedTable] r WHERE r.[Id] = @Id AND r.[RelatedDocumentId] = t.Reference )
WHERE NOT EXISTS (SELECT null FROM [dbo].[OtherRelatedTable] r WHERE r.[Id] = @Id AND r.[RelatedDocumentId] = t.Reference )

@Id=Doc-1
@JSON={}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
UPDATE [dbo].[TestDocumentTbl] SET [AColumn] = @AColumn, [JSON] = @JSON WHERE [Id] = @Id

DELETE FROM [RelatedDocument] WHERE [Id] = @Id
DELETE FROM [dbo].[RelatedDocument] WHERE [Id] = @Id

@Id=Doc-1
@JSON={}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ DELETE FROM @references

INSERT INTO @references VALUES (@relateddocument_0, 'OtherTbl')

DELETE FROM [RelatedDocument] WHERE [Id] = @Id
DELETE FROM [dbo].[RelatedDocument] WHERE [Id] = @Id
AND [RelatedDocumentId] not in (SELECT Reference FROM @references)

INSERT INTO [RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable])
INSERT INTO [dbo].[RelatedDocument] ([Id], [Table], [RelatedDocumentId], [RelatedDocumentTable])
SELECT @Id, 'TestDocumentTbl', Reference, ReferenceTable FROM @references t
WHERE NOT EXISTS (SELECT null FROM [RelatedDocument] r WHERE r.[Id] = @Id AND r.[RelatedDocumentId] = t.Reference )
WHERE NOT EXISTS (SELECT null FROM [dbo].[RelatedDocument] r WHERE r.[Id] = @Id AND r.[RelatedDocumentId] = t.Reference )

@Id=Doc-1
@JSON={}
Expand Down
14 changes: 9 additions & 5 deletions source/Nevermore/QueryBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public static class QueryBuilderExtensions
/// <returns>A plain SQL string representing a create stored procedure query</returns>
public static string AsStoredProcedure<TRecord>(this IQueryBuilder<TRecord> queryBuilder, string storedProcedureName, string schemaName = null) where TRecord : class
{
return new StoredProcedure(queryBuilder.GetSelectBuilder().GenerateSelect(), queryBuilder.Parameters, queryBuilder.ParameterDefaults, storedProcedureName, schemaName).GenerateSql();
var select = queryBuilder.GetSelectBuilder().GenerateSelect();
schemaName ??= select.Schema;
return new StoredProcedure(select, queryBuilder.Parameters, queryBuilder.ParameterDefaults, storedProcedureName, schemaName).GenerateSql();
}

/// <summary>
Expand All @@ -28,8 +30,9 @@ public static string AsStoredProcedure<TRecord>(this IQueryBuilder<TRecord> quer
/// <returns>A plain SQL string representing a create view query</returns>
public static string AsView<TRecord>(this IQueryBuilder<TRecord> queryBuilder, string viewName, string schemaName = null) where TRecord : class
{
schemaName ??= queryBuilder.GetSelectBuilder().GenerateSelect().Schema;
return new View(queryBuilder.GetSelectBuilder().GenerateSelect(), viewName, schemaName).GenerateSql();
var select = queryBuilder.GetSelectBuilder().GenerateSelect();
schemaName ??= select.Schema;
return new View(select, viewName, schemaName).GenerateSql();
}

/// <summary>
Expand All @@ -42,8 +45,9 @@ public static string AsView<TRecord>(this IQueryBuilder<TRecord> queryBuilder, s
/// <returns>A plain SQL string representing a create function query</returns>
public static string AsFunction<TRecord>(this IQueryBuilder<TRecord> queryBuilder, string functionName, string schemaName = null) where TRecord : class
{
schemaName ??= queryBuilder.GetSelectBuilder().GenerateSelect().Schema;
return new Function(queryBuilder.GetSelectBuilder().GenerateSelect(), queryBuilder.Parameters, queryBuilder.ParameterDefaults, functionName, schemaName).GenerateSql();
var select = queryBuilder.GetSelectBuilder().GenerateSelect();
schemaName ??= select.Schema;
return new Function(select, queryBuilder.Parameters, queryBuilder.ParameterDefaults, functionName, schemaName).GenerateSql();
}
}
}

0 comments on commit 93e0593

Please sign in to comment.