Commit 7180c258 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

simple IMPORT tbl FROM CSV ... statment working :)

parent 451314f4
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ point chart examples:
--- TimeDomain!
--- max interpolation gap -> draw as missing data

IMPORT tablename FROM CSV { 'filename' | STDIN }
IMPORT TABLE tablename FROM CSV { 'filename' | STDIN }
    [ [ WITH ]
          [ DELIMITER [ AS ] 'delimiter' ]
          [ ROW_DELIMITER [ AS ] 'row_delimiter' ]
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ set(FNORDMETRIC_UI_SOURCES
    stage/src/fnordmetric/ui/pointchart.cc)

set(FNORDMETRIC_CSV_BACKEND_SOURCES
    stage/src/fnordmetric/query/backends/csv/csvbackend.cc
    stage/src/fnordmetric/query/backends/csv/csvinputstream.cc
    stage/src/fnordmetric/query/backends/csv/csvtableref.cc)

+3 −3
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ public:
    T_RESOLVED_COLUMN,
    T_TABLE_NAME,
    T_DERIVED_COLUMN,
    T_PROPERTY,

    T_SELECT,
    T_SELECT_LIST,
@@ -56,10 +57,9 @@ public:
    T_MOD_EXPR,
    T_POW_EXPR,

    T_SERIES,
    T_SERIES_NAME,
    T_AXIS,
    T_DRAW
    T_DRAW,
    T_IMPORT
  };

  ASTNode(kASTNodeType type);
+56 −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).
 */
#include <memory>
#include <fnordmetric/query/astnode.h>
#include <fnordmetric/query/backends/csv/csvbackend.h>
#include <fnordmetric/query/backends/csv/csvtableref.h>
#include <fnordmetric/query/token.h>
#include <fnordmetric/util/inputstream.h>
#include <fnordmetric/util/runtimeexception.h>

namespace fnordmetric {
namespace query {
namespace csv_backend {

std::unique_ptr<TableRef> CSVBackend::openTable(ASTNode* import) {
  char column_sep = ',';
  char row_sep = '\n';
  char quote_char = '"';
  char escape_char = '\\';
  bool headers = false;
  auto filename = import->getChildren()[1]->getToken()->getString();

  for (int i = 2; i < import->getChildren().size(); ++i) {
    const auto& child = import->getChildren()[i];

    switch (child->getToken()->getType()) {
      case Token::T_HEADER:
        headers = true;
        break;

      default:
        RAISE(
            util::RuntimeException,
            "invalid option for CSV table: %s",
            Token::getTypeName(child->getToken()->getType()));
    }
  }

  auto csv = CSVInputStream::openFile(
      filename,
      column_sep,
      row_sep,
      quote_char);

  return std::unique_ptr<TableRef>(
      new CSVTableRef(std::move(csv), headers));
}


}
}
}
+27 −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_CSVBACKEND_H
#define _FNORDMETRIC_CSVBACKEND_H
#include <memory>
#include <fnordmetric/query/backends/csv/csvtableref.h>

namespace fnordmetric {
namespace query {
class ASTNode;
namespace csv_backend {

class CSVBackend {
public:

  static std::unique_ptr<TableRef> openTable(ASTNode* import);

};

}
}
}
#endif
Loading