Exemple #1
0
        public int Execute()
        {
            int            count   = 0;
            DbQueryBuilder builder = _query.Build(_query.Query.DataSource, ref count);

            return(_query.Query.DataSource.ExecuteNonQuery(builder.Sql, builder.Parameters));
        }
Exemple #2
0
        protected override void OnBuild(DataSource ds, DbQueryBuilder builder, string column)
        {
            DataParameter dp = Query.Query.BuildParameter(_value);

            builder.Append(dp.GetParameterName());
            builder.Append(dp);
        }
Exemple #3
0
        public IList <R> Execute <R>() where R : IDbReader, new()
        {
            DbQueryBuilder builder = _query.Build(_query.Query.DataSource, 0, false);

            builder.Append(';');
            return(_query.Query.DataSource.ExecuteReader <R>(builder.Sql, builder.Parameters));
        }
Exemple #4
0
        public IList <dynamic> Execute()
        {
            DbQueryBuilder builder = _query.Build(_query.Query.DataSource, 0, false);

            builder.Append(';');
            return(_query.Query.DataSource.ExecuteReader(builder.Sql, builder.Parameters));
        }
Exemple #5
0
        private IList <R> ExecuteImpl <R>(int size, long page) where R : IDbReader, new()
        {
            if (_query.Query.Provider.SupperLimit)
            {
                DbQueryBuilder builder = _query.Build(_query.Query.DataSource, 0, false);
                builder.Append(" LIMIT ").Append(size).Append(" OFFSET ").Append((page - 1) * size);
                builder.Append(';');
                return(_query.Query.DataSource.ExecuteReader <R>(builder.Sql, builder.Parameters));
            }

            if (_query.Query.Provider.SupperTop)
            {
                DbQueryBuilder builder = _query.Build(_query.Query.DataSource, page * size, false);
                builder.Append(';');
                IList <R> list = _query.Query.DataSource.ExecuteReader <R>(builder.Sql, builder.Parameters);
                if (page == 1)
                {
                    return(list);
                }
                List <R> array = new List <R>(size);
                for (long i = ((page - 1) * size); i < list.Count; ++i)
                {
                    array.Add(list[(int)i]);
                }
                return(array);
            }

            throw new NotSupportedException();
        }
Exemple #6
0
        public object[] Execute()
        {
            DbQueryBuilder builder = _query.Build(_query.Query.DataSource, 0, false);

            builder.Append(';');
            return(_query.Query.DataSource.ExecuteArray(builder.Sql, builder.Parameters));
        }
Exemple #7
0
        public dynamic Execute()
        {
            DbQueryBuilder builder = _query.Build(_query.Query.DataSource, 0, false);

            builder.Append(';');
            return(_query.Query.DataSource.ExecuteSingleRow(builder.Sql, builder.Parameters));
        }
Exemple #8
0
        public R Execute <R>()
        {
            DbQueryBuilder builder = _query.Build(_query.Query.DataSource, 0, false);

            builder.Append(';');
            return(_query.Query.DataSource.ExecuteScalar <R>(builder.Sql, builder.Parameters));
        }
Exemple #9
0
        DbQueryBuilder IDbJoinQuery <T, A, B> .Build(DataSource ds, long top, bool join)
        {
            DbQueryBuilder builder = new DbQueryBuilder("SELECT");

            if (top > 0)
            {
                builder.Append(" TOP ").Append(top);
            }
            builder.Append(' ');
            if (_select == null || _select.Length == 0)
            {
                if (join)
                {
                    builder.Append((new DbSelect <B>()).Build(ds));
                }
                else
                {
                    builder.Append((new DbSelect()).Build(ds));
                }
            }
            else
            {
                for (int i = 0; i < _select.Length; ++i)
                {
                    if (i > 0)
                    {
                        builder.Append(',');
                    }
                    builder.Append(_select[i].Build(ds));
                }
            }
            builder.Append(" FROM ").Append(ds.Provider.EscapeName(DbTable.GetTableName <B>()));
            return(builder);
        }
Exemple #10
0
        DbQueryBuilder IDbUpdateQuery.Build(DataSource ds, ref int count)
        {
            DbQueryBuilder builder = new DbQueryBuilder("UPDATE ");

            builder.Append(ds.Provider.EscapeName(DbTable.GetTableName <T>()));
            builder.Append(" SET ");
            return(builder);
        }
Exemple #11
0
        private IList <dynamic> ExecuteImpl(int size, long page, out long count)
        {
            bool           group = false;
            DbQueryBuilder qb    = _query.BuildCount(_query.Query.DataSource, 0, false, ref group);

            if (group)
            {
                qb.Append(") AS T").Append(_query.Query.DataSource.PsCount);
            }
            count = Convert.ToInt64(_query.Query.DataSource.ExecuteScalar(qb.Sql, qb.Parameters));

            if (_query.Query.Provider.SupperRowNumber)
            {
                long half    = count / 2;
                long lower   = (page - 1) * size;
                long upper   = page * size;
                bool reverse = lower > half;
                DbQueryRowNumberBuilder builder = _query.BuildRowNumber(_query.Query.DataSource, reverse ? (count - lower) : upper, false, null, reverse);
                builder.Append(")SELECT * FROM CTE WHERE _RowNumber>");
                if (reverse)
                {
                    builder.Append(count - upper);
                }
                else
                {
                    builder.Append(lower);
                }
                if (builder.OrderBy != null)
                {
                    builder.Append(' ').Append(builder.OrderBy);
                }
                builder.Append(';');
                return(_query.Query.DataSource.ExecuteReader(builder.Sql, builder.Parameters));
            }

            if (_query.Query.Provider.SupperLimit)
            {
                DbQueryBuilder builder = _query.Build(_query.Query.DataSource, 0, false);
                builder.Append(" LIMIT ").Append(size).Append(" OFFSET ").Append((page - 1) * size);
                builder.Append(';');
                return(_query.Query.DataSource.ExecuteReader(builder.Sql, builder.Parameters));
            }

            if (_query.Query.Provider.SupperTop)
            {
                DbQueryBuilder builder = _query.Build(_query.Query.DataSource, page * size, false);
                builder.Append(';');
                IList <dynamic> list  = _query.Query.DataSource.ExecuteReader(builder.Sql, builder.Parameters);
                List <dynamic>  array = new List <dynamic>(size);
                for (long i = ((page - 1) * size); i < list.Count; ++i)
                {
                    array.Add(list[(int)i]);
                }
                return(array);
            }

            throw new NotSupportedException();
        }
Exemple #12
0
        DbQueryBuilder IDbUpdateQuery.Build(DataSource ds, ref int count)
        {
            DbQueryBuilder builder = _query.Build(ds, ref count);

            if (_where != null)
            {
                builder.Append(" WHERE ").Append(_where.Build(ds));
            }
            return(builder);
        }
Exemple #13
0
        DbQueryBuilder IDbSubQuery <R> .Build(DataSource ds, long top, bool join)
        {
            DbQueryBuilder builder = _query.Build(ds, top, join);

            if (_where != null)
            {
                builder.Append(" WHERE ").Append(_where.Build(ds));
            }
            return(builder);
        }
Exemple #14
0
        DbQueryBuilder IDbSelectQuery.BuildCount(DataSource ds, long top, bool join, ref bool group)
        {
            DbQueryBuilder builder = new DbQueryBuilder("SELECT COUNT(*) FROM ");

            if (group)
            {
                builder.Append("(SELECT COUNT(*) AS C").Append(ds.PsCount).Append(" FROM ");
            }
            builder.Append(ds.Provider.EscapeName(DbTable.GetTableName <T>()));
            return(builder);
        }
Exemple #15
0
        DbQueryBuilder IDbUpdateQuery.Build(DataSource ds, ref int count)
        {
            DbQueryBuilder builder    = _query.Build(ds, ref count);
            DbQueryBuilder subBuilder = _subQuery.Build(ds, 0, false);

            builder.Append('(');
            builder.Append(subBuilder.Sql);
            builder.Append(')');
            builder.Append(subBuilder.Parameters);
            return(builder);
        }
Exemple #16
0
        public long Execute()
        {
            bool           group   = false;
            DbQueryBuilder builder = _query.BuildCount(_query.Query.DataSource, 0, false, ref group);

            if (group)
            {
                builder.Append(") AS T").Append(_query.Query.DataSource.PsCount);
            }
            builder.Append(';');
            return(Convert.ToInt64(_query.Query.DataSource.ExecuteScalar(builder.Sql, builder.Parameters)));
        }
Exemple #17
0
 internal DbQueryBuilder Append(DbQueryBuilder builder)
 {
     if (builder._sql.Length > 0)
     {
         _sql.Append(builder._sql.ToString());
     }
     if (builder._parameters.Count > 0)
     {
         _parameters.AddRange(builder._parameters);
     }
     return(this);
 }
Exemple #18
0
        public bool Execute()
        {
            DbQueryBuilder builder = _subQuery.Build(_query.DataSource, 0, false);
            StringBuilder  sb      = new StringBuilder();

            sb.Append("INSERT INTO ");
            sb.Append(_query.Provider.EscapeName(DbTable.GetTableName <T>()));
            sb.Append(' ');
            sb.Append(GetNames());
            sb.Append(builder.Sql);
            sb.Append(';');
            return(DbTable.InsertImpl(_query.DataSource, sb.ToString(), builder.Parameters));
        }
Exemple #19
0
        DbQueryBuilder IDbUpdateQuery.Build(DataSource ds, ref int count)
        {
            string         name    = _column.Build(ds);
            DbQueryBuilder builder = _query.Build(ds, ref count);

            if (count++ > 0)
            {
                builder.Append(',');
            }
            builder.Append(name);
            builder.Append('=');
            OnBuild(ds, builder, name);
            return(builder);
        }
Exemple #20
0
        DbQueryBuilder IDbJoinQuery <T, A, B> .Build(DataSource ds, long top, bool join)
        {
            DbQueryBuilder builder = _query.Build(ds, top, join);

            builder.Append(" GROUP BY ");
            for (int i = 0; i < _group.Length; ++i)
            {
                if (i > 0)
                {
                    builder.Append(',');
                }
                builder.Append(_group[i].Build(ds));
            }
            return(builder);
        }
Exemple #21
0
        DbQueryBuilder IDbSelectQuery.Build(DataSource ds, long top, bool join)
        {
            DbQueryBuilder builder = _query.Build(ds, top, join);

            builder.Append(" ORDER BY ");
            for (int i = 0; i < _order.Length; ++i)
            {
                if (i > 0)
                {
                    builder.Append(',');
                }
                builder.Append(_order[i].Build(ds));
            }
            return(builder);
        }
Exemple #22
0
        public int Execute()
        {
            StringBuilder        sb = new StringBuilder();
            List <DataParameter> ps = new List <DataParameter>();

            sb.Append("DELETE FROM ");
            sb.Append(_query.Query.Provider.EscapeName(DbTable.GetTableName <T>()));
            if (_where != null)
            {
                DbQueryBuilder builder = _where.Build(_query.Query.DataSource);
                sb.Append(" WHERE ").Append(builder.Sql);
                ps.AddRange(builder.Parameters);
            }
            sb.Append(';');
            return(_query.Query.DataSource.ExecuteNonQuery(sb.ToString(), ps.ToArray()));
        }
Exemple #23
0
        internal DbQueryBuilder Build(DataSource ds)
        {
            DbQueryBuilder builder = new DbQueryBuilder();

            foreach (object value in _queue)
            {
                if (value is DbWhere)
                {
                    builder.Append(((DbWhere)value).Build(ds));
                }
                else if (value is DbWhereQueue)
                {
                    builder.Append(((DbWhereQueue)value).Build(ds));
                }
                else
                {
                    builder.Append((string)value);
                }
            }
            return(builder);
        }
Exemple #24
0
 internal void Build(DbQueryBuilder builder)
 {
     _parent.Build(builder);
     OnBuild(builder);
 }
Exemple #25
0
 protected virtual void OnBuild(DataSource ds, DbQueryBuilder builder, string column)
 {
 }
Exemple #26
0
 internal override void OnBuild(DbQueryBuilder builder)
 {
     builder.Append(" ELSE ").Append(Expression);
 }
Exemple #27
0
 internal override void OnBuild(DbQueryBuilder builder)
 {
     builder.Append(" END");
 }
Exemple #28
0
        internal DbQueryBuilder Build(DataSource ds)
        {
            if (_type == DbWhereType.Unkown)
            {
                throw new ArgumentException();
            }
            switch (_type)
            {
            case DbWhereType.Default:
            {
                return(new DbQueryBuilder("1=1", null));
            }

            case DbWhereType.Equal:
            {
                if (_value == null)
                {
                    return(new DbQueryBuilder(string.Concat(GetName(ds), " IS NULL"), null));
                }
                DataParameter p = GetParameter(ds);
                return(new DbQueryBuilder(string.Concat(GetName(ds), '=', p.GetParameterName()), p));
            }

            case DbWhereType.NotEqual:
            {
                if (_value == null)
                {
                    return(new DbQueryBuilder(string.Concat("NOT ", GetName(ds), " IS NULL"), null));
                }
                DataParameter p = GetParameter(ds);
                return(new DbQueryBuilder(string.Concat(GetName(ds), "<>", p.GetParameterName()), p));
            }

            case DbWhereType.GreaterThan:
            {
                if (_value == null)
                {
                    throw new ArgumentNullException();
                }
                DataParameter p = GetParameter(ds);
                return(new DbQueryBuilder(string.Concat(GetName(ds), '>', p.GetParameterName()), p));
            }

            case DbWhereType.GreaterThanOrEqual:
            {
                if (_value == null)
                {
                    throw new ArgumentNullException();
                }
                DataParameter p = GetParameter(ds);
                return(new DbQueryBuilder(string.Concat(GetName(ds), ">=", p.GetParameterName()), p));
            }

            case DbWhereType.LessThan:
            {
                if (_value == null)
                {
                    throw new ArgumentNullException();
                }
                DataParameter p = GetParameter(ds);
                return(new DbQueryBuilder(string.Concat(GetName(ds), '<', p.GetParameterName()), p));
            }

            case DbWhereType.LessThanOrEqual:
            {
                if (_value == null)
                {
                    throw new ArgumentNullException();
                }
                DataParameter p = GetParameter(ds);
                return(new DbQueryBuilder(string.Concat(GetName(ds), "<=", p.GetParameterName()), p));
            }

            case DbWhereType.Like:
            {
                if (_value == null)
                {
                    throw new ArgumentNullException();
                }
                DataParameter p = GetParameter(ds, string.Concat('%', Convert.ToString(_value), '%'));
                return(new DbQueryBuilder(string.Concat(GetName(ds), " LIKE ", p.GetParameterName()), p));
            }

            case DbWhereType.LikeBegin:
            {
                if (_value == null)
                {
                    throw new ArgumentNullException();
                }
                DataParameter p = GetParameter(ds, string.Concat(Convert.ToString(_value), '%'));
                return(new DbQueryBuilder(string.Concat(GetName(ds), " LIKE ", p.GetParameterName()), p));
            }

            case DbWhereType.LikeEnd:
            {
                if (_value == null)
                {
                    throw new ArgumentNullException();
                }
                DataParameter p = GetParameter(ds, string.Concat('%', Convert.ToString(_value)));
                return(new DbQueryBuilder(string.Concat(GetName(ds), " LIKE ", p.GetParameterName()), p));
            }

            case DbWhereType.In:
            {
                if (_value == null)
                {
                    throw new ArgumentNullException();
                }
                IEnumerable e = _value as IEnumerable;
                if (e != null)
                {
                    DataParameter        p;
                    List <string>        vs    = new List <string>();
                    List <DataParameter> ps    = new List <DataParameter>();
                    IEnumerator          array = e.GetEnumerator();
                    while (array.MoveNext())
                    {
                        p = GetParameter(ds, array.Current);
                        vs.Add(p.GetParameterName());
                        ps.Add(p);
                    }
                    return(new DbQueryBuilder(string.Concat(GetName(ds), " IN (", string.Join(",", vs.ToArray()), ')'), ps.ToArray()));
                }
                IDbSubQuery <DbWhere> q = _value as IDbSubQuery <DbWhere>;
                if (q != null)
                {
                    DbQueryBuilder builder = q.Build(ds, 0, false);
                    return(new DbQueryBuilder(string.Concat(GetName(ds), " IN (", builder.Sql, ')'), builder.Parameters));
                }
                throw new NotSupportedException();
            }
            }
            throw new NotSupportedException();
        }
Exemple #29
0
 internal override void OnBuild(DbQueryBuilder builder)
 {
     builder.Append(" WHEN ").Append(_when).Append(" THEN ").Append(Expression);
 }
Exemple #30
0
 internal abstract void OnBuild(DbQueryBuilder builder);