MIDAS
Loading...
Searching...
No Matches
HsSqlSchema Struct Reference
Inheritance diagram for HsSqlSchema:
Collaboration diagram for HsSqlSchema:

Public Member Functions

 HsSqlSchema ()
 
 ~HsSqlSchema ()
 
void print (bool print_tags=true) const
 
int get_transaction_count ()
 
void reset_transaction_count ()
 
void increment_transaction_count ()
 
int close_transaction ()
 
int flush_buffers ()
 
int close ()
 
int write_event (const time_t t, const char *data, const int data_size)
 
int match_event_var (const char *event_name, const char *var_name, const int var_index)
 
int read_last_written (const time_t timestamp, const int debug, time_t *last_written)
 
int read_data (const time_t start_time, const time_t end_time, const int num_var, const std::vector< int > &var_schema_index, const int var_index[], const int debug, std::vector< time_t > &last_time, MidasHistoryBufferInterface *buffer[])
 
- Public Member Functions inherited from HsSchema
 HsSchema ()
 
virtual ~HsSchema ()
 

Public Attributes

SqlBasesql
 
std::vector< std::string > disconnected_buffer
 
std::string table_name
 
std::vector< std::string > column_names
 
std::vector< std::string > column_types
 
- Public Attributes inherited from HsSchema
std::string event_name
 
time_t time_from = 0
 
time_t time_to = 0
 
std::vector< HsSchemaEntryvariables
 
std::vector< intoffsets
 
int n_bytes = 0
 
int count_write_undersize = 0
 
int count_write_oversize = 0
 
int write_max_size = 0
 
int write_min_size = 0
 
bool disabled = true
 

Private Attributes

int table_transaction_count
 

Static Private Attributes

static std::map< SqlBase *, intglobal_transaction_count
 

Detailed Description

Definition at line 693 of file history_schema.cxx.

Constructor & Destructor Documentation

◆ HsSqlSchema()

HsSqlSchema::HsSqlSchema ( )
inline

Definition at line 700 of file history_schema.cxx.

701 {
702 sql = 0;
704 }

◆ ~HsSqlSchema()

HsSqlSchema::~HsSqlSchema ( )
inline

Definition at line 706 of file history_schema.cxx.

707 {
708 assert(get_transaction_count() == 0);
709 }
int get_transaction_count()
Here is the call graph for this function:

Member Function Documentation

◆ close()

int HsSqlSchema::close ( )
inlinevirtual

Implements HsSchema.

Definition at line 717 of file history_schema.cxx.

717{ return close_transaction(); }
Here is the call graph for this function:

◆ close_transaction()

int HsSqlSchema::close_transaction ( )

Definition at line 4027 of file history_schema.cxx.

4028{
4029 if (!sql->IsConnected()) {
4030 return HS_SUCCESS;
4031 }
4032
4033 int status = HS_SUCCESS;
4034 if (get_transaction_count() > 0) {
4037 }
4038 return status;
4039}
virtual bool IsConnected()=0
virtual int CommitTransaction(const char *table_name)=0
#define HS_SUCCESS
Definition midas.h:727
DWORD status
Definition odbhist.cxx:39
std::string table_name
void reset_transaction_count()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush_buffers()

int HsSqlSchema::flush_buffers ( )
inlinevirtual

Implements HsSchema.

Definition at line 716 of file history_schema.cxx.

716{ return close_transaction(); }
Here is the call graph for this function:

◆ get_transaction_count()

int HsSqlSchema::get_transaction_count ( )

Definition at line 4405 of file history_schema.cxx.

4405 {
4406 if (!sql || sql->fTransactionPerTable) {
4408 } else {
4410 }
4411}
bool fTransactionPerTable
static std::map< SqlBase *, int > global_transaction_count
Here is the caller graph for this function:

◆ increment_transaction_count()

void HsSqlSchema::increment_transaction_count ( )

Definition at line 4421 of file history_schema.cxx.

4421 {
4422 if (!sql || sql->fTransactionPerTable) {
4424 } else {
4426 }
4427}
Here is the caller graph for this function:

◆ match_event_var()

int HsSqlSchema::match_event_var ( const char event_name,
const char var_name,
const int  var_index 
)
virtual

Reimplemented from HsSchema.

Definition at line 4059 of file history_schema.cxx.

4060{
4061 if (event_name_cmp(this->table_name, event_name)==0) {
4062 for (unsigned j=0; j<this->variables.size(); j++) {
4063 if (var_name_cmp(this->column_names[j], var_name)==0)
4064 return j;
4065 }
4066 }
4067
4068 return HsSchema::match_event_var(event_name, var_name, var_index);
4069}
static int var_name_cmp(const std::string &v1, const char *v2)
static int event_name_cmp(const std::string &e1, const char *e2)
INT j
Definition odbhist.cxx:40
char var_name[256]
Definition odbhist.cxx:41
std::string event_name
std::vector< HsSchemaEntry > variables
virtual int match_event_var(const char *event_name, const char *var_name, const int var_index)
std::vector< std::string > column_names
Here is the call graph for this function:

◆ print()

void HsSqlSchema::print ( bool  print_tags = true) const
virtual

Reimplemented from HsSchema.

Definition at line 797 of file history_schema.cxx.

798{
799 unsigned nv = this->variables.size();
800 printf("event [%s], sql_table [%s], time %s..%s, %d variables, %d bytes\n", this->event_name.c_str(), this->table_name.c_str(), TimeToString(this->time_from).c_str(), TimeToString(this->time_to).c_str(), nv, n_bytes);
801 if (print_tags) {
802 for (unsigned j=0; j<nv; j++) {
803 printf(" %d: name [%s], type [%s] tid %d, n_data %d, n_bytes %d", j, this->variables[j].name.c_str(), rpc_tid_name(this->variables[j].type), this->variables[j].type, this->variables[j].n_data, this->variables[j].n_bytes);
804 printf(", sql_column [%s], sql_type [%s], offset %d", this->column_names[j].c_str(), this->column_types[j].c_str(), this->offsets[j]);
805 printf(", inactive %d", this->variables[j].inactive);
806 printf("\n");
807 }
808 }
809}
const char * rpc_tid_name(INT id)
Definition midas.cxx:11764
static std::string TimeToString(time_t t)
INT type
Definition mana.cxx:269
#define name(x)
Definition midas_macro.h:24
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
std::vector< int > offsets
std::vector< std::string > column_types
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_data()

int HsSqlSchema::read_data ( const time_t  start_time,
const time_t  end_time,
const int  num_var,
const std::vector< int > &  var_schema_index,
const int  var_index[],
const int  debug,
std::vector< time_t > &  last_time,
MidasHistoryBufferInterface buffer[] 
)
virtual

Implements HsSchema.

Definition at line 4316 of file history_schema.cxx.

4322{
4323 bool bad_last_time = false;
4324
4325 if (debug)
4326 printf("SqlHistory::read_data: table [%s], start %s, end %s\n", table_name.c_str(), TimeToString(start_time).c_str(), TimeToString(end_time).c_str());
4327
4328 std::string collist;
4329
4330 for (int i=0; i<num_var; i++) {
4331 int j = var_schema_index[i];
4332 if (j < 0)
4333 continue;
4334 if (collist.length() > 0)
4335 collist += ", ";
4337 }
4338
4339 std::string cmd;
4340 cmd += "SELECT _i_time, ";
4341 cmd += collist;
4342 cmd += " FROM ";
4343 cmd += sql->QuoteId(table_name.c_str());
4344 cmd += " WHERE _i_time>=";
4345 cmd += TimeToString(start_time);
4346 cmd += " and _i_time<=";
4347 cmd += TimeToString(end_time);
4348 cmd += " ORDER BY _i_time;";
4349
4350 int status = sql->Prepare(table_name.c_str(), cmd.c_str());
4351
4352 if (status != DB_SUCCESS)
4353 return HS_FILE_ERROR;
4354
4355 /* Loop through the rows in the result-set */
4356
4357 int count = 0;
4358
4359 while (1) {
4360 status = sql->Step();
4361 if (status != DB_SUCCESS)
4362 break;
4363
4364 count++;
4365
4366 time_t t = sql->GetTime(0);
4367
4368 if (t < start_time || t > end_time)
4369 continue;
4370
4371 int k = 0;
4372
4373 for (int i=0; i<num_var; i++) {
4374 int j = var_schema_index[i];
4375 if (j < 0)
4376 continue;
4377
4378 if (t < last_time[i]) { // protect against duplicate and non-monotonous data
4379 bad_last_time = true;
4380 } else {
4381 double v = sql->GetDouble(1+k);
4382
4383 //printf("Column %d, index %d, Row %d, time %d, value %f\n", k, colindex[k], count, t, v);
4384
4385 buffer[i]->Add(t, v);
4386 last_time[i] = t;
4387 }
4388
4389 k++;
4390 }
4391 }
4392
4393 sql->Finalize();
4394
4395 if (bad_last_time) {
4396 cm_msg(MERROR, "SqlHistory::read_data", "Detected duplicate or non-monotonous data in table \"%s\" for start time %s and end time %s", table_name.c_str(), TimeToString(start_time).c_str(), TimeToString(end_time).c_str());
4397 }
4398
4399 if (debug)
4400 printf("SqlHistory::read_data: table [%s], start %s, end %s, read %d rows\n", table_name.c_str(), TimeToString(start_time).c_str(), TimeToString(end_time).c_str(), count);
4401
4402 return HS_SUCCESS;
4403}
virtual void Add(time_t time, double value)=0
virtual int Finalize()=0
virtual double GetDouble(int column)=0
virtual std::string QuoteId(const char *s)=0
virtual int Prepare(const char *table_name, const char *sql)=0
virtual time_t GetTime(int column)=0
virtual int Step()=0
#define DB_SUCCESS
Definition midas.h:631
#define HS_FILE_ERROR
Definition midas.h:728
#define MERROR
Definition midas.h:559
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
Definition midas.cxx:915
DWORD last_time
Definition mana.cxx:3070
BOOL debug
debug printouts
Definition mana.cxx:254
double count
Definition mdump.cxx:33
INT i
Definition mdump.cxx:32
INT k
Definition odbhist.cxx:40
Here is the call graph for this function:

◆ read_last_written()

int HsSqlSchema::read_last_written ( const time_t  timestamp,
const int  debug,
time_t last_written 
)
virtual

Implements HsSchema.

Definition at line 4269 of file history_schema.cxx.

4272{
4273 if (debug)
4274 printf("SqlHistory::read_last_written: table [%s], timestamp %s\n", table_name.c_str(), TimeToString(timestamp).c_str());
4275
4276 std::string cmd;
4277 cmd += "SELECT _i_time FROM ";
4278 cmd += sql->QuoteId(table_name.c_str());
4279 cmd += " WHERE _i_time < ";
4280 cmd += TimeToString(timestamp);
4281 cmd += " ORDER BY _i_time DESC LIMIT 2;";
4282
4283 int status = sql->Prepare(table_name.c_str(), cmd.c_str());
4284
4285 if (status != DB_SUCCESS)
4286 return status;
4287
4288 time_t lw = 0;
4289
4290 /* Loop through the rows in the result-set */
4291
4292 while (1) {
4293 status = sql->Step();
4294 if (status != DB_SUCCESS)
4295 break;
4296
4297 time_t t = sql->GetTime(0);
4298
4299 if (t >= timestamp)
4300 continue;
4301
4302 if (t > lw)
4303 lw = t;
4304 }
4305
4306 sql->Finalize();
4307
4308 *last_written = lw;
4309
4310 if (debug)
4311 printf("SqlHistory::read_last_written: table [%s], timestamp %s, last_written %s\n", table_name.c_str(), TimeToString(timestamp).c_str(), TimeToString(lw).c_str());
4312
4313 return HS_SUCCESS;
4314}
Here is the call graph for this function:

◆ reset_transaction_count()

void HsSqlSchema::reset_transaction_count ( )

Definition at line 4413 of file history_schema.cxx.

4413 {
4414 if (!sql || sql->fTransactionPerTable) {
4416 } else {
4418 }
4419}
Here is the caller graph for this function:

◆ write_event()

int HsSqlSchema::write_event ( const time_t  t,
const char data,
const int  data_size 
)
virtual

Implements HsSchema.

Definition at line 4145 of file history_schema.cxx.

4146{
4147 HsSqlSchema* s = this;
4148
4149 std::string tags;
4150 std::string values;
4151
4152 for (unsigned i=0; i<s->variables.size(); i++) {
4153 if (s->variables[i].inactive)
4154 continue;
4155
4156 int type = s->variables[i].type;
4157 int n_data = s->variables[i].n_data;
4158 int offset = s->offsets[i];
4159 const char* column_name = s->column_names[i].c_str();
4160
4161 if (offset < 0)
4162 continue;
4163
4164 assert(n_data == 1);
4165 assert(strlen(column_name) > 0);
4166 assert(offset < data_size);
4167
4168 void* ptr = (void*)(data+offset);
4169
4170 tags += ", ";
4171 tags += sql->QuoteId(column_name);
4172
4173 values += ", ";
4174
4175 char buf[1024];
4176 int j=0;
4177
4178 switch (type) {
4179 default:
4180 sprintf(buf, "unknownType%d", type);
4181 break;
4182 case TID_BYTE:
4183 sprintf(buf, "%u",((unsigned char *)ptr)[j]);
4184 break;
4185 case TID_SBYTE:
4186 sprintf(buf, "%d",((signed char*)ptr)[j]);
4187 break;
4188 case TID_CHAR:
4189 // FIXME: quotes
4190 sprintf(buf, "\'%c\'",((char*)ptr)[j]);
4191 break;
4192 case TID_WORD:
4193 sprintf(buf, "%u",((unsigned short *)ptr)[j]);
4194 break;
4195 case TID_SHORT:
4196 sprintf(buf, "%d",((short *)ptr)[j]);
4197 break;
4198 case TID_DWORD:
4199 sprintf(buf, "%u",((unsigned int *)ptr)[j]);
4200 break;
4201 case TID_INT:
4202 sprintf(buf, "%d",((int *)ptr)[j]);
4203 break;
4204 case TID_BOOL:
4205 sprintf(buf, "%u",((unsigned int *)ptr)[j]);
4206 break;
4207 case TID_FLOAT:
4208 // FIXME: quotes
4209 sprintf(buf, "\'%.8g\'",((float*)ptr)[j]);
4210 break;
4211 case TID_DOUBLE:
4212 // FIXME: quotes
4213 sprintf(buf, "\'%.16g\'",((double*)ptr)[j]);
4214 break;
4215 }
4216
4217 values += buf;
4218 }
4219
4220 // 2001-02-16 20:38:40.1
4221 struct tm tms;
4222 localtime_r(&t, &tms); // somebody must call tzset() before this.
4223 char buf[1024];
4224 strftime(buf, sizeof(buf)-1, "%Y-%m-%d %H:%M:%S.0", &tms);
4225
4226 std::string cmd;
4227 cmd = "INSERT INTO ";
4228 cmd += sql->QuoteId(s->table_name.c_str());
4229 cmd += " (_t_time, _i_time";
4230 cmd += tags;
4231 cmd += ") VALUES (";
4232 cmd += sql->QuoteString(buf);
4233 cmd += ", ";
4234 cmd += sql->QuoteString(TimeToString(t).c_str());
4235 cmd += "";
4236 cmd += values;
4237 cmd += ");";
4238
4239 if (sql->IsConnected()) {
4240 if (s->get_transaction_count() == 0)
4241 sql->OpenTransaction(s->table_name.c_str());
4242
4244
4245 int status = sql->Exec(s->table_name.c_str(), cmd.c_str());
4246
4247 // mh2sql who does not call hs_flush_buffers()
4248 // so we should flush the transaction by hand
4249 // some SQL engines have limited transaction buffers... K.O.
4250 if (s->get_transaction_count() > 100000) {
4251 //printf("flush table %s\n", table_name);
4252 sql->CommitTransaction(s->table_name.c_str());
4254 }
4255
4256 if (status != DB_SUCCESS) {
4257 return status;
4258 }
4259 } else {
4260 int status = sql->ExecDisconnected(s->table_name.c_str(), cmd.c_str());
4261 if (status != DB_SUCCESS) {
4262 return status;
4263 }
4264 }
4265
4266 return HS_SUCCESS;
4267}
virtual int Exec(const char *sql)=0
virtual std::string QuoteString(const char *s)=0
virtual int ExecDisconnected(const char *table_name, const char *sql)=0
virtual int OpenTransaction(const char *table_name)=0
#define TID_DOUBLE
Definition midas.h:343
#define TID_SBYTE
Definition midas.h:329
#define TID_BOOL
Definition midas.h:340
#define TID_SHORT
Definition midas.h:334
#define TID_WORD
Definition midas.h:332
#define TID_BYTE
Definition midas.h:327
#define TID_CHAR
Definition midas.h:331
#define TID_INT
Definition midas.h:338
#define TID_FLOAT
Definition midas.h:341
#define TID_DWORD
Definition midas.h:336
void * data
Definition mana.cxx:268
static int offset
Definition mgd.cxx:1500
MUTEX_T * tm
Definition odbedit.cxx:39
void increment_transaction_count()
Here is the call graph for this function:

Member Data Documentation

◆ column_names

std::vector<std::string> HsSqlSchema::column_names

Definition at line 697 of file history_schema.cxx.

◆ column_types

std::vector<std::string> HsSqlSchema::column_types

Definition at line 698 of file history_schema.cxx.

◆ disconnected_buffer

std::vector<std::string> HsSqlSchema::disconnected_buffer

Definition at line 695 of file history_schema.cxx.

◆ global_transaction_count

std::map< SqlBase *, int > HsSqlSchema::global_transaction_count
staticprivate

Definition at line 736 of file history_schema.cxx.

◆ sql

SqlBase* HsSqlSchema::sql

Definition at line 694 of file history_schema.cxx.

◆ table_name

std::string HsSqlSchema::table_name

Definition at line 696 of file history_schema.cxx.

◆ table_transaction_count

int HsSqlSchema::table_transaction_count
private

Definition at line 735 of file history_schema.cxx.


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