ICP  1
ISISDB.h
Go to the documentation of this file.
1 #ifndef ISISDB_H
2 #define ISISDB_H
3 
4 #include "sqlite3.h"
5 
6 static const char* isisdb_per_session_sql = "";
7 
8 // "DROP TABLE IF EXISTS t1; "
9 // "CREATE TEMPORARY TABLE t1 ( run_number INTEGER, source TEXT, period INTEGER, iso_time STRING, unix_time INTEGER, is_start INTEGER ); "
10 // "DROP TRIGGER IF EXISTS on_run_state; "
11 // "CREATE TEMPORARY TRIGGER on_run_state AFTER INSERT ON run_state FOR EACH ROW BEGIN "
12 // "DELETE FROM t1; "
13 // "INSERT INTO t1 VALUES ( -1, 'TRIGGER', -1, new.iso_time, new.unix_time, new.is_start ); " /* 'TRIGGER' or seblocks.source ? */
14 // "UPDATE t1 SET period=(SELECT value FROM config WHERE name='period'); "
15 // "UPDATE t1 SET run_number=(SELECT value FROM config WHERE name='run_number'); "
16 // "INSERT INTO seperiods SELECT t1.run_number,t1.period,t1.iso_time,t1.unix_time,t1.is_start,seblocks.block_name,seblocks.setpoint_value,seblocks.block_value FROM t1,seblocks WHERE seblocks.run_number=t1.run_number; "
17 // "INSERT INTO selog SELECT t1.run_number,t1.source,t1.iso_time,t1.unix_time,seblocks.block_name,seblocks.block_value,seblocks.is_real FROM t1,seblocks WHERE seblocks.run_number=t1.run_number; "
18 // "END; ";
19 
20 class ISISDB
21 {
22  private:
23  sqlite3* m_db;
24  static std::list<sqlite3*> db_list;
25  static const char* db_file;
26 
27  public:
28  ISISDB(bool integrity_check);
29 
30  ~ISISDB();
31 
32  operator sqlite3*()
33  {
34  return m_db;
35  }
36 
37  int closeDatabase();
38 
39  int exec(const char* sql, int (*callback)(void*,int,char**,char**), void* arg)
40  {
41  char last_error[256];
42  char *errmsg = NULL;
43  int ret = sqlite3_exec(m_db, sql, callback, arg, &errmsg);
44  if (ret != SQLITE_OK)
45  {
46  _snprintf_s(last_error, sizeof(last_error), _TRUNCATE, "SELOGGER: SQLITE error %s", errmsg);
47  sqlite3_free(errmsg);
48  throw(std::exception(last_error));
49  }
50  return ret;
51  }
52 
53  const char* version();
54 };
55 
56 #include "Poco/SingletonHolder.h"
57 #include "Poco/Data/Common.h"
58 #include "Poco/Data/SessionPool.h"
59 #include "Poco/Data/SQLite/Connector.h"
60 #include "Poco/Data/MySQl/Connector.h"
61 #include "Poco/Data/RecordSet.h"
62 
63 class SQLITESessionPool : public Poco::Data::SessionPool
64 {
65 public:
66  SQLITESessionPool() : Poco::Data::SessionPool("SQLite", "c:\\data\\selog.sq3", 3, 32, 0)
67  {
68  Poco::Data::SQLite::Connector::registerConnector();
69  }
70 
71  void customizeSession(Poco::Data::Session& session)
72  {
73  session.setProperty("maxRetryAttempts", 1000000);
74  if (strlen(isisdb_per_session_sql) > 0)
75  {
76  Poco::Data::Statement stmt(session);
77  stmt << isisdb_per_session_sql;
78  stmt.execute();
79  poco_assert(stmt.done());
80  }
81  }
82 };
83 
84 extern Poco::SingletonHolder<SQLITESessionPool> sqlite_session_pool;
85 
86 
87 #endif /* ISISDB_H */
sqlite3 * m_db
Definition: ISISDB.h:23
Definition: ISISDB.h:20
static const char * db_file
Definition: ISISDB.h:25
ISISDB(bool integrity_check)
Definition: ISISDB.cpp:94
Poco::SingletonHolder< SQLITESessionPool > sqlite_session_pool
Definition: ISISDB.cpp:357
void customizeSession(Poco::Data::Session &session)
Definition: ISISDB.h:71
~ISISDB()
Definition: ISISDB.cpp:54
static const char * isisdb_per_session_sql
Definition: ISISDB.h:6
static std::list< sqlite3 * > db_list
Definition: ISISDB.h:24
int exec(const char *sql, int(*callback)(void *, int, char **, char **), void *arg)
Definition: ISISDB.h:39
static char last_error[1024]
Definition: selogger.cpp:198
const char * version()
Definition: ISISDB.cpp:47
int closeDatabase()
Definition: ISISDB.cpp:64