Beispiel #1
0
        public string SelectInto <TModel>()
        {
            if ((CustomSelect && OnlyFields == null) || (typeof(TModel) == typeof(T) && !PrefixFieldWithTableName))
            {
                return(ToSelectStatement());
            }

            useFieldName = true;

            var sbSelect    = StringBuilderCache.Allocate();
            var selectDef   = modelDef;
            var orderedDefs = tableDefs;

            if (typeof(TModel) != typeof(List <object>) &&
                typeof(TModel) != typeof(Dictionary <string, object>) &&
                typeof(TModel) != typeof(object) && //dynamic
                !typeof(TModel).IsValueTuple())
            {
                selectDef = typeof(TModel).GetModelDefinition();
                if (selectDef != modelDef && tableDefs.Contains(selectDef))
                {
                    orderedDefs = tableDefs.ToList(); //clone
                    orderedDefs.Remove(selectDef);
                    orderedDefs.Insert(0, selectDef);
                }
            }

            foreach (var fieldDef in selectDef.FieldDefinitions)
            {
                var found = false;

                if (fieldDef.BelongToModelName != null)
                {
                    var tableDef = orderedDefs.FirstOrDefault(x => x.Name == fieldDef.BelongToModelName);
                    if (tableDef != null)
                    {
                        var matchingField = FindWeakMatch(tableDef, fieldDef);
                        if (matchingField != null)
                        {
                            if (OnlyFields == null || OnlyFields.Contains(fieldDef.Name))
                            {
                                if (sbSelect.Length > 0)
                                {
                                    sbSelect.Append(", ");
                                }

                                if (fieldDef.CustomSelect == null)
                                {
                                    if (!fieldDef.IsRowVersion)
                                    {
                                        sbSelect.Append($"{GetQuotedColumnName(tableDef, matchingField.Name)} AS {SqlColumn(fieldDef.Name)}");
                                    }
                                    else
                                    {
                                        sbSelect.Append(DialectProvider.GetRowVersionSelectColumn(fieldDef, DialectProvider.GetTableName(tableDef.ModelName)));
                                    }
                                }
                                else
                                {
                                    sbSelect.Append(fieldDef.CustomSelect + " AS " + fieldDef.FieldName);
                                }

                                continue;
                            }
                        }
                    }
                }

                foreach (var tableDef in orderedDefs)
                {
                    foreach (var tableFieldDef in tableDef.FieldDefinitions)
                    {
                        if (tableFieldDef.Name == fieldDef.Name)
                        {
                            if (OnlyFields != null && !OnlyFields.Contains(fieldDef.Name))
                            {
                                continue;
                            }

                            if (sbSelect.Length > 0)
                            {
                                sbSelect.Append(", ");
                            }

                            var tableAlias = tableDef == modelDef // Use TableAlias if source modelDef
                                ? TableAlias
                                : null;

                            if (fieldDef.CustomSelect == null)
                            {
                                if (!fieldDef.IsRowVersion)
                                {
                                    sbSelect.Append(tableAlias == null
                                        ? GetQuotedColumnName(tableDef, tableFieldDef.Name)
                                        : GetQuotedColumnName(tableDef, tableAlias, tableFieldDef.Name));

                                    if (tableFieldDef.RequiresAlias)
                                    {
                                        sbSelect.Append(" AS ").Append(SqlColumn(fieldDef.Name));
                                    }
                                }
                                else
                                {
                                    sbSelect.Append(DialectProvider.GetRowVersionSelectColumn(fieldDef, DialectProvider.GetTableName(tableAlias ?? tableDef.ModelName)));
                                }
                            }
                            else
                            {
                                sbSelect.Append(tableFieldDef.CustomSelect).Append(" AS ").Append(tableFieldDef.FieldName);
                            }

                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        break;
                    }
                }

                if (!found)
                {
                    // Add support for auto mapping `{Table}{Field}` convention
                    foreach (var tableDef in orderedDefs)
                    {
                        var matchingField = FindWeakMatch(tableDef, fieldDef);
                        if (matchingField != null)
                        {
                            if (OnlyFields != null && !OnlyFields.Contains(fieldDef.Name))
                            {
                                continue;
                            }

                            if (sbSelect.Length > 0)
                            {
                                sbSelect.Append(", ");
                            }

                            var tableAlias = tableDef == modelDef // Use TableAlias if source modelDef
                                ? TableAlias
                                : null;

                            sbSelect.Append($"{DialectProvider.GetQuotedColumnName(tableDef, tableAlias, matchingField)} as {SqlColumn(fieldDef.Name)}");

                            break;
                        }
                    }
                }
            }

            var select = StringBuilderCache.ReturnAndFree(sbSelect);

            var columns = select.Length > 0 ? select : "*";

            SelectExpression = "SELECT " + (selectDistinct ? "DISTINCT " : "") + columns;

            return(ToSelectStatement());
        }
        public string SelectInto <TModel>()
        {
            if ((CustomSelect && OnlyFields == null) || (typeof(TModel) == typeof(T) && !PrefixFieldWithTableName))
            {
                return(ToSelectStatement());
            }

            var sbSelect = StringBuilderCache.Allocate();

            var selectDef   = typeof(TModel).GetModelDefinition();
            var orderedDefs = tableDefs;

            if (selectDef != modelDef && tableDefs.Contains(selectDef))
            {
                orderedDefs = tableDefs.ToList(); //clone
                orderedDefs.Remove(selectDef);
                orderedDefs.Insert(0, selectDef);
            }

            foreach (var fieldDef in selectDef.FieldDefinitions)
            {
                var found = false;

                if (fieldDef.BelongToModelName != null)
                {
                    var tableDef = orderedDefs.FirstOrDefault(x => x.Name == fieldDef.BelongToModelName);
                    if (tableDef != null)
                    {
                        var matchingField = FindWeakMatch(tableDef, fieldDef);
                        if (matchingField != null)
                        {
                            if (OnlyFields == null || OnlyFields.Contains(fieldDef.Name))
                            {
                                if (sbSelect.Length > 0)
                                {
                                    sbSelect.Append(", ");
                                }

                                if (fieldDef.CustomSelect == null)
                                {
                                    sbSelect.AppendFormat("{0} as {1}",
                                                          DialectProvider.GetQuotedColumnName(tableDef, matchingField),
                                                          SqlColumn(fieldDef.Name));
                                }
                                else
                                {
                                    sbSelect.Append(fieldDef.CustomSelect + " AS " + fieldDef.FieldName);
                                }

                                continue;
                            }
                        }
                    }
                }

                foreach (var tableDef in orderedDefs)
                {
                    foreach (var tableFieldDef in tableDef.FieldDefinitions)
                    {
                        if (tableFieldDef.Name == fieldDef.Name)
                        {
                            if (OnlyFields != null && !OnlyFields.Contains(fieldDef.Name))
                            {
                                continue;
                            }

                            if (sbSelect.Length > 0)
                            {
                                sbSelect.Append(", ");
                            }

                            if (fieldDef.CustomSelect == null)
                            {
                                sbSelect.AppendFormat("{0}.{1}",
                                                      SqlTable(tableDef),
                                                      tableFieldDef.GetQuotedName(DialectProvider));

                                if (tableFieldDef.Alias != null)
                                {
                                    sbSelect.Append(" AS ").Append(SqlColumn(fieldDef.Name));
                                }
                            }
                            else
                            {
                                sbSelect.Append(tableFieldDef.CustomSelect + " AS " + tableFieldDef.FieldName);
                            }

                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        break;
                    }
                }

                if (!found)
                {
                    // Add support for auto mapping `{Table}{Field}` convention
                    foreach (var tableDef in orderedDefs)
                    {
                        var matchingField = FindWeakMatch(tableDef, fieldDef);
                        if (matchingField != null)
                        {
                            if (OnlyFields != null && !OnlyFields.Contains(fieldDef.Name))
                            {
                                continue;
                            }

                            if (sbSelect.Length > 0)
                            {
                                sbSelect.Append(", ");
                            }

                            sbSelect.AppendFormat("{0} as {1}",
                                                  DialectProvider.GetQuotedColumnName(tableDef, matchingField),
                                                  SqlColumn(fieldDef.Name));

                            break;
                        }
                    }
                }
            }

            var select = StringBuilderCache.ReturnAndFree(sbSelect);

            var columns = select.Length > 0 ? select : "*";

            SelectExpression = "SELECT " + (selectDistinct ? "DISTINCT " : "") + columns;

            return(ToSelectStatement());
        }