gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
Gambit::Printers::SQLiteReader Class Reference

#include <sqlitereader.hpp>

Inheritance diagram for Gambit::Printers::SQLiteReader:
Collaboration diagram for Gambit::Printers::SQLiteReader:

Public Member Functions

 SQLiteReader (const Options &options)
 
 ~SQLiteReader ()
 
 BOOST_PP_SEQ_FOR_EACH_I (DECLARE_RETRIEVE,, SQL_TYPES) BOOST_PP_SEQ_FOR_EACH_I(DECLARE_RETRIEVE
 
virtual void reset ()
 Base class virtual interface functions. More...
 
virtual ulong get_dataset_length ()
 Get length of input dataset. More...
 
virtual PPIDpair get_next_point ()
 Get next rank/ptID pair in data file. More...
 
virtual PPIDpair get_current_point ()
 Get current rank/ptID pair in data file. More...
 
virtual ulong get_current_index ()
 
virtual bool eoi ()
 Check if 'current point' is past the end of the datasets (and thus invalid!) More...
 
virtual std::size_t get_type (const std::string &label)
 Get type information for a data entry, i.e. More...
 
virtual std::set< std::string > get_all_labels ()
 Get labels of all datasets in the linked group. More...
 
- Public Member Functions inherited from Gambit::Printers::BaseReader
 BaseReader ()
 
virtual ~BaseReader ()
 Destructor. More...
 
template<typename T >
bool retrieve (T &out, const std::string &label)
 Reimplement overload for 'retrieve' that uses the current point as the input for rank/pointID. More...
 
template<typename T >
bool retrieve (T &out, const std::string &label, const uint rank, const ulong pointID)
 
bool retrieve_and_print (const std::string &in_label, const std::string &out_label, BaseBasePrinter &printer, const uint rank, const ulong pointID)
 Retrieve and directly print data to new output. More...
 
- Public Member Functions inherited from Gambit::Printers::BaseBaseReader
virtual ~BaseBaseReader ()
 
template<typename T >
bool retrieve (T &out, const std::string &label, const uint rank, const ulong pointID)
 Printer-retrieve dispatch function. More...
 
template<typename T >
bool retrieve (T &out, const std::string &label)
 Overload for 'retrieve' that uses the current point as the input for rank/pointID. More...
 
bool retrieve_and_print (const std::string &label, BaseBasePrinter &printer, const uint rank, const ulong pointID)
 Retrieve and directly print data to new output. More...
 
bool retrieve_and_print (const std::string &label, BaseBasePrinter &printer)
 Overload for 'retrieve_and_print' that uses the current point as the input for rank/pointID. More...
 
bool retrieve_and_print (const std::string &in_label, const std::string &out_label, BaseBasePrinter &printer)
 As above, but allows for different input/output labels. More...
 
- Public Member Functions inherited from Gambit::Printers::SQLiteBase
 SQLiteBase ()
 
 ~SQLiteBase ()
 

Private Member Functions

void build_column_map ()
 
template<typename T >
get_sql_col (const std::string &col_name)
 
template<class T >
bool _retrieve_template (T &out, const std::string &label, int, const uint rank, const ulong pointID)
 "Master" templated retrieve function. More...
 
template<>
long long int get_sql_col (const std::string &)
 
template<>
double get_sql_col (const std::string &)
 
void move_to_next_point ()
 Move the SQL loop ahead one. More...
 
std::size_t get_col_i (const std::string &col_name)
 Safely access the column_map and throw informative error when column is missing. More...
 
template<>
long long int get_sql_col (const std::string &col_name)
 Template function specialisations for retrieving SQLite column data as various types. More...
 
template<>
double get_sql_col (const std::string &col_name)
 
template<>
std::string get_sql_col (const std::string &col_name)
 

Private Attributes

bool eoi_flag
 
sqlite3_stmt * stmt
 
ulong current_dataset_index
 
PPIDpair current_point
 
std::map< std::string, std::size_t, Utils::ci_lesscolumn_map
 
std::map< std::string, std::string, Utils::ci_lesscolumn_types
 

Additional Inherited Members

- Protected Member Functions inherited from Gambit::Printers::BaseBaseReader
template<typename T >
bool _retrieve (T &, const std::string &label, const uint, const ulong)
 Default _retrieve function. More...
 
- Protected Member Functions inherited from Gambit::Printers::SQLiteBase
std::string get_database_file ()
 
std::string get_table_name ()
 
void set_table_name (const std::string &table_name)
 
void require_output_ready ()
 
void open_db (const std::string &, char access='r')
 
void close_db ()
 
sqlite3 * get_db ()
 
void cout_row (sqlite3_stmt *tmp_stmt)
 
void check_table_exists ()
 
void set_table_exists ()
 
std::map< std::string, std::string, Utils::ci_lessget_column_info ()
 
int submit_sql (const std::string &local_info, const std::string &sqlstr, bool allow_fail=false, sql_callback_fptr callback=NULL, void *data=NULL, char **zErrMsg=NULL)
 

Detailed Description

Definition at line 36 of file sqlitereader.hpp.

Constructor & Destructor Documentation

◆ SQLiteReader()

Gambit::Printers::SQLiteReader::SQLiteReader ( const Options options)

Definition at line 32 of file sqlitereader.cpp.

References build_column_map(), Gambit::Printers::SQLiteBase::check_table_exists(), column_types, Gambit::Printers::SQLiteBase::get_column_info(), Gambit::Options::getValue(), Gambit::Printers::SQLiteBase::open_db(), reset(), and Gambit::Printers::SQLiteBase::set_table_name().

33  : BaseReader()
34  , SQLiteBase()
35  , stmt(NULL)
38  {
39  // Open the database file
40  open_db(options.getValue<std::string>("file"),'r');
41 
42  // Tell the base class what table we want to access
43  set_table_name(options.getValue<std::string>("table"));
44 
45  // Make sure that the requested table exists
47 
48  // Determine the types of all columns in the input table
51 
52  // Set up the reader loop
53  reset();
54  }
void set_table_name(const std::string &table_name)
Definition: sqlitebase.cpp:324
std::map< std::string, std::string, Utils::ci_less > get_column_info()
Definition: sqlitebase.cpp:300
virtual void reset()
Base class virtual interface functions.
void open_db(const std::string &, char access='r')
Definition: sqlitebase.cpp:164
EXPORT_SYMBOLS const PPIDpair nullpoint
Define &#39;nullpoint&#39; const.
std::map< std::string, std::string, Utils::ci_less > column_types
Here is the call graph for this function:

◆ ~SQLiteReader()

Gambit::Printers::SQLiteReader::~SQLiteReader ( )

Definition at line 56 of file sqlitereader.cpp.

References eoi(), and stmt.

57  {
58  if(not eoi() and stmt!=NULL)
59  {
60  // Finalise any existing loop
61  sqlite3_finalize(stmt);
62  }
63  }
virtual bool eoi()
Check if &#39;current point&#39; is past the end of the datasets (and thus invalid!)
Here is the call graph for this function:

Member Function Documentation

◆ _retrieve_template()

template<class T >
bool Gambit::Printers::SQLiteReader::_retrieve_template ( T &  out,
const std::string &  label,
int  ,
const uint  rank,
const ulong  pointID 
)
inlineprivate

"Master" templated retrieve function.

All other retrieve functions should ultimately call this one

Definition at line 98 of file sqlitereader.hpp.

References eoi(), Gambit::LogTags::err, get_col_i(), LOAD_READER, LOCAL_INFO, Gambit::Printers::PPIDpair::pointID, Gambit::Printers::printer_error(), Gambit::Printers::PPIDpair::rank, Gambit::Printers::SQLite_equaltypes(), Gambit::Printers::SQLtype_to_basic, and Gambit::Printers::typecode2sql.

99  {
100  bool valid(false);
101  // The assumption made here is that we are iterating through the database, rather
102  // than attempting to access various random table entries constantly. So this reader object
103  // steps through the database. This means that most of the time, our sql cursor will already
104  // be pointing at the correct rank/pointID pair.
105  // In fact, for now at least, I think I will make it an ERROR if the correct rank/pointID is not
106  // at this cursor location. TODO: Consider whether more general access will be useful in the future.
107  if(not eoi())
108  {
109  if(current_point != PPIDpair(pointID,rank))
110  {
111  std::stringstream err;
112  err<<"Attempted to retrieve '"<<label<<"' from point ("<<rank<<", "<<pointID<<"), however the SQLiteReader object is not presently accessing this point (the 'current_point' is ("<<current_point.rank<<", "<<current_point.pointID<<")). At present this object is only really designed for use by the postprocessor scanner, if you have created another scanner that requires more general reader access then please make a feature request.";
113  printer_error().raise(LOCAL_INFO, err.str());
114  }
115  // Otherwise we are good to go!
116 
117  // First need to check if the result is 'null' for this entry
118  int typecode = sqlite3_column_type(stmt, get_col_i(label));
119  auto it = typecode2sql.find(typecode);
120  if( it == typecode2sql.end() )
121  {
122  std::stringstream err;
123  err<<"Received unrecognised type code from sqlite_column_type! (typecode="<<typecode<<")";
124  printer_error().raise(LOCAL_INFO, err.str());
125  }
126  else if( it->second=="NULL" )
127  {
128  // No data in this column.
129  valid = false;
130  }
131  else if(not SQLite_equaltypes(cpp2sql<T>(),it->second) )
132  {
133  // Seems to be data in the column, but type is not correct!
134  // Retrieve could still work since SQLite can do conversions,
135  // but this probably indicates a bug somewhere in this printer.
136  std::stringstream err;
137  err<<"Attempted to retrieve data from table column '"<<label<<"' as type "<<cpp2sql<T>()<<", but SQLite says that the data has type '"<<it->second<<"'. These must map to the same basic column affinity (these were "<<SQLtype_to_basic.at(cpp2sql<T>())<<" and "<<SQLtype_to_basic.at(it->second)<<" respectively)! This is probably a bug in the SQLiteReader 'retrieve' routines, please report it.";
138  printer_error().raise(LOCAL_INFO, err.str());
139  }
140  else
141  {
142  // Do the retrieval!
143  out = get_sql_col<T>(label);
144  valid = true;
145  }
146  }
147  return valid;
148  }
bool SQLite_equaltypes(const std::string &type1, const std::string &type2)
Definition: sqlitebase.cpp:125
unsigned long long int pointID
EXPORT_SYMBOLS error & printer_error()
Printer errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
virtual bool eoi()
Check if &#39;current point&#39; is past the end of the datasets (and thus invalid!)
const std::map< std::string, std::string, Utils::ci_less > SQLtype_to_basic
std::size_t get_col_i(const std::string &col_name)
Safely access the column_map and throw informative error when column is missing.
const std::map< unsigned int, std::string > typecode2sql
Here is the call graph for this function:

◆ BOOST_PP_SEQ_FOR_EACH_I()

Gambit::Printers::SQLiteReader::BOOST_PP_SEQ_FOR_EACH_I ( DECLARE_RETRIEVE  ,
SQL_TYPES   
)

◆ build_column_map()

void Gambit::Printers::SQLiteReader::build_column_map ( )
private

Definition at line 93 of file sqlitereader.cpp.

References column_map, column_types, and Gambit::Printers::SQLiteBase::require_output_ready().

Referenced by SQLiteReader().

94  {
95  // We will SELECT the columns according to the results of get_column_info,
96  // so there is no need to directly inspect the table again.
98  column_map.clear();
99  std::size_t i=0;
100  for(auto it=column_types.begin(); it!=column_types.end(); ++it)
101  {
102  std::string col_name = it->first;
103  column_map[col_name] = i;
104  i++;
105  }
106  }
std::map< std::string, std::size_t, Utils::ci_less > column_map
std::map< std::string, std::string, Utils::ci_less > column_types
Here is the call graph for this function:
Here is the caller graph for this function:

◆ eoi()

bool Gambit::Printers::SQLiteReader::eoi ( )
virtual

Check if 'current point' is past the end of the datasets (and thus invalid!)

Implements Gambit::Printers::BaseBaseReader.

Definition at line 268 of file sqlitereader.cpp.

References eoi_flag.

Referenced by _retrieve_template(), get_current_point(), move_to_next_point(), reset(), and ~SQLiteReader().

269  {
270  return eoi_flag;
271  }
Here is the caller graph for this function:

◆ get_all_labels()

std::set< std::string > Gambit::Printers::SQLiteReader::get_all_labels ( )
virtual

Get labels of all datasets in the linked group.

Implements Gambit::Printers::BaseBaseReader.

Definition at line 332 of file sqlitereader.cpp.

References column_map.

333  {
334  std::set<std::string> out;
335  for (auto it = column_map.begin(); it != column_map.end(); ++it)
336  {
337  out.insert(it->first);
338  }
339  return out;
340  }
std::map< std::string, std::size_t, Utils::ci_less > column_map

◆ get_col_i()

std::size_t Gambit::Printers::SQLiteReader::get_col_i ( const std::string &  col_name)
private

Safely access the column_map and throw informative error when column is missing.

Definition at line 157 of file sqlitereader.cpp.

References column_map, Gambit::LogTags::err, LOCAL_INFO, and Gambit::Printers::printer_error().

Referenced by _retrieve_template(), get_sql_col(), move_to_next_point(), and Gambit::Printers::SQLiteReader::get_sql_col< std::string >().

158  {
159  auto it = column_map.find(col_name);
160  if(it==column_map.end())
161  {
162  std::stringstream err;
163  err<<"Attempted to retrieve data for column with name '"<<col_name<<"' using SQLiteReader, however this column does not seem to exist in the table we are reading!";
164  printer_error().raise(LOCAL_INFO, err.str());
165  }
166  return it->second;
167  }
EXPORT_SYMBOLS error & printer_error()
Printer errors.
std::map< std::string, std::size_t, Utils::ci_less > column_map
#define LOCAL_INFO
Definition: local_info.hpp:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_current_index()

ulong Gambit::Printers::SQLiteReader::get_current_index ( )
virtual

Implements Gambit::Printers::BaseBaseReader.

Definition at line 262 of file sqlitereader.cpp.

References current_dataset_index.

263  {
264  return current_dataset_index;
265  }

◆ get_current_point()

PPIDpair Gambit::Printers::SQLiteReader::get_current_point ( )
virtual

Get current rank/ptID pair in data file.

Implements Gambit::Printers::BaseBaseReader.

Definition at line 251 of file sqlitereader.cpp.

References current_point, eoi(), and Gambit::Printers::nullpoint.

Referenced by get_next_point().

252  {
253  if(eoi())
254  {
255  // End of data, return nullpoint;
257  }
258  return current_point;
259  }
virtual bool eoi()
Check if &#39;current point&#39; is past the end of the datasets (and thus invalid!)
EXPORT_SYMBOLS const PPIDpair nullpoint
Define &#39;nullpoint&#39; const.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_dataset_length()

ulong Gambit::Printers::SQLiteReader::get_dataset_length ( )
virtual

Get length of input dataset.

Implements Gambit::Printers::BaseBaseReader.

Definition at line 171 of file sqlitereader.cpp.

References Gambit::Printers::SQLiteBase::cout_row(), Gambit::LogTags::err, Gambit::Printers::SQLiteBase::get_db(), Gambit::Printers::SQLiteBase::get_table_name(), LOCAL_INFO, and Gambit::Printers::printer_error().

172  {
173  std::stringstream sql;
174  sql<<"SELECT COUNT(pairID) FROM "<<get_table_name()<<";";
175  sqlite3_stmt *temp_stmt;
176  int rc = sqlite3_prepare_v2(get_db(), sql.str().c_str(), -1, &temp_stmt, NULL);
177  if (rc != SQLITE_OK) {
178  std::stringstream err;
179  err<<"Encountered SQLite error while preparing to measure length of input table: "<<sqlite3_errmsg(get_db());
180  printer_error().raise(LOCAL_INFO, err.str());
181  }
182  rc = sqlite3_step(temp_stmt);
183  cout_row(temp_stmt); // DEBUG
184  if (rc != SQLITE_ROW) {
185  std::stringstream err;
186  err<<"Encountered SQLite error while attempting to measure length of input table: "<<sqlite3_errmsg(get_db());
187  printer_error().raise(LOCAL_INFO, err.str());
188  }
189  long long int rowcount = sqlite3_column_int64(temp_stmt, 0);
190  sqlite3_finalize(temp_stmt);
191  if(rowcount<0)
192  {
193  std::stringstream err;
194  err<<"Row count for input table was measured to be negative ("<<rowcount<<")! This clearly makes no sense and is probably a bug in the SQLiteReader class, please report it.";
195  printer_error().raise(LOCAL_INFO, err.str());
196  }
197  return rowcount;
198  }
EXPORT_SYMBOLS error & printer_error()
Printer errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
void cout_row(sqlite3_stmt *tmp_stmt)
Definition: sqlitebase.cpp:327
Here is the call graph for this function:

◆ get_next_point()

PPIDpair Gambit::Printers::SQLiteReader::get_next_point ( )
virtual

Get next rank/ptID pair in data file.

Implements Gambit::Printers::BaseBaseReader.

Definition at line 243 of file sqlitereader.cpp.

References current_dataset_index, get_current_point(), and move_to_next_point().

244  {
247  return get_current_point();
248  }
void move_to_next_point()
Move the SQL loop ahead one.
virtual PPIDpair get_current_point()
Get current rank/ptID pair in data file.
Here is the call graph for this function:

◆ get_sql_col() [1/6]

template<>
long long int Gambit::Printers::SQLiteReader::get_sql_col ( const std::string &  col_name)
private

Template function specialisations for retrieving SQLite column data as various types.

Definition at line 67 of file sqlitereader.cpp.

References get_col_i(), and stmt.

68  {
69  return sqlite3_column_int64(stmt, get_col_i(col_name));
70  }
std::size_t get_col_i(const std::string &col_name)
Safely access the column_map and throw informative error when column is missing.
Here is the call graph for this function:

◆ get_sql_col() [2/6]

template<>
double Gambit::Printers::SQLiteReader::get_sql_col ( const std::string &  col_name)
private

Definition at line 73 of file sqlitereader.cpp.

References get_col_i(), and stmt.

74  {
75  return sqlite3_column_double(stmt, get_col_i(col_name));
76  }
std::size_t get_col_i(const std::string &col_name)
Safely access the column_map and throw informative error when column is missing.
Here is the call graph for this function:

◆ get_sql_col() [3/6]

template<>
std::string Gambit::Printers::SQLiteReader::get_sql_col ( const std::string &  col_name)
private

Definition at line 79 of file sqlitereader.cpp.

80  {
81  char* p = (char*)sqlite3_column_text(stmt, get_col_i(col_name));
82  if(p==NULL)
83  {
84  std::stringstream err;
85  err<<"Pointer returned by sqlite3_column_text was NULL!";
86  printer_error().raise(LOCAL_INFO, err.str());
87  }
88  return std::string(p);
89  }
EXPORT_SYMBOLS error & printer_error()
Printer errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
std::size_t get_col_i(const std::string &col_name)
Safely access the column_map and throw informative error when column is missing.

◆ get_sql_col() [4/6]

template<typename T >
T Gambit::Printers::SQLiteReader::get_sql_col ( const std::string &  col_name)
private

◆ get_sql_col() [5/6]

template<>
long long int Gambit::Printers::SQLiteReader::get_sql_col ( const std::string &  )
private

◆ get_sql_col() [6/6]

template<>
double Gambit::Printers::SQLiteReader::get_sql_col ( const std::string &  )
private

◆ get_type()

std::size_t Gambit::Printers::SQLiteReader::get_type ( const std::string &  label)
virtual

Get type information for a data entry, i.e.

defines the C++ type which this should be retrieved as, not what it is necessarily literally stored as in the output.

Implements Gambit::Printers::BaseBaseReader.

Definition at line 275 of file sqlitereader.cpp.

References column_types, Gambit::LogTags::err, GET_SQL_TYPE_CASES, LOCAL_INFO, Gambit::Printers::printer_error(), and SQLITE_CPP_TYPES.

276  {
277  // Need to match SQL datatype to a printer type ID code.
278  // In principle we may like to retrieve a certain type of data in a fancy way,
279  // as with ModelParameters or vectors, however we can't really do that in an
280  // automated way because this higher-level information is lost during output.
281  // So the type matching has to be of a basic sort, i.e. individual ModelParameters
282  // elements will be identified as 'double' and so on. But if they are stored that
283  // way in the output, then we should be able to copy them that way too (which is
284  // the main usage of this get_type function), so this should be ok to do.
285  // Currently we only store data in basic types, so those are all that this
286  // function needs to retrieve.
287 
288  // First find out the SQL type for the column with this label
289  auto it = column_types.find(label);
290  if(it==column_types.end())
291  {
292  std::stringstream err;
293  err<<"Column with name '"<<label<<"' is not registered in the column_types map!";
294  printer_error().raise(LOCAL_INFO,err.str());
295  }
296  std::string coltype=it->second;
297 
298  if(coltype=="NULL")
299  {
300  std::stringstream err;
301  err<<"Column with name '"<<label<<"' is registered as having type 'NULL'! This doesn't make sense, only individual table slots with missing data should have type NULL, it should not be the 'affinity' for a whole column. This indicates a bug in the SQLiteReader object, please report it";
302  printer_error().raise(LOCAL_INFO,err.str());
303  }
304 
305  // Now match the SQL datatypes to Printer type IDs (via appropriate C++ types)
306  // TODO might need more careful checking, not sure if e.g. INTEGER type name will
307  // always be retrieved as INTERGER (as opposed to say INT or something else)
308  std::size_t typeID;
309  #define GET_SQL_TYPE_CASES(r,data,elem) \
310  if( SQLite_equaltypes(coltype,cpp2sql<elem>()) )\
311  { \
312  typeID = getTypeID<elem>(); \
313  } \
314  else
315  BOOST_PP_SEQ_FOR_EACH(GET_SQL_TYPE_CASES, _, SQLITE_CPP_TYPES)
316  #undef GET_SQL_TYPE_CASES
317  {
318  std::ostringstream err;
319  err << "Did not recognise retrieved SQL type for data label '"<<label<<"' (its SQL type is registered as '"<<coltype<<"')! This may indicate a bug in the SQLiteReader class, please report it.";
320  printer_error().raise(LOCAL_INFO,err.str());
321  }
322  if(typeID==0)
323  {
324  std::ostringstream err;
325  err << "Did not recognise retrieved Printer type for data label '"<<label<<"' (its SQL type is registered as '"<<coltype<<"')! This may indicate a bug in the Printer system, please report it.";
326  printer_error().raise(LOCAL_INFO,err.str());
327  }
328  return typeID;
329  }
EXPORT_SYMBOLS error & printer_error()
Printer errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
#define SQLITE_CPP_TYPES
Definition: sqlitebase.hpp:53
#define GET_SQL_TYPE_CASES(r, data, elem)
std::map< std::string, std::string, Utils::ci_less > column_types
Here is the call graph for this function:

◆ move_to_next_point()

void Gambit::Printers::SQLiteReader::move_to_next_point ( )
private

Move the SQL loop ahead one.

Definition at line 201 of file sqlitereader.cpp.

References current_point, eoi(), eoi_flag, Gambit::LogTags::err, get_col_i(), Gambit::Printers::SQLiteBase::get_db(), LOCAL_INFO, Gambit::Printers::nullpoint, Gambit::Printers::printer_error(), combine_hdf5::rank, and stmt.

Referenced by get_next_point(), and reset().

202  {
203  if(eoi())
204  {
205  std::stringstream err;
206  err<<"Attempted to move SQLiteReader to next row of input table, but eoi() has been reached! This should have been checked by whatever code called this function!";
207  printer_error().raise(LOCAL_INFO, err.str());
208  }
209  else if(stmt==NULL)
210  {
211  std::stringstream err;
212  err<<"Attempted to move SQLiteReader to next row of input table, but no sql statement has been prepared for iteration!";
213  printer_error().raise(LOCAL_INFO, err.str());
214  }
215  else
216  {
217  // Process the next row
218  int rc = sqlite3_step(stmt);
219  if(rc==SQLITE_ROW)
220  {
221  std::size_t rank = sqlite3_column_int64(stmt, get_col_i("MPIrank"));
222  std::size_t pID = sqlite3_column_int64(stmt, get_col_i("pointID"));
223  current_point = PPIDpair(pID,rank);
224  }
225  else if(rc==SQLITE_DONE)
226  {
227  // We are at the end of the dataset!
228  eoi_flag = true;
230  sqlite3_finalize(stmt);
231  }
232  else
233  {
234  // Not the next row, and not the end, something bad has happened.
235  std::stringstream err;
236  err<<"Encountered SQLite error while processing input file: "<<sqlite3_errmsg(get_db());
237  printer_error().raise(LOCAL_INFO, err.str());
238  }
239  }
240  }
EXPORT_SYMBOLS error & printer_error()
Printer errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
virtual bool eoi()
Check if &#39;current point&#39; is past the end of the datasets (and thus invalid!)
std::size_t get_col_i(const std::string &col_name)
Safely access the column_map and throw informative error when column is missing.
EXPORT_SYMBOLS const PPIDpair nullpoint
Define &#39;nullpoint&#39; const.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

void Gambit::Printers::SQLiteReader::reset ( )
virtual

Base class virtual interface functions.

Reset 'read head' position to first entry

Implements Gambit::Printers::BaseBaseReader.

Definition at line 111 of file sqlitereader.cpp.

References column_types, Gambit::Printers::comma_unless_last(), current_dataset_index, current_point, eoi(), eoi_flag, Gambit::LogTags::err, Gambit::Printers::SQLiteBase::get_database_file(), Gambit::Printers::SQLiteBase::get_db(), Gambit::Printers::SQLiteBase::get_table_name(), LOCAL_INFO, move_to_next_point(), Gambit::Printers::nullpoint, Gambit::Printers::printer_error(), and stmt.

Referenced by SQLiteReader().

112  {
113  if(not eoi() and stmt!=NULL)
114  {
115  // Finalise the previous loop so we can start a new one
116  sqlite3_finalize(stmt);
117  }
118 
119  // Reset loop variables
122  eoi_flag = false;
123 
124  std::stringstream sql;
125  sql<<"SELECT ";
126  for(auto it=column_types.begin(); it!=column_types.end(); ++it)
127  {
128  std::string col_name = it->first;
129  sql<<"`"<<col_name<<"`"<<comma_unless_last(it,column_types);
130  }
131  sql<<" FROM "<<get_table_name();
132 
133  /* Execute SQL statement and iterate through results*/
134  int rc = sqlite3_prepare_v2(get_db(), sql.str().c_str(), -1, &stmt, NULL);
135  if (rc != SQLITE_OK) {
136  std::stringstream err;
137  err<<"Encountered SQLite error while preparing to read data from previous run: "<<sqlite3_errmsg(get_db());
138 #ifdef SQL_DEBUG
139  err << " The attempted SQL statement was:"<<std::endl;
140  err << sql.str() << std::endl;
141 #endif
142  printer_error().raise(LOCAL_INFO, err.str());
143  }
144 
145  // Read first row
147 
148  if(eoi())
149  {
150  std::stringstream err;
151  err<<"Immediately reached end of input after beginning to loop through table "<<get_table_name()<<" in file "<<get_database_file()<<"! Perhaps the table is empty?";
152  printer_error().raise(LOCAL_INFO, err.str());
153  }
154  }
EXPORT_SYMBOLS error & printer_error()
Printer errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
virtual bool eoi()
Check if &#39;current point&#39; is past the end of the datasets (and thus invalid!)
void move_to_next_point()
Move the SQL loop ahead one.
std::string comma_unless_last(Iter it, const Cont &c)
Definition: sqlitebase.hpp:82
EXPORT_SYMBOLS const PPIDpair nullpoint
Define &#39;nullpoint&#39; const.
std::map< std::string, std::string, Utils::ci_less > column_types
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ column_map

std::map<std::string, std::size_t, Utils::ci_less> Gambit::Printers::SQLiteReader::column_map
private

Definition at line 77 of file sqlitereader.hpp.

Referenced by build_column_map(), get_all_labels(), and get_col_i().

◆ column_types

std::map<std::string, std::string, Utils::ci_less> Gambit::Printers::SQLiteReader::column_types
private

Definition at line 80 of file sqlitereader.hpp.

Referenced by build_column_map(), get_type(), reset(), and SQLiteReader().

◆ current_dataset_index

ulong Gambit::Printers::SQLiteReader::current_dataset_index
private

Definition at line 73 of file sqlitereader.hpp.

Referenced by get_current_index(), get_next_point(), and reset().

◆ current_point

PPIDpair Gambit::Printers::SQLiteReader::current_point
private

Definition at line 74 of file sqlitereader.hpp.

Referenced by get_current_point(), move_to_next_point(), and reset().

◆ eoi_flag

bool Gambit::Printers::SQLiteReader::eoi_flag
private

Definition at line 60 of file sqlitereader.hpp.

Referenced by eoi(), move_to_next_point(), and reset().

◆ stmt

sqlite3_stmt* Gambit::Printers::SQLiteReader::stmt
private

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