예제 #1
0
        public void Generate(IStreamProvider streamProvider)
        {
            GenerationContext ctx = new GenerationContext();
            ctx.Dialect = GetDialect();
            ctx.Model = new MappingModelImpl();
            IDriver driver = GetDriver();
            try
            {
                using (IDbConnection connection = driver.CreateConnection())
                {
                    DbConnection dbConn = connection as DbConnection;
                    if (null == dbConn)
                        throw new Exception("Can't convert connection provided by driver to DbConnection");
                    connection.ConnectionString = cfg.connectioninfo.connectionstring;
                    connection.Open();
                    ctx.Schema = ctx.Dialect.GetDataBaseSchema(dbConn);
                    ctx.Configuration = cfg;
                    ctx.Connection = dbConn;
                    ctx.TableExceptions = new TableExceptions(cfg);
                    ctx.NamingStrategy = TypeFactory.Create<INamingStrategy>(cfg.namingstrategy);
                    log.Info("Retrieving working table list");
                    ctx.FilteredTables.AddRange(TableEnumerator.GetInstance(ctx.Schema));
                    foreach (IMetadataStrategy strategy in metaStrategies)
                        strategy.Process(ctx);
                }

                foreach (var clazz in ctx.Model.GetEntities())
                {
                    TextWriter target = streamProvider.GetTextWriter(clazz.name);
                    hibernatemapping mapping = new hibernatemapping();
                    mapping.Items = new object[] { clazz };
                    if (!string.IsNullOrEmpty(cfg.entitiesnamespace))
                        mapping.@namespace = cfg.entitiesnamespace;
                    if (!string.IsNullOrEmpty(cfg.entitiesassembly))
                        mapping.assembly = cfg.entitiesassembly;
                    XmlSerializer ser = new XmlSerializer(typeof(hibernatemapping));

                    XmlSerializerNamespaces empty = new XmlSerializerNamespaces();
                    empty.Add(string.Empty, "urn:nhibernate-mapping-2.2");
                    ser.Serialize(target, mapping,empty);
                    target.Flush();
                    target.Close();
                    streamProvider.EndWrite();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
 private void CreateExceptions(GenerationContext context)
 {
     if (null != context.Configuration.entityexceptions)
     {
         foreach (var v in context.Configuration.entityexceptions)
         {
             try
             {
                 exceptions.Add(new EntityException(v));
             }
             catch (Exception e)
             {
                 Logger.Error("Error creating entity exception", e);
             }
         }
     }
 }
 protected override void OnProcess(GenerationContext context)
 {
     this.context = context;
     foreach (var entity in context.Model.GetEntities())
     {
         logger.Debug(string.Format("{0} working on:", GetType().Name) + entity.table);
         setToRemove = new List<set>();
         var sets = context.Model.GetCollectionsOfEntity(entity.name).OfType<set>();
         foreach( var set in sets )
             CheckIfCanBeMap(set,entity);
         foreach (var rem in setToRemove)
             context.Model.RemoveCollectionFromEntity(entity.name, rem);
     }
     foreach (var erem in entitiesToRemove)
     {
         context.Model.RemoveEntity(erem);
     }
 }
 public void Process(GenerationContext context)
 {
     CreateExceptions(context);
     IList<@class> entities = context.Model.GetEntities();
     foreach (var entity in entities)
     {
         foreach (var exception in exceptions)
         {
             try
             {
                 exception.Apply(entity, context.Model);
             }
             catch (Exception e)
             {
                 Logger.Error("Error applying entity exception:" + e.Message);
             }
         }
     }
 }
 public void Process(GenerationContext context)
 {
     currentContext = context;
     if (context["FKMAP"] == null)
     {
         fkForTables = new Dictionary<string, Dictionary<string, IForeignKeyColumnInfo[]>>();
         try
         {
             CollectForeignKeysFromSchema();
         }
         catch (Exception e)
         {
             logger.Warn("Retrieving foreign keys info from schema failed.", e);
         }
         CollectForeignKeyFromConfig();
         context["FKMAP"] = fkForTables;
     }
     else
     {
         fkForTables = context["FKMAP"] as Dictionary<string, Dictionary<string, IForeignKeyColumnInfo[]>>;
     }
     OnProcess(context);
 }
 protected abstract void OnProcess(GenerationContext context);
예제 #7
0
 protected override void OnProcess(GenerationContext context)
 {
     this.context = context;
     WireSets();
 }
 protected override void OnProcess(GenerationContext context)
 {
     this.context = context;
     foreach (var e in context.Model.GetEntities())
     {
         logger.Debug(string.Format("{0} working on:", GetType().Name) + e.table);
         if (IsManyToManyJoin(e))
         {
             var kc = GetKeyColumns(context.GetTableMetaData(e.schema, e.name));
             bool hasbag = false;
             if (kc.Length == 2)//bag: key spawn two columns
             {
                 hasbag = true;
                 manytoone[] refs = context.Model.GetManyToOnesOfEntity(e.name);
                 for( int i=0;i<refs.Length;++i )
                 {
                     int other = i == 0?1:0;
                     set set = new set();
                     set.table = e.table;
                     set.schema = e.schema;
                     set.catalog = e.catalog;
                     set.name = context.NamingStrategy.GetNameForCollection(refs[other].@class,0);
                     set.key = new key() { column1 = refs[i].column };
                     set.Item = new manytomany()
                         {
                             @class = refs[other].@class
                             ,Items = new column[]{ new column(){ name = refs[other].column}}
                         };
                     context.Model.AddCollectionToEntity(refs[i].@class,set);
                     logger.Warn(string.Format("Collection {0} on entity {1} is a many to many, need 'inverse' to be specified?",set.name,refs[i].@class));
                 }
             }
             else
             if( kc.Length==1 ) //idbag: a single column with the pair id.
             {
                 hasbag = true;
                 manytoone[] refs = context.Model.GetManyToOnesOfEntity(e.name);
                 for (int i = 0; i < refs.Length; ++i)
                 {
                     int other = i == 0 ? 1 : 0;
                     idbag idbag = new idbag();
                     idbag.collectionid = new collectionid()
                             {
                                 column1 = kc[0].Name
                                 ,
                                 type = typeConverter.GetNHType(kc[0])
                                 ,
                                 length = kc[0].ColumnSize > 0 ? kc[0].ColumnSize.ToString() : null
                                 ,
                                 generator = new generator() { @class = "native" }
                             };
                     logger.Warn(string.Format("IdBag collection {0} on entity {1}: collection key generator defaults to native",idbag.name,refs[i].@class));
                     idbag.table = e.table;
                     idbag.schema = e.schema;
                     idbag.catalog = e.catalog;
                     idbag.name = context.NamingStrategy.GetNameForCollection(refs[other].@class, 0);
                     idbag.key = new key() { column1 = refs[i].column };
                     idbag.Item = new manytomany()
                     {
                         @class = refs[other].@class
                         ,
                         Items = new column[] { new column() { name = refs[other].column } }
                     };
                     context.Model.AddCollectionToEntity(refs[i].@class, idbag);
                     logger.Warn(string.Format("Collection {0} on entity {1} is a many to many, need 'inverse' to be specified?", idbag.name, refs[i].@class));
                 }
             }
             if (hasbag)
             {
                 RemoveCurrentCollections(e);
                 context.Model.RemoveEntity(e.name);
             }
         }
     }
 }
예제 #9
0
 protected override void OnProcess(GenerationContext context)
 {
     currentContext = context;
     WireManyToOnes();
 }
예제 #10
0
 public void Process(GenerationContext context)
 {
     currentContext = context;
     GenerateEntities();
 }
예제 #11
0
 protected override void OnProcess(GenerationContext context)
 {
     currentContext = context;
     foreach (DataRow t in currentContext.FilteredTables)
     {
         IColumnMetadata[] keyColumns = new IColumnMetadata[0];
         var tableMetaData = currentContext.Schema.GetTableMetadata(t, true);
         string entityName = currentContext.NamingStrategy.GetEntityNameFromTableName(tableMetaData.Name);
         keyColumns = GetKeyColumns(tableMetaData);
         logger.Debug(string.Format("{0} working on:",GetType().Name )+ tableMetaData.Name);
         // remove key colums for standard properties
         Array.ForEach(keyColumns, q => currentContext.Model.RemovePropertyByColumn(entityName, q.Name));
         @class clazz = currentContext.Model.GetClassFromEntityName(entityName);
         if (null == clazz)
         {
             logger.Warn(string.Format("Inconsistent meta model:entity {0} does not exists anymore",entityName));
         }
         else
         {
             clazz.Item = CreateId(keyColumns,tableMetaData);
         }
     }
 }