예제 #1
0
 internal void UpdateTables(System.Type table1, System.Type table2)
 {
     Table1Type = table1;
     Table2Type = table2;
     Table1     = GXDbHelpers.GetTableName(table1, false, null);
     Table2     = GXDbHelpers.GetTableName(table2, false, null);
 }
예제 #2
0
        public override string ToString()
        {
            UpdateMaps(Settings, Maps, null);
            StringBuilder sb = new StringBuilder();

            sb.Append("Create View ");
            sb.Append(GXDbHelpers.GetTableName(type, true, Parent.Settings.TableQuotation, null));
            sb.Append(" AS ");
            sb.Append(Select.ToString());
            return(sb.ToString());
        }
예제 #3
0
 public string ToString(bool addExecutionTime)
 {
     if (Parent.Updated || Updated)
     {
         StringBuilder sb = new StringBuilder();
         sb.Append("DELETE FROM ");
         sb.Append(GXDbHelpers.GetTableName(Table, true, Parent.Settings.TableQuotation, Parent.Settings.TablePrefix));
         string str = Where.ToString();
         if (!string.IsNullOrEmpty(str))
         {
             sb.Append(" ");
             sb.Append(str);
         }
         Updated = false;
         return(sb.ToString());
     }
     return(string.Empty);
 }
예제 #4
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);
 }
예제 #5
0
        internal static void OrderByToString(GXSelectArgs parent, StringBuilder sb, List <GXOrder> OrderList, List <GXJoin> joinList)
        {
            bool first = true;

            if (OrderList.Count != 0)
            {
                sb.Append(" ORDER BY ");
                first = true;
                foreach (GXOrder it in OrderList)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }
                    if (parent.Settings.Type == DatabaseType.MSSQL && parent.Count != 0)
                    {
                        string table = GXDbHelpers.GetTableName(it.Table, true, '\0', parent.Settings.TablePrefix);
                        sb.Append(GXDbHelpers.AddQuotes(table + '.' + it.Column, parent.Settings.ColumnQuotation));
                        continue;
                    }
                    //Add table name always until there is a way to check are multiple tables used. if (joinList.Count != 0)
                    {
                        string table = GXDbHelpers.GetTableName(it.Table, true, parent.Settings.TableQuotation, parent.Settings.TablePrefix);
                        sb.Append(table);
                        sb.Append('.');
                    }
                    sb.Append(GXDbHelpers.AddQuotes(it.Column, parent.Settings.ColumnQuotation));
                }
                if (parent.Descending)
                {
                    sb.Append(" DESC");
                }
            }
        }
예제 #6
0
 internal static void GroupByToString(GXSelectArgs parent, StringBuilder sb, List <GXOrder> groupList, List <GXJoin> joinList)
 {
     if (groupList.Count != 0)
     {
         sb.Append(" GROUP BY ");
         bool first = true;
         foreach (GXOrder it in groupList)
         {
             if (first)
             {
                 first = false;
             }
             else
             {
                 sb.Append(", ");
             }
             //Table name is not added if there is only one table.
             if (joinList.Count != 0)
             {
                 if (parent.Settings.Type == DatabaseType.MSSQL && parent.Count != 0)
                 {
                     string table = GXDbHelpers.GetTableName(it.Table, true, '\0', parent.Settings.TablePrefix);
                     sb.Append(table);
                     sb.Append('.');
                 }
                 else
                 {
                     string table = GXDbHelpers.GetTableName(it.Table, true, parent.Settings.TableQuotation, parent.Settings.TablePrefix);
                     sb.Append(table);
                     sb.Append('.');
                 }
             }
             sb.Append(GXDbHelpers.AddQuotes(it.Column, parent.Settings.ColumnQuotation));
         }
     }
 }
예제 #7
0
        private void SelectToString(GXDBSettings settings, StringBuilder sb, bool distinct,
                                    Dictionary <Type, List <string> > columnList, List <GXJoin> joinList, UInt32 index, UInt32 count)
        {
            Dictionary <Type, string> asTable = new Dictionary <Type, string>();
            string name;

            foreach (var it in columnList)
            {
                name = GetAsName(joinList, it.Key, settings.SelectUsingAs);
                if (name != null)
                {
                    asTable.Add(it.Key, name);
                }
            }
            sb.Append("SELECT ");
            if (distinct)
            {
                sb.Append("DISTINCT ");
            }
            if (index != 0 || count != 0)
            {
                if (index != 0 && count == 0)
                {
                    throw new ArgumentOutOfRangeException("Count can't be zero if index is given.");
                }
                if (index != 0)
                {
                    if (settings.LimitType == LimitType.Top)
                    {
                        sb.Length = 0;
                        sb.Append("SELECT * FROM (SELECT TOP ");
                        sb.Append(count);
                        sb.Append(" GX.* FROM (");
                        sb.Append("SELECT ");
                        if (distinct)
                        {
                            sb.Append("DISTINCT ");
                        }
                        sb.Append("TOP ");
                        sb.Append(index + count);
                        sb.Append(" ");
                    }
                }
                else
                {
                    if (settings.LimitType == LimitType.Top)
                    {
                        sb.Append("TOP ");
                        sb.Append(count);
                        sb.Append(" ");
                    }
                }
            }
            string tableAs, table;
            bool   first = true;

            foreach (var it in columnList)
            {
                if (asTable.ContainsKey(it.Key))
                {
                    tableAs = asTable[it.Key];
                }
                else
                {
                    tableAs = null;
                }
                if (GXDbHelpers.IsSharedTable(it.Key) || GXDbHelpers.IsAliasName(it.Key))
                {
                    table = GXDbHelpers.AddQuotes(GXDbHelpers.OriginalTableName(it.Key), settings.TableQuotation);
                }
                else
                {
                    table = GXDbHelpers.GetTableName(it.Key, true, settings.TableQuotation, settings.TablePrefix);
                }
                foreach (var col in it.Value)
                {
                    name = null;
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }
                    //Check is this method.
                    int pos = col.LastIndexOf('(');
                    //Table name is not added if only one table.
                    if (pos == -1 && joinList.Count != 0)
                    {
                        if (asTable.Count > 1 && tableAs != null)
                        {
                            sb.Append(tableAs);
                        }
                        else
                        {
                            sb.Append(table);
                        }
                        sb.Append(".");
                    }
                    if (pos == -1) //If table field.
                    {
                        if (col == "*")
                        {
                            sb.Append(col);
                        }
                        else
                        {
                            name = GXDbHelpers.AddQuotes(col, settings.ColumnQuotation);
                            sb.Append(name);
                        }
                        if (tableAs != null)
                        {
                            sb.Append(" AS ");
                            name = GXDbHelpers.AddQuotes(tableAs + "." + col, settings.ColumnQuotation);
                            sb.Append(name);
                        }
                        else if (settings.SelectUsingAs && index == 0)
                        {
                            sb.Append(" AS ");
                            name = GXDbHelpers.AddQuotes(tableAs + "." + col, settings.ColumnQuotation);
                            sb.Append(name);
                        }
                    }
                    else //If method like COUNT(*)
                    {
                        sb.Append(col);
                        if (settings.SelectUsingAs && index == 0)
                        {
                            sb.Append(" AS ");
                            name = GXDbHelpers.AddQuotes(tableAs + "." + col.Substring(0, pos), settings.ColumnQuotation);
                            sb.Append(name);
                        }
                    }
                    if (Maps.ContainsKey(col))
                    {
                        sb.Append(" AS ");
                        sb.Append(GXDbHelpers.AddQuotes(Maps[col], settings.ColumnQuotation));
                    }
                }
            }
            if (columnList.Count == 0)
            {
                sb.Append("*");
            }
            if (joinList.Count == 0)
            {
                sb.Append(" FROM ");
                first = true;
                foreach (var it in columnList)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }
                    sb.Append(GXDbHelpers.GetTableName(it.Key, settings.UseQuotationWhereColumns, settings.TableQuotation, settings.TablePrefix));
                    if (asTable.ContainsKey(it.Key))
                    {
                        sb.Append(" ");
                        sb.Append(GXDbHelpers.AddQuotes(asTable[it.Key], settings.TableQuotation));
                    }
                }
            }
            else
            {
                //If we are adding relation to same table more than once.
                Dictionary <string, int> list = new Dictionary <string, int>();
                sb.Append(" FROM ");
                first = true;
                if (joinList.Count != 1)
                {
                    for (int pos = 0; pos < joinList.Count; ++pos)
                    {
                        sb.Append("(");
                    }
                }
                foreach (var it in joinList)
                {
                    if (first)
                    {
                        sb.Append(GXDbHelpers.AddQuotes(it.Table1, settings.TableQuotation));
                        if (asTable.ContainsKey(it.Table1Type))
                        {
                            sb.Append(" ");
                            sb.Append(GXDbHelpers.AddQuotes(asTable[it.Table1Type], settings.TableQuotation));
                        }
                        else
                        {
                            if (it.Alias1 != null)
                            {
                                sb.Append(" ");
                                sb.Append(it.Alias1);
                            }
                        }
                        first = false;
                    }

                    switch (it.Type)
                    {
                    case JoinType.Inner:
                        sb.Append(" INNER JOIN ");
                        break;

                    case JoinType.Left:
                        sb.Append(" LEFT OUTER JOIN ");
                        break;

                    case JoinType.Right:
                        sb.Append(" RIGHT OUTER JOIN ");
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("Invalid join type.");
                    }
                    sb.Append(GXDbHelpers.AddQuotes(it.Table2, settings.TableQuotation));
                    //Add alias if used and it is not same as table name.
                    if (asTable.ContainsKey(it.Table2Type))
                    {
                        sb.Append(" ");
                        sb.Append(GXDbHelpers.AddQuotes(asTable[it.Table2Type], settings.TableQuotation));
                    }
                    sb.Append(" ON ");
                    if (asTable.ContainsKey(it.Table1Type))
                    {
                        sb.Append(" ");
                        sb.Append(GXDbHelpers.AddQuotes(asTable[it.Table1Type], settings.TableQuotation));
                    }
                    else
                    {
                        sb.Append(GXDbHelpers.AddQuotes(it.Table1, settings.TableQuotation));
                    }
                    sb.Append('.');
                    sb.Append(it.Column1);
                    sb.Append('=');
                    //Add alias if used.
                    if (asTable.ContainsKey(it.Table2Type))
                    {
                        //sb.Append(it.Table2);
                        sb.Append(GXDbHelpers.AddQuotes(asTable[it.Table2Type], settings.TableQuotation));
                    }
                    else
                    {
                        sb.Append(GXDbHelpers.AddQuotes(it.Table2, settings.TableQuotation));
                        if (it.Alias2 == null && it.Index != 0)
                        {
                            sb.Append(it.Index);
                        }
                    }
                    sb.Append('.');
                    sb.Append(it.Column2);
                    if (it.AllowNull1)
                    {
                        sb.Append(" OR ");
                        //Add alias if used.
                        if (asTable.ContainsKey(it.Table1Type))
                        {
                            sb.Append(GXDbHelpers.AddQuotes(asTable[it.Table1Type], settings.TableQuotation));
                        }
                        else
                        {
                            sb.Append(GXDbHelpers.AddQuotes(it.Table1, settings.TableQuotation));
                        }
                        sb.Append('.');
                        sb.Append(it.Column1);
                        sb.Append(" IS NULL");
                    }

                    if (it.AllowNull2)
                    {
                        sb.Append(" OR ");
                        //Add alias if used.
                        if (asTable.ContainsKey(it.Table2Type))
                        {
                            sb.Append(GXDbHelpers.AddQuotes(asTable[it.Table2Type], settings.TableQuotation));
                        }
                        else
                        {
                            sb.Append(GXDbHelpers.AddQuotes(it.Table2, settings.TableQuotation));
                        }
                        sb.Append('.');
                        sb.Append(it.Column2);
                        sb.Append(" IS NULL");
                    }

                    if (joinList.Count != 1)
                    {
                        sb.Append(")");
                    }
                }
            }
            if (index != 0)
            {
                if (settings.LimitType == LimitType.Top)
                {
                    List <Type> tables = new List <Type>();
                    foreach (var it in columnList)
                    {
                        if (!tables.Contains(it.Key))
                        {
                            tables.Add(it.Key);
                        }
                    }
                    GXSerializedItem si = null;
                    foreach (var it in tables)
                    {
                        if ((si = GXSqlBuilder.FindUnique(it)) != null)
                        {
                            break;
                        }
                    }
                    string id;
                    //Add alias if used.
                    if (asTable.ContainsKey((si.Target as PropertyInfo).ReflectedType))
                    {
                        id = GXDbHelpers.AddQuotes(asTable[(si.Target as PropertyInfo).ReflectedType] + "." + GXDbHelpers.GetColumnName(si.Target as PropertyInfo, '\0'), settings.TableQuotation);
                    }
                    else
                    {
                        id = GXDbHelpers.GetColumnName(si.Target as PropertyInfo, '\0');
                    }

                    sb.Append(string.Format(" ORDER BY {0}) AS GX ORDER BY GX.{0} DESC) AS GX2", id));
                }
            }
        }
예제 #8
0
 /// <summary>
 ///  Get table name.
 /// </summary>
 /// <param name="type">Table type.</param>
 /// <param name="addQuoteSeparator">Is quote separator added.</param>
 /// <param name="allowSharedTables"></param>
 /// <returns>Table name.</returns>
 internal string GetTableName(Type type, bool addQuoteSeparator, bool allowSharedTables)
 {
     return(GXDbHelpers.GetTableName(type, addQuoteSeparator, Settings.TableQuotation, Settings.TablePrefix, allowSharedTables));
 }
예제 #9
0
        /// <summary>
        /// Update DB relations.
        /// </summary>
        /// <param name="mainType"></param>
        /// <param name="s"></param>
        private static void UpdateRelations(Type mainType, GXSerializedItem s, bool primaryData, Dictionary <Type, GXRelationTable> relationTable)
        {
            Type type;

            if (primaryData)
            {
                s.Relation              = new GXRelationTable();
                s.Relation.Column       = s;
                s.Relation.PrimaryTable = mainType;
                s.Relation.PrimaryId    = s;
                if ((s.Attributes & Attributes.ForeignKey) != 0)
                {
                    ForeignKeyAttribute[] fks = ((ForeignKeyAttribute[])(s.Target as PropertyInfo).GetCustomAttributes(typeof(ForeignKeyAttribute), false));
                    ForeignKeyAttribute   fk;
                    if (fks.Length == 0)
                    {
                        fk   = null;
                        type = null;
                    }
                    else
                    {
                        fk   = fks[0];
                        type = fk.Type;
                    }
                    //If type is not give in ForeignKeyAttribute.
                    if (type == null)
                    {
                        type = s.Type;
                        if (typeof(IEnumerable).IsAssignableFrom(type))
                        {
                            type = GXInternal.GetPropertyType(type);
                        }
                    }
                    s.Relation.ForeignTable = type;
                    if (fk != null && fk.MapTable != null)
                    {
                        s.Relation.RelationType = RelationType.ManyToMany;
                    }
                    else if (s.Type != typeof(string) && typeof(System.Collections.IEnumerable).IsAssignableFrom(s.Type))
                    {
                        s.Relation.RelationType = RelationType.OneToMany;
                        s.Relation.PrimaryId    = GXSqlBuilder.FindRelation(type, mainType);
                        if (s.Relation.PrimaryId == null)
                        {
                            throw new Exception(string.Format("Relation create failed. Foreign table '{0}' do not have relation to table '{1}'.",
                                                              GXDbHelpers.GetTableName(type, false, null),
                                                              GXDbHelpers.OriginalTableName(mainType)));
                        }
                    }
                    else
                    {
                        s.Relation.RelationType = RelationType.OneToOne;
                    }
                }
                else if ((s.Attributes & Attributes.Relation) != 0)
                {
                    RelationAttribute ra = ((RelationAttribute[])(s.Target as PropertyInfo).GetCustomAttributes(typeof(RelationAttribute), false))[0];
                    type = ra.Target;
                    if (type == null)
                    {
                        type = s.Type;
                    }
                    if (typeof(IEnumerable).IsAssignableFrom(type))
                    {
                        type = GXInternal.GetPropertyType(type);
                    }
                    s.Relation.ForeignTable = type;
                }
            }
            else
            {
                if ((s.Attributes & Attributes.ForeignKey) != 0)
                {
                    ForeignKeyAttribute[] fks = ((ForeignKeyAttribute[])(s.Target as PropertyInfo).GetCustomAttributes(typeof(ForeignKeyAttribute), false));
                    ForeignKeyAttribute   fk;
                    if (fks.Length == 0)
                    {
                        fk   = null;
                        type = null;
                    }
                    else
                    {
                        fk   = fks[0];
                        type = fk.Type;
                    }
                    //If type is not give in ForeignKeyAttribute.
                    if (type == null)
                    {
                        type = s.Type;
                        if (typeof(IEnumerable).IsAssignableFrom(type))
                        {
                            type = GXInternal.GetPropertyType(type);
                        }
                    }
                    GXSerializedItem secondary = FindUnique(type);
                    if (secondary == null)
                    {
                        throw new Exception(string.Format("Table {0} Relation create failed. Class must be derived from IUnique or target type must set in ForeignKey or Relation attribute.",
                                                          GXDbHelpers.GetTableName(mainType, true, '\'', null)));
                    }
                    s.Relation.ForeignId = secondary;
                    //Update relation map fields.
                    if (fk != null && fk.MapTable != null)
                    {
                        foreach (var it in GetProperties(fk.MapTable))
                        {
                            if ((it.Value.Attributes & Attributes.ForeignKey) != 0 &&
                                s.Relation.ForeignTable == it.Value.Relation.ForeignTable)
                            {
                                s.Relation.RelationMapTable = it.Value;
                                break;
                            }
                        }
                    }
                }
                else if ((s.Attributes & Attributes.Relation) != 0)
                {
                    RelationAttribute ra = ((RelationAttribute[])(s.Target as PropertyInfo).GetCustomAttributes(typeof(RelationAttribute), false))[0];
                    type = ra.Target;
                    if (type == null)
                    {
                        type = s.Type;
                    }
                    if (typeof(IEnumerable).IsAssignableFrom(type))
                    {
                        type = GXInternal.GetPropertyType(type);
                    }
                    GXSerializedItem secondary = FindUnique(type);
                    if (secondary == null)
                    {
                        throw new Exception(string.Format("Table {0} Relation create failed. Class must be derived from IUnique or target type must set in ForeignKey or Relation attribute.",
                                                          GXDbHelpers.GetTableName(mainType, true, '\'', null)));
                    }
                    s.Relation.ForeignId = secondary;
                }
            }
        }