/// <summary> /// For Development only, templates are also kept in a disc file. /// This means that Bazaar does the internal update management for us. /// </summary> private static void LoadTemplatesFromBackupFile(String AType, TDataBase dbConnection) { String BackupFilename = TemplateBackupFilename(AType, "*"); String[] BackupFiles = Directory.GetFiles(Path.GetDirectoryName(BackupFilename), Path.GetFileName(BackupFilename)); TDBTransaction Transaction = null; Boolean submissionOk = false; dbConnection.BeginAutoTransaction(IsolationLevel.ReadCommitted, ref Transaction, ref submissionOk, "LoadTemplatesFromBackupFile", delegate { foreach (String fname in BackupFiles) { if (File.Exists(fname)) { String Query = File.ReadAllText(fname); Transaction.DataBaseObj.ExecuteNonQuery(Query, Transaction); submissionOk = true; } } }); }
/// <summary> /// Stores a System Default in the DB. If it was already there it gets updated, if it wasn't there it gets added. /// </summary> /// <remarks>The change gets reflected in the System Defaults Cache the next time the System Defaults Cache /// gets accessed.</remarks> /// <param name="AKey">Name of the System Default.</param> /// <param name="AValue">Value of the System Default.</param> /// <param name="AAdded">True if the System Default got added, false if it already existed.</param> /// <remarks>SystemDefault Names are not case sensitive.</remarks> public void SetSystemDefault(String AKey, String AValue, out bool AAdded) { TDataBase DBConnectionObj = null; TDBTransaction WriteTransaction = null; Boolean SubmissionOK = false; SSystemDefaultsTable SystemDefaultsDT; Boolean Added = false; try { // Open a separate DB Connection... DBConnectionObj = DBAccess.SimpleEstablishDBConnection("SetSystemDefault"); // ...and start a DB Transaction on that separate DB Connection DBConnectionObj.BeginAutoTransaction(IsolationLevel.ReadCommitted, ref WriteTransaction, ref SubmissionOK, "SetSystemDefault", delegate { SystemDefaultsDT = SSystemDefaultsAccess.LoadAll(WriteTransaction); // This will find the row that matches a case-insensitive search of the table primary keys SystemDefaultsDT.CaseSensitive = false; // It is anyway SSystemDefaultsRow match = (SSystemDefaultsRow)SystemDefaultsDT.Rows.Find(AKey); if (match != null) { // I already have this System Default in the DB --> simply update the Value in the DB. // (This will often be the case!) match.DefaultValue = AValue; Added = false; } else { // The System Default isn't in the DB yet --> store it in the DB. var SystemDefaultsDR = SystemDefaultsDT.NewRowTyped(true); SystemDefaultsDR.DefaultCode = AKey; SystemDefaultsDR.DefaultDescription = "Created in OpenPetra"; SystemDefaultsDR.DefaultValue = AValue; SystemDefaultsDT.Rows.Add(SystemDefaultsDR); Added = true; } SSystemDefaultsAccess.SubmitChanges(SystemDefaultsDT, WriteTransaction); SubmissionOK = true; }); AAdded = Added; } catch (Exception Exc) { TLogging.Log( "TSystemDefaultCache.SetSystemDefault: An Exception occured during the saving of the System Default '" + AKey + "'. Value to be saved: + '" + AValue + "'" + Environment.NewLine + Exc.ToString()); throw; } finally { if (SubmissionOK) { // We need to ensure that the next time the System Defaults Caches gets accessed it is refreshed from the DB!!! // Obtain thread-safe access to the FTableCached Field to prevent two (or more) Threads from getting a different // FTableCached value! lock (FTableCachedLockCookie) { FTableCached = false; } } if (DBConnectionObj != null) { DBConnectionObj.CloseDBConnection(); } } }
public static bool ResetDatabase(string AZippedNewDatabaseData) { TDataBase DBConnectionObj = null; List <string> tables = TTableList.GetDBNames(); bool SubmissionResult = false; TDBTransaction ReadWriteTransaction = null; string ClientID = "ClientID"; try { ClientID = DomainManager.GClientID.ToString(); } catch (Exception) { } TProgressTracker.InitProgressTracker(ClientID, Catalog.GetString("Restoring Database..."), tables.Count + 3); try { // Open a separate DB Connection for the importing of the data... DBConnectionObj = DBAccess.SimpleEstablishDBConnection("ExportAllTables"); // ...and start a DB Transaction on that separate DB Connection DBConnectionObj.BeginAutoTransaction(IsolationLevel.Serializable, ref ReadWriteTransaction, ref SubmissionResult, delegate { try { tables.Reverse(); TProgressTracker.SetCurrentState(ClientID, Catalog.GetString("Deleting current data..."), 0); foreach (string table in tables) { ReadWriteTransaction.DataBaseObj.ExecuteNonQuery("DELETE FROM pub_" + table, ReadWriteTransaction); } if (TProgressTracker.GetCurrentState(ClientID).CancelJob == true) { TProgressTracker.FinishJob(ClientID); // As SubmissionResult is still false, a DB Transaction Rollback will get // executed automatically and the Method will be exited with return value 'false'! return; } TSimpleYmlParser ymlParser = new TSimpleYmlParser(PackTools.UnzipString(AZippedNewDatabaseData)); ymlParser.ParseCaptions(); tables.Reverse(); TProgressTracker.SetCurrentState(ClientID, Catalog.GetString("Loading initial tables..."), 1); // one transaction to import the user table and user permissions. otherwise logging in will not be possible if other import fails? bool success = true; success = success && LoadTable("s_user", ymlParser, ReadWriteTransaction); success = success && LoadTable("s_module", ymlParser, ReadWriteTransaction); success = success && LoadTable("s_user_module_access_permission", ymlParser, ReadWriteTransaction); success = success && LoadTable("s_system_defaults", ymlParser, ReadWriteTransaction); success = success && LoadTable("s_system_status", ymlParser, ReadWriteTransaction); // make sure we have the correct database version TFileVersionInfo serverExeInfo = new TFileVersionInfo(TSrvSetting.ApplicationVersion); ReadWriteTransaction.DataBaseObj.ExecuteNonQuery(String.Format( "UPDATE PUB_s_system_defaults SET s_default_value_c = '{0}' WHERE s_default_code_c = 'CurrentDatabaseVersion'", serverExeInfo.ToString()), ReadWriteTransaction); if (!success) { // As SubmissionResult is still TSubmitChangesResult.scrError, a DB Transaction Rollback will get // executed automatically and the Method will be exited with return value 'false'! return; } if (TProgressTracker.GetCurrentState(ClientID).CancelJob == true) { TProgressTracker.FinishJob(ClientID); // As SubmissionResult is still false, a DB Transaction Rollback will get // executed automatically and the Method will be exited with return value 'false'! return; } tables.Remove("s_user"); tables.Remove("s_module"); tables.Remove("s_user_module_access_permission"); tables.Remove("s_system_defaults"); tables.Remove("s_system_status"); FCurrencyPerLedger = new SortedList <int, string>(); int tableCounter = 2; foreach (string table in tables) { TProgressTracker.SetCurrentState(ClientID, String.Format(Catalog.GetString("Loading Table {0}..."), table), tableCounter); tableCounter++; if (TProgressTracker.GetCurrentState(ClientID).CancelJob == true) { TProgressTracker.FinishJob(ClientID); // As SubmissionResult is still false, a DB Transaction Rollback will get // executed automatically and the Method will be exited with return value 'false'! return; } LoadTable(table, ymlParser, ReadWriteTransaction); } TProgressTracker.SetCurrentState(ClientID, Catalog.GetString("Loading Sequences..."), tables.Count + 5 + 3); // set sequences appropriately, not lagging behind the imported data foreach (string seq in TTableList.GetDBSequenceNames()) { LoadSequence(seq, ymlParser, ReadWriteTransaction); } TProgressTracker.SetCurrentState(ClientID, Catalog.GetString("Finishing Restore..."), tables.Count + 5 + 4); SubmissionResult = true; // reset all cached tables TCacheableTablesManager.GCacheableTablesManager.MarkAllCachedTableNeedsRefreshing(); TProgressTracker.FinishJob(ClientID); } catch (Exception e) { TLogging.Log("Problem in ResetDatabase: " + e.ToString()); TLogging.LogStackTrace(TLoggingType.ToLogfile); throw; } }); } finally { if (DBConnectionObj != null) { DBConnectionObj.CloseDBConnection(); } } return(SubmissionResult); }