Esempio n. 1
0
 internal static List <Tuple <T, T2, T3, T4, T5> > SelectMulti <T, T2, T3, T4, T5>(this IDbCommand dbCmd, SqlExpression <T> q)
 {
     q.Select(q.CreateMultiSelect <T, T2, T3, T4, T5, EOT, EOT>(dbCmd.GetDialectProvider()));
     return(dbCmd.ExprConvertToList <Tuple <T, T2, T3, T4, T5> >(q.ToSelectStatement(), q.Params, onlyFields: q.OnlyFields));
 }
        internal static List <Into> LoadListWithReferences <Into, From>(this IDbCommand dbCmd, SqlExpression <From> expr = null, List <string> fieldNames = null)
        {
            var dialectProvider = OrmLiteConfig.DialectProvider;

            if (expr == null)
            {
                expr = dialectProvider.SqlExpression <From>();
            }

            var sql           = expr.SelectInto <Into>(fieldNames);
            var parentResults = dbCmd.ExprConvertToList <Into>(sql);

            var modelDef  = ModelDefinition <Into> .Definition;
            var fieldDefs = modelDef.AllFieldDefinitionsArray.Where(x => x.IsReference);

            expr.Select(dialectProvider.GetQuotedColumnName(modelDef, modelDef.PrimaryKey));
            var subSql = expr.ToSelectStatement();

            foreach (var fieldDef in fieldDefs)
            {
                var listInterface = fieldDef.FieldType.GetTypeWithGenericInterfaceOf(typeof(IList <>));
                if (listInterface != null)
                {
                    var refType     = listInterface.GenericTypeArguments()[0];
                    var refModelDef = refType.GetModelDefinition();

                    var refField = GetRefFieldDef(modelDef, refModelDef, refType);

                    var sqlRef = "SELECT {0} FROM {1} WHERE {2} IN ({3})".Fmt(
                        dialectProvider.GetColumnNames(refModelDef),
                        dialectProvider.GetQuotedTableName(refModelDef),
                        dialectProvider.GetQuotedColumnName(refField),
                        subSql);
                    var childResults = dbCmd.ConvertToList(refType, sqlRef);

                    var           map = new Dictionary <object, List <object> >();
                    List <object> refValues;

                    foreach (var result in childResults)
                    {
                        var refValue = refField.GetValue(result);
                        if (!map.TryGetValue(refValue, out refValues))
                        {
                            map[refValue] = refValues = new List <object>();
                        }
                        refValues.Add(result);
                    }

                    var untypedApi = dbCmd.CreateTypedApi(refType);
                    foreach (var result in parentResults)
                    {
                        var pkValue = modelDef.PrimaryKey.GetValue(result);
                        if (map.TryGetValue(pkValue, out refValues))
                        {
                            var castResults = untypedApi.Cast(refValues);
                            fieldDef.SetValueFn(result, castResults);
                        }
                    }
                }
                else
                {
                    var refType     = fieldDef.FieldType;
                    var refModelDef = refType.GetModelDefinition();

                    var refSelf  = GetSelfRefFieldDefIfExists(modelDef, refModelDef);
                    var refField = refSelf == null
                        ? GetRefFieldDef(modelDef, refModelDef, refType)
                        : GetRefFieldDefIfExists(modelDef, refModelDef);

                    var map = new Dictionary <object, object>();

                    if (refField != null)
                    {
                        var sqlRef = "SELECT {0} FROM {1} WHERE {2} IN ({3})".Fmt(
                            dialectProvider.GetColumnNames(refModelDef),
                            dialectProvider.GetQuotedTableName(refModelDef),
                            dialectProvider.GetQuotedColumnName(refField),
                            subSql);
                        var childResults = dbCmd.ConvertToList(refType, sqlRef);

                        foreach (var result in childResults)
                        {
                            var refValue = refField.GetValue(result);
                            map[refValue] = result;
                        }

                        foreach (var result in parentResults)
                        {
                            object childResult;
                            var    pkValue = modelDef.PrimaryKey.GetValue(result);
                            if (map.TryGetValue(pkValue, out childResult))
                            {
                                fieldDef.SetValueFn(result, childResult);
                            }
                        }
                    }
                    else if (refSelf != null)
                    {
                        //Load Self Table.RefTableId PK
                        expr.Select(dialectProvider.GetQuotedColumnName(refSelf));
                        subSql = expr.ToSelectStatement();

                        var sqlRef = "SELECT {0} FROM {1} WHERE {2} IN ({3})".Fmt(
                            dialectProvider.GetColumnNames(refModelDef),
                            dialectProvider.GetQuotedTableName(refModelDef),
                            dialectProvider.GetQuotedColumnName(refModelDef.PrimaryKey),
                            subSql);
                        var childResults = dbCmd.ConvertToList(refType, sqlRef);

                        foreach (var result in childResults)
                        {
                            var pkValue = refModelDef.PrimaryKey.GetValue(result);
                            map[pkValue] = result;
                        }

                        foreach (var result in parentResults)
                        {
                            object childResult;
                            var    fkValue = refSelf.GetValue(result);
                            if (fkValue != null && map.TryGetValue(fkValue, out childResult))
                            {
                                fieldDef.SetValueFn(result, childResult);
                            }
                        }
                    }
                }
            }

            return(parentResults);
        }
Esempio n. 3
0
 internal static List <Tuple <T, T2, T3, T4, T5, T6, T7> > SelectMulti <T, T2, T3, T4, T5, T6, T7>(this IDbCommand dbCmd, SqlExpression <T> q, string[] tableSelects)
 {
     return(dbCmd.ExprConvertToList <Tuple <T, T2, T3, T4, T5, T6, T7> >(q.Select(q.CreateMultiSelect(tableSelects)).ToSelectStatement(), q.Params, onlyFields: q.OnlyFields));
 }
Esempio n. 4
0
 public IUntypedSqlExpression Select()
 {
     q.Select();
     return(this);
 }
 internal static Task <List <Tuple <T, T2, T3, T4, T5, T6, T7> > > SelectMultiAsync <T, T2, T3, T4, T5, T6, T7>(this IDbCommand dbCmd, SqlExpression <T> q, CancellationToken token)
 {
     q.Select(q.CreateMultiSelect <T, T2, T3, T4, T5, T6, T7>(dbCmd.GetDialectProvider()));
     return(dbCmd.ExprConvertToListAsync <Tuple <T, T2, T3, T4, T5, T6, T7> >(q.ToSelectStatement(), q.Params, q.OnlyFields, token));
 }
Esempio n. 6
0
 internal static Task <List <Tuple <T, T2, T3, T4, T5, T6, T7, T8> > > SelectMultiAsync <T, T2, T3, T4, T5, T6, T7, T8>(this IDbCommand dbCmd, SqlExpression <T> q, string[] tableSelects, CancellationToken token)
 {
     return(dbCmd.ExprConvertToListAsync <Tuple <T, T2, T3, T4, T5, T6, T7, T8> >(q.Select(q.CreateMultiSelect(tableSelects)).ToSelectStatement(QueryType.Select), q.Params, q.OnlyFields, token));
 }