Skip to content

Add stdin support? #169

@imathews

Description

@imathews

It would be quite helpful if the execute_query binary supported reading queries from stdin – especially for very long queries where we might hit system argv limits. Something like echo "SELECT 1 + 1" | execute_query -.

This would only require a minor change to execute_query.cc to read from stdin. Would you accept a PR? The relevant code changes are also below, which I've tested and validated on my own fork:

// Added to includes

#include <sstream>

// Updated RunTool fn:

absl::Status RunTool(const std::vector<std::string>& args) {
  ExecuteQueryConfig config;

  GOOGLESQL_RETURN_IF_ERROR(InitializeExecuteQueryConfig(config));

  GOOGLESQL_ASSIGN_OR_RETURN(std::unique_ptr<ExecuteQueryWriter> writer,
                   MakeWriterFromFlags(config, std::cout));

  if (absl::GetFlag(FLAGS_web)) {
    return RunExecuteQueryWebServer(absl::GetFlag(FLAGS_port));
  }

  std::string sql;
  if (args.size() == 1 && args[0] == "-") {
    // Read SQL from stdin
    std::ostringstream ss;
    ss << std::cin.rdbuf();
    sql = ss.str();
  } else {
    sql = absl::StrJoin(args, " ");
  }
  return ExecuteQuery(sql, config, *writer);
}

// Updated usage string:

const char kUsage[] =
    "Usage: execute_query "
    "{ \"<sql>\" | - | {--web [--port=<port>] } }\n";

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions