5045{
5047
5049 printf("update_schema1\n");
5050
5051
5052
5053 bool schema_ok = true;
5054
5056 for (
int i=0;
i<ntags;
i++) {
5057 for (
unsigned int j=0;
j<tags[
i].
n_data;
j++) {
5058 int tagtype = tags[
i].
type;
5059 std::string tagname = tags[
i].
name;
5061
5062 if (tags[
i].n_data > 1) {
5063 char s[256];
5064 sprintf(s,
"[%d]",
j);
5065 tagname += s;
5066
5067 sprintf(s,
"_%d",
j);
5068 maybe_colname += s;
5069 }
5070
5072
5074
5076 continue;
5082 }
5087 }
5088 } else {
5089
5090 schema_ok = false;
5092 printf("Incompatible column!\n");
5093 if (write_enable) {
5096
5100 }
5101 }
5102 }
5103 }
5104
5106
5107 schema_ok = false;
5109 printf("No column for tag %s!\n", tagname.c_str());
5110
5111 bool found_column = false;
5112
5113 if (write_enable) {
5117 if (typeok) {
5120
5124
5128 }
5130 found_column = true;
5134 }
5135 }
5136 }
5137 }
5138 }
5139
5140
5141 if (!found_column && write_enable) {
5142 std::string col_name = maybe_colname;
5144
5145 bool dupe = false;
5148 dupe = true;
5149 break;
5150 }
5151
5152 time_t now = time(NULL);
5153
5154 bool retry = false;
5155 for (int t=0; t<20; t++) {
5156
5157
5158 if (dupe || retry) {
5159 col_name = maybe_colname;
5160 col_name += "_";
5162 if (t > 0) {
5163 char s[256];
5164 sprintf(s, "_%d", t);
5165 col_name += s;
5166 }
5167 }
5168
5170 printf(
"SqlHistory::update_schema: table [%s], add column [%s] type [%s] for tag [%s]\n", s->
fTableName.c_str(), col_name.c_str(), col_type, tagname.c_str());
5171
5173
5176 printf(
"SqlHistory::update_schema: table [%s], add column [%s] type [%s] for tag [%s] failed: duplicate column name\n", s->
fTableName.c_str(), col_name.c_str(), col_type, tagname.c_str());
5177 retry = true;
5178 continue;
5179 }
5180
5183
5184 break;
5185 }
5186
5189
5193 }
5194 }
5195
5197
5198 schema_ok = false;
5199 cm_msg(
MERROR,
"SqlHistory::update_schema",
"Duplicate tags or SQL columns for history event \"%s\" tag \"%s\"", s->
fEventName.c_str(), tagname.c_str());
5201 }
5202 }
5203 }
5204
5205
5206
5209 bool found = false;
5210
5211 for (
int i=0;
i<ntags;
i++) {
5212 for (
unsigned int j=0;
j<tags[
i].
n_data;
j++) {
5213 std::string tagname = tags[
i].
name;
5214
5215 if (tags[
i].n_data > 1) {
5216 char s[256];
5217 sprintf(s,
"[%d]",
j);
5218 tagname += s;
5219 }
5220
5222 found = true;
5223 break;
5224 }
5225 }
5226
5227 if (found)
5228 break;
5229 }
5230
5231 if (!found) {
5232
5233 schema_ok = false;
5236 if (write_enable) {
5239
5243 }
5244 }
5245 }
5246
5247 if (!write_enable)
5248 if (!schema_ok) {
5250 printf("Return error!\n");
5252 }
5253
5255}
std::vector< int > fOffsets
std::vector< std::string > fColumnNames
std::vector< std::string > fColumnTypes
std::vector< bool > fColumnInactive
virtual bool TypesCompatible(int midas_tid, const char *sql_type)=0
virtual const char * ColumnType(int midas_tid)=0
virtual 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)=0
INT cm_msg_flush_buffer()
const char * rpc_tid_name(INT id)
static std::string MidasNameToSqlName(const char *s)
static int CreateSqlColumn(SqlBase *sql, const char *table_name, const char *column_name, const char *column_type, bool *have_transaction, int debug)