/// <summary> /// Populates the element (including children) with information from its XML definition. /// </summary> /// <param name="Input">XML definition.</param> public override void FromXml(XmlElement Input) { base.FromXml(Input); this.expression = new ExpressionAttribute(Input, "expression"); this.variable = new StringAttribute(Input, "variable"); }
public virtual void SetTable(ISqlBuilder sqlBuilder, MappingSchema mappingSchema, SqlTable table, MethodCallExpression methodCall, Func <Expression, ColumnDescriptor?, ISqlExpression> converter) { table.SqlTableType = SqlTableType.Function; table.Name = Name ?? methodCall.Method.Name; table.PhysicalName = Name ?? methodCall.Method.Name; var expressionStr = table.Name; ExpressionAttribute.PrepareParameterValues(methodCall, ref expressionStr, false, out var knownExpressions, out var genericTypes); if (string.IsNullOrEmpty(expressionStr)) { throw new LinqToDBException($"Cannot retrieve Table Function body from expression '{methodCall}'."); } // Add two fake expressions, TableName and Alias knownExpressions.Insert(0, null); knownExpressions.Insert(0, null); table.TableArguments = ExpressionAttribute.PrepareArguments(expressionStr !, ArgIndices, addDefault: true, knownExpressions, genericTypes, converter).Skip(2).ToArray(); if (Schema != null) { table.Schema = Schema; } if (Database != null) { table.Database = Database; } if (Server != null) { table.Server = Server; } }
/// <summary> /// Populates the element (including children) with information from its XML definition. /// </summary> /// <param name="Input">XML definition.</param> public override void FromXml(XmlElement Input) { base.FromXml(Input); this.expression = new ExpressionAttribute(Input, "expression"); this.testAfter = new BooleanAttribute(Input, "testAfter"); }
/// <summary> /// Populates the element (including children) with information from its XML definition. /// </summary> /// <param name="Input">XML definition.</param> public override void FromXml(XmlElement Input) { base.FromXml(Input); this.from = new ExpressionAttribute(Input, "from"); this.to = new ExpressionAttribute(Input, "to"); this.step = new ExpressionAttribute(Input, "step"); this.variable = new StringAttribute(Input, "variable"); }
static bool ExpressionProperty(FieldInfo p_fieldInfo, Object p_object, GUIContent p_name, IReferencable p_reference) { ExpressionAttribute expressionAttribute = p_fieldInfo.GetAttribute <ExpressionAttribute>(); if (expressionAttribute == null) { return(false); } FieldInfo expressionField = p_object.GetType().GetField(expressionAttribute.expression); FieldInfo useExpressionField = p_object.GetType().GetField(expressionAttribute.useExpression); EditorGUI.BeginChangeCheck(); EditorGUILayout.BeginHorizontal(); if ((bool)useExpressionField.GetValue(p_object)) { GUILayout.BeginHorizontal(); GUI.color = DashEditorCore.EditorConfig.theme.ParameterColor; GUILayout.Label(p_name, GUILayout.Width(160)); HandleReferencing(p_reference, expressionField, true); string expression = GUILayout.TextArea((string)expressionField.GetValue(p_object), GUILayout.ExpandWidth(true)); GUI.color = Color.white; expressionField.SetValue(p_object, expression); GUILayout.EndHorizontal(); } else { PropertyField(p_fieldInfo, p_object, p_reference, p_fieldInfo); } bool useExpression = (bool)useExpressionField.GetValue(p_object); GUI.color = useExpression ? DashEditorCore.EditorConfig.theme.ParameterColor : Color.gray; if (GUILayout.Button(IconManager.GetIcon("Settings_Icon"), GUIStyle.none, GUILayout.Height(16), GUILayout.MaxWidth(16))) { useExpressionField.SetValue(p_object, !useExpression); } GUI.color = Color.white; EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(4); if (EditorGUI.EndChangeCheck()) { return(true); } return(false); }
/// <summary> /// Populates the element (including children) with information from its XML definition. /// </summary> /// <param name="Input">XML definition.</param> public override void FromXml(XmlElement Input) { base.FromXml(Input); this.width = new LengthAttribute(Input, "width"); this.height = new LengthAttribute(Input, "height"); this.maxWidth = new LengthAttribute(Input, "maxWidth"); this.maxHeight = new LengthAttribute(Input, "maxHeight"); this.minWidth = new LengthAttribute(Input, "minWidth"); this.minHeight = new LengthAttribute(Input, "minHeight"); this.keepAspectRatio = new BooleanAttribute(Input, "keepAspectRatio"); this.overflow = new EnumAttribute <Overflow>(Input, "overflow"); this.onClick = new ExpressionAttribute(Input, "onClick"); }
public virtual void SetTable <TContext>(TContext context, ISqlBuilder sqlBuilder, MappingSchema mappingSchema, SqlTable table, MethodCallExpression methodCall, Func <TContext, Expression, ColumnDescriptor?, ISqlExpression> converter) { table.SqlTableType = SqlTableType.Function; var expressionStr = table.Expression = Name ?? methodCall.Method.Name !; ExpressionAttribute.PrepareParameterValues(methodCall, ref expressionStr, false, out var knownExpressions, false, out var genericTypes); if (string.IsNullOrEmpty(expressionStr)) { throw new LinqToDBException($"Cannot retrieve Table Function body from expression '{methodCall}'."); } table.TableName = new SqlObjectName( expressionStr !, Schema: Schema ?? table.TableName.Schema, Database: Database ?? table.TableName.Database, Server: Server ?? table.TableName.Server, Package: Package ?? table.TableName.Package); table.TableArguments = ExpressionAttribute.PrepareArguments(context, string.Empty, ArgIndices, true, knownExpressions, genericTypes, converter); }
/// <summary> /// Populates the element (including children) with information from its XML definition. /// </summary> /// <param name="Input">XML definition.</param> public override void FromXml(XmlElement Input) { base.FromXml(Input); this.condition = new ExpressionAttribute(Input, "condition"); foreach (XmlNode Node in Input.ChildNodes) { if (Node is XmlElement E) { ILayoutElement Child = this.Document.CreateElement(E, this); if (Child is True True) { if (this.ifTrue is null) { this.ifTrue = True; } else { throw new LayoutSyntaxException("If statement already has a True statement."); } } else if (Child is False False) { if (this.ifFalse is null) { this.ifFalse = False; } else { throw new LayoutSyntaxException("If statement already has a False statement."); } } else { throw new LayoutSyntaxException("Unrecognized child element in If statement: " + E.NamespaceURI + "#" + E.LocalName); } } } }
private static Dictionary <string, Type> GetValidExpressionTypes(Type requiredExpressionValueType) { if (!_expressionTypeByEvaluationTypeCache.ContainsKey(requiredExpressionValueType)) { Dictionary <string, Type> expressionTypes = Assembly.GetAssembly(typeof(Expression)).GetTypes().Where( (t) => { if (!typeof(Expression).IsAssignableFrom(t) || t.IsAbstract) { return(false); } ExpressionAttribute[] expressionAttributes = (ExpressionAttribute[])t.GetCustomAttributes(typeof(ExpressionAttribute), false); if (expressionAttributes.Length != 0) { ExpressionAttribute expressionAttribute = expressionAttributes[0]; if (expressionAttribute.IsDynamicType) { return(true); } else { return(requiredExpressionValueType.IsAssignableFrom(expressionAttribute.EvaluationType)); } } else { return(requiredExpressionValueType.IsAssignableFrom(typeof(object))); } }).ToDictionary((t) => t.Name); expressionTypes.Add("-", null); _expressionTypeByEvaluationTypeCache.Add(requiredExpressionValueType, expressionTypes); } return(_expressionTypeByEvaluationTypeCache[requiredExpressionValueType]); }
public void Initialize() { if (isInitialized) { return; } lock (this.initializeLock) { Dictionary <string, FieldInfo> rowFields; Dictionary <string, PropertyInfo> rowProperties; GetRowFieldsAndProperties(out rowFields, out rowProperties); foreach (var fieldInfo in this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)) { if (fieldInfo.FieldType.IsSubclassOf(typeof(Field))) { var field = (Field)fieldInfo.GetValue(this); PropertyInfo property; if (!rowProperties.TryGetValue(fieldInfo.Name, out property)) { property = null; } ColumnAttribute column = null; DisplayNameAttribute display = null; SizeAttribute size = null; ExpressionAttribute expression = null; ScaleAttribute scale = null; MinSelectLevelAttribute selectLevel = null; ForeignKeyAttribute foreignKey = null; LeftJoinAttribute foreignJoin = null; DefaultValueAttribute defaultValue = null; TextualFieldAttribute textualField = null; DateTimeKindAttribute dateTimeKind = null; FieldFlags addFlags = (FieldFlags)0; FieldFlags removeFlags = (FieldFlags)0; if (property != null) { column = property.GetCustomAttribute <ColumnAttribute>(false); display = property.GetCustomAttribute <DisplayNameAttribute>(false); size = property.GetCustomAttribute <SizeAttribute>(false); expression = property.GetCustomAttribute <ExpressionAttribute>(false); scale = property.GetCustomAttribute <ScaleAttribute>(false); selectLevel = property.GetCustomAttribute <MinSelectLevelAttribute>(false); foreignKey = property.GetCustomAttribute <ForeignKeyAttribute>(false); foreignJoin = property.GetCustomAttributes <LeftJoinAttribute>(false).FirstOrDefault(x => x.ToTable == null && x.OnCriteria == null); defaultValue = property.GetCustomAttribute <DefaultValueAttribute>(false); textualField = property.GetCustomAttribute <TextualFieldAttribute>(false); dateTimeKind = property.GetCustomAttribute <DateTimeKindAttribute>(false); var insertable = property.GetCustomAttribute <InsertableAttribute>(false); var updatable = property.GetCustomAttribute <UpdatableAttribute>(false); if (insertable != null && !insertable.Value) { removeFlags |= FieldFlags.Insertable; } if (updatable != null && !updatable.Value) { removeFlags |= FieldFlags.Updatable; } foreach (var attr in property.GetCustomAttributes <SetFieldFlagsAttribute>(false)) { addFlags |= attr.Add; removeFlags |= attr.Remove; } } if (ReferenceEquals(null, field)) { if (property == null) { throw new InvalidProgramException(String.Format( "Field {0} in type {1} is null and has no corresponding property in entity!", fieldInfo.Name, rowType.Name)); } object[] prm = new object[7]; prm[0] = this; // owner prm[1] = column == null ? property.Name : (column.Name.TrimToNull() ?? property.Name); prm[2] = display != null ? new LocalText(display.DisplayName) : null; prm[3] = size != null ? size.Value : 0; prm[4] = (FieldFlags.Default ^ removeFlags) | addFlags; prm[5] = null; prm[6] = null; FieldInfo storage; if (rowFields.TryGetValue("_" + property.Name, out storage) || rowFields.TryGetValue("m_" + property.Name, out storage) || rowFields.TryGetValue(property.Name, out storage)) { prm[5] = CreateFieldGetMethod(storage); prm[6] = CreateFieldSetMethod(storage); } field = (Field)Activator.CreateInstance(fieldInfo.FieldType, prm); fieldInfo.SetValue(this, field); } else { if (size != null) { throw new InvalidProgramException(String.Format( "Field size '{0}' in type {1} can't be overridden by Size attribute!", fieldInfo.Name, rowType.Name)); } if (display != null) { field.Caption = new LocalText(display.DisplayName); } if ((int)addFlags != 0 || (int)removeFlags != 0) { field.Flags = (field.Flags ^ removeFlags) | addFlags; } if (column != null && String.Compare(column.Name, field.Name, StringComparison.OrdinalIgnoreCase) != 0) { throw new InvalidProgramException(String.Format( "Field name '{0}' in type {1} can't be overridden by Column name attribute!", fieldInfo.Name, rowType.Name)); } } if (scale != null) { field.Scale = scale.Value; } if (defaultValue != null) { field.DefaultValue = defaultValue.Value; } if (selectLevel != null) { field.MinSelectLevel = selectLevel.Value; } if (expression != null) { field.Expression = expression.Value; } if (foreignKey != null) { field.ForeignTable = foreignKey.Table; field.ForeignField = foreignKey.Field; } if (foreignJoin != null) { field.ForeignJoinAlias = new LeftJoin(this.joins, field.ForeignTable, foreignJoin.Alias, new Criteria(foreignJoin.Alias, field.ForeignField) == new Criteria(field)); } if (textualField != null) { field.textualField = textualField.Value; } if (dateTimeKind != null && field is DateTimeField) { ((DateTimeField)field).DateTimeKind = dateTimeKind.Value; } if (property != null) { if (property.PropertyType != null && field is IEnumTypeField) { if (property.PropertyType.IsEnum) { (field as IEnumTypeField).EnumType = property.PropertyType; } else { var nullableType = Nullable.GetUnderlyingType(property.PropertyType); if (nullableType != null && nullableType.IsEnum) { (field as IEnumTypeField).EnumType = nullableType; } } } foreach (var attr in property.GetCustomAttributes <LeftJoinAttribute>()) { if (attr.ToTable != null && attr.OnCriteria != null) { new LeftJoin(this.joins, attr.ToTable, attr.Alias, new Criteria(attr.Alias, attr.OnCriteria) == new Criteria(field)); } } field.PropertyName = property.Name; this.byPropertyName[field.PropertyName] = field; field.CustomAttributes = property.GetCustomAttributes(false); } } } foreach (var attr in this.rowType.GetCustomAttributes <LeftJoinAttribute>()) { new LeftJoin(this.joins, attr.ToTable, attr.Alias, new Criteria(attr.OnCriteria)); } foreach (var attr in this.rowType.GetCustomAttributes <OuterApplyAttribute>()) { new OuterApply(this.joins, attr.InnerQuery, attr.Alias); } var propertyDescriptorArray = new PropertyDescriptor[this.Count]; for (int i = 0; i < this.Count; i++) { var field = this[i]; propertyDescriptorArray[i] = new FieldDescriptor(field); } this.propertyDescriptors = new PropertyDescriptorCollection(propertyDescriptorArray); InferTextualFields(); AfterInitialize(); } isInitialized = true; }
public void Initialize() { if (isInitialized) { return; } lock (this.initializeLock) { Dictionary <string, FieldInfo> rowFields; Dictionary <string, IPropertyInfo> rowProperties; GetRowFieldsAndProperties(out rowFields, out rowProperties); var expressionSelector = new DialectExpressionSelector(connectionKey); var rowCustomAttributes = this.rowType.GetCustomAttributes().ToList(); foreach (var fieldInfo in this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)) { if (fieldInfo.FieldType.IsSubclassOf(typeof(Field))) { var field = (Field)fieldInfo.GetValue(this); IPropertyInfo property; if (!rowProperties.TryGetValue(fieldInfo.Name, out property)) { property = null; } ColumnAttribute column = null; DisplayNameAttribute display = null; SizeAttribute size = null; ExpressionAttribute expression = null; ScaleAttribute scale = null; MinSelectLevelAttribute selectLevel = null; ForeignKeyAttribute foreignKey = null; LeftJoinAttribute leftJoin = null; InnerJoinAttribute innerJoin = null; DefaultValueAttribute defaultValue = null; TextualFieldAttribute textualField = null; DateTimeKindAttribute dateTimeKind = null; PermissionAttributeBase readPermission = null; PermissionAttributeBase insertPermission = null; PermissionAttributeBase updatePermission = null; FieldFlags addFlags = (FieldFlags)0; FieldFlags removeFlags = (FieldFlags)0; OriginPropertyDictionary propertyDictionary = null; if (property != null) { var origin = property.GetAttribute <OriginAttribute>(); column = property.GetAttribute <ColumnAttribute>(); display = property.GetAttribute <DisplayNameAttribute>(); size = property.GetAttribute <SizeAttribute>(); var expressions = property.GetAttributes <ExpressionAttribute>(); if (expressions.Any()) { expression = expressionSelector.GetBestMatch(expressions, x => x.Dialect); } scale = property.GetAttribute <ScaleAttribute>(); selectLevel = property.GetAttribute <MinSelectLevelAttribute>(); foreignKey = property.GetAttribute <ForeignKeyAttribute>(); leftJoin = property.GetAttributes <LeftJoinAttribute>() .FirstOrDefault(x => x.ToTable == null && x.OnCriteria == null); innerJoin = property.GetAttributes <InnerJoinAttribute>() .FirstOrDefault(x => x.ToTable == null && x.OnCriteria == null); defaultValue = property.GetAttribute <DefaultValueAttribute>(); textualField = property.GetAttribute <TextualFieldAttribute>(); dateTimeKind = property.GetAttribute <DateTimeKindAttribute>(); readPermission = property.GetAttribute <ReadPermissionAttribute>(); insertPermission = property.GetAttribute <InsertPermissionAttribute>() ?? property.GetAttribute <ModifyPermissionAttribute>() ?? readPermission; updatePermission = property.GetAttribute <UpdatePermissionAttribute>() ?? property.GetAttribute <ModifyPermissionAttribute>() ?? readPermission; if (origin != null) { propertyDictionary = propertyDictionary ?? OriginPropertyDictionary.GetPropertyDictionary(this.rowType); try { if (!expressions.Any() && expression == null) { expression = new ExpressionAttribute(propertyDictionary.OriginExpression( property.Name, origin, expressionSelector, "", rowCustomAttributes)); } if (display == null) { display = new DisplayNameAttribute(propertyDictionary.OriginDisplayName(property.Name, origin)); } if (size == null) { size = propertyDictionary.OriginAttribute <SizeAttribute>(property.Name, origin); } if (scale == null) { scale = propertyDictionary.OriginAttribute <ScaleAttribute>(property.Name, origin); } } catch (DivideByZeroException) { throw new InvalidProgramException(String.Format( "Infinite recursion detected while determining origins " + "for property '{0}' on row type '{1}'", property.Name, rowType.FullName)); } } var insertable = property.GetAttribute <InsertableAttribute>(); var updatable = property.GetAttribute <UpdatableAttribute>(); if (insertable != null && !insertable.Value) { removeFlags |= FieldFlags.Insertable; } if (updatable != null && !updatable.Value) { removeFlags |= FieldFlags.Updatable; } foreach (var attr in property.GetAttributes <SetFieldFlagsAttribute>()) { addFlags |= attr.Add; removeFlags |= attr.Remove; } } if (ReferenceEquals(null, field)) { if (property == null) { throw new InvalidProgramException(String.Format( "Field {0} in type {1} is null and has no corresponding property in entity!", fieldInfo.Name, rowType.Name)); } object[] prm = new object[7]; prm[0] = this; // owner prm[1] = column == null ? property.Name : (column.Name.TrimToNull() ?? property.Name); prm[2] = display != null ? new LocalText(display.DisplayName) : null; prm[3] = size != null ? size.Value : 0; var defaultFlags = FieldFlags.Default; if (fieldInfo.FieldType.GetCustomAttribute <NotMappedAttribute>() != null) { defaultFlags |= FieldFlags.NotMapped; } prm[4] = (defaultFlags ^ removeFlags) | addFlags; prm[5] = null; prm[6] = null; FieldInfo storage; if (rowFields.TryGetValue("_" + property.Name, out storage) || rowFields.TryGetValue("m_" + property.Name, out storage) || rowFields.TryGetValue(property.Name, out storage)) { prm[5] = CreateFieldGetMethod(storage); prm[6] = CreateFieldSetMethod(storage); } field = (Field)Activator.CreateInstance(fieldInfo.FieldType, prm); fieldInfo.SetValue(this, field); } else { if (size != null) { throw new InvalidProgramException(String.Format( "Field size '{0}' in type {1} can't be overridden by Size attribute!", fieldInfo.Name, rowType.FullName)); } if (display != null) { field.Caption = new LocalText(display.DisplayName); } if ((int)addFlags != 0 || (int)removeFlags != 0) { field.Flags = (field.Flags ^ removeFlags) | addFlags; } if (column != null && String.Compare(column.Name, field.Name, StringComparison.OrdinalIgnoreCase) != 0) { throw new InvalidProgramException(String.Format( "Field name '{0}' in type {1} can't be overridden by Column name attribute!", fieldInfo.Name, rowType.FullName)); } } if (scale != null) { field.Scale = scale.Value; } if (defaultValue != null) { field.DefaultValue = defaultValue.Value; } if (selectLevel != null) { field.MinSelectLevel = selectLevel.Value; } if (expression != null) { field.Expression = expression.Value; } if (foreignKey != null) { field.ForeignTable = foreignKey.Table; field.ForeignField = foreignKey.Field; } if ((leftJoin != null || innerJoin != null) && field.ForeignTable.IsEmptyOrNull()) { throw new InvalidProgramException(String.Format("Property {0} of row type {1} has a [LeftJoin] or [InnerJoin] attribute " + "but its foreign table is undefined. Make sure it has a valid [ForeignKey] attribute!", fieldInfo.Name, rowType.FullName)); } if ((leftJoin != null || innerJoin != null) && field.ForeignField.IsEmptyOrNull()) { throw new InvalidProgramException(String.Format("Property {0} of row type {1} has a [LeftJoin] or [InnerJoin] attribute " + "but its foreign field is undefined. Make sure it has a valid [ForeignKey] attribute!", fieldInfo.Name, rowType.FullName)); } if (leftJoin != null) { field.ForeignJoinAlias = new LeftJoin(this.joins, field.ForeignTable, leftJoin.Alias, new Criteria(leftJoin.Alias, field.ForeignField) == new Criteria(field)); } if (innerJoin != null) { field.ForeignJoinAlias = new InnerJoin(this.joins, field.ForeignTable, innerJoin.Alias, new Criteria(innerJoin.Alias, field.ForeignField) == new Criteria(field)); } if (textualField != null) { field.textualField = textualField.Value; } if (dateTimeKind != null && field is DateTimeField) { ((DateTimeField)field).DateTimeKind = dateTimeKind.Value; } if (readPermission != null) { field.readPermission = readPermission.Permission ?? "?"; } if (insertPermission != null) { field.insertPermission = insertPermission.Permission ?? "?"; } if (updatePermission != null) { field.updatePermission = updatePermission.Permission ?? "?"; } if (property != null) { if (property.PropertyType != null && field is IEnumTypeField) { if (property.PropertyType.IsEnum) { (field as IEnumTypeField).EnumType = property.PropertyType; } else { var nullableType = Nullable.GetUnderlyingType(property.PropertyType); if (nullableType != null && nullableType.IsEnum) { (field as IEnumTypeField).EnumType = nullableType; } } } foreach (var attr in property.GetAttributes <LeftJoinAttribute>()) { if (attr.ToTable != null && attr.OnCriteria != null) { new LeftJoin(this.joins, attr.ToTable, attr.Alias, new Criteria(attr.Alias, attr.OnCriteria) == new Criteria(field)); } } foreach (var attr in property.GetAttributes <InnerJoinAttribute>()) { if (attr.ToTable != null && attr.OnCriteria != null) { new InnerJoin(this.joins, attr.ToTable, attr.Alias, new Criteria(attr.Alias, attr.OnCriteria) == new Criteria(field)); } } field.PropertyName = property.Name; this.byPropertyName[field.PropertyName] = field; field.customAttributes = property.GetAttributes <Attribute>().ToArray(); } } } foreach (var attr in rowCustomAttributes.OfType <LeftJoinAttribute>()) { new LeftJoin(this.joins, attr.ToTable, attr.Alias, new Criteria(attr.OnCriteria)); } foreach (var attr in rowCustomAttributes.OfType <InnerJoinAttribute>()) { new InnerJoin(this.joins, attr.ToTable, attr.Alias, new Criteria(attr.OnCriteria)); } foreach (var attr in rowCustomAttributes.OfType <OuterApplyAttribute>()) { new OuterApply(this.joins, attr.InnerQuery, attr.Alias); } #if !COREFX var propertyDescriptorArray = new PropertyDescriptor[this.Count]; for (int i = 0; i < this.Count; i++) { var field = this[i]; propertyDescriptorArray[i] = new FieldDescriptor(field); } this.propertyDescriptors = new PropertyDescriptorCollection(propertyDescriptorArray); #endif InferTextualFields(); AfterInitialize(); } isInitialized = true; }
/// <summary> /// Populates the element (including children) with information from its XML definition. /// </summary> /// <param name="Input">XML definition.</param> public override void FromXml(XmlElement Input) { base.FromXml(Input); this.condition = new ExpressionAttribute(Input, "condition"); }