gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
sqlitebase.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
16 
17 #ifndef __sqlitebase_hpp__
18 #define __sqlitebase_hpp__
19 
20 #include <map>
21 #include <string>
22 
23 // Gambit
24 #include "gambit/Utils/util_functions.hpp" // Need Utils::ci_less to make map find() functions case-insensitive, since SQLite is case insensitive
25 
26 // Forward declare sqlite3 objects
27 struct sqlite3;
28 struct sqlite3_stmt;
29 
30 namespace Gambit
31 {
32  namespace Printers
33  {
34  // Compute unique integer from two integers
35  // We use this to turn MPI rank and point ID integers into an SQLite row ID
36  inline std::size_t pairfunc(const std::size_t i, const std::size_t j)
37  {
38  // The Cantor pairing function should be good enough for this purpose I think
39  // If we exceed the maximum size of size_t then we'll have to use a more space-efficient pairing function
40  return ((i+j)*(i+j+1))/2 + j;
41  }
42 
43 
44  // Type of function pointer for SQLite callback function
45  typedef int sql_callback_fptr(void*, int, char**, char**);
46 
47  // Callback function for counting columns in table
48  int col_name_callback(void* colmap_in, int /*count*/, char** data, char** /* columns */);
49 
50  // Matching of SQL types to C++ types
51  typedef long long int llint;
52  typedef std::string str;
53  #define SQLITE_CPP_TYPES (llint) (double) (str)
54  template<typename T> std::string cpp2sql();
55  template<> std::string cpp2sql<long long int>();
56  template<> std::string cpp2sql<double>();
57  template<> std::string cpp2sql<std::string>();
58 
59  // Matching of SQLite "type codes" to SQL types
60  // Names should match results of cpp2sql above
61  std::map<unsigned int,std::string> define_typecodes();
62  extern const std::map<unsigned int,std::string> typecode2sql;
63 
64  // Map from all SQLite "suggestion" types into the five "base" SQLite types
65  // Used for type comparisons.
66  std::map<std::string,std::string, Utils::ci_less> fill_SQLtype_to_basic();
67  extern const std::map<std::string,std::string, Utils::ci_less> SQLtype_to_basic;
68 
69  // Compare SQLite data types to see if they are equivalent to the same basic 'affinity' for a column
70  bool SQLite_equaltypes(const std::string& type1, const std::string& type2);
71 
72  // Returns new iterator pointing to next element
73  template <typename Iter>
74  Iter next_el(Iter iter)
75  {
76  return ++iter;
77  }
78 
79  // Return a comma unless iterator is the last in the supplied iterable
80  // (or if it points to end())
81  template <typename Iter, typename Cont>
82  std::string comma_unless_last(Iter it, const Cont& c)
83  {
84  std::string out("");
85  if((it == c.end()) || (next_el(it) == c.end()))
86  { /* this is the last element or end(), do nothing */ }
87  else
88  { out = ","; }
89  return out;
90  }
91 
93  class SQLiteBase
94  {
95  public:
96  SQLiteBase();
97  ~SQLiteBase();
98 
99  protected:
100  std::string get_database_file();
101  std::string get_table_name();
102  void set_table_name(const std::string& table_name);
103 
104  // Verify that the outbase database is open and the results table exists
105  void require_output_ready();
106 
107  // Open database and 'attach' it to this object
108  // A database will be created if it doesn't exist
109  void open_db(const std::string&, char access='r');
110 
111  // Close the database file that is attached to this object
112  void close_db();
113 
114  // Get the pointer to the target SQLite database
115  sqlite3* get_db();
116 
117  // Dump a row of results to std::cout
118  void cout_row(sqlite3_stmt* tmp_stmt);
119 
120  // Check that the required table exists
121  // Sets 'table_exists' to true if successful, otherwise throws error
122  void check_table_exists();
123 
124  // Sets the 'table_exists' flag to true
125  void set_table_exists();
126 
127  // Get names and types for all columns in the target table
128  // (Output is a map from names to types)
129  std::map<std::string, std::string, Utils::ci_less> get_column_info();
130 
131  // Submit an SQL statement to the database
132  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);
133 
134  private:
135  // Path to SQLite database file
136  std::string database_file;
137 
138  // Name of data table to be accessed
139  std::string table_name;
140 
141  // Pointer to target sqlite3 database
142  sqlite3* db;
143 
144  // Bool to record if we already have a database file open
146 
147  // Bool to record if the table to be accessed exists yet
149 
150  };
151 
152  }
153 }
154 
155 #endif
void set_table_name(const std::string &table_name)
Definition: sqlitebase.cpp:324
greatScanData data
Definition: great.cpp:38
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)
Definition: sqlitebase.cpp:258
bool SQLite_equaltypes(const std::string &type1, const std::string &type2)
Definition: sqlitebase.cpp:125
std::string cpp2sql< double >()
Definition: sqlitebase.cpp:63
std::string cpp2sql< long long int >()
Definition: sqlitebase.cpp:62
int sql_callback_fptr(void *, int, char **, char **)
Definition: sqlitebase.hpp:45
long long int llint
Definition: sqlitebase.hpp:51
std::map< std::string, std::string, Utils::ci_less > fill_SQLtype_to_basic()
Definition: sqlitebase.cpp:83
General small utility functions.
std::map< unsigned int, std::string > define_typecodes()
Definition: sqlitebase.cpp:69
std::string str
Definition: sqlitebase.hpp:52
std::string comma_unless_last(Iter it, const Cont &c)
Definition: sqlitebase.hpp:82
int col_name_callback(void *colmap_in, int, char **data, char **)
Definition: sqlitebase.cpp:43
const std::map< std::string, std::string, Utils::ci_less > SQLtype_to_basic
std::map< std::string, std::string, Utils::ci_less > get_column_info()
Definition: sqlitebase.cpp:300
Iter next_el(Iter iter)
Definition: sqlitebase.hpp:74
void open_db(const std::string &, char access='r')
Definition: sqlitebase.cpp:164
std::string cpp2sql()
void cout_row(sqlite3_stmt *tmp_stmt)
Definition: sqlitebase.cpp:327
SQLite base class for both reader and writer.
Definition: sqlitebase.hpp:93
TODO: see if we can use this one:
Definition: Analysis.hpp:33
std::size_t pairfunc(const std::size_t i, const std::size_t j)
Definition: sqlitebase.hpp:36
const std::map< unsigned int, std::string > typecode2sql