Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Microsoft.Research/Clousot.Cache/Clousot.Cache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Entity" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
46 changes: 46 additions & 0 deletions Microsoft.Research/Clousot.Cache/SQLCacheModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Linq;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.Research.CodeAnalysis.Caching.Models;
Expand Down Expand Up @@ -558,6 +561,45 @@ public string CacheName
{
get { return this.cachename; }
}

protected void DetachDeletedDb(string connection)
{
// If anyone has manually deleted the cache file, it might be still registered with LocalDB.
// If this is the case, we can't create a new database and the user is stuck unless he knows the tricky internals of LocalDB and how to get rid of the registration.
// => If we use LocalDB and the file does not exist, we try to detach it.
try
{
var connectionString = new DbConnectionStringBuilder { ConnectionString = connection };
var fileName = (string)connectionString["AttachDbFileName"];
if (File.Exists(fileName))
return;

var catalog = (string)connectionString["Initial Catalog"];
var dataSource = (string)connectionString["Data Source"];

using (var master = new DataContext(string.Format(CultureInfo.InvariantCulture, @"Data Source={0};Initial Catalog=master;Integrated Security=True", dataSource)))
{
master.ExecuteCommand(@"exec sp_detach_db {0}", catalog);
}
}
catch (ArgumentException)
{
// Not using LocalDB at all (any of the connection string parameters did not exist).
}
catch (SqlException)
{
// The file was never registered (first time used) or is already detached.
}
catch (Exception ex)
{
// Ignore other errors, could be any DbProvider specific exception.
// Usually we won't get here, but the active provider might not be LocalDB, in this case we know nothing about the provider and the exceptions it might raise.
if (this.trace)
{
Console.WriteLine("[cache] DetachDeletedDb failed: {0}", ex.Message);
}
}
}
}

public class SqlCacheModelNoCreate : SQLCacheModel
Expand Down Expand Up @@ -614,6 +656,8 @@ public SqlCacheModelClearExisting(string connection, string cachename, bool trac
: base(connection, cachename, trace)
{
Contract.Requires(cachename != null);

DetachDeletedDb(connection);
}
}

Expand All @@ -637,6 +681,8 @@ public SqlCacheModelDropOnModelChange(string connection, string cachename, bool
: base(connection, cachename, trace)
{
Contract.Requires(cachename != null);

DetachDeletedDb(connection);
}
}

Expand Down