MIDAS
Loading...
Searching...
No Matches
SqliteHistory Class Reference
Inheritance diagram for SqliteHistory:
Collaboration diagram for SqliteHistory:

Public Member Functions

 SqliteHistory ()
 
int read_table_and_event_names (HsSchemaVector *sv)
 
int read_column_names (HsSchemaVector *sv, const char *table_name, const char *event_name)
 
int create_table (HsSchemaVector *sv, const char *event_name, time_t timestamp)
 
int update_column (const char *event_name, const char *table_name, const char *column_name, const char *column_type, const char *tag_name, const char *tag_type, const time_t timestamp, bool active, bool *have_transaction)
 
- Public Member Functions inherited from SqlHistoryBase
 SqlHistoryBase ()
 
virtual ~SqlHistoryBase ()
 
int hs_set_debug (int debug)
 set debug level, returns previous debug level
 
int hs_connect (const char *connect_string)
 returns HS_SUCCESS
 
int hs_disconnect ()
 disconnect from history, returns HS_SUCCESS
 
HsSchemanew_event (const char *event_name, time_t timestamp, int ntags, const TAG tags[])
 
int read_schema (HsSchemaVector *sv, const char *event_name, const time_t timestamp)
 
- Public Member Functions inherited from SchemaHistoryBase
 SchemaHistoryBase ()
 
virtual ~SchemaHistoryBase ()
 
int hs_define_event (const char *event_name, time_t timestamp, int ntags, const TAG tags[])
 see hs_define_event(), returns HS_SUCCESS or HS_FILE_ERROR
 
int hs_write_event (const char *event_name, time_t timestamp, int buffer_size, const char *buffer)
 see hs_write_event(), returns HS_SUCCESS or HS_FILE_ERROR
 
int hs_flush_buffers ()
 flush buffered data to storage where it is visible to mhttpd
 
int hs_clear_cache ()
 clear internal cache, returns HS_SUCCESS
 
int hs_get_events (time_t t, std::vector< std::string > *pevents)
 get list of events that exist(ed) at given time and later (value 0 means "return all events from beginning of time"), returns HS_SUCCESS
 
int hs_get_tags (const char *event_name, time_t t, std::vector< TAG > *ptags)
 get list of history variables for given event (use event names returned by hs_get_events()) that exist(ed) at given time and later (value 0 means "all variables for this event that ever existed"), also see hs_get_tags(), returns HS_SUCCESS
 
int hs_get_last_written (time_t timestamp, int num_var, const char *const event_name[], const char *const var_name[], const int var_index[], time_t last_written[])
 
int hs_read_buffer (time_t start_time, time_t end_time, int num_var, const char *const event_name[], const char *const var_name[], const int var_index[], MidasHistoryBufferInterface *buffer[], int hs_status[])
 returns HS_SUCCESS
 
int hs_read (time_t start_time, time_t end_time, time_t interval, int num_var, const char *const event_name[], const char *const var_name[], const int var_index[], int num_entries[], time_t *time_buffer[], double *data_buffer[], int st[])
 see hs_read(), returns HS_SUCCESS
 
int hs_read_binned (time_t start_time, time_t end_time, int num_bins, int num_var, const char *const event_name[], const char *const var_name[], const int var_index[], int num_entries[], int *count_bins[], double *mean_bins[], double *rms_bins[], double *min_bins[], double *max_bins[], time_t *bins_first_time[], double *bins_first_value[], time_t *bins_last_time[], double *bins_last_value[], time_t last_time[], double last_value[], int st[])
 returns HS_SUCCESS
 
- Public Member Functions inherited from MidasHistoryInterface
 MidasHistoryInterface ()
 history type: MIDAS, ODBC, SQLITE, etc
 
virtual ~MidasHistoryInterface ()
 

Additional Inherited Members

- Public Attributes inherited from SqlHistoryBase
SqlBasefSql
 
- Public Attributes inherited from MidasHistoryInterface
char name [NAME_LENGTH]
 
char type [NAME_LENGTH]
 history channel name
 
- Protected Member Functions inherited from SqlHistoryBase
int update_schema (HsSqlSchema *s, const time_t timestamp, const int ntags, const TAG tags[], bool write_enable)
 
int update_schema1 (HsSqlSchema *s, const time_t timestamp, const int ntags, const TAG tags[], bool write_enable, bool *have_transaction)
 
- Protected Attributes inherited from SchemaHistoryBase
int fDebug
 
std::string fConnectString
 
HsSchemaVector fWriterCurrentSchema
 
std::vector< HsSchema * > fEvents
 
HsSchemaVector fSchema
 

Detailed Description

Definition at line 5143 of file history_schema.cxx.

Constructor & Destructor Documentation

◆ SqliteHistory()

SqliteHistory::SqliteHistory ( )
inline

Definition at line 5146 of file history_schema.cxx.

5146 { // ctor
5147#ifdef HAVE_SQLITE
5148 fSql = new Sqlite();
5149#endif
5150 }
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
Here is the call graph for this function:

Member Function Documentation

◆ create_table()

int SqliteHistory::create_table ( HsSchemaVector sv,
const char event_name,
time_t  timestamp 
)
virtual

Implements SqlHistoryBase.

Definition at line 5310 of file history_schema.cxx.

5311{
5312 if (fDebug)
5313 printf("SqliteHistory::create_table: event [%s], timestamp %s\n", event_name, TimeToString(timestamp).c_str());
5314
5315 int status;
5316 bool have_transaction = false;
5317 std::string table_name = MidasNameToSqlName(event_name);
5318
5319 // FIXME: what about duplicate table names?
5320 status = CreateSqlTable(fSql, table_name.c_str(), &have_transaction);
5321
5322 //if (status == DB_KEY_EXIST) {
5323 // return ReadSqliteTableSchema(fSql, sv, table_name.c_str(), fDebug);
5324 //}
5325
5326 if (status != HS_SUCCESS) {
5327 // FIXME: ???
5328 // FIXME: at least close or revert the transaction
5329 return status;
5330 }
5331
5332 std::string cmd;
5333
5334 std::string en;
5335 en += "_event_name_";
5336 en += table_name;
5337
5338 cmd = "CREATE TABLE ";
5339 cmd += fSql->QuoteId(en.c_str());
5340 cmd += " (table_name TEXT NOT NULL, event_name TEXT NOT NULL, _i_time INTEGER NOT NULL);";
5341
5342 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5343
5344 cmd = "INSERT INTO ";
5345 cmd += fSql->QuoteId(en.c_str());
5346 cmd += " (table_name, event_name, _i_time) VALUES (";
5347 cmd += fSql->QuoteString(table_name.c_str());
5348 cmd += ", ";
5349 cmd += fSql->QuoteString(event_name);
5350 cmd += ", ";
5351 cmd += fSql->QuoteString(TimeToString(timestamp).c_str());
5352 cmd += ");";
5353
5354 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5355
5356 std::string cn;
5357 cn += "_column_names_";
5358 cn += table_name;
5359
5360 cmd = "CREATE TABLE ";
5361 cmd += fSql->QuoteId(cn.c_str());
5362 cmd += " (table_name TEXT NOT NULL, column_name TEXT NOT NULL, tag_name TEXT NOT NULL, tag_type TEXT NOT NULL, column_type TEXT NOT NULL, _i_time INTEGER NOT NULL);";
5363
5364 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5365
5366 status = fSql->CommitTransaction(table_name.c_str());
5367 if (status != DB_SUCCESS) {
5368 return HS_FILE_ERROR;
5369 }
5370
5371 return ReadSqliteTableSchema(fSql, sv, table_name.c_str(), fDebug);
5372}
virtual int CommitTransaction(const char *table_name)=0
virtual std::string QuoteId(const char *s)=0
virtual int Exec(const char *sql)=0
virtual std::string QuoteString(const char *s)=0
#define DB_SUCCESS
Definition midas.h:631
#define HS_SUCCESS
Definition midas.h:727
#define HS_FILE_ERROR
Definition midas.h:728
static std::string MidasNameToSqlName(const char *s)
static std::string TimeToString(time_t t)
static int ReadSqliteTableSchema(SqlBase *sql, HsSchemaVector *sv, const char *table_name, int debug)
static int CreateSqlTable(SqlBase *sql, const char *table_name, bool *have_transaction, bool set_default_timestamp=false)
DWORD status
Definition odbhist.cxx:39
Here is the call graph for this function:

◆ read_column_names()

int SqliteHistory::read_column_names ( HsSchemaVector sv,
const char table_name,
const char event_name 
)
virtual

Implements SqlHistoryBase.

Definition at line 5189 of file history_schema.cxx.

5190{
5191 if (fDebug)
5192 printf("SqliteHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5193
5194 // for all schema for table_name, prepopulate is with column names
5195
5196 std::vector<std::string> columns;
5197 fSql->ListColumns(table_name, &columns);
5198
5199 // first, populate column names
5200
5201 for (unsigned i=0; i<sv->size(); i++) {
5202 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5203
5204 if (s->table_name != table_name)
5205 continue;
5206
5207 // schema should be empty at this point
5208 //assert(s->variables.size() == 0);
5209
5210 for (unsigned j=0; j<columns.size(); j+=2) {
5211 const char* cn = columns[j+0].c_str();
5212 const char* ct = columns[j+1].c_str();
5213
5214 if (strcmp(cn, "_t_time") == 0)
5215 continue;
5216 if (strcmp(cn, "_i_time") == 0)
5217 continue;
5218
5219 bool found = false;
5220
5221 for (unsigned k=0; k<s->column_names.size(); k++) {
5222 if (s->column_names[k] == cn) {
5223 found = true;
5224 break;
5225 }
5226 }
5227
5228 //printf("column [%s] sql type [%s]\n", cn.c_str(), ct);
5229
5230 if (!found) {
5232 se.name = cn;
5233 se.type = 0;
5234 se.n_data = 1;
5235 se.n_bytes = 0;
5236 s->variables.push_back(se);
5237 s->column_names.push_back(cn);
5238 s->column_types.push_back(ct);
5239 s->offsets.push_back(-1);
5240 }
5241 }
5242 }
5243
5244 // then read column name information
5245
5246 std::string tn;
5247 tn += "_column_names_";
5248 tn += table_name;
5249
5250 std::string cmd;
5251 cmd = "SELECT column_name, tag_name, tag_type, _i_time FROM ";
5252 cmd += fSql->QuoteId(tn.c_str());
5253 cmd += " WHERE table_name=";
5254 cmd += fSql->QuoteString(table_name);
5255 cmd += " ORDER BY _i_time ASC;";
5256
5257 int status = fSql->Prepare(table_name, cmd.c_str());
5258
5259 if (status != DB_SUCCESS) {
5260 return status;
5261 }
5262
5263 while (1) {
5264 status = fSql->Step();
5265
5266 if (status != DB_SUCCESS)
5267 break;
5268
5269 // NOTE: SQL "SELECT ORDER BY _i_time ASC" returns data sorted by time
5270 // in this code we use the data from the last data row
5271 // so if multiple rows are present, the latest one is used
5272
5273 std::string col_name = fSql->GetText(0);
5274 std::string tag_name = fSql->GetText(1);
5275 std::string tag_type = fSql->GetText(2);
5277
5278 //printf("read table [%s] column [%s] tag name [%s] time %s\n", table_name, col_name.c_str(), tag_name.c_str(), TimeToString(xxx_time).c_str());
5279
5280 // make sure a schema exists at this time point
5281 NewSqlSchema(sv, table_name, schema_time);
5282
5283 // add this information to all schema
5284
5285 for (unsigned i=0; i<sv->size(); i++) {
5286 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5287 if (s->table_name != table_name)
5288 continue;
5289 if (s->time_from < schema_time)
5290 continue;
5291
5292 //printf("add column to schema %d\n", s->time_from);
5293
5294 for (unsigned j=0; j<s->column_names.size(); j++) {
5295 if (col_name != s->column_names[j])
5296 continue;
5297 s->variables[j].name = tag_name;
5298 s->variables[j].type = rpc_name_tid(tag_type.c_str());
5299 s->variables[j].n_data = 1;
5300 s->variables[j].n_bytes = rpc_tid_size(s->variables[j].type);
5301 }
5302 }
5303 }
5304
5305 status = fSql->Finalize();
5306
5307 return HS_SUCCESS;
5308}
virtual int Finalize()=0
virtual int ListColumns(const char *table, std::vector< std::string > *plist)=0
virtual int Prepare(const char *table_name, const char *sql)=0
virtual time_t GetTime(int column)=0
virtual int Step()=0
virtual const char * GetText(int column)=0
int rpc_name_tid(const char *name)
Definition midas.cxx:11778
INT rpc_tid_size(INT id)
Definition midas.cxx:11757
static HsSqlSchema * NewSqlSchema(HsSchemaVector *sv, const char *table_name, time_t t)
INT i
Definition mdump.cxx:32
INT j
Definition odbhist.cxx:40
INT k
Definition odbhist.cxx:40
std::string name
std::vector< int > offsets
std::vector< HsSchemaEntry > variables
std::string table_name
std::vector< std::string > column_types
std::vector< std::string > column_names
Here is the call graph for this function:

◆ read_table_and_event_names()

int SqliteHistory::read_table_and_event_names ( HsSchemaVector sv)
virtual

Implements SqlHistoryBase.

Definition at line 5158 of file history_schema.cxx.

5159{
5160 int status;
5161
5162 if (fDebug)
5163 printf("SqliteHistory::read_table_and_event_names!\n");
5164
5165 // loop over all tables
5166
5167 std::vector<std::string> tables;
5169 if (status != DB_SUCCESS)
5170 return status;
5171
5172 for (unsigned i=0; i<tables.size(); i++) {
5173 const char* table_name = tables[i].c_str();
5174
5175 const char* s;
5176 s = strstr(table_name, "_event_name_");
5177 if (s == table_name)
5178 continue;
5179 s = strstr(table_name, "_column_names_");
5180 if (s == table_name)
5181 continue;
5182
5183 status = ReadSqliteTableSchema(fSql, sv, table_name, fDebug);
5184 }
5185
5186 return HS_SUCCESS;
5187}
virtual int ListTables(std::vector< std::string > *plist)=0
Here is the call graph for this function:

◆ update_column()

int SqliteHistory::update_column ( const char event_name,
const char table_name,
const char column_name,
const char column_type,
const char tag_name,
const char tag_type,
const time_t  timestamp,
bool  active,
bool have_transaction 
)
virtual

Implements SqlHistoryBase.

Definition at line 5374 of file history_schema.cxx.

5375{
5376 if (fDebug)
5377 printf("SqliteHistory::update_column: event [%s], table [%s], column [%s], new name [%s], timestamp %s\n", event_name, table_name, column_name, tag_name, TimeToString(timestamp).c_str());
5378
5379 int status = StartSqlTransaction(fSql, table_name, have_transaction);
5380 if (status != HS_SUCCESS)
5381 return status;
5382
5383 // FIXME: quotes
5384 std::string cmd;
5385 cmd = "INSERT INTO \'_column_names_";
5386 cmd += table_name;
5387 cmd += "\' (table_name, column_name, tag_name, tag_type, column_type, _i_time) VALUES (\'";
5388 cmd += table_name;
5389 cmd += "\', \'";
5390 cmd += column_name;
5391 cmd += "\', \'";
5392 cmd += tag_name;
5393 cmd += "\', \'";
5394 cmd += tag_type;
5395 cmd += "\', \'";
5396 cmd += column_type;
5397 cmd += "\', \'";
5398 cmd += TimeToString(timestamp);
5399 cmd += "\');";
5400 status = fSql->Exec(table_name, cmd.c_str());
5401
5402 return status;
5403}
static int StartSqlTransaction(SqlBase *sql, const char *table_name, bool *have_transaction)
Here is the call graph for this function:

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