Beispiel #1
0
        internal static string GetPropertyOrColumnName(PropertyInfo property, bool ignoreMappings, IEntityMap entityMap, bool isColumn)
        {
            string propertyOrColumnName;

            if (ignoreMappings)
            {
                propertyOrColumnName = property.Name;
            }
            else if (entityMap != null)
            {
                var propertyMap = entityMap.Properties.FirstOrDefault(p => p.Property.PropertyName == property.Name);
                if (propertyMap != null && ((isColumn && propertyMap.Property.MappedColumnName != null) || propertyMap.Property.MappedPropertyName != null))
                {
                    propertyOrColumnName = isColumn ? propertyMap.Property.MappedColumnName : propertyMap.Property.MappedPropertyName;
                }
                else
                {
                    propertyOrColumnName = isColumn ? MapColumnAttribute.GetMappedColumnName(property) : MapPropertyAttribute.GetMappedPropertyName(property);
                }
            }
            else
            {
                propertyOrColumnName = isColumn ? MapColumnAttribute.GetMappedColumnName(property) : MapPropertyAttribute.GetMappedPropertyName(property);
            }
            return(propertyOrColumnName);
        }
Beispiel #2
0
        public ReflectedProperty(PropertyInfo property, int position = -1, bool readAttributes = true)
        {
            PropertyName   = property.Name;
            PropertyType   = property.PropertyType;
            IsPersistent   = true;
            IsSelectable   = true;
            IsSerializable = true;
            IsBinary       = property.PropertyType == typeof(byte[]);
            IsSimpleList   = !IsBinary && Reflector.IsSimpleList(property.PropertyType);
            IsDataEntity   = Reflector.IsDataEntity(property.PropertyType);

            Type elementType;

            IsDataEntityList  = Reflector.IsDataEntityList(property.PropertyType, out elementType);
            ElementType       = elementType;
            IsPolymorphicList = IsDataEntityList && elementType != null && elementType.IsAbstract && !elementType.IsInterface;

            if (IsDataEntityList)
            {
                IsList          = true;
                IsListInterface = property.PropertyType.GetGenericTypeDefinition() == typeof(IList <>);
            }
            else
            {
                IsList = !IsBinary && Reflector.IsList(property.PropertyType);
                if (IsList)
                {
                    ElementType     = Reflector.GetElementType(property.PropertyType);
                    IsListInterface = property.PropertyType.GetGenericTypeDefinition() == typeof(IList <>);
                }
            }

            IsSimpleType   = !IsBinary && Reflector.IsSimpleType(property.PropertyType);
            IsNullableType = Reflector.IsNullableType(property.PropertyType);
            CanWrite       = property.CanWrite;
            CanRead        = property.CanRead;
            Position       = position;
            IsObject       = IsDataEntity || (property.PropertyType.IsClass && !IsSimpleType && !IsSimpleList && !IsBinary && !IsList);
            IsObjectList   = IsDataEntityList || (IsList && ElementType != null && ElementType.IsClass && !Reflector.IsSimpleType(ElementType) && !Reflector.IsSimpleList(ElementType));

            Converter = null;

            MappedColumnName = property.Name;

            if (readAttributes)
            {
                if (IsListInterface)
                {
                    Sorted   = property.GetCustomAttributes(typeof(SortedAttribute), false).Cast <SortedAttribute>().FirstOrDefault();
                    Distinct = property.GetCustomAttributes(typeof(DistinctAttribute), false).Cast <DistinctAttribute>().FirstOrDefault();
                }

                MappedColumnName   = MapColumnAttribute.GetMappedColumnName(property);
                MappedPropertyName = MapPropertyAttribute.GetMappedPropertyName(property);

                var typeConverter = TypeConverterAttribute.GetTypeConverter(property);
                if (typeConverter != null)
                {
                    AddConverter(typeConverter.TypeConverterType);
                }

                var items = property.GetCustomAttributes(true).OfType <PropertyAttribute>();
                foreach (var item in items)
                {
                    var primaryKeyAttribute = item as PrimaryKeyAttribute;
                    if (primaryKeyAttribute != null)
                    {
                        IsPrimaryKey = true;
                        KeyPosition  = primaryKeyAttribute.Position;
                    }
                    else
                    {
                        var generategAttribute = item as Generate.UsingAttribute;
                        if (generategAttribute != null)
                        {
                            Generator = generategAttribute.Type;
                        }
                        else if (item is Generate.NativeAttribute)
                        {
                            IsAutoGenerated = true;
                        }
                        else
                        {
                            var referencesAttribute = item as ReferencesAttribute;
                            if (referencesAttribute != null)
                            {
                                Parent      = referencesAttribute.Parent;
                                RefPosition = referencesAttribute.Position;
                            }
                            else
                            {
                                var parameterAttribute = item as ParameterAttribute;
                                if (parameterAttribute != null)
                                {
                                    ParameterName = parameterAttribute.Name;
                                    Direction     = parameterAttribute.Direction;
                                }
                                else if (item is DoNotPersistAttribute)
                                {
                                    IsPersistent = false;
                                }
                                else if (item is DoNotSelectAttribute)
                                {
                                    IsSelectable = false;
                                }
                                else if (item is DoNotSerializeAttribute)
                                {
                                    IsSerializable = false;
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        private static Tuple <string, Param[], List <object> > GetCommitStatement(ChangeNode rootNode, DbConnection connection)
        {
            var dialect      = DialectFactory.GetProvider(connection);
            var dataEntities = new List <object>();
            var sql          = new StringBuilder();
            var statementId  = 0;

            // Inserts
            var newNodes     = GetChanges(rootNode, ObjectState.New);
            var tableCreated = false;

            var tempTableName = dialect.GetTemporaryTableName("ID");
            var allParameters = new List <Param>();

            foreach (var newNode in newNodes.Where(n => n.IsObject))
            {
                var item = newNode.Value;
                if (item != null)
                {
                    dataEntities.Add(item);
                }
                else
                {
                    continue;
                }

                var objectType      = newNode.Value.GetType();
                var propertyMap     = Reflector.GetPropertyMap(objectType);
                var autoGenProperty = propertyMap.Where(p => p.Key.CanWrite && p.Value != null && p.Value.IsAutoGenerated).Select(p => p.Key).FirstOrDefault();
                var autoGenType     = DbType.String;

                if (!tableCreated)
                {
                    if (autoGenProperty != null)
                    {
                        autoGenType = Reflector.ClrToDbType(autoGenProperty.PropertyType);
                    }
                    sql.AppendFormat(dialect.CreateTemporaryTable("ID", new Dictionary <string, DbType> {
                        { "StatementId", DbType.Int32 }, { "GeneratedId", autoGenType }, { "ParameterName", DbType.AnsiString }, { "PropertyName", DbType.AnsiString }
                    })).AppendLine();
                    tableCreated = true;
                }

                var parameters = ObjectExtensions.GetInsertParameters(newNode.Value, propertyMap, statementId);
                allParameters.AddRange(parameters);

                if (parameters.Length > 0)
                {
                    string commandName      = SqlBuilder.GetInsertStatement(objectType, parameters, dialect);
                    var    autoGenParameter = parameters.FirstOrDefault(p => p.IsAutoGenerated);

                    if (autoGenParameter != null)
                    {
                        sql.AppendFormat(dialect.DeclareVariable(autoGenParameter.Name, autoGenType)).AppendLine();
                        sql.AppendFormat(dialect.AssignVariable(autoGenParameter.Name, autoGenParameter.Type.GetDefault())).AppendLine();
                    }
                    sql.AppendLine(commandName);

                    if (autoGenParameter != null)
                    {
                        sql.AppendFormat(dialect.ComputeAutoIncrement(autoGenParameter.Name, () => SqlBuilder.GetTableNameForSql(objectType, dialect))).AppendLine();
                        sql.AppendFormat("INSERT INTO " + tempTableName + " ({4}StatementId{5}, {4}GeneratedId{5}, {4}ParameterName{5}, {4}PropertyName{5}) VALUES ({0}, {1}, '{2}', '{3}')", statementId, dialect.EvaluateVariable(dialect.ParameterPrefix + autoGenParameter.Name), autoGenParameter.Name, autoGenProperty.Name, dialect.IdentifierEscapeStartCharacter, dialect.IdentifierEscapeEndCharacter).AppendLine();
                    }

                    statementId++;
                }
            }

            if (newNodes.Count > 0)
            {
                sql.AppendLine("SELECT * FROM " + tempTableName);
            }

            if (tableCreated && !dialect.SupportsTemporaryTables)
            {
                sql.AppendLine("DROP TABLE " + tempTableName);
            }

            // Updates
            var dirtyNodes       = GetChanges(rootNode, ObjectState.Dirty);
            var dirtyNodeParents = dirtyNodes.Where(n => n.IsSimpleLeaf).Select(n => n.Parent).Distinct();

            foreach (var dirtyNode in dirtyNodeParents)
            {
                var objectType  = dirtyNode.Value.GetType();
                var propertyMap = Reflector.GetPropertyMap(objectType).ToDictionary(p => p.Key.Name, p => p);

                var parameters = new List <Param>();
                foreach (var change in dirtyNode.Nodes.Where(n => n.IsSimpleLeaf))
                {
                    KeyValuePair <PropertyInfo, ReflectedProperty> map;
                    if (propertyMap.TryGetValue(change.PropertyName, out map))
                    {
                        var property      = map.Key;
                        var parameterName = change.PropertyName;
                        if (map.Value != null && !string.IsNullOrEmpty(map.Value.ParameterName))
                        {
                            parameterName = map.Value.ParameterName;
                        }

                        parameters.Add(new Param {
                            Name = parameterName + "_" + statementId, Value = change.Value, Source = MapColumnAttribute.GetMappedColumnName(property)
                        });
                    }
                }

                var primaryKey = new List <Param>();
                foreach (var primaryKeyMap in propertyMap.Values.Where(p => p.Value.IsPrimaryKey))
                {
                    var value = dirtyNode.Value.Property(primaryKeyMap.Key.Name);

                    var parameterName = primaryKeyMap.Key.Name;
                    if (primaryKeyMap.Value != null && !string.IsNullOrEmpty(primaryKeyMap.Value.ParameterName))
                    {
                        parameterName = primaryKeyMap.Value.ParameterName;
                    }
                    primaryKey.Add(new Param {
                        Name = parameterName + "_" + statementId, Value = value, Source = MapColumnAttribute.GetMappedColumnName(primaryKeyMap.Key)
                    });
                }

                var commandName = SqlBuilder.GetUpdateStatement(objectType, parameters, primaryKey, dialect);
                allParameters.AddRange(parameters);
                allParameters.AddRange(primaryKey);
                sql.Append(commandName).AppendLine();

                statementId++;
            }

            // Deletes
            var deletedNodes = GetChanges(rootNode, ObjectState.Deleted);

            foreach (var deletedNode in deletedNodes)
            {
                var objectType  = deletedNode.Value.GetType();
                var propertyMap = Reflector.GetPropertyMap(objectType);

                var parameters  = ObjectExtensions.GetDeleteParameters(deletedNode.Value, propertyMap, statementId);
                var commandName = SqlBuilder.GetDeleteStatement(objectType, parameters, dialect);
                allParameters.AddRange(parameters);
                sql.Append(commandName).AppendLine();

                statementId++;
            }

            return(Tuple.Create(sql.ToString(), allParameters.ToArray(), dataEntities));
        }