public void AnlegenNNRelation() { string testFile = "Test.Xml"; NDOMapping mapping = NDOMapping.Create(testFile); Class c = mapping.AddStandardClass(classMitarbeiter, assName, null); c.AddStandardRelation("dieReisen", "Reisekosten.Reise", false, "", false, false); c = mapping.AddStandardClass("Reisekosten.Reise", assName, null); c.AddStandardRelation("dieMitarbeiter", classMitarbeiter, false, "", false, false); mapping.Save(); mapping = new NDOMapping(testFile); Assert.AreEqual(1, mapping.Connections.Count, "Keine Connection"); Assert.AreEqual(2, mapping.Classes.Count, "Keine Klasse"); c = (Class)mapping.FindClass(classMitarbeiter); Assert.AreEqual("Mitarbeiter", c.TableName, "TableName falsch"); Assert.NotNull(c.Oid, "Keine Oid"); Assert.AreEqual(1, c.Relations.Count, "Keine Relation"); Relation r = c.FindRelation("dieReisen"); Assert.NotNull(r, "Relation dieReisen nicht gefunden"); Assert.AreEqual("IDMitarbeiter", ((ForeignKeyColumn)r.ForeignKeyColumns[0]).Name, "ForeignKeyColumnName falsch"); Assert.AreEqual("n:n", r.GetRelationType(), "Relationstyp falsch"); Assert.AreEqual("IDReise", ((ForeignKeyColumn)r.MappingTable.ChildForeignKeyColumns[0]).Name, "ChildForeignKeyColumnName von MappingTable falsch"); Assert.AreEqual("relMitarbeiterReise", r.MappingTable.TableName, "TableName von MappingTable falsch"); c = mapping.FindClass("Reisekosten.Reise"); r = c.FindRelation("dieMitarbeiter"); Assert.NotNull(r, "Relation dieMitarbeiter nicht gefunden"); Assert.AreEqual("relMitarbeiterReise", r.MappingTable.TableName, "TableName von MappingTable falsch"); Assert.AreEqual("n:n", r.GetRelationType(), "Relationstyp falsch"); Assert.AreEqual("IDReise", ((ForeignKeyColumn)r.ForeignKeyColumns[0]).Name, "ForeignKeyColumnName falsch"); Assert.AreEqual("IDMitarbeiter", ((ForeignKeyColumn)r.MappingTable.ChildForeignKeyColumns[0]).Name, "ChildForeignKeyColumnName von MappingTable falsch"); }
public void MergeNewConnStringClassChanged() { string file1 = "File1.Xml"; string file2 = "File2.Xml"; NDOMapping map1 = NDOMapping.Create(file1); map1.AddStandardClass(classMitarbeiter, assName, null); ((Connection)map1.Connections[0]).Name = "Alter ConnectionString"; map1.Save(); NDOMapping map2 = NDOMapping.Create(file2); map2.AddStandardClass(classMitarbeiter, assName, null); ((Connection)map2.Connections[0]).Name = "Neuer ConnectionString"; ((Class)map2.Classes[0]).AssemblyName = "New Assembly-Name"; map2.Save(); map1 = new NDOMapping(file1); map2 = new NDOMapping(file2); map1.MergeMapping(map2); Assert.AreEqual(2, map1.Connections.Count, "Falsche Anzahl Connections"); Assert.AreEqual(1, map1.Classes.Count, "Falsche Anzahl Klassen"); Class c = map1.FindClass(classMitarbeiter); Assert.NotNull(c, "Mitarbeiter nicht gefunden"); Assert.AreEqual("C1", c.ConnectionId, "Connection falsch"); File.Delete(file1); File.Delete(file2); }
public void MergeNewConnStringFromDummy2() { string file1 = "File1.Xml"; string file2 = "File2.Xml"; NDOMapping map1 = NDOMapping.Create(file1); map1.AddStandardClass(classMitarbeiter, assName, null); map1.Save(); NDOMapping map2 = NDOMapping.Create(file2); map2.AddStandardClass(classMitarbeiter, assName, null); map2.Save(); map1 = new NDOMapping(file1); map2 = new NDOMapping(file2); Assert.AreEqual(Connection.DummyConnectionString, ((Connection)map1.Connections[0]).Name, "Must be dummy connection"); Assert.AreEqual(Connection.DummyConnectionString, ((Connection)map2.Connections[0]).Name, "Must be dummy connection"); ((Connection)map1.Connections[0]).Name = "Some new string"; map1.MergeMapping(map2); Assert.AreEqual(1, map1.Connections.Count, "Falsche Anzahl Connections"); Class c = map1.FindClass(classMitarbeiter); Assert.NotNull(c, "Mitarbeiter nicht gefunden"); Assert.AreEqual("C0", c.ConnectionId, "Connection falsch"); File.Delete(file1); File.Delete(file2); }
public void GleicherKlassenName() { string testFile = "Text.Xml"; string newclassMitarbeiter = classMitarbeiter.Replace("Personal", "Test"); NDOMapping mapping = NDOMapping.Create(testFile); mapping.AddStandardClass(classMitarbeiter, assName, null); mapping.AddStandardClass(newclassMitarbeiter, assName, null); mapping.Save(); mapping = new NDOMapping(testFile); Class c = mapping.FindClass(classMitarbeiter); Assert.NotNull(c, classMitarbeiter + " nicht gefunden"); Class c2 = mapping.FindClass(newclassMitarbeiter); Assert.NotNull(c, newclassMitarbeiter + " nicht gefunden"); Assert.That(c.TableName != c2.TableName, "TableNames mössen ungleich sein"); }
public void RelationTest() { NDOMapping mapping = pm.NDOMapping; Class reiseClass = mapping.FindClass(typeof(Reise)); Class pkwClass = mapping.FindClass(typeof(PKWFahrt)); Assert.NotNull(reiseClass, "Mapping for Reise not found"); Assert.NotNull(pkwClass, "Mapping for pkw not found"); Relation r1 = reiseClass.FindRelation("belege"); Assert.NotNull(r1, "Relation not found #1"); Assert.NotNull(r1.ForeignRelation, "ForeignRelation of Reise not found"); Relation r2 = pkwClass.FindRelation("reise"); Assert.NotNull(r2, "Relation not found #2"); Assert.NotNull(r2.ForeignRelation, "ForeignRelation of PKWFahrt not found"); Assert.That(r1 == r2.ForeignRelation, "Back relation wrong"); // Debug.WriteLine(r1.Parent.FullName + "->" + r1.ReferencedTypeName); // Debug.WriteLine(r1.ForeignRelation.Parent.FullName + "->" + r1.ForeignRelation.ReferencedTypeName); // Debug.WriteLine(r2.Parent.FullName + "->" + r2.ReferencedTypeName); // Debug.WriteLine(r2.ForeignRelation.Parent.FullName + "->" + r2.ForeignRelation.ReferencedTypeName); }
private Class GetParentClass(OqlExpression exp, string[] arr, out string fieldName, out Relation relation) { Class relClass = this.cls; NDOMapping mappings = relClass.Parent; int i; relation = null; for (i = 0; i < arr.Length - 1; i++) { relation = relClass.FindRelation(arr[i]); if (relation == null) // must be a value type or embedded type { break; } if (this.queryContext.ContainsKey(relation)) { relClass = this.queryContext[relation]; } else { relClass = mappings.FindClass(relation.ReferencedType); } } fieldName = String.Join(".", arr, i, arr.Length - i); if (relClass.FindField(fieldName) == null && fieldName.IndexOf('.') > -1) { // We might have a value type or embedded type here. // These don't have accessor names. // So we try to find the field via the standard column name. // Note, that this doesn't work, if the column name was remapped. // But we don't see another solution for this situation. string tempName = fieldName.Replace('.', '_'); var field = relClass.Fields.FirstOrDefault(f => f.Column.Name == tempName); if (field != null) { fieldName = field.Name; } } return(relClass); }
public static List <FieldEntry> GetFieldEntries(Type t, NDOMapping mapping) { List <FieldEntry> result = new List <FieldEntry>(); Class cl = mapping.FindClass(t); if (cl == null) { throw new Exception("Class mapping for type " + t.FullName + " not found."); } foreach (Field f in cl.Fields) { FieldInfo fi = t.GetField(f.Name, BindingFlags.Instance | BindingFlags.NonPublic); if (fi != null && fi.FieldType != typeof(byte[])) { result.Add(new FieldEntry(fi, f)); } } return(result); }
/// <summary> /// Generates all relations for a given relation name /// </summary> /// <param name="resultClass"></param> /// <param name="name"></param> /// <param name="relationSet"></param> public void CreateContextForName(Class resultClass, string name, ICollection <Relation> relationSet) { if (name.IndexOf(".") == -1) { return; } string[] namearr = name.Split(new char[] { '.' }); Class parentClass = resultClass; // parentClass will be overwritten in each iteration Relation resultRelation = null; for (int i = 0; i < namearr.Length - 1; i++) { string fieldName = namearr[i]; Relation rel = parentClass.FindRelation(fieldName); if (null == rel) { break; } resultRelation = rel; Class relClass = mappings.FindClass(rel.ReferencedTypeName); if (null == relClass) { throw new NDOException(17, "Can't find mapping information for class " + rel.ReferencedTypeName); } if (relClass.Subclasses.Count() > 0) { if (!relationSet.Contains(resultRelation)) // relationSet might be a HashSet { relationSet.Add(resultRelation); } } // will be overwritten until we have the innermost table parentClass = relClass; } }
public void AnlegenKlasse() { NDOMapping mapping = NDOMapping.Create(testFile); mapping.AddStandardClass(classMitarbeiter, assName, null); mapping.Save(); mapping = new NDOMapping(testFile); Assert.AreEqual(1, mapping.Connections.Count, "Keine Connection"); Assert.AreEqual(1, mapping.Classes.Count, "Keine Klasse"); Class c = (Class)mapping.Classes[0]; Assert.AreEqual("Mitarbeiter", c.TableName, "TableName falsch"); Assert.NotNull(c.Oid, "Keine Oid"); Assert.AreEqual("C0", mapping.FindConnection(c).ID, "Connection C0 nicht gefunden"); c = mapping.FindClass(classMitarbeiter); Assert.NotNull(c, "FindClass fehlgeschlagen"); File.Delete(testFile); }
public void Initialize(NDOMapping mappings, Relation r) { Connection con = mappings.FindConnection(r.MappingTable.ConnectionId); this.provider = mappings.GetProvider(con); // The connection object will be initialized in the pm, to // enable the callback for getting the real connection string. // CheckTransaction is the place, where this happens. this.connection = null; selectCommand = provider.NewSqlCommand(connection); insertCommand = provider.NewSqlCommand(connection); deleteCommand = provider.NewSqlCommand(connection); dataAdapter = provider.NewDataAdapter(selectCommand, null, insertCommand, deleteCommand); this.relation = r; // // select // string sql = string.Format("SELECT * FROM {0} WHERE ", provider.GetQualifiedTableName(r.MappingTable.TableName)); selectCommand.CommandText = sql; // // insert // sql = "INSERT INTO {0}({1}) VALUES ({2})"; //{0} = TableName: Mitarbeiter //{1} = FieldList: vorname, nachname //{2} = FieldList mit @: // IDOwn1, IDOwn2 string columns = string.Empty; string namedParameters = string.Empty; new ForeignKeyIterator(r).Iterate ( delegate(ForeignKeyColumn fkColumn, bool isLastElement) { columns += provider.GetQuotedName(fkColumn.Name); namedParameters += GetParameter(provider, fkColumn.Name); provider.AddParameter(insertCommand, provider.GetNamedParameter(fkColumn.Name), provider.GetDbType(fkColumn.SystemType), provider.GetDefaultLength(fkColumn.SystemType), fkColumn.Name); columns += ", "; namedParameters += ", "; } ); // IDOther1, IDOther2 new ForeignKeyIterator(r.MappingTable).Iterate ( delegate(ForeignKeyColumn fkColumn, bool isLastElement) { columns += provider.GetQuotedName(fkColumn.Name); namedParameters += GetParameter(provider, fkColumn.Name); provider.AddParameter(insertCommand, provider.GetNamedParameter(fkColumn.Name), provider.GetDbType(fkColumn.SystemType), provider.GetDefaultLength(fkColumn.SystemType), fkColumn.Name); if (!isLastElement) { columns += ", "; namedParameters += ", "; } } ); // TCOwn if (r.ForeignKeyTypeColumnName != null) { columns += ", " + provider.GetQuotedName(r.ForeignKeyTypeColumnName); namedParameters += ", " + GetParameter(provider, r.ForeignKeyTypeColumnName); provider.AddParameter(insertCommand, provider.GetNamedParameter(r.ForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), provider.GetDefaultLength(typeof(int)), r.ForeignKeyTypeColumnName); } // TCOther if (r.MappingTable.ChildForeignKeyTypeColumnName != null) { columns += ", " + provider.GetQuotedName(r.MappingTable.ChildForeignKeyTypeColumnName); namedParameters += ", " + GetParameter(provider, r.MappingTable.ChildForeignKeyTypeColumnName); provider.AddParameter(insertCommand, provider.GetNamedParameter(r.MappingTable.ChildForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), provider.GetDefaultLength(typeof(int)), r.MappingTable.ChildForeignKeyTypeColumnName); } insertCommand.CommandText = string.Format(sql, provider.GetQualifiedTableName(r.MappingTable.TableName), columns, namedParameters); Class relatedClass = mappings.FindClass(r.ReferencedTypeName); // // delete // sql = "DELETE FROM {0} WHERE ({1})"; //{0} = Tabellenname: Mitarbeiter //{1} = Where-Bedingung: id = @Original_id string where = string.Empty; new ForeignKeyIterator(r).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex) { where += provider.GetQuotedName(fkColumn.Name) + " = " + GetParameter(provider, "Original_" + fkColumn.Name) + " AND "; provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + fkColumn.Name), provider.GetDbType(fkColumn.SystemType), fkColumn.Size, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), fkColumn.Name, System.Data.DataRowVersion.Original, null); } ); new ForeignKeyIterator(r.MappingTable).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex) { where += provider.GetQuotedName(fkColumn.Name) + " = " + GetParameter(provider, "Original_" + fkColumn.Name); if (!isLastIndex) { where += " AND "; } provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + fkColumn.Name), provider.GetDbType(fkColumn.SystemType), fkColumn.Size, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), fkColumn.Name, System.Data.DataRowVersion.Original, null); } ); if (r.ForeignKeyTypeColumnName != null) { where += " AND " + provider.GetQuotedName(r.ForeignKeyTypeColumnName) + " = " + GetParameter(provider, "Original_" + r.ForeignKeyTypeColumnName); provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + r.ForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), r.ForeignKeyTypeColumnName, System.Data.DataRowVersion.Original, null); } if (r.MappingTable.ChildForeignKeyTypeColumnName != null) { where += " AND " + provider.GetQuotedName(r.MappingTable.ChildForeignKeyTypeColumnName) + " = " + GetParameter(provider, "Original_" + r.MappingTable.ChildForeignKeyTypeColumnName); provider.AddParameter(deleteCommand, provider.GetNamedParameter("Original_" + r.MappingTable.ChildForeignKeyTypeColumnName), provider.GetDbType(typeof(int)), 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), r.MappingTable.ChildForeignKeyTypeColumnName, System.Data.DataRowVersion.Original, null); } deleteCommand.CommandText = string.Format(sql, provider.GetQualifiedTableName(r.MappingTable.TableName), where); }
/// <summary> /// Initializes the PersistenceHandler /// </summary> /// <param name="ndoMapping">Mapping information.</param> /// <param name="t">Type for which the Handler is constructed.</param> /// <param name="disposeCallback">Method to be called at the end of the usage. The method can be used to push back the object to the PersistenceHandlerPool.</param> public void Initialize(NDOMapping ndoMapping, Type t, Action <Type, IPersistenceHandler> disposeCallback) { this.ndoMapping = ndoMapping; this.classMapping = ndoMapping.FindClass(t); this.timeStampColumn = classMapping.TimeStampColumn; this.typeNameColumn = classMapping.TypeNameColumn; this.hasAutoincrementedColumn = false; foreach (OidColumn oidColumn in this.classMapping.Oid.OidColumns) { if (oidColumn.AutoIncremented) { this.hasAutoincrementedColumn = true; this.autoIncrementColumn = oidColumn; break; } } this.hasGuidOid = this.classMapping.HasGuidOid; this.tableName = classMapping.TableName; Connection connInfo = ndoMapping.FindConnection(classMapping); this.provider = ndoMapping.GetProvider(connInfo); this.qualifiedTableName = provider.GetQualifiedTableName(tableName); // The connection object will be initialized by the pm, to // enable connection string housekeeping. // CheckTransaction is the place, where this happens. this.conn = null; var columnListGenerator = CreateColumnListGenerator(classMapping); this.hollowFields = columnListGenerator.HollowFields; this.hollowFieldsWithAlias = columnListGenerator.HollowFieldsWithAlias; this.namedParamList = columnListGenerator.ParamList; this.fieldList = columnListGenerator.BaseList; this.selectFieldList = columnListGenerator.SelectList; this.selectFieldListWithAlias = columnListGenerator.SelectListWithAlias; this.guidlength = provider.GetDefaultLength(typeof(Guid)); if (this.guidlength == 0) { this.guidlength = provider.SupportsNativeGuidType ? 16 : 36; } this.disposeCallback = disposeCallback; this.selectCommand = provider.NewSqlCommand(conn); this.insertCommand = provider.NewSqlCommand(conn); this.updateCommand = provider.NewSqlCommand(conn); this.deleteCommand = provider.NewSqlCommand(conn); this.dataAdapter = provider.NewDataAdapter(selectCommand, updateCommand, insertCommand, deleteCommand); this.type = t; CollectFields(); // Alle Feldinformationen landen in persistentField // determine the relations, which have a foreign key // column in the table of the given class relationInfos = new RelationCollector(this.classMapping) .CollectRelations().ToList(); GenerateSelectCommand(); GenerateInsertCommand(); GenerateUpdateCommand(); GenerateDeleteCommand(); }