private bool CheckGuidType() { // Since the generator might be called from the enhancer // with no provider defined, we can't use GetProvider // because it throws an exeption, if no provider exists. //IProvider provider = mappings.GetProvider(c); Connection conn = mappings.FindConnection(c); if (conn == null) { throw new NDOException(102, "No connection defined in class mapping for type " + c.FullName + ". Check your mapping file."); } if (conn.Type == string.Empty) { return(true); } IProvider provider = NDOProviderFactory.Instance.GetProviderOrDefault(conn.Type); if (provider == null) { return(true); } return(provider.SupportsNativeGuidType); }
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); }
protected string CreateTable(DataTable dt) { StringBuilder sb = new StringBuilder(); IProvider provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName]; if (provider == null) { throw new Exception("Can't find NDO provider '" + concreteGenerator.ProviderName + "'."); } string tableName = QualifiedTableName.Get(dt.TableName, provider); Class cl = FindClass(dt.TableName, mappings); if (cl != null) { Connection conn = mappings.FindConnection(cl); if (conn != null) { concreteGenerator.ConnectToDatabase(conn.Name); } } sb.Append(concreteGenerator.BeginnTable(tableName)); sb.Append('\n'); int vorletzterIndex = dt.Columns.Count - 1; DataColumn[] primaryKeyColumns = dt.PrimaryKey; bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0; for (int i = 0; i < dt.Columns.Count; i++) { System.Data.DataColumn dc = dt.Columns[i]; bool isPrimary = false; foreach (DataColumn pkc in primaryKeyColumns) { if (pkc.ColumnName == dc.ColumnName) { isPrimary = true; break; } } sb.Append(CreateColumn(dc, cl, provider, isPrimary)); if (i < vorletzterIndex) { sb.Append(","); sb.Append('\n'); } } // vorletzterIndex = dt.ParentRelations.Count - 1; // if (vorletzterIndex > -1) // sb.Append(","); // // for (int i = 0; i < dt.ParentRelations.Count; i++) // { // DataRelation dr = dt.ParentRelations[i]; // sb.Append(concreteGenerator.CreateForeignKeyConstraint(dr.ChildColumns, dr.ParentColumns, provider.GetQuotedName(dr.RelationName), dr.ParentTable.TableName)); // if (i < vorletzterIndex) // sb.Append(","); // } if (concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable && hasPrimaryKeyColumns) { sb.Append(","); sb.Append('\n'); } if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable) { sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider)); } else { sb.Append('\n'); } sb.Append(concreteGenerator.EndTable(tableName)); sb.Append('\n'); // CreateIndex(primaryKeyColumns, sb, dt, provider); if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable) { sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider)); } sb.Append('\n'); return(sb.ToString()); }
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(); }