using System; using System.Collections; using System.Diagnostics; using System.Data; using MySql; using MySql.Data; using MySql.Data.MySqlClient; namespace WmData{ /// /// WmDataLiveLink is the object which maintains a collection of DataAdapters /// used to get and set data from and to the database. /// /// Created 12th of may 2006 /// Renamed to WmDataAdaptorManager (from WmDataLiveLink) to prevent confusion /// with the high availability WmDataLink system /// public class WmDataAdapterManager { protected DateTime created; protected ArrayList dataAdapters; protected ArrayList adaptedObjectNames; protected WmDataBaseCreator dbCreator; protected WmDataBaseCredentials dataBaseCredentials; protected WmData data; public WmDataAdapterManager(WmData data, WmDataBaseCredentials dataBaseCredentials) { this.data = data; this.dataBaseCredentials = dataBaseCredentials; created = DateTime.Now; dataAdapters = new ArrayList(); adaptedObjectNames = new ArrayList(); } public void createAdapterForObject(Type objectType){ lock (dataAdapters.SyncRoot) { string tableName = WmDataTools.getTableName(objectType); // An additional check to prevent duplicate creation of adapters in a // heavy multithreading setting. if (!isAdapted(tableName)) { WmDataAdapter adapter = new WmDataAdapter(data, dataBaseCredentials, objectType); adaptedObjectNames.Add(tableName); adaptedObjectNames.Sort(); dataAdapters.Add(adapter); } } } /// /// Creates (and fills) or refreshes dataset table, corresponding with the object /// public void fill(string tableName){ WmDataAdapter adapter = getDataAdapter(tableName); if(adapter!=null){ adapter.fill(); }else{ Trace.WriteLine("WmDataAdapterManager: Warning! Couldn't fill/refresh datatable in dataset for object "+tableName); } } /// /// Updates hosting table of the update object. Updating includes inserts, delete and updates. /// public void update(string tableName){ WmDataAdapter adapter = getDataAdapter(tableName); if(adapter!=null){ adapter.update(); }else{ Trace.WriteLine("WmDataAdapterManager: Warning! Couldn't update object live backend. Object=" + tableName); } } protected WmDataAdapter getDataAdapter(string tableName){ ArrayList results = new ArrayList(); lock (dataAdapters.SyncRoot) { foreach (WmDataAdapter ad in dataAdapters) { if (ad.TableName.Equals(tableName)) { return ad; } } } return null; } public bool isAdapted(string tableName){ lock (adaptedObjectNames.SyncRoot) { if (adaptedObjectNames.Contains(tableName.ToLower())) { return true; } else { return false; } } } public WmData Data { get { return data; } } } }