Esempio n. 1
0
 /// <summary>
 /// Get as name.
 /// </summary>
 /// <param name="joinList"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 /// <remarks>
 /// As name is used if:
 /// 1. Data from different classes is saved to same table.
 /// 2. Several classes are using same class.
 /// 3. User is defined it using Alias attribute.
 /// </remarks>
 internal static string GetAsName(List <GXJoin> joinList, Type type, bool selectUsingAs)
 {
     //Data from different classes is saved to same table.
     if (selectUsingAs || GXDbHelpers.IsSharedTable(type))
     {
         string name   = GXDbHelpers.OriginalTableName(type);
         int    cnt    = 0;
         GXJoin target = null;
         foreach (GXJoin it in joinList)
         {
             if (type == it.Table1Type)
             {
                 target = it;
             }
             else if (it.Alias2 == name || it.Table2 == name)
             {
                 ++cnt;
                 //If we have more than one class that are sharing same table.
                 if (cnt != 1)
                 {
                     break;
                 }
             }
         }
         if (cnt > 1)
         {
             name = GXDbHelpers.GetTableName(type, false, '\0', null, false);
             if (target != null)
             {
                 target.Alias2 = name;
             }
             return(name);
         }
         if (selectUsingAs)
         {
             return(name);
         }
     }
     if (GXDbHelpers.IsAliasName(type))
     {
         return(GXDbHelpers.OriginalTableName(type));
     }
     return(null);
 }
Esempio n. 2
0
        internal static void UpdateJoins(GXDBSettings settings, GXJoinCollection list, List <GXJoin> joins)
        {
            char             separtor = settings.ColumnQuotation;
            bool             allowNull;
            MemberExpression me;

            foreach (KeyValuePair <JoinType, BinaryExpression> it in list.List)
            {
                GXJoin join = new GXJoin();
                join.Type = it.Key;
                me        = GetMemberExpression(it.Value.Left, out allowNull);
                MemberInfo m = me.Member;
                Expression e = me.Expression;
                join.Column1    = GXDbHelpers.GetColumnName(m, separtor);
                join.AllowNull1 = allowNull;
                m               = GetMemberExpression(it.Value.Right, out allowNull).Member;
                join.Column2    = GXDbHelpers.GetColumnName(m, separtor);
                join.AllowNull2 = allowNull;
                join.UpdateTables(e.Type, m.DeclaringType);
                joins.Add(join);
            }
        }
Esempio n. 3
0
        private void GetRelations(Type type, List <GXJoin> joinList, List <Type> tables,
                                  Dictionary <Type, List <string> > columns, bool getRelations)
        {
            if (!tables.Contains(type))
            {
                //TODO: lisää shared table attribute.
                bool ignorebaseType = false;
                foreach (var it in tables)
                {
                    if (GXDbHelpers.IsSharedTable(it) && type.IsAssignableFrom(it.BaseType))
                    {
                        ignorebaseType = true;
                        break;
                    }
                }
                if (ignorebaseType)
                {
                    return;
                }
                tables.Add(type);

                Type          tp;
                bool          added;
                List <string> excluded  = null;
                List <string> cols      = null;
                string        tableName = null;
                if (columns != null && columns.ContainsKey(type))
                {
                    cols = columns[type];
                    if (cols.Contains("*"))
                    {
                        cols = null;
                    }
                }
                Dictionary <string, GXSerializedItem> properties = GXSqlBuilder.GetProperties(type);
                foreach (var it in properties)
                {
                    if (cols != null && !cols.Contains(it.Key))
                    {
                        continue;
                    }
                    if (!IsExcluded(it.Key, excluded))
                    {
                        if (it.Value.Relation != null && getRelations)
                        {
                            GXJoin j = new GXJoin();
                            tp           = it.Value.Relation.PrimaryTable;
                            j.Table1Type = tp;
                            bool shared = GXDbHelpers.IsSharedTable(tp);
                            if (shared || GXDbHelpers.IsAliasName(tp))
                            {
                                j.Alias1 = GXDbHelpers.OriginalTableName(tp);
                            }
                            j.Table2Type = it.Value.Relation.ForeignTable;
                            j.UpdateTables(tp, it.Value.Relation.ForeignTable);
                            //If nullable.
                            Type tp2 = it.Value.Relation.ForeignId.Type;
                            j.AllowNull1 = !shared && tp2.IsGenericType && tp2.GetGenericTypeDefinition() == typeof(Nullable <>);
                            if (!j.AllowNull1)
                            {
                                tp2          = it.Value.Relation.PrimaryId.Type;
                                j.AllowNull2 = !GXDbHelpers.IsSharedTable(it.Value.Relation.ForeignTable) &&
                                               tp2.IsGenericType && tp2.GetGenericTypeDefinition() == typeof(Nullable <>);
                            }
                            if (GXDbHelpers.IsSharedTable(it.Value.Relation.ForeignTable) ||
                                GXDbHelpers.IsAliasName(it.Value.Relation.ForeignTable))
                            {
                                j.Alias2 = GXDbHelpers.OriginalTableName(it.Value.Relation.ForeignTable);
                            }
                            if (it.Value.Relation.RelationType == RelationType.OneToOne ||
                                it.Value.Relation.RelationType == RelationType.Relation)
                            {
                                j.Column2 = GXDbHelpers.GetColumnName(it.Value.Relation.PrimaryId.Relation.ForeignId.Target as PropertyInfo, Parent.Settings.ColumnQuotation);
                                j.Column1 = GXDbHelpers.GetColumnName(it.Value.Relation.PrimaryId.Target as PropertyInfo, Parent.Settings.ColumnQuotation);
                                if (tables.Contains(it.Value.Relation.ForeignTable))
                                {
                                    continue;
                                }
                                //If nullable.
                                tp2          = it.Value.Relation.PrimaryId.Relation.ForeignId.Type;
                                j.AllowNull1 = !shared && tp2.IsGenericType && tp2.GetGenericTypeDefinition() == typeof(Nullable <>);
                                if (!j.AllowNull1)
                                {
                                    tp2          = it.Value.Relation.PrimaryId.Relation.PrimaryId.Type;
                                    j.AllowNull2 = !GXDbHelpers.IsSharedTable(it.Value.Relation.ForeignTable) &&
                                                   tp2.IsGenericType && tp2.GetGenericTypeDefinition() == typeof(Nullable <>);
                                }
                            }
                            else if (it.Value.Relation.RelationType == RelationType.OneToMany)
                            {
                                j.Column1 = GXDbHelpers.GetColumnName(it.Value.Relation.PrimaryId.Relation.ForeignId.Target as PropertyInfo, Parent.Settings.ColumnQuotation);
                                j.Column2 = GXDbHelpers.GetColumnName(it.Value.Relation.PrimaryId.Target as PropertyInfo, Parent.Settings.ColumnQuotation);
                                //If nullable.
                                tp2          = it.Value.Relation.ForeignId.Type;
                                j.AllowNull1 = !shared && tp2.IsGenericType && tp2.GetGenericTypeDefinition() == typeof(Nullable <>);
                                if (!j.AllowNull1)
                                {
                                    tp2          = it.Value.Relation.PrimaryId.Type;
                                    j.AllowNull2 = !GXDbHelpers.IsSharedTable(it.Value.Relation.ForeignTable) &&
                                                   tp2.IsGenericType && tp2.GetGenericTypeDefinition() == typeof(Nullable <>);
                                }
                            }
                            else if (it.Value.Relation.RelationType == RelationType.ManyToMany)
                            {
                                j.Table2Type = it.Value.Relation.RelationMapTable.Relation.PrimaryTable;
                                j.UpdateTables(tp, it.Value.Relation.RelationMapTable.Relation.PrimaryTable);
                                j.Column2 = GXDbHelpers.GetColumnName(GXSqlBuilder.FindRelation(it.Value.Relation.RelationMapTable.Relation.PrimaryTable, tp).Target as PropertyInfo, Parent.Settings.ColumnQuotation);
                                j.Column1 = GXDbHelpers.GetColumnName(GXSqlBuilder.FindUnique(tp).Target as PropertyInfo, Parent.Settings.ColumnQuotation);
                                added     = false;
                                //Check that join is not added already.
                                string j1 = j.Table1;
                                string j2 = j.Table2;
                                foreach (var it4 in joinList)
                                {
                                    string t1 = it4.Table1;
                                    string t2 = it4.Table2;
                                    if ((j1 == t1 && j2 == t2) || (j2 == t1 && j1 == t2))
                                    {
                                        added = true;
                                        break;
                                    }
                                }
                                if (!added)
                                {
                                    joinList.Add(j);
                                    j = new GXJoin();
                                    Type tmp = it.Value.Type;
                                    j.AllowNull1 = !!GXDbHelpers.IsSharedTable(tmp) && tmp.IsGenericType && tmp.GetGenericTypeDefinition() == typeof(Nullable <>);
                                    j.UpdateTables(it.Value.Relation.RelationMapTable.Relation.PrimaryTable, it.Value.Relation.ForeignTable);
                                    j.Column1 = GXDbHelpers.GetColumnName(it.Value.Relation.RelationMapTable.Target as PropertyInfo, Parent.Settings.ColumnQuotation);
                                    j.Column2 = GXDbHelpers.GetColumnName(it.Value.Relation.ForeignId.Target as PropertyInfo, Parent.Settings.ColumnQuotation);
                                    joinList.Add(j);
                                    tables.Add(it.Value.Relation.RelationMapTable.Relation.PrimaryTable);
                                    if (cols != null)
                                    {
                                        tables.Add(it.Value.Relation.ForeignTable);
                                    }
                                }
                                j = null;
                            }
                            if (j != null)
                            {
                                //Check that join is not added already.
                                added = false;
                                string j1    = j.Table1;
                                string j2    = j.Table2;
                                int    index = -1;
                                foreach (var it4 in joinList)
                                {
                                    string t1 = it4.Table1;
                                    string t2 = it4.Table2;
                                    if ((j1 == t1 && j2 == t2) || (j2 == t1 && j1 == t2))
                                    {
                                        added = true;
                                        break;
                                    }
                                    if (j.Alias2 != null && it4.Alias2 == j.Alias2)
                                    {
                                        ++index;
                                    }
                                }
                                //If we have multiple referenced to same table.
                                if (index != -1)
                                {
                                    j.Index = index + 1;
                                }
                                if (!added)
                                {
                                    joinList.Add(j);
                                }
                            }
                            if (cols == null || cols.Contains(it.Key))
                            {
                                GetRelations(it.Value.Relation.ForeignTable, joinList, tables, columns, getRelations);
                            }
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Column " + tableName + "." + it.Key + " Skipped.");
                    }
                }
            }
        }