00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 char *module_name = "drms_addkey";
00038 char *version_id = "1.0";
00039
00040 #include <jsoc_main.h>
00041
00042 ModuleArgs_t module_args[] = {
00043 {ARG_STRING, "ds", "", "name of series to be modified"},
00044 {ARG_STRING, "key", "", "key name of column to be added"},
00045 {ARG_NUME, "type", "", "data type for column to be added",
00046 "string, short, int, longlong, float, double, time, link"},
00047 {ARG_STRING, "format", "Not Specified", "format for key value report"},
00048 {ARG_STRING, "unit", "Not Specified", "units for key value"},
00049 {ARG_STRING, "default", "Not Specified", "default value"},
00050 {ARG_STRING, "desc", "Not Specified", "key description"},
00051 {ARG_STRING, "link", "Not Specified", "link target"},
00052 {ARG_STRING, "linkey", "Not Specified",
00053 "link target keyword (default: key)"},
00054 {ARG_FLAG, "c", "", "key is constant"},
00055 {ARG_FLAG, "n", "", "do not commit, just print SQL commands"},
00056 {ARG_FLAG, "v", "", "run verbose"},
00057 {ARG_END}
00058 };
00059
00060 int DoIt (void) {
00061 CmdParams_t *params = &cmdparams;
00062 DB_Text_Result_t *qres;
00063 unsigned int seqnum;
00064 int rowct;
00065 char *ns;
00066 char *sqltyp[] = {"TEXT", "SMALLINT", "INTEGER", "BIGINT", "REAL",
00067 "DOUBLE PRECISION", "DOUBLE PRECISION", "*link*"};
00068 char *drmstype[] = {"string", "short", "int", "longlong", "float",
00069 "double", "time", "int"};
00070 char *def_fmt[] = {"%s", "%d", "%d", "%lld", "%g", "%lg", "0", "0"};
00071 char *def_val[] = {"", "-32768", "-2147483648", "-18446744073709551616LL",
00072 "NaN", "NaN", "NaN", ""};
00073 char cmd[DRMS_MAXQUERYLEN];
00074 char warning[128], module_ident[64], defstr[32];
00075
00076 char *ds = strdup (params_get_str (params, "ds"));
00077 char *key = strdup (params_get_str (params, "key"));
00078 char *fmt = strdup (params_get_str (params, "format"));
00079 char *unit = strdup (params_get_str (params, "unit"));
00080 char *desc = strdup (params_get_str (params, "desc"));
00081 char *defval = strdup (params_get_str (params, "default"));
00082 char *link = strdup (params_get_str (params, "link"));
00083 char *linkey = strdup (params_get_str (params, "linkey"));
00084 int typval = params_get_int (params, "type");
00085 int fmt_provided = strcmp (fmt, "Not Specified");
00086 int unit_provided = strcmp (unit, "Not Specified");
00087 int desc_provided = strcmp (desc, "Not Specified");
00088 int def_provided = strcmp (defval, "Not Specified");
00089 int link_provided = strcmp (link, "Not Specified");
00090 int linkey_provided = strcmp (linkey, "Not Specified");
00091 int isconstant = params_isflagset (params, "c") ? 1 : 0;
00092 int commit = params_isflagset (params, "n") ? 0 : 1;
00093 int verbose = params_isflagset (params, "v");
00094 int islink = (typval == 7);
00095
00096 snprintf (module_ident, 64, "%s v %s", module_name, version_id);
00097 if (verbose) printf ("%s:\n", module_ident);
00098
00099 if (strchr (ds, '.')) {
00100 ns = strtok (ds, ".");
00101 ds = strdup (params_get_str (params, "ds"));
00102 } else {
00103 fprintf (stderr, "Error: %s is not a valid series name: no namespace\n", ds);
00104 return 1;
00105 }
00106
00107 if (drms_series_isreplicated (drms_env, ds)) {
00108 fprintf (stderr, "Series %s is in slony replication;\n", ds);
00109 fprintf (stderr, " its structure must not be altered\n");
00110 return 1;
00111 }
00112
00113 sprintf (cmd, "SELECT keywordname FROM %s.drms_keyword ", ns);
00114 sprintf (cmd, "%s WHERE seriesname ILIKE \'%s\' ", cmd, ds);
00115 sprintf (cmd, "%s AND keywordname ILIKE \'%s\'", cmd, key);
00116 if ((qres = drms_query_txt (drms_env->session, cmd)) == NULL) {
00117 fprintf (stderr, "Error: can\'t connect to DRMS database\n");
00118 return 1;
00119 }
00120 if (qres->num_rows) {
00121 printf ("series %s already contains keyword %s\n", ds, qres->field[0][0]);
00122 return 0;
00123 }
00124
00125 sprintf (cmd, "SELECT max(persegment) FROM %s.drms_keyword ", ns);
00126 sprintf (cmd, "%s WHERE seriesname ILIKE \'%s\' ", cmd, ds);
00127 if ((qres = drms_query_txt (drms_env->session, cmd)) == NULL) {
00128 fprintf (stderr,
00129 "Error: could not get maximum sequence number from %s.drms_keyword\n",
00130 ns);
00131 return 1;
00132 }
00133 seqnum = atoi (qres->field[0][0]);
00134
00135 seqnum += 0x10000;
00136
00137 seqnum &= 0xffff0000;
00138
00139 if (!fmt_provided && !islink) {
00140 fprintf (stderr, "Warning: format unspecified; using %s\n", def_fmt[typval]);
00141 fprintf (stderr, " alter by updating %s.drms_keyword later\n", ns);
00142 fmt = def_fmt[typval];
00143 fmt_provided = 1;
00144 }
00145 if (!unit_provided && !islink) {
00146 fprintf (stderr, "Warning: unit unspecified;");
00147 if (typval == 6) {
00148 fprintf (stderr, " using UT;");
00149 unit = strdup ("UT");
00150 unit_provided = 1;
00151 }
00152 fprintf (stderr, "\n");
00153 fprintf (stderr, " alter by updating %s.drms_keyword later\n", ns);
00154 }
00155 if (!def_provided && !islink) {
00156 if (isconstant) {
00157 fprintf (stderr,
00158 "Error: a default value must be specified for a constant key\n");
00159 return 1;
00160 }
00161 if (typval == 6) {
00162 sprint_time (defstr, DRMS_MISSING_DOUBLE, unit, atoi (fmt));
00163 sprintf (warning, "Warning: using default value of %s\n", defstr);
00164 } else {
00165 fprintf (stderr, "Warning: default unspecified; using default value of \'%s\'\n", def_val[typval]);
00166 sprintf (defstr, "%s", def_val[typval]);
00167 }
00168 fprintf (stderr, " alter by updating %s.drms_keyword later\n", ns);
00169 defval = defstr;
00170 }
00171 if (islink) {
00172 if (isconstant) {
00173 fprintf (stderr, "Error: a link keyword cannot be constant\n");
00174 return 1;
00175 }
00176
00177
00178 if (!link_provided) {
00179 fprintf (stderr, "Error: a link must be provided for a linked keyword\n");
00180 return 1;
00181 }
00182 if (!linkey_provided) {
00183 fprintf (stderr,
00184 "Warning: link key name unspecified; using default value of %s\n",
00185 key);
00186 linkey = strdup (key);
00187 }
00188
00189 unit_provided = 0;
00190 fmt_provided = 0;
00191 }
00192
00193 if (verbose) {
00194 if (isconstant) {
00195 printf ("adding constant key: %s (%s) = \n", key, sqltyp[typval]);
00196 } else printf ("adding key: %s (%s)\n", key, sqltyp[typval]);
00197 }
00198
00199
00200 sprintf (cmd, "INSERT INTO %s.drms_keyword", ns);
00201 sprintf (cmd, "%s (seriesname, keywordname, type, islink, isconstant, persegment",
00202 cmd);
00203 if (islink) sprintf (cmd, "%s, linkname, targetkeyw", cmd);
00204 else sprintf (cmd, "%s, defaultval", cmd);
00205 if (unit_provided) sprintf (cmd, "%s, unit", cmd);
00206 if (fmt_provided) sprintf (cmd, "%s, format", cmd);
00207 if (desc_provided) sprintf (cmd, "%s, description", cmd);
00208 else fprintf (stderr,
00209 "Warning: description not specified; update %s.drms_keyword later\n", ns);
00210 sprintf (cmd,
00211 "%s) VALUES (\'%s\', \'%s\', \'%s\', %d, %d, %u",
00212 cmd, ds, key, drmstype[typval], islink, isconstant, seqnum);
00213 if (islink) sprintf (cmd, "%s, \'%s\', \'%s\'", cmd, link, linkey);
00214 else sprintf (cmd, "%s, \'%s\'", cmd, defval);
00215 if (unit_provided) sprintf (cmd, "%s, \'%s\'", cmd, unit);
00216 if (fmt_provided) sprintf (cmd, "%s, \'%s\'", cmd, fmt);
00217 if (desc_provided) sprintf (cmd, "%s, \'%s\'", cmd, desc);
00218 sprintf (cmd, "%s)", cmd);
00219 if (verbose || !commit) printf ("%s\n", cmd);
00220
00221 if (commit) {
00222 if (drms_dms (drms_env->session, &rowct, cmd)) {
00223 fprintf (stderr, "Error: unable to execute SQL command\n %s\n", cmd);
00224 return 1;
00225 }
00226 if (verbose) printf ("%d row(s) added to %s.drms_keyword\n", rowct, ns);
00227 }
00228
00229 if (isconstant) return 0;
00230
00231 if (islink) return 0;
00232 sprintf (cmd, "ALTER TABLE %s ADD COLUMN %s %s", ds, key, sqltyp[typval]);
00233 if (verbose || !commit) printf ("%s\n", cmd);
00234
00235
00236 if (commit) {
00237 if (drms_dms (drms_env->session, &rowct, cmd)) {
00238 fprintf (stderr, "Error: unable to execute SQL command\n %s\n", cmd);
00239 return 1;
00240 }
00241 if (verbose) printf ("column %s added to %s; %d rows updated\n", key, ds, rowct);
00242 }
00243
00244 return 0;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259