Example #1
0
 public ConcreteJoin(Joinable right, Expr condition, Joinable left = null, bool outer = false)
 {
     this.left      = left;
     this.right     = right;
     this.outer     = outer;
     this.condition = condition;
 }
Example #2
0
        public Query Join(params Join[] joins)
        {
            foreach (var join in joins.Cast <ConcreteJoin>())
            {
                from = join.Left(from, root);
            }

            return(this);
        }
Example #3
0
        internal string GetSelectFragment(string entitySuffix, string collectionSuffix, OuterJoinableAssociation next)
        {
            switch (SelectMode)
            {
            case SelectMode.Undefined:
            case SelectMode.Fetch:
#pragma warning disable 618
                return(Joinable.SelectFragment(
                           next?.Joinable,
                           next?.RHSAlias,
                           RHSAlias,
                           entitySuffix,
                           collectionSuffix,
                           ShouldFetchCollectionPersister()));

#pragma warning restore 618

            case SelectMode.FetchLazyProperties:
#pragma warning disable 618
                return(ReflectHelper.CastOrThrow <ISupportSelectModeJoinable>(Joinable, "fetch lazy properties")
                       .SelectFragment(
                           next?.Joinable,
                           next?.RHSAlias,
                           RHSAlias,
                           entitySuffix,
                           collectionSuffix,
                           ShouldFetchCollectionPersister(),
                           true));

#pragma warning restore 618

            case SelectMode.FetchLazyPropertyGroup:
                return(ReflectHelper.CastOrThrow <ISupportLazyPropsJoinable>(Joinable, "fetch lazy property")
                       .SelectFragment(
                           next?.Joinable,
                           next?.RHSAlias,
                           RHSAlias,
                           collectionSuffix,
                           ShouldFetchCollectionPersister(),
                           new EntityLoadInfo(entitySuffix)
                {
                    LazyProperties = EntityFetchLazyProperties,
                    IncludeLazyProps = SelectMode == SelectMode.FetchLazyProperties,
                }));

            case SelectMode.ChildFetch:
                return(ReflectHelper.CastOrThrow <ISupportSelectModeJoinable>(Joinable, "child fetch select mode")
                       .IdentifierSelectFragment(RHSAlias, entitySuffix));

            case SelectMode.JoinOnly:
                return(string.Empty);

            default:
                throw new ArgumentOutOfRangeException(nameof(SelectMode), $"{SelectMode} is unexpected.");
            }
        }
Example #4
0
        public Query From(Joinable from)
        {
            if (root == null)
            {
                root = from as Table;
            }

            this.from = from;
            return(this);
        }
Example #5
0
 internal Join Left(Joinable left, Table root)
 {
     return(new ConcreteJoin(right, Rewrite(condition, root, right.GetTable()), left, outer));
 }
Example #6
0
        /// <summary>
        /// Get a Join on TWO tables.
        /// </summary>
        /// <param name="tables"></param>
        /// <param name="c"></param>
        /// <param name="g"></param>
        /// <param name="o"></param>
        /// <returns></returns>
        public Result Join(Joinable[] tables, Condition[] c, GroupBy g, OrderBy[] o)
        {
            string sql = "SELECT ";
            List<string> fields = new List<string>();
            foreach (Joinable j in tables)
            {
                sql += String.Join(",", j.GetFields(true)) + ", ";
                foreach (string item in j.GetFields(true))
                {
                    fields.Add(item);
                }
            }
            sql = sql.Remove(sql.Length - 2);
            sql += " FROM ";
            foreach (Joinable j in tables)
            {
                sql += j.GetTableName() + ", ";
            }
            sql = sql.Remove(sql.Length - 2);
            sql += " WHERE (";
            sql += tables[0].GetJoinOn(tables[1]) + "=" + tables[1].GetJoinOn(tables[0]); //TODO: make this work for more tables...
            sql += ") ";
            if (c != null)
            {
                sql += " AND (";
                foreach (Condition item in c)
                {
                    sql += item.ToString() + " AND ";
                }
                sql = sql.Remove(sql.Length - 5);
                sql += ") ";
            }
            if (g != null)
            {
                sql += g.ToString();
            }
            if (o != null)
            {
                sql += " ORDER BY ";
                foreach (OrderBy item in o)
                {
                    sql += item.ToShortString() + ", ";
                }
                sql = sql.Remove(sql.Length - 2);
            }
            this.cmd.CommandText = sql;
            Result r = new Result(this.cmd.CommandText);
            if (this.connection.State != System.Data.ConnectionState.Open) this.connection.Open();
            SQLiteDataReader re = this.cmd.ExecuteReader();
            while (re.Read())
            {
                Row row = new Row(re.StepCount);

                foreach (string f in fields)
                {
                    row.AddCell(f, re[f].ToString());
                }

                r.AddRow(row);
            }
            re.Close();
            return r;
        }
Example #7
0
        public Result getRow(Joinable j, string[] fields, Condition[] c = null, GroupBy g = null, OrderBy[] o = null, int limit = 0)
        {
            this.cmd.CommandText = "SELECT " + String.Join(",", fields) + " FROM " + j.GetTableName();
            if (c != null)
            {
                this.cmd.CommandText += " WHERE (";
                foreach (Condition item in c)
                {
                    this.cmd.CommandText += item.ToString() + " AND ";
                }
                this.cmd.CommandText = this.cmd.CommandText.Remove(this.cmd.CommandText.Length - 5);
                this.cmd.CommandText += ") ";
            }
            if (g != null)
            {
                this.cmd.CommandText += g.ToString();
            }
            if (o != null)
            {
                this.cmd.CommandText += " ORDER BY ";
                foreach (OrderBy item in o)
                {
                    this.cmd.CommandText += item.ToShortString() + ", ";
                }
                this.cmd.CommandText = this.cmd.CommandText.Remove(this.cmd.CommandText.Length - 2);
            }
            if (limit != 0)
            {
                this.cmd.CommandText += " LIMIT " + limit.ToString();
            }
            Result r = new Result(this.cmd.CommandText);
            if (this.connection.State != System.Data.ConnectionState.Open) this.connection.Open();
            SQLiteDataReader re = this.cmd.ExecuteReader();
            while (re.Read())
            {
                Row row = new Row(re.StepCount);

                foreach (string f in fields)
                {
                    row.AddCell(f, re[f].ToString());
                }

                r.AddRow(row);
            }
            re.Close();
            return r;
        }