Exemplo n.º 1
0
        internal UpdateSqlBodyInfo Build()
        {
            var sqlBody = new UpdateSqlBodyInfo
            {
                TableName = TableName
            };

            if (whereClauses.Count > 0)
            {
                var firstWhere = whereClauses[0];
                var sql        = firstWhere.BuildSqlString(dialect);
                AddToParameterList(sql.Item2);
                var wherebuilder = new StringBuilder();
                wherebuilder.AppendFormat("{0} ", sql.Item1);

                if (whereClauses.Count > 1)
                {
                    for (var i = 1; i < whereClauses.Count; i++)
                    {
                        var where = whereClauses[i].BuildSqlString(dialect, i);
                        AddToParameterList(where.Item2);

                        var prep = "AND";
                        if (whereClauses[i].PreOperator != SqlOperator.AND)
                        {
                            prep = "OR";
                        }

                        wherebuilder.AppendFormat("{0} {1} ", prep, where.Item1);
                    }
                }

                for (int i = 0; i < columnNames.Count; i++)
                {
                    sqlBody.Columns.Add(columnNames[i], Tuple.Create(Parameters[i], false));
                }
                sqlBody.WhereExpression = wherebuilder.ToString().Trim();
            }
            else
            {
                throw new DataAccessException("Update missing where statement. Goliath cannot run an update without filters");
            }

            return(sqlBody);
        }
Exemplo n.º 2
0
        void LoadColumns(UpdateSqlExecutionList execList, EntityMap entityMap, EntityAccessor accessor)
        {
            var updateBodyInfo = new UpdateSqlBodyInfo()
            {
                TableName = entityMap.TableName
            };

            var trackable = entity as ITrackable;

            if (trackable != null)
            {
                var changes = trackable.ChangeTracker.GetChangedItems();
                foreach (var item in changes)
                {
                    var prop = entityMap.GetProperty(item.ItemName);
                    if (prop == null)
                    {
                        if (entityMap.IsSubClass && !execList.Statements.ContainsKey(entityMap.Extends))
                        {
                            var parentMap = session.SessionFactory.DbSettings.Map.GetEntityMap(entityMap.Extends);
                            LoadColumns(execList, parentMap, accessor);
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (prop == null || prop.IgnoreOnUpdate || prop.IsPrimaryKey)
                    {
                        continue;
                    }

                    var propInfo = accessor.Properties[prop.Name];
                    if (propInfo == null)
                    {
                        throw new MappingException("Could not find mapped property " + prop.Name + " inside " + entityMap.FullName);
                    }

                    AddColumnAndParameterToUpdateInfo(execList, updateBodyInfo, entityMap, prop, propInfo, accessor);
                }
            }
            else
            {
                foreach (var pInfo in accessor.Properties)
                {
                    var prop = entityMap.GetProperty(pInfo.Value.PropertyName);
                    if (prop == null)
                    {
                        if (entityMap.IsSubClass && !execList.Statements.ContainsKey(entityMap.Extends))
                        {
                            var parentMap = session.SessionFactory.DbSettings.Map.GetEntityMap(entityMap.Extends);
                            LoadColumns(execList, parentMap, accessor);
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (prop == null || prop.IgnoreOnUpdate || prop.IsPrimaryKey)
                    {
                        continue;
                    }

                    AddColumnAndParameterToUpdateInfo(execList, updateBodyInfo, entityMap, prop, pInfo.Value, accessor);
                }
            }

            execList.Statements.Add(entityMap.FullName, updateBodyInfo);
        }
Exemplo n.º 3
0
        void AddColumnAndParameterToUpdateInfo(UpdateSqlExecutionList execList, UpdateSqlBodyInfo updateBodyInfo, EntityMap entityMap, Property prop, PropertyAccessor propInfo, EntityAccessor accessor)
        {
            object val   = propInfo.GetMethod(entity);
            bool   isRel = false;

            if (prop is Relation)
            {
                var rel = (Relation)prop;
                if (updateBodyInfo.Columns.ContainsKey(prop.ColumnName))
                {
                    return;
                }

                isRel = true;
                if (val != null)
                {
                    if (rel.RelationType == RelationshipType.ManyToOne)
                    {
                        var store       = new EntityAccessorStore();
                        var relMap      = session.SessionFactory.DbSettings.Map.GetEntityMap(rel.ReferenceEntityName);
                        var relAccessor = store.GetEntityAccessor(val.GetType(), relMap);

                        var relPinfo = relAccessor.Properties[rel.ReferenceProperty];

                        if (relPinfo == null)
                        {
                            throw new MappingException(string.Format("could not find property {0} in mapped entity {1}", rel.ReferenceProperty, relMap.FullName));
                        }

                        val = relPinfo.GetMethod(val);
                    }
                    else if (rel.RelationType == RelationshipType.ManyToMany)
                    {
                        var trackableCollection = val as ITrackableCollection;
                        if (trackableCollection != null)
                        {
                            AddInsertManyToManyOperation(execList, trackableCollection.InsertedItems, rel, accessor, true);
                            AddInsertManyToManyOperation(execList, trackableCollection.DeletedItems, rel, accessor, false);
                        }
                        return;
                        //NOTE: if not trackable collection used mapped statement to add or remove many to many associations
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else
            {
                Tuple <QueryParam, bool> etuple;
                if (updateBodyInfo.Columns.TryGetValue(prop.ColumnName, out etuple))
                {
                    if (etuple.Item2)
                    {
                        updateBodyInfo.Columns.Remove(prop.ColumnName);
                    }
                    else
                    {
                        return;
                    }
                }
            }

            //Tuple<QueryParam, bool> tuple = Tuple.Create(new QueryParam(string.Format("{0}_{1}",entityMap.TableAlias, prop.ColumnName)) { Value = val }, isRel);
            Tuple <QueryParam, bool> tuple = Tuple.Create(QueryParam.CreateParameter(prop, string.Format("{0}_{1}", TableQueryMap.CreatePrefix(2, 2), prop.ColumnName), val), isRel);

            updateBodyInfo.Columns.Add(prop.ColumnName, tuple);
        }