| 267 | } |
| 268 | |
| 269 | void PostgreSQLHandler::processQuery() |
| 270 | { |
| 271 | try |
| 272 | { |
| 273 | std::unique_ptr<PostgreSQLProtocol::Messaging::Query> query = |
| 274 | message_transport->receive<PostgreSQLProtocol::Messaging::Query>(); |
| 275 | |
| 276 | if (isEmptyQuery(query->query)) |
| 277 | { |
| 278 | message_transport->send(PostgreSQLProtocol::Messaging::EmptyQueryResponse()); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | bool psycopg2_cond = query->query == "BEGIN" || query->query == "COMMIT"; // psycopg2 starts and ends queries with BEGIN/COMMIT commands |
| 283 | bool jdbc_cond = query->query.find("SET extra_float_digits") != String::npos || query->query.find("SET application_name") != String::npos; // jdbc starts with setting this parameter |
| 284 | if (psycopg2_cond || jdbc_cond) |
| 285 | { |
| 286 | message_transport->send( |
| 287 | PostgreSQLProtocol::Messaging::CommandComplete( |
| 288 | PostgreSQLProtocol::Messaging::CommandComplete::classifyQuery(query->query), 0)); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | const auto & settings = connection_context->getSettingsRef(); |
| 293 | std::vector<String> queries; |
| 294 | auto parse_res = splitMultipartQuery( |
| 295 | query->query, queries, settings.max_query_size, settings.max_parser_depth, ParserSettings::valueOf(settings)); |
| 296 | if (!parse_res.second) |
| 297 | throw Exception("Cannot parse and execute the following part of query: " + String(parse_res.first), ErrorCodes::SYNTAX_ERROR); |
| 298 | |
| 299 | for (const auto & spl_query : queries) |
| 300 | { |
| 301 | /// FIXME why do we execute all queries in a single connection context? |
| 302 | CurrentThread::QueryScope query_scope{connection_context}; |
| 303 | ReadBufferFromString read_buf(spl_query); |
| 304 | executeQuery(read_buf, *out, false, connection_context, {}); |
| 305 | |
| 306 | PostgreSQLProtocol::Messaging::CommandComplete::Command command = |
| 307 | PostgreSQLProtocol::Messaging::CommandComplete::classifyQuery(spl_query); |
| 308 | message_transport->send(PostgreSQLProtocol::Messaging::CommandComplete(command, 0), true); |
| 309 | } |
| 310 | |
| 311 | } |
| 312 | catch (const Exception & e) |
| 313 | { |
| 314 | message_transport->send( |
| 315 | PostgreSQLProtocol::Messaging::ErrorOrNoticeResponse( |
| 316 | PostgreSQLProtocol::Messaging::ErrorOrNoticeResponse::ERROR, "2F000", "Query execution failed.\n" + e.displayText()), |
| 317 | true); |
| 318 | throw; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | bool PostgreSQLHandler::isEmptyQuery(const String & query) |
| 323 | { |
nothing calls this directly
no test coverage detected