Table

Overview

The table is the kind of output returned from all MQL statements which return something, except for "SELECT (ALL|FOCUS) OBJECTS", which returns a sheaf.

A table is a list of TableRows. A TableRow, in turn, is a list of strings.

The columns are numbered beginning with 1, not 0.

Java-style iterators are provided for accessing the rows and their contents.

C++ interface


#include <table.h>

typedef enum { 
  kTCString, 
  kTCInteger, 
  kTCID_D, 
  kTCMonad_m, 
  kTCBool, 
  kTCEnum 
} TableColumnType;

class TableRowIterator {
public:
  TableRowIterator(TableRow *pMotherTableRow);
  TableRowIterator();
  ~TableRowIterator();
  bool hasNext() const; // Is the iterator == end iterator?  Doesn't alter iterator
  std::string next(); // Gets current and advances iterator afterwards
  std::string previous(); // Regresses iterator and then gets current
  std::string current(); // Gets current without altering iterator
  std::string getColumn(int column); // Gets column in current without touching iterator. First column is 1.
};

class TableRow : public std::list<std::string> {
public:
  TableRow();
  ~TableRow();
  TableRowIterator iterator();  // Gets iterator pointing to beginning
  std::string getColumn(int column); // Gets column.  First column is 1.
  unsigned int size() const;  // Gets number of columns
};

class TableIterator {
public:
  TableIterator();
  TableIterator(Table *pMotherTable);
  TableIterator(const TableIterator& other);
  ~TableIterator();
  bool hasNext() const; // Is the iterator == end iterator?  Doesn't alter iterator
  TableRow& next(); // Gets current and advances iterator afterwards
  TableRow& previous();  // Regresses iterator and then gets current
  TableRow& current(); // Gets current without altering iterator
  std::string next(int column);  // Gets column in current and then advances iterator
  std::string getColumn(int column); // Gets column in current without altering iterator.  First column is 1.
};


class Table {
public:
  Table();
  ~Table();
  // Reading from a table
  TableIterator iterator();

  // Find row with string str at column column_index. First column is 1.
  TableIterator find(const std::string& str, int column_index, 
                    bool bCompareCaseInsensitively = true);

  // Find row with str1 and str2 at their respective columns. First column is 1.
  TableIterator find(const std::string& str1, int column_index1, 
                     const std::string& str2, int column_index2, 
                     bool bCompareCaseInsensitively = true);
  void erase(TableIterator& d);
  void clear();

  // First column is 1.
  std::string getColumn(TableIterator& i, int column_index);
  long size() const; // Get number of rows

  // Adding to a table
  void append(const std::string& str);
  void newline();
  void appendHeader(std::string header_name, 
                    TableColumnType tc_type, 
                    std::string enum_type = "");

  // First column is 1.
  void updateRow(TableIterator& i, const std::string new_value, int column_index);

  // Should be called before append.
  void startNewRow();

  // Printing a table
  void printConsole(EMdFOutput *pOut) const;
  void printXML(EMdFOutput *pOut) const;
  static void printDTD(EMdFOutput *pOut);
};


Examples

Iterators (reading from a table)

Here is an example of how to use the iterators.


  Table *pTable; // Assumed to be initialized from somewhere

  // Get first string of first row
  TableIterator ti = pTable->iterator();
  std::string firstRowCol = pTable->getColumn(ti, 1);  // Note how column 1 is the first.

  // out table
  ti = pTable->iterator();
  while (ti.hasNext()) {
    // Get iterator for current row
    TableRowIterator tri = ti.current().iterator();

    // out row
    while (tri.hasNext()) {
      // out string plus horizontal tab, and advance iterator
      printf("%s\t", tri.next());
    }

    // out newline
    printf("\n");

    // Advance iterator
    ti.next();
  }

  // Find something in table
  int column_to_search = 2;
  TableIterator ti2 = pTable->find("StringToFind", column_to_search);
  if (ti2.hasNext()) {
    printf("String found!\n");
    TableRow& tr = ti.current();
    printf("Column 1 = '%s'\n", tr.getColumn(1));
  } else {
    printf("String not found.\n");
  }

Adding to a table

Here's an example of how to add to a table:


  // Create header (not really necessary if you know what you've
  // placed into the table; mostly for aesthetics when printing
  // and for communicating with other parts of the program)
  pTable->appendHeader("first_monad", kTCMonad_m);
  pTable->appendHeader("last_monad", kTCMonad_m);
  
  // Start a new row (call this method before starting a new row)
  pTable->startNewRow();

  // Add row 1
  pTable->append(1); // Monad 1 in first_monad
  pTable->append(4); // Monad 4 in last_monad


  // Add row 2
  pTable->startNewRow();
  pTable->append(10); // Monad 10 in first_monad
  pTable->append(13); // Monad 13 in last_monad


Previous:Statement
Up:Part II: APIs
Next:Sheaf