Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        /// <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();
        }