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

Constructor & Destructor Documentation

◆ SqliteHistory()

SqliteHistory::SqliteHistory ( )
inline

Definition at line 5216 of file history_schema.cxx.

5216 { // ctor
5217#ifdef HAVE_SQLITE
5218 fSql = new Sqlite();
5219#endif
5220 }
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 5381 of file history_schema.cxx.

5382{
5383 if (fDebug)
5384 printf("SqliteHistory::create_table: event [%s], timestamp %s\n", event_name, TimeToString(timestamp).c_str());
5385
5386 int status;
5387 bool have_transaction = false;
5388 std::string table_name = MidasNameToSqlName(event_name);
5389
5390 // FIXME: what about duplicate table names?
5391 status = CreateSqlTable(fSql, table_name.c_str(), &have_transaction);
5392
5393 //if (status == DB_KEY_EXIST) {
5394 // return ReadSqliteTableSchema(fSql, sv, table_name.c_str(), fDebug);
5395 //}
5396
5397 if (status != HS_SUCCESS) {
5398 // FIXME: ???
5399 // FIXME: at least close or revert the transaction
5400 return status;
5401 }
5402
5403 std::string cmd;
5404
5405 std::string en;
5406 en += "_event_name_";
5407 en += table_name;
5408
5409 cmd = "CREATE TABLE ";
5410 cmd += fSql->QuoteId(en.c_str());
5411 cmd += " (table_name TEXT NOT NULL, event_name TEXT NOT NULL, _i_time INTEGER NOT NULL);";
5412
5413 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5414
5415 cmd = "INSERT INTO ";
5416 cmd += fSql->QuoteId(en.c_str());
5417 cmd += " (table_name, event_name, _i_time) VALUES (";
5418 cmd += fSql->QuoteString(table_name.c_str());
5419 cmd += ", ";
5420 cmd += fSql->QuoteString(event_name);
5421 cmd += ", ";
5422 cmd += fSql->QuoteString(TimeToString(timestamp).c_str());
5423 cmd += ");";
5424
5425 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5426
5427 std::string cn;
5428 cn += "_column_names_";
5429 cn += table_name;
5430
5431 cmd = "CREATE TABLE ";
5432 cmd += fSql->QuoteId(cn.c_str());
5433 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);";
5434
5435 status = fSql->Exec(table_name.c_str(), cmd.c_str());
5436
5437 status = fSql->CommitTransaction(table_name.c_str());
5438 if (status != DB_SUCCESS) {
5439 return HS_FILE_ERROR;
5440 }
5441
5442 return ReadSqliteTableSchema(fSql, sv, table_name.c_str(), fDebug);
5443}
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 5259 of file history_schema.cxx.

5260{
5261 if (fDebug)
5262 printf("SqliteHistory::read_column_names: table [%s], event [%s]\n", table_name, event_name);
5263
5264 // for all schema for table_name, prepopulate is with column names
5265
5266 std::vector<std::string> columns;
5267 fSql->ListColumns(table_name, &columns);
5268
5269 // first, populate column names
5270
5271 for (unsigned i=0; i<sv->size(); i++) {
5272 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5273
5274 if (s->fTableName != table_name)
5275 continue;
5276
5277 // schema should be empty at this point
5278 //assert(s->fVariables.size() == 0);
5279
5280 for (unsigned j=0; j<columns.size(); j+=2) {
5281 const char* cn = columns[j+0].c_str();
5282 const char* ct = columns[j+1].c_str();
5283
5284 if (strcmp(cn, "_t_time") == 0)
5285 continue;
5286 if (strcmp(cn, "_i_time") == 0)
5287 continue;
5288
5289 bool found = false;
5290
5291 for (unsigned k=0; k<s->fColumnNames.size(); k++) {
5292 if (s->fColumnNames[k] == cn) {
5293 found = true;
5294 break;
5295 }
5296 }
5297
5298 //printf("column [%s] sql type [%s]\n", cn.c_str(), ct);
5299
5300 if (!found) {
5302 se.name = cn;
5303 se.type = 0;
5304 se.n_data = 1;
5305 se.n_bytes = 0;
5306 s->fVariables.push_back(se);
5307 s->fColumnNames.push_back(cn);
5308 s->fColumnTypes.push_back(ct);
5309 s->fColumnInactive.push_back(false);
5310 s->fOffsets.push_back(-1);
5311 }
5312 }
5313 }
5314
5315 // then read column name information
5316
5317 std::string tn;
5318 tn += "_column_names_";
5319 tn += table_name;
5320
5321 std::string cmd;
5322 cmd = "SELECT column_name, tag_name, tag_type, _i_time FROM ";
5323 cmd += fSql->QuoteId(tn.c_str());
5324 cmd += " WHERE table_name=";
5325 cmd += fSql->QuoteString(table_name);
5326 cmd += " ORDER BY _i_time ASC;";
5327
5328 int status = fSql->Prepare(table_name, cmd.c_str());
5329
5330 if (status != DB_SUCCESS) {
5331 return status;
5332 }
5333
5334 while (1) {
5335 status = fSql->Step();
5336
5337 if (status != DB_SUCCESS)
5338 break;
5339
5340 // NOTE: SQL "SELECT ORDER BY _i_time ASC" returns data sorted by time
5341 // in this code we use the data from the last data row
5342 // so if multiple rows are present, the latest one is used
5343
5344 std::string col_name = fSql->GetText(0);
5345 std::string tag_name = fSql->GetText(1);
5346 std::string tag_type = fSql->GetText(2);
5348
5349 //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());
5350
5351 // make sure a schema exists at this time point
5352 NewSqlSchema(sv, table_name, schema_time);
5353
5354 // add this information to all schema
5355
5356 for (unsigned i=0; i<sv->size(); i++) {
5357 HsSqlSchema* s = (HsSqlSchema*)(*sv)[i];
5358 if (s->fTableName != table_name)
5359 continue;
5360 if (s->fTimeFrom < schema_time)
5361 continue;
5362
5363 //printf("add column to schema %d\n", s->fTimeFrom);
5364
5365 for (unsigned j=0; j<s->fColumnNames.size(); j++) {
5366 if (col_name != s->fColumnNames[j])
5367 continue;
5368 s->fVariables[j].name = tag_name;
5369 s->fVariables[j].type = rpc_name_tid(tag_type.c_str());
5370 s->fVariables[j].n_data = 1;
5371 s->fVariables[j].n_bytes = rpc_tid_size(s->fVariables[j].type);
5372 }
5373 }
5374 }
5375
5376 status = fSql->Finalize();
5377
5378 return HS_SUCCESS;
5379}
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 5228 of file history_schema.cxx.

5229{
5230 int status;
5231
5232 if (fDebug)
5233 printf("SqliteHistory::read_table_and_event_names!\n");
5234
5235 // loop over all tables
5236
5237 std::vector<std::string> tables;
5239 if (status != DB_SUCCESS)
5240 return status;
5241
5242 for (unsigned i=0; i<tables.size(); i++) {
5243 const char* table_name = tables[i].c_str();
5244
5245 const char* s;
5246 s = strstr(table_name, "_event_name_");
5247 if (s == table_name)
5248 continue;
5249 s = strstr(table_name, "_column_names_");
5250 if (s == table_name)
5251 continue;
5252
5253 status = ReadSqliteTableSchema(fSql, sv, table_name, fDebug);
5254 }
5255
5256 return HS_SUCCESS;
5257}
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 5445 of file history_schema.cxx.

5446{
5447 if (fDebug)
5448 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());
5449
5450 int status = StartSqlTransaction(fSql, table_name, have_transaction);
5451 if (status != HS_SUCCESS)
5452 return status;
5453
5454 // FIXME: quotes
5455 std::string cmd;
5456 cmd = "INSERT INTO \'_column_names_";
5457 cmd += table_name;
5458 cmd += "\' (table_name, column_name, tag_name, tag_type, column_type, _i_time) VALUES (\'";
5459 cmd += table_name;
5460 cmd += "\', \'";
5461 cmd += column_name;
5462 cmd += "\', \'";
5463 cmd += tag_name;
5464 cmd += "\', \'";
5465 cmd += tag_type;
5466 cmd += "\', \'";
5467 cmd += column_type;
5468 cmd += "\', \'";
5469 cmd += TimeToString(timestamp);
5470 cmd += "\');";
5471 status = fSql->Exec(table_name, cmd.c_str());
5472
5473 return status;
5474}
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: