00001
00048 #include "jsoc_main.h"
00049
00050 char *module_name = "createtabstructure";
00051
00052 typedef enum
00053 {
00054 kCrtabErr_Success = 0,
00055 kCrtabErr_MissingSeriesName,
00056 kCrtabErr_DBQuery,
00057 kCrtabErr_Argument,
00058 kCrtabErr_FileIO,
00059 kCrtabErr_UnknownSeries,
00060 kCrtabErr_OutOfMemory,
00061 kCrtabErr_Internal
00062 } CrtabError_t;
00063
00064 #define kSeriesin "in"
00065 #define kSeriesout "out"
00066 #define kOwner "owner"
00067 #define kArchive "archive"
00068 #define kRetention "retention"
00069 #define kTapegroup "tapegroup"
00070 #define kFile "file"
00071 #define kNotSpec "NOTSPECIFIED"
00072 #define kFlagUTF8 "u"
00073
00074 ModuleArgs_t module_args[] =
00075 {
00076 {ARG_STRING, kSeriesin, kNotSpec, "Input series used to make SQL that generates structure."},
00077 {ARG_STRING, kSeriesout, kNotSpec, "Output series for which output SQL will generate structure."},
00078 {ARG_STRING, kOwner, kNotSpec, "Series owner (database role) override."},
00079 {ARG_STRING, kArchive, kNotSpec, "Series archive value override."},
00080 {ARG_STRING, kRetention, kNotSpec, "Series retetnion value override."},
00081 {ARG_STRING, kTapegroup, kNotSpec, "Series tapgegroup value override."},
00082 {ARG_STRING, kFile, kNotSpec, "Optional output file (output printed to stdout otherwise)."},
00083 {ARG_FLAG, kFlagUTF8, NULL, "If set, output UTF8 text, otherwise output text in database encoding."},
00084 {ARG_END}
00085 };
00086
00087
00088 static char *replacestr(char *str, int size, const char *orig, const char *rep)
00089 {
00090 char *buffer = NULL;
00091 char *pc = NULL;
00092 char *ret = str;
00093
00094 buffer = malloc(size);
00095
00096 if (buffer)
00097 {
00098 if ((pc = strstr(str, orig)))
00099 {
00100 if (pc - str < size)
00101 {
00102 snprintf(buffer, pc - str + 1, "%s", str);
00103 }
00104
00105 buffer[pc - str] = '\0';
00106 snprintf(buffer + (pc - str), size - (pc - str), "%s%s", rep, pc + strlen(orig));
00107 snprintf(str, size, "%s", buffer);
00108 ret = pc + strlen(rep);
00109 }
00110
00111 free(buffer);
00112 }
00113
00114 return ret;
00115 }
00116
00117 static int EscapeQuotes(const char *orig, char *out, int szout)
00118 {
00119 const char *pin = orig;
00120 int istr;
00121 int err = 0;
00122
00123 if (orig)
00124 {
00125 for (istr = 0; *pin && istr < szout - 1; istr++, pin++)
00126 {
00127 if (*pin == '\'')
00128 {
00129 if (istr > szout - 3)
00130 {
00131 err = 1;
00132 break;
00133 }
00134
00135 out[istr++] = '\\';
00136 }
00137
00138 out[istr] = *pin;
00139 }
00140
00141 if (istr >= szout)
00142 {
00143 err = 1;
00144 }
00145 else
00146 {
00147 out[istr] = '\0';
00148 }
00149 }
00150
00151 return err;
00152 }
00153
00154 static CrtabError_t CrtabGetTableOID(DRMS_Env_t *env, const char *ns, const char *table, char **oid)
00155 {
00156 char query[DRMS_MAXQUERYLEN];
00157 DB_Binary_Result_t *qres = NULL;
00158 DRMS_Session_t *session = env->session;
00159 CrtabError_t err = kCrtabErr_Success;
00160
00161 snprintf(query, sizeof(query), "SELECT c.oid, n.nspname, c.relname FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ '^(%s)$' AND n.nspname ~ '^(%s)$' ORDER BY 2, 3", table, ns);
00162
00163 if (!oid)
00164 {
00165 fprintf(stderr, "Missing required argument 'oid'.\n");
00166 err = kCrtabErr_Argument;
00167 }
00168 else if ((qres = drms_query_bin(session, query)) == NULL)
00169 {
00170 fprintf(stderr, "Invalid database query: '%s'\n", query);
00171 err = kCrtabErr_DBQuery;
00172 }
00173 else
00174 {
00175 if (qres->num_rows != 1)
00176 {
00177 fprintf(stderr, "Unexpected database response to query '%s'\n", query);
00178 err = kCrtabErr_DBQuery;
00179 }
00180 else
00181 {
00182
00183 char ioid[8];
00184 *oid = malloc(sizeof(char) * 64);
00185
00186
00187
00188
00189 memcpy(ioid, qres->column->data, 4);
00190
00191 #if __BYTE_ORDER == __LITTLE_ENDIAN
00192 db_byteswap(DB_INT4, 1, ioid);
00193 #endif
00194
00195 snprintf(*oid, 64, "%d", *((int *)ioid));
00196 db_free_binary_result(qres);
00197 }
00198 }
00199
00200 return err;
00201 }
00202
00203 static CrtabError_t GetColumnLists(DRMS_Env_t *env,
00204 const char *oid,
00205 char **collist,
00206 char **colnames)
00207 {
00208 CrtabError_t err = kCrtabErr_Success;
00209 char query[DRMS_MAXQUERYLEN];
00210 DB_Binary_Result_t *qres = NULL;
00211 DRMS_Session_t *session = env->session;
00212
00213 snprintf(query, sizeof(query), "SELECT a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod), (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) as defval, a.attnotnull FROM pg_catalog.pg_attribute a WHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum", oid);
00214
00215 if ((qres = drms_query_bin(session, query)) == NULL)
00216 {
00217 fprintf(stderr, "Invalid database query: '%s'\n", query);
00218 err = kCrtabErr_DBQuery;
00219 }
00220 else
00221 {
00222 if (qres->num_cols != 4)
00223 {
00224 fprintf(stderr, "Unexpected database response to query '%s'\n", query);
00225 err = kCrtabErr_DBQuery;
00226 }
00227 else
00228 {
00229 char *list = NULL;
00230 int irow;
00231 size_t strsize = DRMS_MAXQUERYLEN;
00232 char colname[512];
00233
00234 if (collist)
00235 {
00236 char buf[512];
00237 char dtype[512];
00238 char defval[512] = {0};
00239 char notnull[512] = {0};
00240
00241 list = malloc(sizeof(char) * strsize);
00242 memset(list, 0, sizeof(char) * strsize);
00243
00244 for (irow = 0; irow < qres->num_rows; irow++)
00245 {
00246 if (irow)
00247 {
00248 list = base_strcatalloc(list, ", ", &strsize);
00249 }
00250
00251
00252 db_binary_field_getstr(qres, irow, 0, sizeof(colname), colname);
00253 db_binary_field_getstr(qres, irow, 1, sizeof(dtype), dtype);
00254 db_binary_field_getstr(qres, irow, 2, sizeof(buf), buf);
00255
00256 *defval = '\0';
00257 *notnull = '\0';
00258
00259 if (strlen(buf) > 0)
00260 {
00261 snprintf(defval, sizeof(defval), "default %s", buf);
00262 }
00263
00264 db_binary_field_getstr(qres, irow, 3, sizeof(buf), buf);
00265
00266
00267
00268
00269 if ((int)(*((int *)buf)) == 1)
00270 {
00271 snprintf(notnull, sizeof(notnull), "not null");
00272 }
00273
00274 snprintf(buf, sizeof(buf), "%s %s %s %s", colname, dtype, defval, notnull);
00275 list = base_strcatalloc(list, buf, &strsize);
00276 }
00277
00278 *collist = list;
00279 list = NULL;
00280 }
00281
00282 if (colnames)
00283 {
00284 list = malloc(sizeof(char) * strsize);
00285 memset(list, 0, sizeof(char) * strsize);
00286
00287 for (irow = 0; irow < qres->num_rows; irow++)
00288 {
00289 if (irow)
00290 {
00291 list = base_strcatalloc(list, ", ", &strsize);
00292 }
00293
00294
00295 db_binary_field_getstr(qres, irow, 0, sizeof(colname), colname);
00296 list = base_strcatalloc(list, colname, &strsize);
00297 }
00298
00299 *colnames = list;
00300 list = NULL;
00301 }
00302
00303 db_free_binary_result(qres);
00304 }
00305 }
00306
00307 return err;
00308 }
00309
00310 static CrtabError_t GetRows(DRMS_Env_t *env,
00311 const char *series,
00312 const char *ns,
00313 const char *table,
00314 char *oid,
00315 char *colnames,
00316 int doUtf8,
00317 DB_Binary_Result_t **res)
00318 {
00319 CrtabError_t err = kCrtabErr_Success;
00320 char query[DRMS_MAXQUERYLEN * 2];
00321 DB_Binary_Result_t *qres = NULL;
00322 DRMS_Session_t *session = env->session;
00323 char *lcseries = strdup(series);
00324
00325 if (lcseries)
00326 {
00327 strtolower(lcseries);
00328
00329
00330
00331 if (doUtf8)
00332 {
00333
00334
00335
00336 char *thisCol = NULL;
00337 char colBuf[DRMS_MAXKEYNAMELEN + 64];
00338 char *wrappedColNames = NULL;
00339 size_t szWrappedColNames = 1024;
00340 int firstTime = 1;
00341
00342 wrappedColNames = calloc(1, szWrappedColNames);
00343 if (wrappedColNames)
00344 {
00345 for (thisCol = strtok(colnames, " ,"); thisCol; thisCol = strtok(NULL, " ,"))
00346 {
00347 if (!firstTime)
00348 {
00349 wrappedColNames = base_strcatalloc(wrappedColNames, ", ", &szWrappedColNames);
00350 }
00351 else
00352 {
00353 firstTime = 0;
00354 }
00355
00356
00357 snprintf(query, sizeof(query), "SELECT a.atttypid = 'text'::regtype::integer FROM pg_catalog.pg_attribute a WHERE a.attrelid = '%s' AND a.attname = '%s' AND NOT a.attisdropped", oid, thisCol);
00358 if ((qres = drms_query_bin(session, query)) == NULL)
00359 {
00360 fprintf(stderr, "Invalid database query: '%s'\n", query);
00361 err = kCrtabErr_DBQuery;
00362 }
00363 else
00364 {
00365 if (qres->num_rows == 1 && qres->num_cols == 1)
00366 {
00367 char resp = db_binary_field_getchar(qres, 0, 0);
00368 if (resp)
00369 {
00370
00371 snprintf(colBuf, sizeof(colBuf), "convert_to(%s, 'UTF8')", thisCol);
00372 }
00373 else
00374 {
00375 snprintf(colBuf, sizeof(colBuf), "%s", thisCol);
00376 }
00377 }
00378 else
00379 {
00380 err = kCrtabErr_DBQuery;
00381 }
00382
00383 db_free_binary_result(qres);
00384 qres = NULL;
00385
00386 if (err)
00387 {
00388 break;
00389 }
00390 }
00391
00392 wrappedColNames = base_strcatalloc(wrappedColNames, colBuf, &szWrappedColNames);
00393 }
00394
00395 if (!err)
00396 {
00397 snprintf(query, sizeof(query), "SELECT %s FROM %s.%s WHERE lower(seriesname) = '%s'", wrappedColNames, ns, table, lcseries);
00398 }
00399 }
00400 else
00401 {
00402 err = kCrtabErr_OutOfMemory;
00403 }
00404 }
00405 else
00406 {
00407 snprintf(query, sizeof(query), "SELECT %s FROM %s.%s WHERE lower(seriesname) = '%s'", colnames, ns, table, lcseries);
00408 }
00409
00410 if (!err)
00411 {
00412 if ((qres = drms_query_bin(session, query)) == NULL)
00413 {
00414 fprintf(stderr, "Invalid database query: '%s'\n", query);
00415 err = kCrtabErr_DBQuery;
00416 }
00417 else
00418 {
00419
00420 *res = qres;
00421 }
00422 }
00423
00424 free(lcseries);
00425 }
00426 else
00427 {
00428 err = kCrtabErr_OutOfMemory;
00429 }
00430
00431 return err;
00432 }
00433
00434 #if 0
00435 if (rowinslist)
00436 {
00437
00438
00439
00440
00441
00442 char colname[512];
00443
00444 list = malloc(sizeof(char) * strside);
00445 memset(list, 0, sizeof(char) * strsize);
00446 list = base_strcatalloc(list, "(", &strsize);
00447
00448 for (irow = 0; irow < qres->num_rows; irow++)
00449 {
00450 if (irow)
00451 {
00452 list = base_strcatalloc(list, ", ", &strsize);
00453 }
00454
00455
00456 db_binary_field_getstr(qres, irow, 0, sizeof(colname), colname);
00457 list = base_strcatalloc(list, colname, &strsize);
00458 }
00459
00460 list = base_strcatalloc(list, ") values (", &strsize);
00461
00462 for (irow = 0; irow < qres->num_rows; irow++)
00463 {
00464 if (irow)
00465 {
00466 list = base_strcatalloc(list, ",", &strsize);
00467 }
00468
00469 list = base_strcatalloc(list, "?", &strsize);
00470 }
00471
00472 list = base_strcatalloc(list, ")", &strsize);
00473
00474 *rowinslist = list;
00475 list = NULL;
00476 }
00477 #endif
00478
00479
00480 static CrtabError_t CreateSQLTable(FILE *fptr, const char *table, const char *collist)
00481 {
00482 CrtabError_t err = kCrtabErr_Success;
00483
00484
00485
00486 fprintf(fptr, "CREATE TABLE %s (%s);\n", table, collist);
00487
00488 return err;
00489 }
00490
00491 static CrtabError_t CreateSQLSequence(FILE *fptr, const char *table)
00492 {
00493 CrtabError_t err = kCrtabErr_Success;
00494 fprintf(fptr, "CREATE SEQUENCE %s_seq;\n", table);
00495 return err;
00496 }
00497
00498 static CrtabError_t CreateSQLIndices(FILE *fptr,
00499 DRMS_Env_t *env,
00500 const char *oid,
00501 const char *seriesin,
00502 const char *tablein,
00503 const char* seriesout,
00504 const char *tableout)
00505 {
00506 CrtabError_t err = kCrtabErr_Success;
00507 DRMS_Session_t *session = env->session;
00508 char query[DRMS_MAXQUERYLEN];
00509 char createbuf[DRMS_MAXQUERYLEN];
00510 DB_Binary_Result_t *qres = NULL;
00511 int irow;
00512
00513 snprintf(query, sizeof(query), "SELECT c2.relname, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true) FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid AND i.indisvalid = 't' ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", oid);
00514
00515 if ((qres = drms_query_bin(session, query)) == NULL)
00516 {
00517 fprintf(stderr, "Invalid database query: '%s'\n", query);
00518 err = kCrtabErr_DBQuery;
00519 }
00520 else
00521 {
00522 if (qres->num_cols != 2)
00523 {
00524 fprintf(stderr, "Unexpected database response to query '%s'\n", query);
00525 err = kCrtabErr_DBQuery;
00526 }
00527 else
00528 {
00529 char *pc = NULL;
00530 char *nextpc = NULL;
00531
00532 for (irow = 0; irow < qres->num_rows; irow++)
00533 {
00534
00535 db_binary_field_getstr(qres, irow, 1, sizeof(createbuf), createbuf);
00536
00537
00538
00539 if (strstr(createbuf, "(recnum)"))
00540 {
00541 continue;
00542 }
00543
00544
00545 if (strcmp(seriesin, seriesout) != 0)
00546 {
00547 pc = createbuf;
00548 while ((nextpc = replacestr(pc, sizeof(createbuf) + (pc - createbuf), seriesin, seriesout)) != pc)
00549 {
00550 pc = nextpc;
00551 }
00552 }
00553
00554 if (strcmp(tablein, tableout) != 0)
00555 {
00556 pc = createbuf;
00557 while ((nextpc = replacestr(pc, sizeof(createbuf) + (pc - createbuf), tablein, tableout)) != pc)
00558 {
00559 pc = nextpc;
00560 }
00561 }
00562
00563 fprintf(fptr, "%s;\n", createbuf);
00564 }
00565 }
00566
00567 db_free_binary_result(qres);
00568 }
00569
00570 return err;
00571 }
00572
00573 static CrtabError_t CreateSQLGrantPerms(FILE *fptr, DRMS_Env_t *env, const char *series, const char *ns, const char *owner)
00574 {
00575 CrtabError_t err = kCrtabErr_Success;
00576 DRMS_Session_t *session = env->session;
00577 char grantbuf[DRMS_MAXQUERYLEN];
00578 DB_Binary_Result_t *qres = NULL;
00579
00580 fprintf(fptr, "GRANT SELECT ON %s TO public;\n", series);
00581 fprintf(fptr, "GRANT SELECT ON %s_seq TO public;\n", series);
00582 fprintf(fptr, "GRANT DELETE ON %s TO sumsadmin;\n", series);
00583
00584 if (owner)
00585 {
00586 fprintf(fptr, "GRANT select, insert, update, delete ON %s TO %s;\n", series, owner);
00587 fprintf(fptr, "GRANT update ON %s_seq TO %s;\n", series, owner);
00588 }
00589 else
00590 {
00591 sprintf(grantbuf, "SELECT owner FROM admin.ns WHERE name = '%s'", ns);
00592 if ((qres = drms_query_bin(session, grantbuf)) == NULL)
00593 {
00594 fprintf(stderr, "Invalid database query: '%s'\n", grantbuf);
00595 err = kCrtabErr_DBQuery;
00596 }
00597 else
00598 {
00599 if (qres->num_rows != 1)
00600 {
00601 fprintf(stderr, "Unexpected database response to query '%s'\n", grantbuf);
00602 err = kCrtabErr_DBQuery;
00603 }
00604 else
00605 {
00606 char nsowner[512];
00607
00608 db_binary_field_getstr(qres, 0, 0, sizeof(nsowner), nsowner);
00609
00610 fprintf(fptr, "GRANT select, insert, update, delete ON %s TO %s;\n", series, nsowner);
00611 fprintf(fptr, "GRANT update ON %s_seq TO %s;\n", series, nsowner);
00612 }
00613
00614 db_free_binary_result(qres);
00615 }
00616 }
00617
00618 return err;
00619 }
00620
00621 static CrtabError_t CreateSQLInsertIntoTable(FILE *fptr,
00622 DRMS_Env_t *env,
00623 const char *series,
00624 const char *seriesout,
00625 const char *ns,
00626 const char *table,
00627 int doUtf8)
00628 {
00629 CrtabError_t err = kCrtabErr_Success;
00630 int irow;
00631 int icol;
00632 size_t strsize = 1024;
00633 char *list = NULL;
00634 char val[1024];
00635 char *oid = NULL;
00636 char *colnames = NULL;
00637 DB_Binary_Result_t *rows = NULL;
00638
00639 err = CrtabGetTableOID(env, ns, table, &oid);
00640
00641 if (!err)
00642 {
00643 err = GetColumnLists(env, oid, NULL, &colnames);
00644 }
00645
00646 err = GetRows(env, series, ns, table, oid, colnames, doUtf8, &rows);
00647
00648 for (irow = 0; irow < rows->num_rows; irow++)
00649 {
00650 list = malloc(sizeof(char) * strsize);
00651 memset(list, 0, sizeof(char) * strsize);
00652
00653 for (icol = 0; icol < rows->num_cols; icol++)
00654 {
00655 if (icol)
00656 {
00657 list = base_strcatalloc(list, ", ", &strsize);
00658 }
00659
00660 db_binary_field_getstr(rows, irow, icol, sizeof(val), val);
00661
00662 if (((rows->column)[icol]).type == DB_STRING)
00663 {
00664 char escval[1024];
00665
00666
00667 if (strcasecmp(val, series) == 0 && strcmp(val, seriesout) != 0)
00668 {
00669 snprintf(val, sizeof(val), "%s", seriesout);
00670 }
00671
00672
00673 if (EscapeQuotes(val, escval, sizeof(escval)))
00674 {
00675 err = kCrtabErr_Internal;
00676 break;
00677 }
00678
00679 list = base_strcatalloc(list, "E'", &strsize);
00680 list = base_strcatalloc(list, escval, &strsize);
00681 list = base_strcatalloc(list, "'", &strsize);
00682 }
00683 else
00684 {
00685 list = base_strcatalloc(list, val, &strsize);
00686 }
00687 }
00688
00689 fprintf(fptr, "INSERT INTO %s VALUES (%s);\n", table, list);
00690 free(list);
00691 }
00692
00693 if (oid)
00694 {
00695 free(oid);
00696 }
00697
00698 if (colnames)
00699 {
00700 free(colnames);
00701 }
00702
00703 if (rows)
00704 {
00705 db_free_binary_result(rows);
00706 }
00707
00708 return err;
00709 }
00710
00711 static CrtabError_t CreateSQLUpdateTable(FILE *fptr,
00712 DRMS_Env_t *env,
00713 const char *ns,
00714 const char *table,
00715 const char *colname,
00716 const char *colvalue,
00717 const char *where,
00718 const char *whereout)
00719 {
00720 CrtabError_t err = kCrtabErr_Success;
00721 DRMS_Session_t *session = env->session;
00722 char query[DRMS_MAXQUERYLEN];
00723 DB_Binary_Result_t *qres = NULL;
00724
00725
00726 snprintf(query, sizeof(query), "SELECT count(*) from %s.%s where %s", ns, table, where);
00727 if ((qres = drms_query_bin(session, query)) == NULL)
00728 {
00729 fprintf(stderr, "Invalid database query: '%s'\n", query);
00730 err = kCrtabErr_DBQuery;
00731 }
00732 else
00733 {
00734 if (qres->num_rows == 0)
00735 {
00736 fprintf(stderr, "where clause '%s' does not select a record.\n", where);
00737 err = kCrtabErr_DBQuery;
00738 }
00739 if (qres->num_rows != 1)
00740 {
00741 fprintf(stderr, "where clause '%s' selects more than one record.\n", where);
00742 err = kCrtabErr_DBQuery;
00743 }
00744 else
00745 {
00746 fprintf(fptr, "UPDATE %s SET %s = %s where %s;\n", table, colname, colvalue, whereout);
00747 }
00748
00749 db_free_binary_result(qres);
00750 }
00751
00752 return err;
00753 }
00754
00755 static int CreateSQL(FILE *fptr, DRMS_Env_t *env,
00756 const char *seriesin,
00757 const char *seriesout,
00758 const char *archive,
00759 const char *retention,
00760 const char *tapegroup,
00761 const char *owner,
00762 int doUtf8)
00763 {
00764 CrtabError_t err = kCrtabErr_Success;
00765 char *series = NULL;
00766 char *oid = NULL;
00767 char *ns = NULL;
00768 char *table = NULL;
00769 char *collist = NULL;
00770 char *seriesnew = NULL;
00771 char *nsnew = NULL;
00772 char *tablenew = NULL;
00773
00774 series = strdup(seriesin);
00775 strtolower(series);
00776
00777 seriesnew = strdup(seriesout);
00778 strtolower(seriesnew);
00779
00780 get_namespace(series, &ns, &table);
00781 get_namespace(seriesnew, &nsnew, &tablenew);
00782 if (!ns || !table)
00783 {
00784 fprintf(stderr, "Invalid argument 'seriesin' (%s).\n", seriesin);
00785 err = kCrtabErr_Argument;
00786 }
00787 else if (!nsnew || !tablenew)
00788 {
00789 fprintf(stderr, "Invalid argument 'seriesout' (%s).\n", seriesout);
00790 err = kCrtabErr_Argument;
00791 }
00792 else
00793 {
00794 err = CrtabGetTableOID(env, ns, table, &oid);
00795
00796 if (!err)
00797 {
00798 err = GetColumnLists(env, oid, &collist, NULL);
00799 }
00800
00801 if (!err)
00802 {
00803
00804 fprintf(fptr, "SET search_path TO %s;\n", nsnew);
00805 }
00806
00807
00808 if (!err)
00809 {
00810 err = CreateSQLTable(fptr, tablenew, collist);
00811
00812
00813 if (!err)
00814 {
00815 fprintf(fptr, "ALTER TABLE %s ADD CONSTRAINT %s_pkey PRIMARY KEY (recnum);\n", tablenew, tablenew);
00816 }
00817
00818
00819 if (!err)
00820 {
00821 err = CreateSQLIndices(fptr, env, oid, ns, table, nsnew, tablenew);
00822 }
00823
00824
00825 if (!err)
00826 {
00827 err = CreateSQLSequence(fptr, tablenew);
00828 }
00829
00830
00831
00832 if (!err)
00833 {
00834 err = CreateSQLGrantPerms(fptr, env, seriesnew, ns, owner);
00835 }
00836 }
00837
00838
00839 if (!err)
00840 {
00841 char where[512];
00842 char whereout[512];
00843
00844 char *lcseriesin = strdup(seriesin);
00845 char *lcseriesout = strdup(seriesout);
00846
00847 if (lcseriesin && lcseriesout)
00848 {
00849 strtolower(lcseriesin);
00850 strtolower(lcseriesout);
00851
00852 snprintf(where, sizeof(where), "lower(seriesname) = '%s'", lcseriesin);
00853 snprintf(whereout, sizeof(whereout), "lower(seriesname) = '%s'", lcseriesout);
00854 err = CreateSQLInsertIntoTable(fptr, env, seriesin, seriesout, ns, DRMS_MASTER_SERIES_TABLE, doUtf8);
00855
00856
00857 if (!err)
00858 {
00859 if (archive)
00860 {
00861 err = CreateSQLUpdateTable(fptr, env, ns, DRMS_MASTER_SERIES_TABLE, "archive", archive, where, whereout);
00862 }
00863 }
00864
00865 if (!err)
00866 {
00867 if (retention)
00868 {
00869 err = CreateSQLUpdateTable(fptr, env, ns, DRMS_MASTER_SERIES_TABLE, "retention", retention, where, whereout);
00870 }
00871 }
00872
00873 if (!err)
00874 {
00875 if (tapegroup)
00876 {
00877 err = CreateSQLUpdateTable(fptr, env, ns, DRMS_MASTER_SERIES_TABLE, "tapegroup", tapegroup, where, whereout);
00878 }
00879 }
00880
00881 if (!err)
00882 {
00883 if (owner)
00884 {
00885 char *buf = malloc(strlen(owner) + 3);
00886 if (buf)
00887 {
00888 snprintf(buf, strlen(owner) + 3, "\'%s\'", owner);
00889 err = CreateSQLUpdateTable(fptr, env, ns, DRMS_MASTER_SERIES_TABLE, "owner", buf, where, whereout);
00890 free(buf);
00891 }
00892 }
00893 }
00894
00895 if (lcseriesin)
00896 {
00897 free(lcseriesin);
00898 }
00899
00900 if (lcseriesout)
00901 {
00902 free(lcseriesout);
00903 }
00904 }
00905 else
00906 {
00907 err = kCrtabErr_OutOfMemory;
00908 }
00909 }
00910
00911
00912 if (!err)
00913 {
00914 err = CreateSQLInsertIntoTable(fptr, env, seriesin, seriesout, ns, DRMS_MASTER_LINK_TABLE, doUtf8);
00915 }
00916
00917
00918 if (!err)
00919 {
00920 err = CreateSQLInsertIntoTable(fptr, env, seriesin, seriesout, ns, DRMS_MASTER_KEYWORD_TABLE, doUtf8);
00921 }
00922
00923
00924 if (!err)
00925 {
00926 err = CreateSQLInsertIntoTable(fptr, env, seriesin, seriesout, ns, DRMS_MASTER_SEGMENT_TABLE, doUtf8);
00927 }
00928
00929 free(ns);
00930 free(table);
00931
00932 free(nsnew);
00933 free(tablenew);
00934
00935 if (series)
00936 {
00937 free(series);
00938 }
00939
00940 if (seriesnew)
00941 {
00942 free(seriesnew);
00943 }
00944
00945 if (oid)
00946 {
00947 free(oid);
00948 }
00949
00950 if (collist)
00951 {
00952 free(collist);
00953 }
00954 }
00955
00956 fflush(fptr);
00957
00958 return err;
00959 }
00960
00961 int DoIt(void)
00962 {
00963 CrtabError_t err = kCrtabErr_Success;
00964 int drmsstat = DRMS_SUCCESS;
00965 const char *series = NULL;
00966 const char *seriesout = NULL;
00967 const char *archive = NULL;
00968 const char *retention = NULL;
00969 const char *tapegroup = NULL;
00970 const char *owner = NULL;
00971 const char *file = NULL;
00972 int doUtf8 = 0;
00973 FILE *fptr = NULL;
00974
00975 series = cmdparams_get_str(&cmdparams, kSeriesin, NULL);
00976 seriesout = cmdparams_get_str(&cmdparams, kSeriesout, NULL);
00977
00978
00979 archive = cmdparams_get_str(&cmdparams, kArchive, NULL);
00980 if (strcmp(archive, kNotSpec) == 0)
00981 {
00982 archive = NULL;
00983 }
00984
00985 retention = cmdparams_get_str(&cmdparams, kRetention, NULL);
00986 if (strcmp(retention, kNotSpec) == 0)
00987 {
00988 retention = NULL;
00989 }
00990
00991 tapegroup = cmdparams_get_str(&cmdparams, kTapegroup, NULL);
00992 if (strcmp(tapegroup, kNotSpec) == 0)
00993 {
00994 tapegroup = NULL;
00995 }
00996
00997 owner = cmdparams_get_str(&cmdparams, kOwner, NULL);
00998 if (strcmp(owner, kNotSpec) == 0)
00999 {
01000 owner = NULL;
01001 }
01002
01003 file = cmdparams_get_str(&cmdparams, kFile, NULL);
01004 if (strcmp(file, kNotSpec) == 0)
01005 {
01006 fptr = stdout;
01007 }
01008 else
01009 {
01010 fptr = fopen(file, "w");
01011 if (!fptr)
01012 {
01013 fprintf(stderr, "Unable to open file '%s'.\n", file);
01014 err = kCrtabErr_FileIO;
01015 }
01016 }
01017
01018 doUtf8 = cmdparams_isflagset(&cmdparams, kFlagUTF8);
01019
01020 if (!err)
01021 {
01022 if (strcmp(series, kNotSpec) == 0 && (cmdparams_numargs(&cmdparams) < 1 || (series = cmdparams_getarg(&cmdparams, 1)) == NULL))
01023 {
01024 fprintf(stderr, "Missing argument {series=}<seriesname>.\n");
01025 err = kCrtabErr_Argument;
01026 }
01027 }
01028
01029 if (!err)
01030 {
01031 if (drms_series_exists(drms_env, series, &drmsstat))
01032 {
01033 CreateSQL(fptr, drms_env, series, seriesout, archive, retention, tapegroup, owner, doUtf8);
01034 }
01035 else
01036 {
01037 fprintf(stdout, "Unknown series '%s'.\n", series);
01038 err = kCrtabErr_UnknownSeries;
01039 }
01040 }
01041
01042 return err;
01043 }