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)
 
HsSchemamaybe_reopen (const char *event_name, time_t timestamp, HsSchema *s)
 
- 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 = 0
 
std::string fConnectString
 
HsSchemaVector fWriterSchema
 
std::vector< HsSchema * > fWriterEvents
 
HsSchemaVector fReaderSchema
 

Detailed Description

Definition at line 5329 of file history_schema.cxx.

Constructor & Destructor Documentation

◆ SqliteHistory()

SqliteHistory::SqliteHistory ( )
inline

Definition at line 5332 of file history_schema.cxx.

5332 { // ctor
5333#ifdef HAVE_SQLITE
5334 fSql = new Sqlite();
5335#endif
5336 }

Member Function Documentation

◆ create_table()

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

Implements SqlHistoryBase.

Definition at line 5490 of file history_schema.cxx.

5491{
5492 if (fDebug)
5493 printf("SqliteHistory::create_table: event [%s], timestamp %s\n", event_name, TimeToString(timestamp).c_str());
5494
5495 int status;
5496 bool have_transaction = false;
5497 std::string table_name = MidasNameToSqlName(event_name);
5498
5499 // FIXME: what about duplicate table names?
5500 status = CreateSqlTable(fSql, table_name.c_str(), &have_transaction);
5501
5502 //if (status == DB_KEY_EXIST) {
5503 // return ReadSqliteTableSchema(fSql, sv, table_name.c_str(), fDebug);
5504 //}
5505
5506 if (status != HS_SUCCESS) {
5507 // FIXME: ???
5508 // FIXME: at least close or revert the transaction
5509 return status;
5510 }
5511
5512 std::string cmd;
5513
5514 std::string en;
5515 en += "_event_name_";
5516 en += table_name;
5517
5518 cmd = "CREATE TABLE ";
5519 cmd += fSql->QuoteId(en.c_str());
5520 cmd += " (table_name TEXT NOT NULL, event_name TEXT NOT NULL, _i_time INTEGER NOT NULL);";
5521
5522 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5523
5524 cmd = "INSERT INTO ";
5525 cmd += fSql->QuoteId(en.c_str());
5526 cmd += " (table_name, event_name, _i_time) VALUES (";
5527 cmd += fSql->QuoteString(table_name.c_str());
5528 cmd += ", ";
5529 cmd += fSql->QuoteString(event_name);
5530 cmd += ", ";
5531 cmd += fSql->QuoteString(TimeToString(timestamp).c_str());
5532 cmd += ");";
5533
5534 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5535
5536 std::string cn;
5537 cn += "_column_names_";
5538 cn += table_name;
5539
5540 cmd = "CREATE TABLE ";
5541 cmd += fSql->QuoteId(cn.c_str());
5542 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);";
5543
5544 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5545
5546 status = fSql->CommitTransaction(table_name.c_str());
5547 if (status != DB_SUCCESS) {
5548 return HS_FILE_ERROR;
5549 }
5550
5551 return ReadSqliteTableSchema(fSql, sv, table_name.c_str(), fDebug);
5552}
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:632
#define HS_SUCCESS
Definition midas.h:728
#define HS_FILE_ERROR
Definition midas.h:729
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 5375 of file history_schema.cxx.

5376{
5377 if (fDebug)
5378 printf("SqliteHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5379
5380 // for all schema for table_name, prepopulate is with column names
5381
5382 std::vector<std::string> columns;
5383 fSql->ListColumns(table_name, &columns);
5384
5385 // first, populate column names
5386
5387 for (size_t i=0; i<sv->size(); i++) {
5388 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5389
5390 if (s->fTableName != table_name)
5391 continue;
5392
5393 // schema should be empty at this point
5394 //assert(s->fVariables.size() == 0);
5395
5396 for (size_t j=0; j<columns.size(); j+=2) {
5397 const char* cn = columns[j+0].c_str();
5398 const char* ct = columns[j+1].c_str();
5399
5400 if (strcmp(cn, "_t_time") == 0)
5401 continue;
5402 if (strcmp(cn, "_i_time") == 0)
5403 continue;
5404
5405 bool found = (s->find_column_index(cn) >= 0);
5406
5407 //printf("column [%s] sql type [%s]\n", cn.c_str(), ct);
5408
5409 if (!found) {
5410 HsSchemaEntry se;
5411 se.name = cn;
5412 se.type = 0;
5413 se.n_data = 1;
5414 se.n_bytes = 0;
5415 s->fColumnIndexCache[cn] = s->fColumnNames.size();
5416 s->fVariables.push_back(se);
5417 s->fColumnNames.push_back(cn);
5418 s->fColumnTypes.push_back(ct);
5419 s->fColumnInactive.push_back(false);
5420 s->fOffsets.push_back(-1);
5421 }
5422 }
5423 }
5424
5425 // then read column name information
5426
5427 std::string tn;
5428 tn += "_column_names_";
5429 tn += table_name;
5430
5431 std::string cmd;
5432 cmd = "SELECT column_name, tag_name, tag_type, _i_time FROM ";
5433 cmd += fSql->QuoteId(tn.c_str());
5434 cmd += " WHERE table_name=";
5435 cmd += fSql->QuoteString(table_name);
5436 cmd += " ORDER BY _i_time ASC;";
5437
5438 int status = fSql->Prepare(table_name, cmd.c_str());
5439
5440 if (status != DB_SUCCESS) {
5441 return status;
5442 }
5443
5444 while (1) {
5445 status = fSql->Step();
5446
5447 if (status != DB_SUCCESS)
5448 break;
5449
5450 // NOTE: SQL "SELECT ORDER BY _i_time ASC" returns data sorted by time
5451 // in this code we use the data from the last data row
5452 // so if multiple rows are present, the latest one is used
5453
5454 std::string col_name = fSql->GetText(0);
5455 std::string tag_name = fSql->GetText(1);
5456 std::string tag_type = fSql->GetText(2);
5457 time_t schema_time = fSql->GetTime(3);
5458
5459 //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());
5460
5461 // make sure a schema exists at this time point
5462 NewSqlSchema(sv, table_name, schema_time);
5463
5464 // add this information to all schema
5465
5466 for (size_t i=0; i<sv->size(); i++) {
5467 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5468 if (s->fTableName != table_name)
5469 continue;
5470 if (s->fTimeFrom < schema_time)
5471 continue;
5472
5473 //printf("add column to schema %d\n", s->fTimeFrom);
5474
5475 int j = s->find_column_index(col_name);
5476 if (j < 0)
5477 continue;
5478 s->fVariables[j].name = tag_name;
5479 s->fVariables[j].type = rpc_name_tid(tag_type.c_str());
5480 s->fVariables[j].n_data = 1;
5481 s->fVariables[j].n_bytes = rpc_tid_size(s->fVariables[j].type);
5482 }
5483 }
5484
5485 status = fSql->Finalize();
5486
5487 return HS_SUCCESS;
5488}
std::vector< int > fOffsets
std::vector< HsSchemaEntry > fVariables
size_t size() const
std::vector< std::string > fColumnNames
std::vector< std::string > fColumnTypes
std::unordered_map< std::string, size_t > fColumnIndexCache
int find_column_index(const std::string &column_name)
std::vector< bool > fColumnInactive
std::string fTableName
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:11918
INT rpc_tid_size(INT id)
Definition midas.cxx:11897
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 type
int n_data
std::string name
int n_bytes
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 5344 of file history_schema.cxx.

5345{
5346 int status;
5347
5348 if (fDebug)
5349 printf("SqliteHistory::read_table_and_event_names!\n");
5350
5351 // loop over all tables
5352
5353 std::vector<std::string> tables;
5354 status = fSql->ListTables(&tables);
5355 if (status != DB_SUCCESS)
5356 return status;
5357
5358 for (size_t i=0; i<tables.size(); i++) {
5359 const char* table_name = tables[i].c_str();
5360
5361 const char* s;
5362 s = strstr(table_name, "_event_name_");
5363 if (s == table_name)
5364 continue;
5365 s = strstr(table_name, "_column_names_");
5366 if (s == table_name)
5367 continue;
5368
5369 status = ReadSqliteTableSchema(fSql, sv, table_name, fDebug);
5370 }
5371
5372 return HS_SUCCESS;
5373}
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 5554 of file history_schema.cxx.

5555{
5556 if (fDebug)
5557 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());
5558
5559 int status = StartSqlTransaction(fSql, table_name, have_transaction);
5560 if (status != HS_SUCCESS)
5561 return status;
5562
5563 // FIXME: quotes
5564 std::string cmd;
5565 cmd = "INSERT INTO \'_column_names_";
5566 cmd += table_name;
5567 cmd += "\' (table_name, column_name, tag_name, tag_type, column_type, _i_time) VALUES (\'";
5568 cmd += table_name;
5569 cmd += "\', \'";
5570 cmd += column_name;
5571 cmd += "\', \'";
5572 cmd += tag_name;
5573 cmd += "\', \'";
5574 cmd += tag_type;
5575 cmd += "\', \'";
5576 cmd += column_type;
5577 cmd += "\', \'";
5578 cmd += TimeToString(timestamp);
5579 cmd += "\');";
5580 status = fSql->Exec(table_name, cmd.c_str());
5581
5582 return status;
5583}
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: