1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-22 12:04:27 -05:00

handle case when no folders or ciphers on change

This commit is contained in:
Kyle Spearrin 2017-05-03 17:16:24 -04:00
parent 1a4c8b2db7
commit 94fdb72d75

View File

@ -222,23 +222,33 @@ namespace Bit.Core.Repositories.SqlServer
// 3. Bulk bopy into temp tables. // 3. Bulk bopy into temp tables.
if(ciphers.Any())
{
using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction))
{ {
bulkCopy.DestinationTableName = "#TempCipher"; bulkCopy.DestinationTableName = "#TempCipher";
var dataTable = BuildCiphersTable(ciphers); var dataTable = BuildCiphersTable(ciphers);
bulkCopy.WriteToServer(dataTable); bulkCopy.WriteToServer(dataTable);
} }
}
if(folders.Any())
{
using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction))
{ {
bulkCopy.DestinationTableName = "#TempFolder"; bulkCopy.DestinationTableName = "#TempFolder";
var dataTable = BuildFoldersTable(folders); var dataTable = BuildFoldersTable(folders);
bulkCopy.WriteToServer(dataTable); bulkCopy.WriteToServer(dataTable);
} }
}
// 4. Insert into real tables from temp tables and clean up. // 4. Insert into real tables from temp tables and clean up.
var sqlUpdate = @" var sql = string.Empty;
if(ciphers.Any())
{
sql += @"
UPDATE UPDATE
[dbo].[Cipher] [dbo].[Cipher]
SET SET
@ -249,8 +259,12 @@ namespace Bit.Core.Repositories.SqlServer
INNER JOIN INNER JOIN
#TempCipher TC ON C.Id = TC.Id #TempCipher TC ON C.Id = TC.Id
WHERE WHERE
C.[UserId] = @UserId C.[UserId] = @UserId";
}
if(folders.Any())
{
sql += @"
UPDATE UPDATE
[dbo].[Folder] [dbo].[Folder]
SET SET
@ -261,12 +275,14 @@ namespace Bit.Core.Repositories.SqlServer
INNER JOIN INNER JOIN
#TempFolder TF ON F.Id = TF.Id #TempFolder TF ON F.Id = TF.Id
WHERE WHERE
F.[UserId] = @UserId F.[UserId] = @UserId";
}
sql += @"
DROP TABLE #TempCipher DROP TABLE #TempCipher
DROP TABLE #TempFolder"; DROP TABLE #TempFolder";
using(var cmd = new SqlCommand(sqlUpdate, connection, transaction)) using(var cmd = new SqlCommand(sql, connection, transaction))
{ {
cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = user.Id; cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = user.Id;
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();