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 5304 of file history_schema.cxx.

Constructor & Destructor Documentation

◆ SqliteHistory()

SqliteHistory::SqliteHistory ( )
inline

Definition at line 5307 of file history_schema.cxx.

5307 { // ctor
5308#ifdef HAVE_SQLITE
5309 fSql = new Sqlite();
5310#endif
5311 }
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 5472 of file history_schema.cxx.

5473{
5474 if (fDebug)
5475 printf("SqliteHistory::create_table: event [%s], timestamp %s\n", event_name, TimeToString(timestamp).c_str());
5476
5477 int status;
5478 bool have_transaction = false;
5479 std::string table_name = MidasNameToSqlName(event_name);
5480
5481 // FIXME: what about duplicate table names?
5482 status = CreateSqlTable(fSql, table_name.c_str(), &have_transaction);
5483
5484 //if (status == DB_KEY_EXIST) {
5485 // return ReadSqliteTableSchema(fSql, sv, table_name.c_str(), fDebug);
5486 //}
5487
5488 if (status != HS_SUCCESS) {
5489 // FIXME: ???
5490 // FIXME: at least close or revert the transaction
5491 return status;
5492 }
5493
5494 std::string cmd;
5495
5496 std::string en;
5497 en += "_event_name_";
5498 en += table_name;
5499
5500 cmd = "CREATE TABLE ";
5501 cmd += fSql->QuoteId(en.c_str());
5502 cmd += " (table_name TEXT NOT NULL, event_name TEXT NOT NULL, _i_time INTEGER NOT NULL);";
5503
5504 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5505
5506 cmd = "INSERT INTO ";
5507 cmd += fSql->QuoteId(en.c_str());
5508 cmd += " (table_name, event_name, _i_time) VALUES (";
5509 cmd += fSql->QuoteString(table_name.c_str());
5510 cmd += ", ";
5511 cmd += fSql->QuoteString(event_name);
5512 cmd += ", ";
5513 cmd += fSql->QuoteString(TimeToString(timestamp).c_str());
5514 cmd += ");";
5515
5516 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5517
5518 std::string cn;
5519 cn += "_column_names_";
5520 cn += table_name;
5521
5522 cmd = "CREATE TABLE ";
5523 cmd += fSql->QuoteId(cn.c_str());
5524 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);";
5525
5526 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5527
5528 status = fSql->CommitTransaction(table_name.c_str());
5529 if (status != DB_SUCCESS) {
5530 return HS_FILE_ERROR;
5531 }
5532
5533 return ReadSqliteTableSchema(fSql, sv, table_name.c_str(), fDebug);
5534}
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 5350 of file history_schema.cxx.

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

5320{
5321 int status;
5322
5323 if (fDebug)
5324 printf("SqliteHistory::read_table_and_event_names!\n");
5325
5326 // loop over all tables
5327
5328 std::vector<std::string> tables;
5330 if (status != DB_SUCCESS)
5331 return status;
5332
5333 for (size_t i=0; i<tables.size(); i++) {
5334 const char* table_name = tables[i].c_str();
5335
5336 const char* s;
5337 s = strstr(table_name, "_event_name_");
5338 if (s == table_name)
5339 continue;
5340 s = strstr(table_name, "_column_names_");
5341 if (s == table_name)
5342 continue;
5343
5344 status = ReadSqliteTableSchema(fSql, sv, table_name, fDebug);
5345 }
5346
5347 return HS_SUCCESS;
5348}
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 5536 of file history_schema.cxx.

5537{
5538 if (fDebug)
5539 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());
5540
5541 int status = StartSqlTransaction(fSql, table_name, have_transaction);
5542 if (status != HS_SUCCESS)
5543 return status;
5544
5545 // FIXME: quotes
5546 std::string cmd;
5547 cmd = "INSERT INTO \'_column_names_";
5548 cmd += table_name;
5549 cmd += "\' (table_name, column_name, tag_name, tag_type, column_type, _i_time) VALUES (\'";
5550 cmd += table_name;
5551 cmd += "\', \'";
5552 cmd += column_name;
5553 cmd += "\', \'";
5554 cmd += tag_name;
5555 cmd += "\', \'";
5556 cmd += tag_type;
5557 cmd += "\', \'";
5558 cmd += column_type;
5559 cmd += "\', \'";
5560 cmd += TimeToString(timestamp);
5561 cmd += "\');";
5562 status = fSql->Exec(table_name, cmd.c_str());
5563
5564 return status;
5565}
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: