Esempio n. 1
0
		public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
		{
			SqlStatementKind kind = Target.BuildSql(b, context);
			SqlStatementKind resultKind;
			switch (kind) {
				case SqlStatementKind.Select:
					b.Append(" WHERE ");
					resultKind = SqlStatementKind.SelectWhere;
					break;
				case SqlStatementKind.SelectGroupBy:
					b.Append(" HAVING ");
					resultKind = SqlStatementKind.SelectGroupByHaving;
					break;
				default:
					WrapSqlIntoNestedStatement(b, context);
					b.Append(" WHERE ");
					resultKind = SqlStatementKind.SelectWhere;
					break;
			}
			for (int i = 0; i < Conditions.Count; i++) {
				if (i > 0)
					b.Append(" AND ");
				BuildSqlForCondition(b, context, Conditions[i]);
			}
			b.AppendLine();
			return resultKind;
		}
Esempio n. 2
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            SqlStatementKind kind = Target.BuildSql(b, context);
            SqlStatementKind resultKind;

            switch (kind)
            {
            case SqlStatementKind.Select:
                b.Append(" WHERE ");
                resultKind = SqlStatementKind.SelectWhere;
                break;

            case SqlStatementKind.SelectGroupBy:
                b.Append(" HAVING ");
                resultKind = SqlStatementKind.SelectGroupByHaving;
                break;

            default:
                WrapSqlIntoNestedStatement(b, context);
                b.Append(" WHERE ");
                resultKind = SqlStatementKind.SelectWhere;
                break;
            }
            for (int i = 0; i < Conditions.Count; i++)
            {
                if (i > 0)
                {
                    b.Append(" AND ");
                }
                BuildSqlForCondition(b, context, Conditions[i]);
            }
            b.AppendLine();
            return(resultKind);
        }
Esempio n. 3
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            Target.BuildSql(b, context);

            CallTreeNodeSqlNameSet oldNames = context.CurrentNameSet;
            CallTreeNodeSqlNameSet newNames = new CallTreeNodeSqlNameSet(context);

            string query = "SELECT "
                           + SqlAs(oldNames.NameID, newNames.NameID) + ", "
                           + SqlAs("SUM(" + oldNames.CpuCyclesSpent + ")", newNames.CpuCyclesSpent) + ", "
                           + SqlAs("SUM(" + oldNames.CpuCyclesSpentSelf + ")", newNames.CpuCyclesSpentSelf) + ", "
                           + SqlAs("SUM(" + oldNames.CallCount + ")", newNames.CallCount) + ", "
                           + SqlAs("MAX(" + oldNames.HasChildren + ")", newNames.HasChildren) + ", "
                           + SqlAs("SUM(" + oldNames.ActiveCallCount + ")", newNames.ActiveCallCount);

            if (context.HasIDList)
            {
                query += ", " + SqlAs("GROUP_CONCAT(" + oldNames.ID + ")", newNames.ID);
            }
            query += Environment.NewLine + "FROM (" + Environment.NewLine;
            b.Insert(0, query);
            b.AppendLine(") GROUP BY " + oldNames.NameID);
            context.SetCurrent(newNames, SqlTableType.None, context.HasIDList);

            return(SqlStatementKind.SelectGroupBy);
        }
Esempio n. 4
0
		public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
		{
			SqlStatementKind kind = Target.BuildSql(b, context);
			if (kind == SqlStatementKind.SelectOrderBy)
				WrapSqlIntoNestedStatement(b, context);
			
			b.Append(" ORDER BY ");
			
			for (int i = 0; i < arguments.Count; i++) {
				StringWriter w = new StringWriter();
				ExpressionSqlWriter writer = new ExpressionSqlWriter(w, context, arguments[i].Argument.Parameters[0]);
				writer.Write(arguments[i].Argument.Body);
				
				if (i == 0)
					b.Append(w.ToString());
				else
					b.Append(", " + w.ToString());
				
				if (arguments[i].Descending)
					b.Append(" DESC");
				else
					b.Append(" ASC");
			}
			
			return SqlStatementKind.SelectOrderBy;
		}
Esempio n. 5
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            if (context.RequireIDList)
            {
                throw new NotSupportedException();
            }

            CallTreeNodeSqlNameSet newNames = new CallTreeNodeSqlNameSet(context);

            context.SetCurrent(newNames, SqlTableType.None, false);

            b.AppendLine("SELECT "
                         + SqlAs("nameid", newNames.NameID) + ", "
                         + SqlAs("SUM(cpucyclesspent)", newNames.CpuCyclesSpent) + ", "
                         + SqlAs("SUM(cpucyclesspentself)", newNames.CpuCyclesSpentSelf) + ", "
                         + SqlAs("SUM(callcount)", newNames.CallCount) + ", "
                         + SqlAs("MAX(hasChildren)", newNames.HasChildren) + ", "
                         + SqlAs("SUM(CASE WHEN datasetid = " + context.StartDataSet.ID + " THEN activecallcount ELSE 0 END)", newNames.ActiveCallCount));
            b.AppendLine("FROM Functions");
            if (StartDataSet >= 0)
            {
                b.AppendLine("WHERE datasetid BETWEEN " + StartDataSet + " AND " + EndDataSet);
            }
            b.AppendLine("GROUP BY nameid");
            return(SqlStatementKind.SelectGroupBy);
        }
Esempio n. 6
0
        static void BuildSqlForCondition(StringBuilder b, SqlQueryContext context, LambdaExpression condition)
        {
            Debug.Assert(condition.Parameters.Count == 1);
            StringWriter        w      = new StringWriter(CultureInfo.InvariantCulture);
            ExpressionSqlWriter writer = new ExpressionSqlWriter(w, context, condition.Parameters[0]);

            writer.Write(condition.Body);
            b.Append(w.ToString());
        }
		public ExpressionSqlWriter(TextWriter w, SqlQueryContext context, ParameterExpression callTreeNodeParameter)
		{
			if (w == null)
				throw new ArgumentNullException("w");
			if (context == null)
				throw new ArgumentNullException("context");
			this.w = w;
			this.context = context;
			this.nameSet = context.CurrentNameSet;
			this.callTreeNodeParameter = callTreeNodeParameter;
		}
Esempio n. 8
0
		public CallTreeNodeSqlNameSet(SqlQueryContext c)
		{
			string prefix = c.GenerateUniqueVariableName();
			ID = prefix + "ID";
			NameID = prefix + "NameID";
			CpuCyclesSpent = prefix + "CpuCyclesSpent";
			CpuCyclesSpentSelf = prefix + "CpuCyclesSpentSelf";
			CallCount = prefix + "CallCount";
			HasChildren = prefix + "HasChildren";
			ActiveCallCount = prefix + "ActiveCallCount";
		}
Esempio n. 9
0
		public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
		{
			SqlStatementKind kind = Target.BuildSql(b, context);
			if (kind == SqlStatementKind.SelectLimit)
				WrapSqlIntoNestedStatement(b, context);
			
			b.Append(" LIMIT " + Length);
			b.AppendLine(" OFFSET " + Start);
			
			return SqlStatementKind.SelectLimit;
		}
Esempio n. 10
0
        public CallTreeNodeSqlNameSet(SqlQueryContext c)
        {
            string prefix = c.GenerateUniqueVariableName();

            ID                 = prefix + "ID";
            NameID             = prefix + "NameID";
            CpuCyclesSpent     = prefix + "CpuCyclesSpent";
            CpuCyclesSpentSelf = prefix + "CpuCyclesSpentSelf";
            CallCount          = prefix + "CallCount";
            HasChildren        = prefix + "HasChildren";
            ActiveCallCount    = prefix + "ActiveCallCount";
        }
Esempio n. 11
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            SqlStatementKind kind = Target.BuildSql(b, context);

            if (kind == SqlStatementKind.SelectLimit)
            {
                WrapSqlIntoNestedStatement(b, context);
            }

            b.Append(" LIMIT " + Length);
            b.AppendLine(" OFFSET " + Start);

            return(SqlStatementKind.SelectLimit);
        }
 public ExpressionSqlWriter(TextWriter w, SqlQueryContext context, ParameterExpression callTreeNodeParameter)
 {
     if (w == null)
     {
         throw new ArgumentNullException("w");
     }
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     this.w       = w;
     this.context = context;
     this.nameSet = context.CurrentNameSet;
     this.callTreeNodeParameter = callTreeNodeParameter;
 }
Esempio n. 13
0
		public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
		{
			CallTreeNodeSqlNameSet newNames = new CallTreeNodeSqlNameSet(context);
			context.SetCurrent(newNames, SqlTableType.Calls, true);
			
			b.AppendLine("SELECT "
			             + SqlAs("nameid", newNames.NameID) + ", "
			             + SqlAs("cpucyclesspent", newNames.CpuCyclesSpent) + ", "
			             + SqlAs("cpucyclesspentself", newNames.CpuCyclesSpentSelf) + ", "
			             + SqlAs("callcount", newNames.CallCount) + ", "
			             + SqlAs("(id != endid)", newNames.HasChildren) + ", "
			             + SqlAs("((id BETWEEN " + context.StartDataSet.RootID + " AND " + context.StartDataSet.CallEndID
			                     + ") AND isActiveAtStart)", newNames.ActiveCallCount) + ", "
			             + SqlAs("id", newNames.ID));
			b.AppendLine("FROM Calls");
			return SqlStatementKind.Select;
		}
Esempio n. 14
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            CallTreeNodeSqlNameSet newNames = new CallTreeNodeSqlNameSet(context);

            context.SetCurrent(newNames, SqlTableType.Calls, true);

            b.AppendLine("SELECT "
                         + SqlAs("nameid", newNames.NameID) + ", "
                         + SqlAs("cpucyclesspent", newNames.CpuCyclesSpent) + ", "
                         + SqlAs("cpucyclesspentself", newNames.CpuCyclesSpentSelf) + ", "
                         + SqlAs("callcount", newNames.CallCount) + ", "
                         + SqlAs("(id != endid)", newNames.HasChildren) + ", "
                         + SqlAs("((id BETWEEN " + context.StartDataSet.RootID + " AND " + context.StartDataSet.CallEndID
                                 + ") AND isActiveAtStart)", newNames.ActiveCallCount) + ", "
                         + SqlAs("id", newNames.ID));
            b.AppendLine("FROM Calls");
            return(SqlStatementKind.Select);
        }
Esempio n. 15
0
		/// <summary>
		/// Wraps the current SQL statement into an inner select, allowing to continue with "WHERE" queries
		/// even after ORDER BY or LIMIT.
		/// </summary>
		protected static void WrapSqlIntoNestedStatement(StringBuilder b, SqlQueryContext context)
		{
			CallTreeNodeSqlNameSet oldNames = context.CurrentNameSet;
			CallTreeNodeSqlNameSet newNames = new CallTreeNodeSqlNameSet(context);
			
			string query = "SELECT "
				+ SqlAs(oldNames.NameID, newNames.NameID) + ", "
				+ SqlAs(oldNames.CpuCyclesSpent, newNames.CpuCyclesSpent) + ", "
				+ SqlAs(oldNames.CpuCyclesSpentSelf, newNames.CpuCyclesSpentSelf) + ", "
				+ SqlAs(oldNames.CallCount, newNames.CallCount) + ", "
				+ SqlAs(oldNames.HasChildren, newNames.HasChildren) + ", "
				+ SqlAs(oldNames.ActiveCallCount, newNames.ActiveCallCount);
			if (context.HasIDList) {
				query += ", " + SqlAs(oldNames.ID, newNames.ID);
			}
			query += Environment.NewLine + "FROM (" + Environment.NewLine;
			b.Insert(0, query);
			b.AppendLine(")");
			context.SetCurrent(newNames, SqlTableType.None, context.HasIDList);
		}
Esempio n. 16
0
        public IQueryable <CallTreeNode> Execute(SQLiteQueryProvider provider, QueryExecutionOptions options)
        {
            StringBuilder   b       = new StringBuilder();
            SqlQueryContext context = new SqlQueryContext(provider);

            BuildSql(b, context);
            if (options.HasLoggers)
            {
                options.WriteLogLine(b.ToString());
            }
            Stopwatch            w      = Stopwatch.StartNew();
            IList <CallTreeNode> result = provider.RunSQLNodeList(b.ToString(), context.HasIDList);

            w.Stop();
            if (options.HasLoggers)
            {
                options.WriteLogLine("Query returned " + result.Count + " rows in " + w.Elapsed);
            }
            return(result.AsQueryable());
        }
Esempio n. 17
0
		public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
		{
			if (context.RequireIDList)
				throw new NotSupportedException();
			
			CallTreeNodeSqlNameSet newNames = new CallTreeNodeSqlNameSet(context);
			context.SetCurrent(newNames, SqlTableType.None, false);
			
			b.AppendLine("SELECT "
			             + SqlAs("nameid", newNames.NameID) + ", "
			             + SqlAs("SUM(cpucyclesspent)", newNames.CpuCyclesSpent) + ", "
			             + SqlAs("SUM(cpucyclesspentself)", newNames.CpuCyclesSpentSelf) + ", "
			             + SqlAs("SUM(callcount)", newNames.CallCount) + ", "
			             + SqlAs("MAX(hasChildren)", newNames.HasChildren) + ", "
			             + SqlAs("SUM(CASE WHEN datasetid = " + context.StartDataSet.ID + " THEN activecallcount ELSE 0 END)", newNames.ActiveCallCount));
			b.AppendLine("FROM Functions");
			if (StartDataSet >= 0)
				b.AppendLine("WHERE datasetid BETWEEN " + StartDataSet + " AND " + EndDataSet);
			b.AppendLine("GROUP BY nameid");
			return SqlStatementKind.SelectGroupBy;
		}
Esempio n. 18
0
        /// <summary>
        /// Wraps the current SQL statement into an inner select, allowing to continue with "WHERE" queries
        /// even after ORDER BY or LIMIT.
        /// </summary>
        protected static void WrapSqlIntoNestedStatement(StringBuilder b, SqlQueryContext context)
        {
            CallTreeNodeSqlNameSet oldNames = context.CurrentNameSet;
            CallTreeNodeSqlNameSet newNames = new CallTreeNodeSqlNameSet(context);

            string query = "SELECT "
                           + SqlAs(oldNames.NameID, newNames.NameID) + ", "
                           + SqlAs(oldNames.CpuCyclesSpent, newNames.CpuCyclesSpent) + ", "
                           + SqlAs(oldNames.CpuCyclesSpentSelf, newNames.CpuCyclesSpentSelf) + ", "
                           + SqlAs(oldNames.CallCount, newNames.CallCount) + ", "
                           + SqlAs(oldNames.HasChildren, newNames.HasChildren) + ", "
                           + SqlAs(oldNames.ActiveCallCount, newNames.ActiveCallCount);

            if (context.HasIDList)
            {
                query += ", " + SqlAs(oldNames.ID, newNames.ID);
            }
            query += Environment.NewLine + "FROM (" + Environment.NewLine;
            b.Insert(0, query);
            b.AppendLine(")");
            context.SetCurrent(newNames, SqlTableType.None, context.HasIDList);
        }
Esempio n. 19
0
        public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
        {
            SqlStatementKind kind = Target.BuildSql(b, context);

            if (kind == SqlStatementKind.SelectOrderBy)
            {
                WrapSqlIntoNestedStatement(b, context);
            }

            b.Append(" ORDER BY ");

            for (int i = 0; i < arguments.Count; i++)
            {
                StringWriter        w      = new StringWriter();
                ExpressionSqlWriter writer = new ExpressionSqlWriter(w, context, arguments[i].Argument.Parameters[0]);
                writer.Write(arguments[i].Argument.Body);

                if (i == 0)
                {
                    b.Append(w.ToString());
                }
                else
                {
                    b.Append(", " + w.ToString());
                }

                if (arguments[i].Descending)
                {
                    b.Append(" DESC");
                }
                else
                {
                    b.Append(" ASC");
                }
            }

            return(SqlStatementKind.SelectOrderBy);
        }
Esempio n. 20
0
		public override SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context)
		{
			Target.BuildSql(b, context);
			
			CallTreeNodeSqlNameSet oldNames = context.CurrentNameSet;
			CallTreeNodeSqlNameSet newNames = new CallTreeNodeSqlNameSet(context);
			
			string query = "SELECT "
				+ SqlAs(oldNames.NameID, newNames.NameID) + ", "
				+ SqlAs("SUM(" + oldNames.CpuCyclesSpent + ")", newNames.CpuCyclesSpent) + ", "
				+ SqlAs("SUM(" + oldNames.CpuCyclesSpentSelf + ")", newNames.CpuCyclesSpentSelf) + ", "
				+ SqlAs("SUM(" + oldNames.CallCount + ")", newNames.CallCount) + ", "
				+ SqlAs("MAX(" + oldNames.HasChildren + ")", newNames.HasChildren) + ", "
				+ SqlAs("SUM(" + oldNames.ActiveCallCount + ")", newNames.ActiveCallCount);
			if (context.HasIDList) {
				query += ", " + SqlAs("GROUP_CONCAT(" + oldNames.ID + ")", newNames.ID);
			}
			query += Environment.NewLine + "FROM (" + Environment.NewLine;
			b.Insert(0, query);
			b.AppendLine(") GROUP BY " + oldNames.NameID);
			context.SetCurrent(newNames, SqlTableType.None, context.HasIDList);
			
			return SqlStatementKind.SelectGroupBy;
		}
Esempio n. 21
0
 /// <summary>
 /// SQL construction documentation see SQLiteQueryProvider documentation.
 /// </summary>
 public abstract SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context);
Esempio n. 22
0
		public IQueryable<CallTreeNode> Execute(SQLiteQueryProvider provider, QueryExecutionOptions options)
		{
			StringBuilder b = new StringBuilder();
			SqlQueryContext context = new SqlQueryContext(provider);
			BuildSql(b, context);
			if (options.HasLoggers)
				options.WriteLogLine(b.ToString());
			Stopwatch w = Stopwatch.StartNew();
			IList<CallTreeNode> result = provider.RunSQLNodeList(b.ToString(), context.HasIDList);
			w.Stop();
			if (options.HasLoggers) {
				options.WriteLogLine("Query returned " + result.Count + " rows in " + w.Elapsed);
			}
			return result.AsQueryable();
		}
Esempio n. 23
0
		static void BuildSqlForCondition(StringBuilder b, SqlQueryContext context, LambdaExpression condition)
		{
			Debug.Assert(condition.Parameters.Count == 1);
			StringWriter w = new StringWriter(CultureInfo.InvariantCulture);
			ExpressionSqlWriter writer = new ExpressionSqlWriter(w, context, condition.Parameters[0]);
			writer.Write(condition.Body);
			b.Append(w.ToString());
		}
Esempio n. 24
0
		/// <summary>
		/// SQL construction documentation see SQLiteQueryProvider documentation.
		/// </summary>
		public abstract SqlStatementKind BuildSql(StringBuilder b, SqlQueryContext context);