ROSE  0.11.54.0
Classes | Public Types | Public Member Functions | List of all members
SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 > Class Template Reference

Description

template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
class SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >

In-memory representation of a database table.

These can be used to hold results of a database table, to build a table in memory, or (mostly) for printing a table in an easy-to-read format. For instance, here's a very simple, no-frills way to print the results from a query:

SqlDatabase::StatementPtr stmt = tx->statement("select id, ninsns, name, addr from table where ninsns = ?");

Tables can also be used to operate on the results of a query. For instance, to calculate the average number of instructions and bytes per function, one could use a counting-loop-over-array paradigm, which is more comfortable to some programmers than iterators:

typedef SqlDatabase::Table<size_t, size_t> InsnBytesTable;
InsnBytesTable table(tx->statement("select ninsns, bytes from functions"));
size_t ninsns_sum=0, nbytes_sum=0;
for (size_t i=0; i<table.size(); ++i) {
ninsns_sum += table[i].v0; // value from column #0
nbytes_sum += table[i].v1; // value from column #1
}
std::cout <<"average number of instructions: " <<(double)ninsns_sum/table.size() <<"\n"
<<"average size in bytes: " <<(double)nbytes_sum/table.size() <<"\n";

Tables can also accumulate the results from more than one query as long as those queries return the same number of columns and column types:

MyTable.insert(tx->query("select id, ninsns, name from functions where size < ?")->bind(0, 100));
MyTable.insert(tx->query("select hashval, size, desc from failures"));

If you want headers at the top of a printed table, give the columns names like this:

ResultsTable table(stmt->bind(0, ninsns));
table.headers("Id", "NInsns", "Name", "Address");

It is also possible to specify a rendering for certain columns. For instance, the query above returns an address of type rose_addr_t for column #3 (column numbering is always zero-origin). If we want the address to be formatted as a zero-padded, eight character hexadecimal number with a "0x" prefix, then we could do that with code like this (building from the previous example):

struct AddrRenderer: SqlDatabase::Renderer<rose_addr_t> {
std::string operator()(const rose_addr_t &value, size_t width) const override {
return StringUtility::addrToString(value);
}
} addressRenderer;
table.renderers().r3 = &addrRenderer;

If we want the headers to be printed again every 50th row, we can do that with a PrePostRow functor. These functors have row info available to them so they can do fancier things too, like printing separators between major portions of a table.

struct PeriodicSeparator: SqlDatabase::PrePostRow<ResultsTable> {
void operator()(std::ostream &out, const ResultsTable *table, size_t rownum, const std::vector<size_t> &widths) {
if (0!=rownum && 0==rownum % 50)
table->print_rowsep(out, widths);
}
} periodicSeparator;
table.prepost(&periodicSeparator, NULL);

Definition at line 716 of file SqlDatabase.h.

#include <SqlDatabase.h>

Classes

struct  Renderers
 Functors for rendering column values. More...
 
struct  Tuple
 One row of a table. More...
 

Public Types

enum  { MAXCOLS =16 }
 
typedef std::vector< TupleRows
 All rows of a table. More...
 

Public Member Functions

 Table ()
 Creates a new, empty table. More...
 
 Table (const StatementPtr &stmt)
 Creates a new table and initializes it by running the specified database query. More...
 
size_t size () const
 Return the number of rows in the table. More...
 
bool empty () const
 Returns true if table has no rows. More...
 
void clear ()
 Clear the table by removing all rows. More...
 
void insert (const StatementPtr &stmt)
 Insert more rows into a table by running the specified query. More...
 
template<typename T >
std::string render (const Renderer< T > *r, const T &value, size_t width=0) const
 Render a single value.
 
std::vector< size_t > colsizes () const
 Compute column widths. More...
 
void print_headers (std::ostream &out, const std::vector< size_t > &widths) const
 Print the headers for a table. More...
 
void print_rowsep (std::ostream &out, const std::vector< size_t > &widths) const
 Print the row separator that goes between the headers and the data. More...
 
void print_row (std::ostream &out, const Tuple &t, const std::vector< size_t > &widths) const
 Print one row of a table. More...
 
void print (std::ostream &out) const
 Print all rows of the table to the specified stream. More...
 
void header (int colnum, const std::string &hdr)
 Headers to use when printing the table.
 
const std::string & header (int colnum) const
 Headers to use when printing the table.
 
void headers (const std::string &h0, const std::string &h1="", const std::string &h2="", const std::string &h3="", const std::string &h4="", const std::string &h5="", const std::string &h6="", const std::string &h7="", const std::string &h8="", const std::string &h9="", const std::string &h10="", const std::string &h11="", const std::string &h12="", const std::string &h13="", const std::string &h14="", const std::string &h15="")
 Headers to use when printing the table.
 
void reprint_headers (size_t nrows)
 How often should headers be reprinted?
 
size_t reprint_headers () const
 How often should headers be reprinted?
 
Renderersrenderers ()
 Accessor for value renderers. More...
 
const Renderersrenderers () const
 Accessor for value renderers. More...
 
void prepost (PrePostRow< Table > *pre, PrePostRow< Table > *post)
 Accessor for functors called before and after each row is printed.
 
std::pair< PrePostRow< Table > *, PrePostRow< Table > * > prepost () const
 Accessor for functors called before and after each row is printed.
 
Tupleoperator[] (size_t i)
 Return a reference to a particular row.
 
const Tupleoperator[] (size_t i) const
 Return a reference to a particular row.
 
void insert (const Tuple &tuple)
 Add a new row to the end of the table.
 
void insert (const T00 &v00, const T01 &v01=T01(), const T02 &v02=T02(), const T03 &v03=T03(), const T04 &v04=T04(), const T05 &v05=T05(), const T06 &v06=T06(), const T07 &v07=T07(), const T08 &v08=T08(), const T09 &v09=T09(), const T10 &v10=T10(), const T11 &v11=T11(), const T12 &v12=T12(), const T13 &v13=T13(), const T14 &v14=T14(), const T15 &v15=T15())
 Add a new row to the end of the table.
 
void line_prefix (const std::string &s)
 String to print at the beginning of every line of output.
 
const std::string & line_prefix () const
 String to print at the beginning of every line of output.
 

Member Typedef Documentation

template<typename T00 , typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
typedef std::vector<Tuple> SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::Rows

All rows of a table.

Definition at line 753 of file SqlDatabase.h.

Member Enumeration Documentation

template<typename T00 , typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
anonymous enum
Enumerator
MAXCOLS 

Maximum number of columns allowed in a table.

Definition at line 718 of file SqlDatabase.h.

Constructor & Destructor Documentation

template<typename T00 , typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::Table ( )
inline

Creates a new, empty table.

Definition at line 756 of file SqlDatabase.h.

template<typename T00 , typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::Table ( const StatementPtr stmt)
inlineexplicit

Creates a new table and initializes it by running the specified database query.

Definition at line 759 of file SqlDatabase.h.

References SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::insert().

Member Function Documentation

template<typename T00 , typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
Renderers& SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::renderers ( )
inline

Accessor for value renderers.

Use these to change how values are displayed. For instance, if a column has values that are addresses and you want to display them with StringUtility::addrToString(), then use code like this:

struct AddrRender: SqlDatabase::Renderer<rose_addr_t> {
std::string operator()(const rose_addr_t &value, size_t width) const override {
return StringUtility::addrToString(value);
}
} addrRender;
table.renderers().r2 = &addrRenderer;

The render pointer should not be deleted before the table is done being used.

Definition at line 807 of file SqlDatabase.h.

template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
const Renderers& SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::renderers ( ) const
inline

Accessor for value renderers.

Use these to change how values are displayed. For instance, if a column has values that are addresses and you want to display them with StringUtility::addrToString(), then use code like this:

struct AddrRender: SqlDatabase::Renderer<rose_addr_t> {
std::string operator()(const rose_addr_t &value, size_t width) const override {
return StringUtility::addrToString(value);
}
} addrRender;
table.renderers().r2 = &addrRenderer;

The render pointer should not be deleted before the table is done being used.

Definition at line 808 of file SqlDatabase.h.

template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
size_t SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::size ( void  ) const
inline

Return the number of rows in the table.

Definition at line 823 of file SqlDatabase.h.

Referenced by SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::print().

template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
bool SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::empty ( ) const
inline
template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
void SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::clear ( )
inline

Clear the table by removing all rows.

Definition at line 829 of file SqlDatabase.h.

template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
void SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::insert ( const StatementPtr stmt)
inline

Insert more rows into a table by running the specified query.

The table is not cleared first.

Definition at line 850 of file SqlDatabase.h.

template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
std::vector<size_t> SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::colsizes ( ) const
inline

Compute column widths.

Column widths are computed by internally printing the rows of the table and measuring the maximum width for each column. The renderers are called with zero for the column widths.

Definition at line 879 of file SqlDatabase.h.

References SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::MAXCOLS, and SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::render().

Referenced by SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::print().

template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
void SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::print_headers ( std::ostream &  out,
const std::vector< size_t > &  widths 
) const
inline
template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
void SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::print_rowsep ( std::ostream &  out,
const std::vector< size_t > &  widths 
) const
inline

Print the row separator that goes between the headers and the data.

Definition at line 924 of file SqlDatabase.h.

Referenced by SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::print().

template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
void SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::print_row ( std::ostream &  out,
const Tuple t,
const std::vector< size_t > &  widths 
) const
inline
template<typename T00, typename T01 = NoColumn, typename T02 = NoColumn, typename T03 = NoColumn, typename T04 = NoColumn, typename T05 = NoColumn, typename T06 = NoColumn, typename T07 = NoColumn, typename T08 = NoColumn, typename T09 = NoColumn, typename T10 = NoColumn, typename T11 = NoColumn, typename T12 = NoColumn, typename T13 = NoColumn, typename T14 = NoColumn, typename T15 = NoColumn>
void SqlDatabase::Table< T00, T01, T02, T03, T04, T05, T06, T07, T08, T09, T10, T11, T12, T13, T14, T15 >::print ( std::ostream &  out) const
inline

The documentation for this class was generated from the following file: