forked from diegose/NHPad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NHibernateSchemaReader.cs
60 lines (56 loc) · 2.49 KB
/
NHibernateSchemaReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Collections.Generic;
using System.Linq;
using LINQPad.Extensibility.DataContext;
using NHibernate;
using NHibernate.Metadata;
using NHibernate.Type;
namespace NHPad
{
public static class NHibernateSchemaReader
{
public static List<ExplorerItem> GetSchema(ISessionFactory sessionFactory)
{
var items = sessionFactory.GetAllClassMetadata().Values
.Select(x => new ExplorerItem(x.EntityName, ExplorerItemKind.QueryableObject, ExplorerIcon.Table)
{
Children = x.PropertyNames.Select(p => GetPropertyItem(x, p)).ToList(),
Tag = x.GetMappedClass(EntityMode.Poco)
}).ToList();
foreach (var property in items.SelectMany(x => x.Children))
{
var type = (IType)property.Tag;
var manyToOne = type as ManyToOneType;
if (manyToOne != null)
property.HyperlinkTarget = items.Single(x => Equals(x.Tag, type.ReturnedClass));
}
return items;
}
private static ExplorerItem GetPropertyItem(IClassMetadata classMetadata, string propertyName)
{
return new ExplorerItem(string.Format("{0}", propertyName),
GetKind(classMetadata, propertyName),
GetIcon(classMetadata, propertyName))
{
Tag = classMetadata.GetPropertyType(propertyName)
};
}
static ExplorerItemKind GetKind(IClassMetadata classMetadata, string propertyName)
{
var propertyType = classMetadata.GetPropertyType(propertyName);
return propertyType.IsCollectionType
? ExplorerItemKind.CollectionLink
: propertyType.IsAssociationType
? ExplorerItemKind.ReferenceLink
: ExplorerItemKind.Property;
}
static ExplorerIcon GetIcon(IClassMetadata classMetadata, string propertyName)
{
var propertyType = classMetadata.GetPropertyType(propertyName);
return propertyType.IsCollectionType
? ExplorerIcon.OneToMany
: propertyType.IsAssociationType
? ExplorerIcon.ManyToOne
: ExplorerIcon.Column;
}
}
}