Browse Source

minor changes to ensure that the user is prompted when an attempt it made to overwrite and existing local metadata file

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1704 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Dickon Field 20 years ago
parent
commit
c4fe8d4e5a
  1. 36
      src/AddIns/Misc/SharpDbTools/Project/Src/Model/DbModelInfoService.cs
  2. 16
      src/AddIns/Misc/SharpDbTools/Project/Src/ServerBrowserTool.cs

36
src/AddIns/Misc/SharpDbTools/Project/Src/Model/DbModelInfoService.cs

@ -122,7 +122,14 @@ namespace SharpDbTools.Model
return modelInfo; return modelInfo;
} }
public static void SaveToFile(string name) /// <summary>
///
/// </summary>
/// <param name="name">the logical name of the DbModelInfo to save to a file</param>
/// <param name="overwriteExistingFile">if true, any existing file will be overwritten</param>
/// <returns>true if the DbModelInfo was saved, false if not. It will not be saved if
/// either overwriteExistingFile is set to true, and there is an existing file</returns>
public static bool SaveToFile(string name, bool overwriteExistingFile)
{ {
string path = GetSaveLocation(); string path = GetSaveLocation();
DbModelInfo modelInfo = null; DbModelInfo modelInfo = null;
@ -134,21 +141,28 @@ namespace SharpDbTools.Model
string filePath = path + @"\" + name + ".metadata"; string filePath = path + @"\" + name + ".metadata";
LoggingService.Debug("writing metadata to: " + filePath); LoggingService.Debug("writing metadata to: " + filePath);
if (File.Exists(filePath)) { if (File.Exists(filePath)) {
File.Delete(filePath); if (overwriteExistingFile) {
File.Delete(filePath);
} else {
return false;
}
} }
using (StreamWriter sw = File.CreateText(filePath)) { using (StreamWriter sw = File.CreateText(filePath)) {
string xml = modelInfo.GetXml(); string xml = modelInfo.GetXml();
sw.Write(xml); sw.Write(xml);
sw.Flush(); sw.Flush();
sw.Close(); sw.Close();
return true;
} }
} else {
throw new DbModelInfoDoesNotExistException(name);
} }
} }
public static void SaveAll() public static void SaveAll()
{ {
foreach (string name in cache.Keys) { foreach (string name in cache.Keys) {
SaveToFile(name); SaveToFile(name, true);
} }
} }
@ -218,4 +232,20 @@ namespace SharpDbTools.Model
return saveLocation; return saveLocation;
} }
} }
public class DbModelInfoDoesNotExistException: ApplicationException
{
string name;
public DbModelInfoDoesNotExistException(string dbModelInfoName): base()
{
this.name = dbModelInfoName;
}
public string Name {
get {
return name;
}
}
}
} }

16
src/AddIns/Misc/SharpDbTools/Project/Src/ServerBrowserTool.cs

@ -276,8 +276,20 @@ namespace SharpDbTools
public void SaveDbModelInfoClickHandler(object sender, EventArgs e) public void SaveDbModelInfoClickHandler(object sender, EventArgs e)
{ {
LoggingService.Debug("save all metadata clicked"); // save each DbModelInfo separately, confirming overwrite where necessary
DbModelInfoService.SaveAll(); LoggingService.Debug("save all metadata clicked - will iterate through each and attempt to save");
IList<string> names = DbModelInfoService.Names;
foreach (string name in names) {
bool saved = DbModelInfoService.SaveToFile(name, false);
if (!saved) {
DialogResult result = MessageBox.Show("Overwrite existing file for connection: " + name + "?",
"File exists for connection", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result.Equals(DialogResult.Yes)) {
DbModelInfoService.SaveToFile(name, true);
}
}
}
} }
public void LoadMetadataFromFileClickHandler(object sender, EventArgs e) public void LoadMetadataFromFileClickHandler(object sender, EventArgs e)

Loading…
Cancel
Save