InterpreterShowProcesslistQuery.h 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#pragma once

#include <DB/IO/ReadBufferFromString.h>

#include <DB/Interpreters/executeQuery.h>

#include <DB/Parsers/ASTQueryWithOutput.h>
#include <DB/Parsers/ASTIdentifier.h>


namespace DB
{
	
	
/** Вернуть список запросов, исполняющихся прямо сейчас.
  */
class InterpreterShowProcesslistQuery
{
public:
	InterpreterShowProcesslistQuery(ASTPtr query_ptr_, Context & context_)
		: query_ptr(query_ptr_), context(context_) {}

	BlockIO execute()
	{
25
		return executeQuery(getRewrittenQuery(), context, true);
26 27 28 29 30 31 32
	}

	BlockInputStreamPtr executeAndFormat(WriteBuffer & buf)
	{
		String query = getRewrittenQuery();
		ReadBufferFromString in(query);
		BlockInputStreamPtr query_plan;
33
		executeQuery(in, buf, context, query_plan, true);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		return query_plan;
	}

private:
	ASTPtr query_ptr;
	Context context;

	String getRewrittenQuery()
	{
		const ASTQueryWithOutput & query = dynamic_cast<const ASTQueryWithOutput &>(*query_ptr);

		std::stringstream rewritten_query;
		rewritten_query << "SELECT * FROM system.processes";

		if (query.format)
			rewritten_query << " FORMAT " << dynamic_cast<const ASTIdentifier &>(*query.format).name;

		return rewritten_query.str();
	}
};


}