206 int verbose_level = 1;
207 std::string connect_string;
208 std::string working_schema;
209 int schema_version = -1;
210 bool ls_base_versions_only =
false;
211 bool ls_get_size =
false;
212 bool ls_usage =
false;
213 bool ls_description =
false;
214 bool list_db =
false;
218 std::string partition;
219 std::string acrhived_since;
220 std::string acrhived_till;
221 std::string sorted_by;
222 const char* tmp_acrhived_since(0);
223 const char* tmp_acrhived_till(0);
225 boost::local_time::time_zone_ptr tz_ptr;
227 for (
int i = 1; i < argc; i++) {
228 const char* cp = argv[i];
230 if (!strcmp(cp,
"-h") || !strcmp(cp,
"--help")) {
232 return (EXIT_SUCCESS);
233 }
else if (!strcmp(cp,
"-b") || !strcmp(cp,
"--base-data-only")) {
234 ls_base_versions_only =
true;
235 }
else if (!strcmp(cp,
"-l") || !strcmp(cp,
"--list-releases")) {
237 }
else if (!strcmp(cp,
"-z") || !strcmp(cp,
"--show-size")) {
239 }
else if (!strcmp(cp,
"-u") || !strcmp(cp,
"--show-usage")) {
241 }
else if (!strcmp(cp,
"-d") || !strcmp(cp,
"--show-description")) {
242 ls_description =
true;
245 else if (!strcmp(cp,
"-t") || !strcmp(cp,
"--sorted-by")) {
248 return (EXIT_FAILURE);
252 }
else if (!strcmp(cp,
"-r") || !strcmp(cp,
"--release")) {
255 return (EXIT_FAILURE);
259 }
else if (!strcmp(cp,
"-e") || !strcmp(cp,
"--user")) {
262 return (EXIT_FAILURE);
266 }
else if (!strcmp(cp,
"-o") || !strcmp(cp,
"--host")) {
269 return (EXIT_FAILURE);
273 }
else if (!strcmp(cp,
"-p") || !strcmp(cp,
"--partition")) {
276 return (EXIT_FAILURE);
280 }
else if (!strcmp(cp,
"-v") || !strcmp(cp,
"--verbose-level")) {
283 return (EXIT_FAILURE);
285 verbose_level = atoi(argv[i]);
287 }
else if (!strcmp(cp,
"-s") || !strcmp(cp,
"--schema-version")) {
292 schema_version = atoi(argv[i]);
294 }
else if (!strcmp(cp,
"-c") || !strcmp(cp,
"--connect-string")) {
299 connect_string = argv[i];
301 }
else if (!strcmp(cp,
"-w") || !strcmp(cp,
"--working-schema")) {
306 working_schema = argv[i];
308 }
else if (!strcmp(cp,
"-S") || !strcmp(cp,
"--archived-since")) {
313 tmp_acrhived_since = argv[i];
315 }
else if (!strcmp(cp,
"-T") || !strcmp(cp,
"--archived-till")) {
320 tmp_acrhived_till = argv[i];
322 }
else if (!strcmp(cp,
"-Z") || !strcmp(cp,
"--time-zone")) {
330 std::cerr <<
"ERROR: Unexpected parameter: \"" << cp <<
"\"\n\n";
332 return (EXIT_FAILURE);
340 if (!strcmp(tz,
"list-regions")) {
342 std::cout << i << std::endl;
348 }
catch (
const std::exception& ex) {
349 std::cerr <<
"ERROR: time-zone db access failed: " << ex.what() << std::endl;
354 if (connect_string.empty()) {
355 std::cerr <<
"ERROR: the connect string is required\n";
360 if (working_schema.empty()) {
361 std::cerr <<
"ERROR: the working schema is required\n";
367 if (tmp_acrhived_since) {
372 if (tmp_acrhived_till) {
376 }
catch (
const std::exception& ex) {
377 std::cerr <<
"ERROR: " << ex.what() << std::endl;
383 std::unique_ptr<coral::ConnectionService> connection;
386 std::unique_ptr<coral::ISessionProxy>
session(
390 coral::ITable& schema_table =
session->schema(working_schema).tableHandle(
"OKSSCHEMA");
391 std::unique_ptr<coral::IQuery> query(schema_table.newQuery());
392 query->setRowCacheSize(10);
393 query->setDistinct();
394 query->addToOutputList(
"RELEASE");
395 coral::ICursor& cursor = query->execute();
397 std::set<std::string> names;
399 while (cursor.next()) {
400 names.insert(cursor.currentRow().begin()->data<std::string>());
403 std::cout <<
"Found configuration archives for " << names.size() <<
" releases:\n";
405 for (std::set<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
406 std::cout <<
" - \'" << *i <<
'\'' << std::endl;
409 if (schema_version == 0) {
412 if (schema_version == 0) {
413 throw std::runtime_error(
"Cannot get HEAD schema version.");
417 coral::ITable& archive_table =
session->schema(working_schema).tableHandle(
"OKSARCHIVE");
419 std::list<std::string> conditions_list;
420 coral::AttributeList conditions;
422 std::unique_ptr<coral::IQuery> query(
session->schema(working_schema).newQuery());
423 query->setRowCacheSize(1000);
425 query->addToOutputList(
"A.SCHEMAVERSION");
426 query->addToOutputList(
"A.BASEVERSION");
427 query->addToOutputList(
"A.VERSION");
428 query->addToOutputList(
"B.RELEASE");
429 query->addToOutputList(
"A.TIME");
430 query->addToOutputList(
"A.CREATEDBY");
431 query->addToOutputList(
"A.HOST");
433 if (ls_description) {
434 query->addToOutputList(
"A.DESCRIPTION");
437 coral::IQueryDefinition& subQuery1 = query->defineSubQuery(
"A");
438 subQuery1.addToTableList(
"OKSDATA");
439 query->addToTableList(
"A");
441 coral::IQueryDefinition& subQuery2 = query->defineSubQuery(
"B");
442 subQuery2.addToTableList(
"OKSSCHEMA");
443 query->addToTableList(
"B");
445 conditions_list.push_back(
"A.SCHEMAVERSION = B.VERSION");
476 if (schema_version != -1) {
477 conditions.extend<
int>(
"ver");
478 conditions[
"ver"].data<
int>() = schema_version;
480 if (ls_base_versions_only) {
481 conditions_list.push_back(
"A.SCHEMAVERSION = :ver AND A.VERSION = A.BASEVERSION");
483 conditions_list.push_back(
"A.SCHEMAVERSION = :ver");
485 }
else if (ls_base_versions_only) {
486 conditions_list.push_back(
"A.VERSION = A.BASEVERSION");
489 if (!release.empty()) {
490 conditions.extend<std::string>(
"rel");
491 conditions[
"rel"].data<std::string>() = release;
492 conditions_list.push_back(
"B.RELEASE = :rel");
495 if (!partition.empty()) {
497 if (ls_description ==
false) {
498 throw std::runtime_error(
"cannot search by partition without \"-d\" or \"-u\" command line option");
500 conditions.extend<std::string>(
"prt1");
501 conditions[
"prt1"].data<std::string>() = std::string(
"%partition ") + partition +
'%';
502 conditions_list.push_back(
"A.DESCRIPTION like :prt1");
506 if (!acrhived_since.empty()) {
507 conditions.extend<std::string>(
"ars");
508 conditions[
"ars"].data<std::string>() = acrhived_since;
510 query->setDistinct();
511 coral::IQueryDefinition& subQuery3 = query->defineSubQuery(
"C");
512 subQuery3.addToTableList(
"OKSARCHIVE");
513 query->addToTableList(
"C");
514 std::string condition(
"C.TIME >= :ars AND A.SCHEMAVERSION = C.SCHEMAVERSION AND A.VERSION = C.DATAVERSION");
515 if (!partition.empty()) {
516 conditions.extend<std::string>(
"prt2");
517 conditions[
"prt2"].data<std::string>() = partition;
518 condition += (
has_a_pattern(partition) ?
" AND C.PARTITION like :prt2" :
" AND C.PARTITION = :prt2");
520 conditions_list.push_back(condition);
522 conditions_list.push_back(
"A.TIME >= :ars");
526 if (!acrhived_till.empty()) {
527 conditions.extend<std::string>(
"art");
528 conditions[
"art"].data<std::string>() = acrhived_till;
529 conditions_list.push_back(
"A.TIME <= :art");
533 conditions.extend<std::string>(
"usr");
534 conditions[
"usr"].data<std::string>() = user;
535 conditions_list.push_back(
has_a_pattern(user) ?
"A.CREATEDBY like :usr" :
"A.CREATEDBY = :usr");
539 conditions.extend<std::string>(
"hst");
540 conditions[
"hst"].data<std::string>() = host;
541 conditions_list.push_back(
has_a_pattern(host) ?
"A.HOST like :hst" :
"A.HOST = :hst");
544 if (!sorted_by.empty()) {
545 for (
const char* p = sorted_by.c_str(); *p != 0; ++p) {
546 if (ls_description ==
false && (*p ==
'p' || *p ==
'P')) {
547 throw std::runtime_error(
"cannot sort by partition without \"-d\" command line option");
552 query->addToOrderList(
"A.SCHEMAVERSION");
553 query->addToOrderList(
"A.VERSION");
556 query->addToOrderList(
"A.SCHEMAVERSION DESC");
557 query->addToOrderList(
"A.VERSION DESC");
560 query->addToOrderList(
"A.TIME");
563 query->addToOrderList(
"A.TIME DESC");
566 query->addToOrderList(
"A.CREATEDBY");
569 query->addToOrderList(
"A.CREATEDBY DESC");
572 query->addToOrderList(
"A.HOST");
575 query->addToOrderList(
"A.HOST DESC");
578 query->addToOrderList(
"A.DESCRIPTION");
581 query->addToOrderList(
"A.DESCRIPTION DESC");
586 std::stringstream text;
587 text <<
"ERROR: wrong value \'" << *p <<
"\' in the command line parameter: \"--sorted-by\" or \"-t\"";
588 throw std::runtime_error(text.str().c_str());
593 std::string conditions_str;
595 for (std::list<std::string>::const_iterator i = conditions_list.begin(); i != conditions_list.end(); ++i) {
596 if (i != conditions_list.begin())
597 conditions_str +=
" AND ";
598 conditions_str += *i;
601 query->setCondition(conditions_str, conditions);
603 coral::ICursor& cursor = query->execute();
605 std::vector<TableRow> rows;
614 (tz_ptr ?
"Date (local time)" :
"Date (UTC)"),
624 coral::ITable& table;
625 } nums[] = { { 0,
session->schema(working_schema).tableHandle(
"OKSOBJECT") },
626 { 0,
session->schema(working_schema).tableHandle(
"OKSDATAVAL") },
627 { 0,
session->schema(working_schema).tableHandle(
"OKSDATAREL") } };
629 while (cursor.next()) {
630 const coral::AttributeList& row = cursor.currentRow();
632 std::string time, host, user;
634 std::string description;
635 if (ls_description && !row[
"A.DESCRIPTION"].isNull())
636 description = row[
"A.DESCRIPTION"].data<std::string>();
638 if (!row[
"B.RELEASE"].isNull())
639 r = row[
"B.RELEASE"].data<std::string>();
640 int sv = row[
"A.SCHEMAVERSION"].data<
int>();
641 int dv = row[
"A.VERSION"].data<
int>();
642 nums[0].num = nums[1].num = nums[2].num = 0;
645 coral::AttributeList sizes_query_conditions;
646 sizes_query_conditions.extend<
int>(
"sv");
647 sizes_query_conditions[
"sv"].data<
int>() = sv;
648 sizes_query_conditions.extend<
int>(
"dv");
649 sizes_query_conditions[
"dv"].data<
int>() = dv;
651 for (
unsigned int i = 0; i < 3; ++i) {
652 std::unique_ptr<coral::IQuery> query(nums[i].table.newQuery());
653 query->setRowCacheSize(1);
654 query->addToOutputList(
"COUNT(*)");
655 query->defineOutputType(
"COUNT(*)",
"long");
656 query->setCondition(
"SCHEMAVERSION = :sv AND DATAVERSION = :dv", sizes_query_conditions);
658 coral::ICursor& c = query->execute();
661 nums[i].num = (int)c.currentRow().begin()->data<
long>();
670 row[
"A.BASEVERSION"].data<int>(),
681 std::unique_ptr<coral::IQuery> query(archive_table.newQuery());
682 coral::AttributeList archive_query_conditions;
683 archive_query_conditions.extend<
int>(
"sv");
684 archive_query_conditions[
"sv"].data<
int>() = sv;
685 archive_query_conditions.extend<
int>(
"dv");
686 archive_query_conditions[
"dv"].data<
int>() = dv;
687 query->setRowCacheSize(10);
688 query->addToOutputList(
"TIME");
689 query->addToOutputList(
"PARTITION");
690 query->addToOutputList(
"CREATEDBY");
691 query->addToOutputList(
"HOST");
692 query->addToOutputList(
"RUNNUMBER");
694 if (!sorted_by.empty()) {
695 for (
const char* p = sorted_by.c_str(); *p != 0; ++p) {
699 query->addToOrderList(
"RUNNUMBER");
702 query->addToOrderList(
"RUNNUMBER DESC");
705 query->addToOrderList(
"TIME");
708 query->addToOrderList(
"TIME DESC");
711 query->addToOrderList(
"CREATEDBY");
714 query->addToOrderList(
"CREATEDBY DESC");
717 query->addToOrderList(
"HOST");
720 query->addToOrderList(
"HOST DESC");
723 query->addToOrderList(
"PARTITION");
726 query->addToOrderList(
"PARTITION DESC");
732 std::string condition(
"SCHEMAVERSION = :sv AND DATAVERSION = :dv");
734 if (!acrhived_since.empty()) {
735 archive_query_conditions.extend<std::string>(
"ars");
736 archive_query_conditions[
"ars"].data<std::string>() = acrhived_since;
737 condition +=
" AND TIME >= :ars";
740 if (!acrhived_till.empty()) {
741 archive_query_conditions.extend<std::string>(
"art");
742 archive_query_conditions[
"art"].data<std::string>() = acrhived_till;
743 condition +=
" AND TIME <= :art";
746 if (!partition.empty()) {
747 archive_query_conditions.extend<std::string>(
"prt");
748 archive_query_conditions[
"prt"].data<std::string>() = partition;
749 condition += (
has_a_pattern(partition) ?
" AND PARTITION like :prt" :
" AND PARTITION = :prt");
752 query->setCondition(condition, archive_query_conditions);
754 coral::ICursor& c = query->execute();
757 const coral::AttributeList& ar_row = c.currentRow();
758 std::string ar_time, ar_host, ar_user;
761 if (!ar_row[
"PARTITION"].isNull())
762 p = ar_row[
"PARTITION"].data<std::string>();
764 std::ostringstream s;
765 s <<
"partition: " << p <<
" run: " << ar_row[
"RUNNUMBER"].data<
int>();
767 rows.push_back(
TableRow(
' ',
'|', 0, 0, 0, r, ar_time, ar_user, ar_host, s.str()));
776 unsigned short cw[] = { 1, 1, 1, 1, 1, 1, 1 };
778 for (
auto& i : rows) {
779 for (
unsigned short j = 0; j <
sizeof(cw) /
sizeof(
unsigned short); ++j) {
780 unsigned short len = i.m_items[j].size();
790 bool align_left[] = {
false,
true,
true,
true,
true,
false,
true };
792 for (std::vector<TableRow>::const_iterator i = rows.begin(); i != rows.end(); ++i) {
793 std::cout.fill((*i).m_fill);
794 for (
unsigned short j = 0; j <
sizeof(cw) /
sizeof(
unsigned short); ++j) {
795 std::cout << (*i).m_separator << (*i).m_fill;
796 std::cout.setf((align_left[j] ? std::ios::left : std::ios::right), std::ios::adjustfield);
797 std::cout.width(cw[j]);
798 std::cout << (*i).m_items[j] << (*i).m_fill;
800 std::cout << (*i).m_separator << std::endl;
805 session->transaction().commit();
813 catch (coral::Exception& e) {
814 std::cerr <<
"CORAL exception: " << e.what() << std::endl;
818 catch (std::exception& e) {
819 std::cerr <<
"Standard C++ exception : " << e.what() << std::endl;
824 std::cerr <<
"Exception caught (...)" << std::endl;