Commit 6320e9f1 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

queryservice, outputstream stubs

parent b4e85247
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@
all:
	mkdir -p target/tests
	mkdir -p stage/src
	ln -s ../../../../src stage/src/fnordmetric || true
	test -e stage/src/fnordmetric || ln -s ../../../../src stage/src/fnordmetric || true
	(cd target && cmake .. && make)

test: all
+35 −5
Original line number Diff line number Diff line
@@ -8,16 +8,18 @@
#include <stdio.h>
#include <string.h>
#include <fnordmetric/util/unittest.h>
#include <fnordmetric/util/outputstream.h>
#include <fnordmetric/query/backends/csv/csvtableref.h>
#include <fnordmetric/query/query.h>
#include <fnordmetric/query/parser.h>
#include <fnordmetric/query/token.h>
#include <fnordmetric/query/tokenize.h>
#include <fnordmetric/query/executable.h>
#include <fnordmetric/query/parser.h>
#include <fnordmetric/query/query.h>
#include <fnordmetric/query/queryservice.h>
#include <fnordmetric/query/resultlist.h>
#include <fnordmetric/query/tableref.h>
#include <fnordmetric/query/tablescan.h>
#include <fnordmetric/query/tablerepository.h>
#include <fnordmetric/query/resultlist.h>
#include <fnordmetric/query/token.h>
#include <fnordmetric/query/tokenize.h>

using namespace fnordmetric::query;

@@ -958,3 +960,31 @@ TEST_CASE(QueryTest, SimpleEndToEndTest, [] () {
});


TEST_CASE(QueryTest, TestQueryService, [] () {
  auto query =
      "  IMPORT TABLE gbp_per_country "
      "     FROM CSV 'test/fixtures/gbp_per_country_simple.csv' HEADER;"
      ""
      "  DRAW BAR CHART;"
      "  DRAW BOTTOM AXIS;"
      "  DRAW LEFT AXIS;"
      ""
      "  SELECT"
      "    'gross domestic product per country' as series,"
      "    country as x,"
      "    gbp as y"
      "  FROM"
      "    gbp_per_country"
      "  LIMIT 30;";

  QueryService query_service;
  auto input = fnordmetric::util::StringInputStream::fromString(query);
  auto output = fnordmetric::util::OutputStream::getStdout();

  query_service.executeQuery(
      input.get(),
      QueryService::FORMAT_CSV,
      output.get());
});

+53 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * Licensed under the MIT license (see LICENSE).
 */
#ifndef _FNORDMETRIC_QUERYSERVICE_H
#define _FNORDMETRIC_QUERYSERVICE_H
#include <fnordmetric/query/query.h>

namespace fnordmetric {
namespace util {
  class InputStream;
  class OutputStream;
}

namespace query {

/**
 * The query service is the default entry point for executing all queries. A
 * QueryService instance is thread safe.
 */
class QueryService {
public:
  enum kFormat {
    FORMAT_CSV
  };

  /**
   * Create a new query service
   */
  QueryService();

  /**
   * Execute a query. This may raise an exception.
   */
  void executeQuery(
      util::InputStream* query,
      kFormat output_format,
      util::OutputStream* output_stream);

  /**
   * Register a query backend
   */
  void registerBackend();

protected:

};

}
}
#endif
+6 −3
Original line number Diff line number Diff line
@@ -7,8 +7,8 @@
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include "inputstream.h"
#include "runtimeexception.h"
#include <fnordmetric/util/inputstream.h>
#include <fnordmetric/util/runtimeexception.h>

namespace fnordmetric {
namespace util {
@@ -27,7 +27,10 @@ std::unique_ptr<FileInputStream> FileInputStream::openFile(
  return std::unique_ptr<FileInputStream>(csv_file);
}

FileInputStream::FileInputStream(int fd) : fd_(fd) {}
FileInputStream::FileInputStream(
    int fd,
    bool close_on_destroy /* = false */) :
    fd_(fd) {}

bool FileInputStream::readNextByte(char* target) {
  if (buf_pos_ >= buf_len_) {
+39 −5
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ public:

  /**
   * Open a new file input stream from the provided file path. Throws an
   * exception if the file cannot be opened.
   * exception if the file cannot be opened. The file will be automatically
   * closed on destroy.
   *
   * @param file_path the path to the file
   */
@@ -45,12 +46,14 @@ public:
      const std::string& file_path);

  /**
   * Create a new FileInputStream instance from the provided filedescriptor. The
   * input stream takes ownership of the fd and will close() it when destructed.
   * Create a new FileInputStream instance from the provided filedescriptor. If
   * close on_destroy is true, the fd will be close()ed when the input stream
   * is destroyed.
   *
   * @param fd a valid an opened fd, transfer ownership and close on destruct
   * @param fd a valid fd
   * @param close_on_destroy close the fd on destroy?
   */
  explicit FileInputStream(int fd);
  explicit FileInputStream(int fdd, bool close_on_destroy = false);

  /**
   * Read the next byte from the file. Returns true if the next byte was read
@@ -75,6 +78,37 @@ protected:
  int fd_;
};

class StringInputStream : public InputStream {
public:

  /**
   * Create a new InputStream from the provided string
   *
   * @param string the input string
   */
  static std::unique_ptr<StringInputStream> fromString(
      const std::string& string);

  /**
   * Create a new InputStream from the provided string
   *
   * @param string the input string
   */
  StringInputStream(const std::string& string);

  /**
   * Read the next byte from the file. Returns true if the next byte was read
   * and false if the end of the stream was reached.
   *
   * @param target the target char pointer
   */
  bool readNextByte(char* target) override;

protected:
  const std::string& str_;
  size_t cur_;
};

}
}
#endif
Loading