Exemple #1
0
 public object GetColumnValue(PocoData pd, object target, Func <PocoColumn, object, object> callback = null)
 {
     callback = callback ?? ((_, o) => o);
     if (this.ReferenceType == ReferenceType.Foreign)
     {
         PocoMember member = pd.Members.Single(x => x.MemberInfoData == this.MemberInfoData);
         PocoMember column = member.PocoMemberChildren.SingleOrDefault(x => x.Name == member.ReferenceMemberName);
         if (column == null)
         {
             throw new Exception(string.Format("Could not find member on '{0}' with name '{1}'", member.MemberInfoData.MemberType, member.ReferenceMemberName));
         }
         return(callback(column.PocoColumn, column.PocoColumn.GetValue(target)));
     }
     else
     {
         return(callback(this, this.GetValue(target)));
     }
 }
        public static void SetForeignList <T>(Func <T, IList> listFunc, PocoMember foreignMember, object prevPoco)
        {
            if (listFunc == null || foreignMember == null)
            {
                return;
            }

            IList currentList = listFunc((T)prevPoco);

            if (currentList == null)
            {
                return;
            }

            foreach (object item in currentList)
            {
                foreignMember.SetValue(item, prevPoco);
            }
        }
        public static void SetListValue <T>(Func <T, IList> listFunc, PocoMember pocoMember, object prevPoco, T poco)
        {
            IList prevList    = listFunc((T)prevPoco);
            IList currentList = listFunc(poco);

            if (prevList == null && currentList != null)
            {
                prevList = pocoMember.CreateList();
                pocoMember.SetValue(prevPoco, prevList);
            }

            if (prevList != null && currentList != null)
            {
                foreach (object item in currentList)
                {
                    prevList.Add(item);
                }
            }
        }
Exemple #4
0
            public static PreparedInsertSql PrepareInsertSql <T>(Database database, PocoData pd, string tableName, string primaryKeyName, bool autoIncrement, T poco)
            {
                List <string> names       = new List <string>();
                List <string> values      = new List <string>();
                List <object> rawvalues   = new List <object>();
                int           index       = 0;
                string        versionName = "";

                foreach (PocoColumn pocoColumn in pd.Columns.Values)
                {
                    // Don't insert result columns
                    if (pocoColumn.ResultColumn ||
                        (pocoColumn.ComputedColumn && (pocoColumn.ComputedColumnType == ComputedColumnType.Always || pocoColumn.ComputedColumnType == ComputedColumnType.ComputedOnInsert)) ||
                        (pocoColumn.VersionColumn && pocoColumn.VersionColumnType == VersionColumnType.RowVersion))
                    {
                        continue;
                    }

                    // Don't insert the primary key (except under oracle where we need bring in the next sequence value)
                    if (autoIncrement && primaryKeyName != null && string.Compare(pocoColumn.ColumnName, primaryKeyName, true) == 0)
                    {
                        // Setup auto increment expression
                        string autoIncExpression = database.DatabaseType.GetAutoIncrementExpression(pd.TableInfo);
                        if (autoIncExpression != null)
                        {
                            names.Add(pocoColumn.ColumnName);
                            values.Add(autoIncExpression);
                        }
                        continue;
                    }

                    names.Add(database.DatabaseType.EscapeSqlIdentifier(pocoColumn.ColumnName));
                    values.Add(string.Format("{0}{1}", database._paramPrefix, index++));

                    object val;
                    if (pocoColumn.ReferenceType == ReferenceType.Foreign)
                    {
                        PocoMember member = pd.Members.Single(x => x.MemberInfoData == pocoColumn.MemberInfoData);
                        PocoMember column = member.PocoMemberChildren.Single(x => x.Name == member.ReferenceMemberName);
                        val = database.ProcessMapper(column.PocoColumn, column.PocoColumn.GetValue(poco));
                    }
                    else
                    {
                        val = database.ProcessMapper(pocoColumn, pocoColumn.GetValue(poco));
                    }

                    if (pocoColumn.VersionColumn && pocoColumn.VersionColumnType == VersionColumnType.Number)
                    {
                        val         = Convert.ToInt64(val) > 0 ? val : 1;
                        versionName = pocoColumn.ColumnName;
                    }

                    rawvalues.Add(val);
                }

                string sql          = string.Empty;
                string outputClause = String.Empty;

                if (autoIncrement || !string.IsNullOrEmpty(pd.TableInfo.SequenceName))
                {
                    outputClause = database.DatabaseType.GetInsertOutputClause(primaryKeyName, pd.TableInfo.UseOutputClause);
                }

                if (names.Count != 0)
                {
                    sql = string.Format("INSERT INTO {0} ({1}){2} VALUES ({3})",
                                        database.DatabaseType.EscapeTableName(tableName),
                                        string.Join(",", names.ToArray()),
                                        outputClause,
                                        string.Join(",", values.ToArray()));
                }
                else
                {
                    sql = database.DatabaseType.GetDefaultInsertSql(tableName, primaryKeyName, pd.TableInfo.UseOutputClause, names.ToArray(), values.ToArray());
                }

                return(new PreparedInsertSql()
                {
                    PocoData = pd,
                    Sql = sql,
                    Rawvalues = rawvalues,
                    VersionName = versionName
                });
            }
        private IEnumerable <PocoMemberPlan> GetPocoMembers(MapperCollection mapper, ColumnInfo[] columnInfos, List <MemberInfo> memberInfos, string prefix = null)
        {
            MemberInfo[] capturedMembers = memberInfos.ToArray();
            string       capturedPrefix  = prefix;

            foreach (ColumnInfo columnInfo in columnInfos)
            {
                if (columnInfo.IgnoreColumn)
                {
                    continue;
                }

                Type memberInfoType = columnInfo.MemberInfo.GetMemberInfoType();
                if (columnInfo.ReferenceType == ReferenceType.Many)
                {
                    memberInfoType = memberInfoType.GetGenericArguments().First();
                }

                PocoMemberPlan[]  childrenPlans      = new PocoMemberPlan[0];
                TableInfoPlan     childTableInfoPlan = null;
                List <MemberInfo> members            = new List <MemberInfo>(capturedMembers)
                {
                    columnInfo.MemberInfo
                };

                if (columnInfo.ComplexMapping || columnInfo.ReferenceType != ReferenceType.None)
                {
                    if (capturedMembers.GroupBy(x => x.GetMemberInfoType()).Any(x => x.Count() >= 2))
                    {
                        continue;
                    }

                    ColumnInfo[] childColumnInfos = this.GetColumnInfos(memberInfoType);

                    if (columnInfo.ReferenceType != ReferenceType.None)
                    {
                        childTableInfoPlan = this.GetTableInfo(memberInfoType, childColumnInfos, members);
                    }

                    string newPrefix = JoinStrings(capturedPrefix, columnInfo.ReferenceType != ReferenceType.None ? "" : (columnInfo.ComplexPrefix ?? columnInfo.MemberInfo.Name));

                    childrenPlans = this.GetPocoMembers(mapper, childColumnInfos, members, newPrefix).ToArray();
                }

                MemberInfo capturedMemberInfo = columnInfo.MemberInfo;
                ColumnInfo capturedColumnInfo = columnInfo;

                List <MemberAccessor> accessors = this.GetMemberAccessors(members);
                Type           memberType       = capturedMemberInfo.GetMemberInfoType();
                bool           isList           = IsList(capturedMemberInfo);
                Type           listType         = GetListType(memberType, isList);
                bool           isDynamic        = capturedMemberInfo.IsDynamic();
                FastCreate     fastCreate       = GetFastCreate(memberType, mapper, isList, isDynamic);
                string         columnName       = this.GetColumnName(capturedPrefix, capturedColumnInfo.ColumnName ?? capturedMemberInfo.Name);
                MemberInfoData memberInfoData   = new MemberInfoData(capturedMemberInfo);

                yield return(tableInfo =>
                {
                    PocoColumn pc = new PocoColumn
                    {
                        ReferenceType = capturedColumnInfo.ReferenceType,
                        TableInfo = tableInfo,
                        MemberInfoData = memberInfoData,
                        MemberInfoChain = members,
                        ColumnName = columnName,
                        ResultColumn = capturedColumnInfo.ResultColumn,
                        ForceToUtc = capturedColumnInfo.ForceToUtc,
                        ComputedColumn = capturedColumnInfo.ComputedColumn,
                        ComputedColumnType = capturedColumnInfo.ComputedColumnType,
                        ColumnType = capturedColumnInfo.ColumnType,
                        ColumnAlias = capturedColumnInfo.ColumnAlias,
                        VersionColumn = capturedColumnInfo.VersionColumn,
                        VersionColumnType = capturedColumnInfo.VersionColumnType,
                        SerializedColumn = capturedColumnInfo.SerializedColumn
                    };

                    pc.SetMemberAccessors(accessors);

                    TableInfo childrenTableInfo = childTableInfoPlan == null ? tableInfo : childTableInfoPlan();
                    List <PocoMember> children = childrenPlans.Select(plan => plan(childrenTableInfo)).ToList();

                    // Cascade ResultColumn down
                    foreach (PocoMember child in children.Where(child => child.PocoColumn != null && pc.ResultColumn))
                    {
                        child.PocoColumn.ResultColumn = true;
                    }

                    PocoMember pocoMember = new PocoMember()
                    {
                        MemberInfoData = memberInfoData,
                        MemberInfoChain = members,
                        IsList = isList,
                        IsDynamic = isDynamic,
                        PocoColumn = capturedColumnInfo.ComplexMapping ? null : pc,
                        ReferenceType = capturedColumnInfo.ReferenceType,
                        ReferenceMemberName = capturedColumnInfo.ReferenceMemberName,
                        PocoMemberChildren = children,
                    };

                    pocoMember.SetMemberAccessor(accessors[accessors.Count - 1], fastCreate, listType);

                    return pocoMember;
                });
            }
        }