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

Constructor & Destructor Documentation

◆ SqliteHistory()

SqliteHistory::SqliteHistory ( )
inline

Definition at line 5212 of file history_schema.cxx.

5212 { // ctor
5213#ifdef HAVE_SQLITE
5214 fSql = new Sqlite();
5215#endif
5216 }
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 5377 of file history_schema.cxx.

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

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

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

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