5022{
5024
5026 printf("update_schema1\n");
5027
5028
5029
5030 bool schema_ok = true;
5031
5033 for (
int i=0;
i<ntags;
i++) {
5034 for (
unsigned int j=0;
j<tags[
i].
n_data;
j++) {
5035 int tagtype = tags[
i].
type;
5036 std::string tagname = tags[
i].
name;
5038
5039 if (tags[
i].n_data > 1) {
5040 char s[256];
5041 sprintf(s,
"[%d]",
j);
5042 tagname += s;
5043
5044 sprintf(s,
"_%d",
j);
5045 maybe_colname += s;
5046 }
5047
5049
5051
5053 continue;
5059 }
5064 }
5065 } else {
5066
5067 schema_ok = false;
5069 printf("Incompatible column!\n");
5070 if (write_enable) {
5073
5077 }
5078 }
5079 }
5080 }
5081
5083
5084 schema_ok = false;
5086 printf("No column for tag %s!\n", tagname.c_str());
5087
5088 bool found_column = false;
5089
5090 if (write_enable) {
5094 if (typeok) {
5097
5101
5105 }
5107 found_column = true;
5111 }
5112 }
5113 }
5114 }
5115 }
5116
5117
5118 if (!found_column && write_enable) {
5119 std::string col_name = maybe_colname;
5121
5122 bool dupe = false;
5125 dupe = true;
5126 break;
5127 }
5128
5129 time_t now = time(NULL);
5130
5131 bool retry = false;
5132 for (int t=0; t<20; t++) {
5133
5134
5135 if (dupe || retry) {
5136 col_name = maybe_colname;
5137 col_name += "_";
5139 if (t > 0) {
5140 char s[256];
5141 sprintf(s, "_%d", t);
5142 col_name += s;
5143 }
5144 }
5145
5147 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());
5148
5150
5153 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());
5154 retry = true;
5155 continue;
5156 }
5157
5160
5161 break;
5162 }
5163
5166
5170 }
5171 }
5172
5174
5175 schema_ok = false;
5176 cm_msg(
MERROR,
"SqlHistory::update_schema",
"Duplicate tags or SQL columns for history event \"%s\" tag \"%s\"", s->
fEventName.c_str(), tagname.c_str());
5178 }
5179 }
5180 }
5181
5182
5183
5186 bool found = false;
5187
5188 for (
int i=0;
i<ntags;
i++) {
5189 for (
unsigned int j=0;
j<tags[
i].
n_data;
j++) {
5190 std::string tagname = tags[
i].
name;
5191
5192 if (tags[
i].n_data > 1) {
5193 char s[256];
5194 sprintf(s,
"[%d]",
j);
5195 tagname += s;
5196 }
5197
5199 found = true;
5200 break;
5201 }
5202 }
5203
5204 if (found)
5205 break;
5206 }
5207
5208 if (!found) {
5209
5210 schema_ok = false;
5213 if (write_enable) {
5216
5220 }
5221 }
5222 }
5223
5224 if (!write_enable)
5225 if (!schema_ok) {
5227 printf("Return error!\n");
5229 }
5230
5232}
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)