📄 classbinder.cs
字号:
? ForeignKeyDirection.ForeignKeyFromParent
: ForeignKeyDirection.ForeignKeyToParent);
InitOuterJoinFetchSetting(node, model);
InitLaziness(node, model, true);
XmlAttribute fkNode = node.Attributes["foreign-key"];
if (fkNode != null)
model.ForeignKeyName = fkNode.Value;
XmlAttribute ukName = node.Attributes["property-ref"];
if (ukName != null)
model.ReferencedPropertyName = ukName.Value;
model.ReferencedEntityName = GetEntityName(node, mappings);
model.PropertyName = node.Attributes["name"].Value;
}
protected IDictionary<string, MetaAttribute> GetMetas(XmlNode node)
{
Dictionary<string, MetaAttribute> map = new Dictionary<string, MetaAttribute>();
foreach (XmlNode metaNode in node.SelectNodes(HbmConstants.nsMeta, namespaceManager))
{
string name = metaNode.Attributes["attribute"].Value;
MetaAttribute meta;
if (!map.TryGetValue(name, out meta))
{
meta = new MetaAttribute(name);
map[name] = meta;
}
meta.AddValue(metaNode.InnerText);
}
return map;
}
protected System.Type GetPropertyType(XmlNode definingNode, System.Type containingType, string propertyName)
{
if (definingNode.Attributes["class"] != null)
return ClassForNameChecked(definingNode.Attributes["class"].Value, mappings,
"could not find class: {0}");
else if (containingType == null)
return null;
string access = PropertyAccess(definingNode);
return ReflectHelper.ReflectedPropertyClass(containingType, propertyName, access);
}
protected static bool IsCallable(XmlNode element)
{
XmlAttribute attrib = element.Attributes["callable"];
return attrib != null && "true".Equals(attrib.Value);
}
protected static ExecuteUpdateResultCheckStyle GetResultCheckStyle(XmlNode element, bool callable)
{
XmlAttribute attr = element.Attributes["check"];
if (attr == null)
// use COUNT as the default. This mimics the old behavior, although
// NONE might be a better option moving forward in the case of callable
return ExecuteUpdateResultCheckStyle.Count;
return ExecuteUpdateResultCheckStyle.Parse(attr.Value);
}
protected void ParseFilter(XmlNode filterElement, IFilterable filterable)
{
string name = GetPropertyName(filterElement);
if(name.IndexOf('.') > -1)
throw new MappingException("Filter name can't contain the character '.'(point): " + name);
string condition = filterElement.InnerText;
if (condition == null || StringHelper.IsEmpty(condition.Trim()))
if (filterElement.Attributes != null)
{
XmlAttribute propertyNameNode = filterElement.Attributes["condition"];
condition = (propertyNameNode == null) ? null : propertyNameNode.Value;
}
//TODO: bad implementation, cos it depends upon ordering of mapping doc
// fixing this requires that Collection/PersistentClass gain access
// to the Mappings reference from Configuration (or the filterDefinitions
// map directly) sometime during Configuration.buildSessionFactory
// (after all the types/filter-defs are known and before building
// persisters).
if (StringHelper.IsEmpty(condition))
condition = mappings.GetFilterDefinition(name).DefaultFilterCondition;
if (condition == null)
throw new MappingException("no filter condition found for filter: " + name);
log.Debug("Applying filter [" + name + "] as [" + condition + "]");
filterable.AddFilter(name, condition);
}
protected void BindColumns(XmlNode node, SimpleValue model, bool isNullable, bool autoColumn,
string propertyPath)
{
Table table = model.Table;
//COLUMN(S)
XmlAttribute columnAttribute = node.Attributes["column"];
if (columnAttribute == null)
{
int count = 0;
foreach (XmlNode columnElement in node.SelectNodes(HbmConstants.nsColumn, namespaceManager))
{
Column col = new Column();
col.Value = model;
col.TypeIndex = count++;
BindColumn(columnElement, col, isNullable);
string name = columnElement.Attributes["name"].Value;
col.Name = mappings.NamingStrategy.ColumnName(name);
if (table != null)
table.AddColumn(col);
//table=null -> an association, fill it in later
model.AddColumn(col);
//column index
BindIndex(columnElement.Attributes["index"], table, col);
//column group index (although it can serve as a separate column index)
BindIndex(node.Attributes["index"], table, col);
BindUniqueKey(columnElement.Attributes["unique-key"], table, col);
BindUniqueKey(node.Attributes["unique-key"], table, col);
}
}
else
{
Column col = new Column();
col.Value = model;
BindColumn(node, col, isNullable);
col.Name = mappings.NamingStrategy.ColumnName(columnAttribute.Value);
if (table != null)
table.AddColumn(col);
model.AddColumn(col);
//column group index (although can serve as a separate column index)
BindIndex(node.Attributes["index"], table, col);
BindUniqueKey(node.Attributes["unique-key"], table, col);
}
if (autoColumn && model.ColumnSpan == 0)
{
Column col = new Column();
col.Value = model;
BindColumn(node, col, isNullable);
col.Name = mappings.NamingStrategy.PropertyToColumnName(propertyPath);
model.Table.AddColumn(col);
model.AddColumn(col);
//column group index (although can serve as a separate column index)
BindIndex(node.Attributes["index"], table, col);
BindUniqueKey(node.Attributes["unique-key"], table, col);
}
}
protected static void BindColumn(XmlNode node, Column model, bool isNullable)
{
XmlAttribute lengthNode = node.Attributes["length"];
if (lengthNode != null)
model.Length = int.Parse(lengthNode.Value);
XmlAttribute nullNode = node.Attributes["not-null"];
model.IsNullable = (nullNode != null) ? !StringHelper.BooleanValue(nullNode.Value) : isNullable;
XmlAttribute unqNode = node.Attributes["unique"];
model.IsUnique = unqNode != null && StringHelper.BooleanValue(unqNode.Value);
XmlAttribute chkNode = node.Attributes["check"];
model.CheckConstraint = chkNode != null ? chkNode.Value : string.Empty;
XmlAttribute typeNode = node.Attributes["sql-type"];
model.SqlType = (typeNode == null) ? null : typeNode.Value;
}
protected static void BindIndex(XmlAttribute indexAttribute, Table table, Column column)
{
if (indexAttribute != null && table != null)
{
StringTokenizer tokens = new StringTokenizer(indexAttribute.Value, ", ");
foreach (string token in tokens)
table.GetOrCreateIndex(token).AddColumn(column);
}
}
protected static void BindUniqueKey(XmlAttribute uniqueKeyAttribute, Table table, Column column)
{
if (uniqueKeyAttribute != null && table != null)
{
StringTokenizer tokens = new StringTokenizer(uniqueKeyAttribute.Value, ", ");
foreach (string token in tokens)
table.GetOrCreateUniqueKey(token).AddColumn(column);
}
}
protected static void InitLaziness(XmlNode node, ToOne fetchable, bool defaultLazy)
{
XmlAttribute lazyNode = node.Attributes["lazy"];
if (lazyNode != null && "no-proxy".Equals(lazyNode.Value))
//fetchable.UnwrapProxy = true;
fetchable.IsLazy = true;
//TODO: better to degrade to lazy="false" if uninstrumented
else
InitLaziness(node, fetchable, "proxy", defaultLazy);
}
protected static void InitLaziness(XmlNode node, IFetchable fetchable, string proxyVal, bool defaultLazy)
{
XmlAttribute lazyNode = node.Attributes["lazy"];
bool isLazyTrue = lazyNode == null
?
defaultLazy && fetchable.IsLazy
: //fetch="join" overrides default laziness
lazyNode.Value.Equals(proxyVal); //fetch="join" overrides default laziness
fetchable.IsLazy = isLazyTrue;
}
protected static void InitOuterJoinFetchSetting(XmlNode node, IFetchable model)
{
XmlAttribute fetchNode = node.Attributes["fetch"];
FetchMode fetchStyle;
bool lazy = true;
if (fetchNode == null)
{
XmlAttribute jfNode = node.Attributes["outer-join"];
if (jfNode == null)
if ("many-to-many".Equals(node.Name))
{
//NOTE SPECIAL CASE:
// default to join and non-lazy for the "second join"
// of the many-to-many
lazy = false;
fetchStyle = FetchMode.Join;
}
else if ("one-to-one".Equals(node.Name))
{
//NOTE SPECIAL CASE:
// one-to-one constrained=falase cannot be proxied,
// so default to join and non-lazy
lazy = ((OneToOne) model).IsConstrained;
fetchStyle = lazy ? FetchMode.Default : FetchMode.Join;
}
else
fetchStyle = FetchMode.Default;
else
{
// use old (HB 2.1) defaults if outer-join is specified
string eoj = jfNode.Value;
if ("auto".Equals(eoj))
fetchStyle = FetchMode.Default;
else
{
bool join = "true".Equals(eoj);
fetchStyle = join
?
FetchMode.Join
:
FetchMode.Select;
}
}
}
else
{
bool join = "join".Equals(fetchNode.Value);
fetchStyle = join
?
FetchMode.Join
:
FetchMode.Select;
}
model.FetchMode = fetchStyle;
model.IsLazy = lazy;
}
protected string PropertyAccess(XmlNode node)
{
XmlAttribute accessNode = node.Attributes["access"];
return accessNode != null ? accessNode.Value : mappings.DefaultAccess;
}
protected IType GetTypeFromXML(XmlNode node)
{
IType type;
IDictionary parameters = null;
XmlAttribute typeAttribute = node.Attributes["type"];
if (typeAttribute == null)
typeAttribute = node.Attributes["id-type"]; //for an any
string typeName;
if (typeAttribute != null)
typeName = typeAttribute.Value;
else
{
XmlNode typeNode = node.SelectSingleNode(HbmConstants.nsType, namespaceManager);
if (typeNode == null) //we will have to use reflection
return null;
XmlAttribute nameAttribute = typeNode.Attributes["name"]; //we know it exists because the schema validate it
typeName = nameAttribute.Value;
parameters = new Hashtable();
foreach (XmlNode childNode in typeNode.ChildNodes)
parameters.Add(childNode.Attributes["name"].Value,
childNode.InnerText.Trim());
}
type = TypeFactory.HeuristicType(typeName, parameters);
if (type == null)
throw new MappingException("could not interpret type: " + typeAttribute.Value);
return type;
}
private static string GetEntityName(XmlNode elem, Mappings model)
{
// TODO: H3.2 Implement real entityName (look at IEntityPersister for feature)
//string entityName = XmlHelper.GetAttributeValue(elem, "entity-name");
//return entityName == null ? GetClassName( elem.Attributes[ "class" ], model ) : entityName;
XmlAttribute att = elem.Attributes["class"];
if (att == null)
return null;
return GetClassName(att.Value, model);
}
private static string GetQualifiedClassName(XmlNode elem, Mappings model)
{
XmlAttribute att = elem.Attributes["class"];
if (att == null)
return null;
return GetQualifiedClassName(att.Value, model);
}
protected XmlNodeList SelectNodes(XmlNode node, string xpath)
{
return node.SelectNodes(xpath, namespaceManager);
}
protected static string GetPropertyName(XmlNode node)
{
if (node.Attributes != null)
{
XmlAttribute propertyNameNode = node.Attributes["name"];
return (propertyNameNode == null) ? null : propertyNameNode.Value;
}
return null;
}
protected static void LogMappedProperty(Mapping.Property property)
{
if (log.IsDebugEnabled)
{
string msg = "Mapped property: " + property.Name;
string columns = Columns(property.Value);
if (columns.Length > 0)
msg += " -> " + columns;
if (property.Type != null)
msg += ", type: " + property.Type.Name;
log.Debug(msg);
}
}
protected static void BindIndex(string indexAttribute, Table table, Column column)
{
if (indexAttribute != null && table != null)
{
StringTokenizer tokens = new StringTokenizer(indexAttribute, ", ");
foreach (string token in tokens)
table.GetOrCreateIndex(token).AddColumn(column);
}
}
protected static void BindUniqueKey(string uniqueKeyAttribute, Table table, Column column)
{
if (uniqueKeyAttribute != null && table != null)
{
StringTokenizer tokens = new StringTokenizer(uniqueKeyAttribute, ", ");
foreach (string token in tokens)
table.GetOrCreateUniqueKey(token).AddColumn(column);
}
}
protected static void BindColumn(HbmColumn columnSchema, Column column, bool isNullable)
{
if (columnSchema.length != null)
column.Length = int.Parse(columnSchema.length);
column.IsNullable = columnSchema.notnullSpecified ? !columnSchema.notnull : isNullable;
column.IsUnique = columnSchema.uniqueSpecified && columnSchema.unique;
column.CheckConstraint = columnSchema.check ?? string.Empty;
column.SqlType = columnSchema.sqltype;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -