Exemplo n.º 1
0
        public SqlExpression <T> Join <Source, Target>(Expression <Func <Source, Target, bool> > joinExpr = null)
        {
            PrefixFieldWithTableName = true;

            var sourceDef = typeof(Source).GetModelDefinition();
            var targetDef = typeof(Target).GetModelDefinition();

            if (tableDefs.Count == 0)
            {
                tableDefs.Add(modelDef);
            }
            if (!tableDefs.Contains(sourceDef))
            {
                tableDefs.Add(sourceDef);
            }
            if (!tableDefs.Contains(targetDef))
            {
                tableDefs.Add(targetDef);
            }

            var fromExpr = FromExpression;
            var sbJoin   = new StringBuilder();

            string sqlExpr;

            if (joinExpr != null)
            {
                sqlExpr = Visit(joinExpr).ToString();
            }
            else
            {
                var refField = OrmLiteReadExtensions.GetRefFieldDef(sourceDef, targetDef, typeof(Source));
                if (refField == null)
                {
                    throw new ArgumentException("Could not infer relationship between {0} and {1}"
                                                .Fmt(sourceDef.ModelName, targetDef.ModelName));
                }

                sqlExpr = "\n({0}.{1} = {2}.{3})".Fmt(
                    sourceDef.ModelName.SqlTable(),
                    sourceDef.PrimaryKey.FieldName.SqlColumn(),
                    targetDef.ModelName.SqlTable(),
                    refField.FieldName.SqlColumn());
            }

            sbJoin.Append(" INNER JOIN {0} ".Fmt(targetDef.ModelName.SqlTable()));
            sbJoin.Append(" ON ");
            sbJoin.Append(sqlExpr);

            FromExpression = fromExpr + sbJoin;

            return(this);
        }
        private SqlExpression <T> InternalJoin(string joinType,
                                               Expression joinExpr, ModelDefinition sourceDef, ModelDefinition targetDef)
        {
            PrefixFieldWithTableName = true;

            var fromExpr = FromExpression;
            var sbJoin   = new StringBuilder();

            string sqlExpr;

            //Changes how Sql Expressions are generated.
            useFieldName = true;
            sep          = " ";

            if (joinExpr != null)
            {
                sqlExpr = Visit(joinExpr).ToString();
            }
            else
            {
                var parentDef = sourceDef;
                var childDef  = targetDef;

                var refField = OrmLiteReadExtensions.GetRefFieldDefIfExists(parentDef, childDef);
                if (refField == null)
                {
                    parentDef = targetDef;
                    childDef  = sourceDef;
                    refField  = OrmLiteReadExtensions.GetRefFieldDefIfExists(parentDef, childDef);
                }

                if (refField == null)
                {
                    throw new ArgumentException("Could not infer relationship between {0} and {1}"
                                                .Fmt(sourceDef.ModelName, targetDef.ModelName));
                }

                sqlExpr = "\n({0}.{1} = {2}.{3})".Fmt(
                    DialectProvider.GetQuotedTableName(parentDef),
                    SqlColumn(parentDef.PrimaryKey.FieldName),
                    DialectProvider.GetQuotedTableName(childDef),
                    SqlColumn(refField.FieldName));
            }

            var joinDef = tableDefs.Contains(targetDef) && !tableDefs.Contains(sourceDef)
                              ? sourceDef
                              : targetDef;

            sbJoin.Append(" {0} {1} ".Fmt(joinType, SqlTable(joinDef)));
            sbJoin.Append(" ON ");
            sbJoin.Append(sqlExpr);

            FromExpression = fromExpr + sbJoin;

            if (!tableDefs.Contains(sourceDef))
            {
                tableDefs.Add(sourceDef);
            }
            if (!tableDefs.Contains(targetDef))
            {
                tableDefs.Add(targetDef);
            }

            return(this);
        }