private void FindAndPaintNode(IDatabaseObject dbObject) { if (dbObject.GetType() == typeof(Table)) { foreach (var item in ((Table)dbObject).Columns) { FindAndPaintNode(item); } } else { var treeNodes = treeView1.Nodes.Find(dbObject.Name, true); if (treeNodes.Count() > 0) { var treeNode = treeNodes[0]; PaintNode(treeNode); } } }
public NavigatieScherm(Form previousForm, IDatabaseObject databaseObject) { this.previousForm = previousForm; this.databaseObject = databaseObject; InitializeComponent(); this.attributePanel.AutoScroll = true; this.idLabel.Text = "Id: " + databaseObject.Id; this.primaryObjectName.Text = databaseObject.GetType().Name; foreach (KeyValuePair <string, Type> attribute in databaseObject.RequiredData) { AddAttributeTextBox(attributePanel, attribute, databaseObject.ObjectData[attribute.Key]); } secondaryObjectName.Text = databaseObject.ObjectData["contentsNaam"].ToString(); secondaryObjectSelection.DisplayMember = "Naam"; secondaryObjectSelection.DataSource = this.databaseObject.ObjectData["contents"]; }
public static void Save(IDatabaseObject _database) { Type type = _database.GetType(); CheckDirectory(AppDomain.CurrentDomain.BaseDirectory + "/Database/" + type.Name); using (BufferWriter writer = new BufferWriter()) { PropertyInfo[] propertyes = type.GetProperties(); for (int i = 0; i < propertyes.Length; ++i) { switch (propertyes[i].PropertyType.Name) { case "String": writer.String((String)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "Char": writer.Char((Char)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "Double": writer.Double((Double)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "Boolean": writer.Boolean((Boolean)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "Byte": writer.Byte((Byte)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "Single": writer.Float((Single)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "Byte[]": Byte[] buffer = (Byte[])propertyes[i].GetGetMethod(true).Invoke(_database, new object[0]); writer.Int32(buffer.Length); writer.Bytes(buffer); break; case "Int16": writer.Int16((Int16)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "Int32": writer.Int32((Int32)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "Int64": writer.Int64((Int64)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "UInt16": writer.UInt16((UInt16)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "UInt32": writer.UInt32((UInt32)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; case "UInt64": writer.UInt64((UInt64)propertyes[i].GetGetMethod(true).Invoke(_database, new object[0])); break; } } File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + "/Database/" + type.Name + "/" + _database.ID + ".sdb", writer.Buffer); } }
public static IDatabaseObject Load(Type _type, uint _uid) { IDatabaseObject database = null; CheckDirectory(AppDomain.CurrentDomain.BaseDirectory + "/Database/" + _type.Name); try { if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/Database/" + _type.Name + "/" + _uid + ".sdb")) { if (ListLastUID.ContainsKey(_type) == false || _uid > ListLastUID[_type]) { ListLastUID[_type] = _uid; } database = (IDatabaseObject)_type.Assembly.CreateInstance(_type.FullName); database.ID = _uid; byte[] buffer_database = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "/Database/" + _type.Name + "/" + _uid + ".sdb"); using (BufferReader reader = new BufferReader(buffer_database)) { PropertyInfo[] propertyes = database.GetType().GetProperties(); for (int i = 0; i < propertyes.Length; ++i) { switch (propertyes[i].PropertyType.Name) { case "String": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.String() }); break; case "Char": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Char() }); break; case "Double": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Double() }); break; case "Boolean": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Boolean() }); break; case "Byte": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Byte() }); break; case "Single": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Float() }); break; case "Byte[]": int buffer_len = reader.Int32(); propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Bytes(buffer_len) }); break; case "Int16": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Int16() }); break; case "Int32": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Int32() }); break; case "Int64": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Int64() }); break; case "UInt16": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.UInt16() }); break; case "UInt32": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.UInt32() }); break; case "UInt64": propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.UInt64() }); break; } } } return(database); } } catch (Exception ex) { ConsoleSystem.LogError($"[Database.Manager]: Exception from load {_type.Name} id {_uid} database: " + ex.Message); } return(null); }
/// -------------------------------------------------------------------------------- /// <summary> /// Extracts the fields to save to the database from the objItem.SaveFields function. /// The fields are then written to the database using either an SQL INSERT or UPDATE /// depending on whether the object has already been saved. If the collection has /// implemented IDatabaseObjects.KeyFieldName then objItem's key is also validated to /// ensure it is not null and unique within the collection. If objCollection has /// implemented IDatabaseObjects.Subset then objItem should exist within objCollection. /// If not, a duplicate key error may occur if the obItem's key is being used in /// another subset in the same table. If a record is being amended /// (IDatabaseObject.IsSaved returns true) then the function will "AND" the collection's /// IDatabaseObjects.Subset conditions and the objItem's IDatabaseObject.DistinctValue /// value to create the WHERE clause in the UPDATE statement. Therefore, the /// combination of the IDatabaseObjects.Subset and IDatabaseObject.DistinctValue /// conditions MUST identify only one record in the table. Otherwise multiple records /// will be updated with the same data. If data is only inserted and not amended /// (usually a rare occurance) then this requirement is unnecessary. /// </summary> /// /// <param name="objCollection"> /// The collection which contains or will contain the object to save. /// </param> /// /// <param name="objItem"> /// The object to save to the database. The values saved to the database are extracted from the /// SQLFieldValues object returned from IDatabaseObject.SaveFields. /// </param> /// /// <example> Saves a product object (Me) to the database. /// <code> /// Public Sub Save() /// /// objDatabase.ObjectSave(NorthwindDB.Products, Me) /// /// End Sub /// </code> /// </example> /// -------------------------------------------------------------------------------- /// public void ObjectSave(IDatabaseObjects objCollection, IDatabaseObject objItem) { SQL.SQLFieldValues objFieldValues; var objNewGUID = Guid.Empty; var autoAssignment = MergeDistinctFieldAutoAssignmentAndDistinctFieldAutoIncrements(objCollection); objFieldValues = objItem.SaveFields(); if (objFieldValues == null) throw new Exceptions.DatabaseObjectsException(objItem.GetType().Name + " IDatabaseObject.SaveFields not implemented"); //Add the distinct field value if it hasn't been added via the SaveFields sub if (!objFieldValues.Exists(objCollection.DistinctFieldName())) { if (autoAssignment == SQL.FieldValueAutoAssignmentType.None) objFieldValues.Add(objCollection.DistinctFieldName(), objItem.DistinctValue); else if (autoAssignment == SQL.FieldValueAutoAssignmentType.NewUniqueIdentifier) { //For a new object, with a GUID that should be automatically assigned //Create a new GUID for the distinct field so that it saved for the INSERT if (!objItem.IsSaved) { objNewGUID = System.Guid.NewGuid(); objFieldValues.Add(objCollection.DistinctFieldName(), objNewGUID); } } } #if !DEBUG ItemKeyEnsureValid(objCollection, objItem, objFieldValues); #endif using (ConnectionScope objConnection = new ConnectionScope(this)) { if (objItem.IsSaved) { var objUpdate = new SQL.SQLUpdate(); objUpdate.TableName = objCollection.TableName(); objUpdate.Fields.Add(objFieldValues); objUpdate.Where.Add(objCollection.DistinctFieldName(), SQL.ComparisonOperator.EqualTo, objItem.DistinctValue); var objSubset = objCollection.Subset(); if (objSubset != null && !objSubset.IsEmpty) { objUpdate.Where.Add(objSubset); } if (objConnection.ExecuteNonQuery(objUpdate) != 1) throw new Exceptions.RecordDoesNotExistException(objCollection, objItem); } else { var objInsert = new SQL.SQLInsert(); objInsert.TableName = objCollection.TableName(); objInsert.Fields = objFieldValues; objConnection.ExecuteNonQuery(objInsert); if (autoAssignment == SQL.FieldValueAutoAssignmentType.NewUniqueIdentifier) objItem.DistinctValue = objNewGUID; else if (autoAssignment == SQL.FieldValueAutoAssignmentType.AutoIncrement) objItem.DistinctValue = objConnection.ExecuteScalar(new SQL.SQLAutoIncrementValue()); object objRollbackDistinctValue = objItem.DistinctValue; objItem.IsSaved = true; if (Transaction.Current != null) { Transaction.Current.EnlistVolatile(new TransactionExecuteActionOnRollback(() => objItem.IsSaved = false), EnlistmentOptions.None); Transaction.Current.EnlistVolatile(new TransactionExecuteActionOnRollback(() => objItem.DistinctValue = objRollbackDistinctValue), EnlistmentOptions.None); } } } }
private void ItemKeyEnsureValid(IDatabaseObjects objCollection, IDatabaseObject objItem, SQL.SQLFieldValues objFieldValues) { SQL.SQLSelect objSelect; object objKeyFieldValue; SQL.SQLConditions objSubset; //If the key field is set and the key field is specified in the object if (objCollection.KeyFieldName() != string.Empty && objFieldValues.Exists(objCollection.KeyFieldName())) { objKeyFieldValue = ItemKeyFieldValue(objCollection, objItem, objFieldValues); if (objKeyFieldValue is string) { if (String.IsNullOrEmpty((string)objKeyFieldValue)) throw new Exceptions.DatabaseObjectsException(objItem.GetType().Name + " " + objCollection.KeyFieldName() + " field is Null"); } objSelect = new SQL.SQLSelect(); objSelect.Tables.Add(objCollection.TableName()); objSelect.Fields.Add(objCollection.KeyFieldName()); objSelect.Where.Add(objCollection.KeyFieldName(), SQL.ComparisonOperator.EqualTo, objKeyFieldValue); objSubset = objCollection.Subset(); if (objSubset != null && !objSubset.IsEmpty) objSelect.Where.Add(objSubset); if (objItem.IsSaved) objSelect.Where.Add(objCollection.DistinctFieldName(), SQL.ComparisonOperator.NotEqualTo, objItem.DistinctValue); using (ConnectionScope objConnection = new ConnectionScope(this)) using (IDataReader objReader = objConnection.Execute(objSelect)) if (objReader.Read()) throw new Exceptions.ObjectAlreadyExistsException(objItem, objKeyFieldValue); } }
public ObjectAlreadyExistsException(IDatabaseObject objItem, object objKey) : base(objItem.GetType().Name + ": '" + objKey.ToString() + "'") { pobjItem = objItem; pobjKey = objKey; }
public ObjectDoesNotExistException(IDatabaseObject objItem) : base(objItem.GetType().Name + ": '" + objItem.DistinctValue.ToString() + "'") { pobjDistinctOrKeyValue = objItem.DistinctValue; }
public ObjectAlreadyLockedException(IDatabaseObjects objCollection, IDatabaseObject objObject) : base(objObject.GetType().Name + "." + objCollection.DistinctFieldName() + " " + objObject.DistinctValue.ToString() + " is already locked") { }