public void Update(DataTable t) { var schema = GetSchema(); var dataClass = schema.FindClassByID(t.TableName); if (dataClass != null) { foreach (DataRow r in t.Rows) { switch (r.RowState) { case DataRowState.Added: InsertDataRow(dataClass, r); break; case DataRowState.Modified: UpdateDataRow(dataClass, r); break; case DataRowState.Deleted: DeleteDataRow(dataClass, r); break; } } t.AcceptChanges(); return; } // check for relation table var relation = schema.FindRelationshipByID(t.TableName); if (relation != null) { foreach (DataColumn c in t.Columns) { if (c.ColumnName != "subject_id" && c.ColumnName != "object_id") { throw new ArgumentException(String.Format("{0} does not exist in {1}", c.ColumnName, t.TableName)); } } if (!t.Columns.Contains("subject_id")) { throw new ArgumentException("subject_id column is required"); } if (!t.Columns.Contains("object_id")) { throw new ArgumentException("object_id column is required"); } foreach (DataRow r in t.Rows) { switch (r.RowState) { case DataRowState.Added: ObjectContainerStorage.AddRelation( new ObjectRelation( Convert.ToInt64(r["subject_id"]), relation, Convert.ToInt64(r["object_id"]))); break; case DataRowState.Deleted: ObjectContainerStorage.RemoveRelation( new ObjectRelation( Convert.ToInt64(r["subject_id", DataRowVersion.Original]), relation, Convert.ToInt64(r["object_id", DataRowVersion.Original])) ); break; default: throw new NotSupportedException(String.Format("Relation doesn't support row state {0}", r.RowState)); } } t.AcceptChanges(); return; } UnderlyingDalc.Update(t); }
public void Insert(string tableName, IDictionary <string, IQueryValue> data) { var schema = GetSchema(); var dataClass = schema.FindClassByID(tableName); if (dataClass != null) { var objContainer = new ObjectContainer(dataClass); foreach (var changeEntry in data) { if (!(changeEntry.Value is QConst)) { throw new NotSupportedException( String.Format("{0} value type is not supported", changeEntry.Value.GetType())); } objContainer[changeEntry.Key] = ((QConst)changeEntry.Value).Value; } ObjectContainerStorage.Insert(objContainer); return; } // check for relation table var relation = schema.FindRelationshipByID(tableName); if (relation != null) { long?subjId = null; long?objId = null; foreach (var entry in data) { var valConst = entry.Value as QConst; if (valConst == null) { throw new NotSupportedException( String.Format("Value {0} for {1} is not supported", entry.Value, entry.Key)); } if (entry.Key == "subject_id") { subjId = Convert.ToInt64(((QConst)entry.Value).Value); } else if (entry.Key == "object_id") { objId = Convert.ToInt64(((QConst)entry.Value).Value); } else { throw new ArgumentException(String.Format("{0} does not exist in {1}", entry.Key, tableName)); } } if (!subjId.HasValue) { throw new ArgumentException("subject_id is required"); } if (!objId.HasValue) { throw new ArgumentException("object_id is required"); } ObjectContainerStorage.AddRelation( new ObjectRelation(subjId.Value, relation, objId.Value)); return; } UnderlyingDalc.Insert(tableName, data); }