DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
oks_get_schema.cxx
Go to the documentation of this file.
1//
2// DUNE DAQ modification notice:
3// This file has been modified from the original ATLAS oks_utils source for the DUNE DAQ project.
4// Fork baseline commit: c2e7dfc7 (2022-03-30).
5// Renamed since fork: yes (from src/bin/oks_get_schema.cpp to apps/oks_get_schema.cxx).
6//
7
8#include "CoralBase/Exception.h"
9#include "CoralKernel/Context.h"
10#include "RelationalAccess/ITransaction.h"
11#include "RelationalAccess/ConnectionService.h"
12#include "RelationalAccess/ISessionProxy.h"
13
14#include <unistd.h>
15#include <iostream>
16#include <stdexcept>
17
18#include "oks/kernel.hpp"
19#include "oks/ral.hpp"
20
21static void
23{
24 std::cout <<
25 "usage: oks_get_schema\n"
26 " -c | --connect-string connect_string\n"
27 " -w | --working-schema schema_name\n"
28 " [-e | --head-schema-version [release-name]]\n"
29 " [-n | --schema-version schema_version]\n"
30 " [-f | --out-file schema_file]\n"
31 " [-v | --verbose-level verbosity_level]\n"
32 " [-h | --help]\n"
33 "\n"
34 "Options/Arguments:\n"
35 " -c connect_string database connection string\n"
36 " -w schema_name name of working schema\n"
37 " -e [release-name] get head schema version for \'" TDAQ_CMT_RELEASE "\' release (or defined explicitly)\n"
38 " -n schema_version get schema by version\n"
39 " -f out_schema_file name for output schema file\n"
40 " -v verbosity_level set verbose output level (0 - silent, 1 - normal, 2 - extended, 3 - debug, ...)\n"
41 " -h print this message\n"
42 "\n"
43 "Description:\n"
44 " The utility creates oks file for given version of schema stored in relational database.\n\n";
45}
46
47
48static void
49no_param(const char * s)
50{
51 std::cerr << "ERROR: no parameter for " << s << " provided\n\n";
52}
53
54
55int main(int argc, char *argv[])
56{
57
58 // parse command line
59
60 int verbose_level = 1;
61 std::string connect_string;
62 std::string working_schema;
63 std::string password;
64 std::string file;
65 std::string release;
66 int schema_version = -1;
67 bool remove_file = false;
68
69 for(int i = 1; i < argc; i++) {
70 const char * cp = argv[i];
71
72 if(!strcmp(cp, "-h") || !strcmp(cp, "--help")) {
73 usage();
74 return (EXIT_SUCCESS);
75 }
76 else if(!strcmp(cp, "-v") || !strcmp(cp, "--verbose-level")) {
77 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { verbose_level = atoi(argv[i]); }
78 }
79 else if(!strcmp(cp, "-n") || !strcmp(cp, "--schema-version")) {
80 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { schema_version = atoi(argv[i]); }
81 }
82 else if(!strcmp(cp, "-e") || !strcmp(cp, "--head-schema-version")) {
83 schema_version = 0;
84 if(argc > (i+1) && argv[i+1][0] != '-') {++i; release = argv[i]; }
85 }
86 else if(!strcmp(cp, "-c") || !strcmp(cp, "--connect-string")) {
87 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { connect_string = argv[i]; }
88 }
89 else if(!strcmp(cp, "-w") || !strcmp(cp, "--working-schema")) {
90 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { working_schema = argv[i]; }
91 }
92 else if(!strcmp(cp, "-f") || !strcmp(cp, "--out-file")) {
93 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { file = argv[i]; }
94 }
95 else {
96 std::cerr << "ERROR: Unexpected parameter: \"" << cp << "\"\n\n";
97 usage();
98 return (EXIT_FAILURE);
99 }
100 }
101
102 if(schema_version == -1) {
103 std::cerr << "ERROR: specify schema version or use -e parameter to get HEAD schema version\n";
104 usage();
105 return EXIT_FAILURE;
106 }
107
108 if(connect_string.empty()) {
109 std::cerr << "ERROR: the connect string is required\n";
110 usage();
111 return EXIT_FAILURE;
112 }
113
114 if(working_schema.empty()) {
115 std::cerr << "ERROR: the working schema is required\n";
116 usage();
117 return EXIT_FAILURE;
118 }
119
120
121 // create oks kernel and schema file
122
123 VerboseMsg2("Creating OKS kernel...");
124
125 ::OksKernel kernel;
126
127
128 try {
129
130
131 if(file.empty()) {
132 file = kernel.get_tmp_file("/tmp/unknown.schema.xml");
133 VerboseMsg2("The name of output schema file was not provided.\n * use file \'" << file << '\'');
134 remove_file = true;
135 }
136
137 VerboseMsg3("Creating OKS schema file...");
138 ::OksFile * fh = kernel.new_schema(file);
139
140
141 {
142 std::unique_ptr<coral::ConnectionService> connection;
143 {
144 std::unique_ptr<coral::ISessionProxy> session (oks::ral::start_coral_session(connect_string, coral::ReadOnly, connection, verbose_level));
145
146 if(schema_version == 0) {
147 schema_version = oks::ral::get_head_schema_version(session.get(), working_schema, release.c_str(), verbose_level);
148 if(schema_version < 0) {
149 throw std::runtime_error( (schema_version == 0) ? "Cannot get head schema" : "Cannot get given schema" );
150 }
151 }
152
153 oks::ral::get_schema(kernel, session.get(), working_schema, schema_version, true, verbose_level);
154
155 VerboseMsg5("Dump schema:\n" << kernel);
156
157 VerboseMsg2("Committing...");
158 session->transaction().commit();
159
160 VerboseMsg2("Ending user session..."); // delete session by unique_ptr<>
161 }
162
163 VerboseMsg2("Disconnecting..."); // delete connection by unique_ptr<>
164 }
165
166 if(remove_file) {
167 VerboseMsg2("Removing temporal schema file \'" << file << "\'...");
168 unlink(file.c_str());
169 }
170 else {
171 VerboseMsg2("Saving oks schema...");
172 kernel.save_schema(fh);
173 }
174
175 VerboseMsg2("Exiting...");
176
177 }
178
179 catch ( coral::Exception& e ) {
180 std::cerr << "CORAL exception: " << e.what() << std::endl;
181 return 1;
182 }
183
184 catch ( oks::exception & ex) {
185 std::cerr << "OKS exception:\n" << ex << std::endl;
186 return 2;
187 }
188
189 catch ( std::exception& e ) {
190 std::cerr << "Standard C++ exception : " << e.what() << std::endl;
191 return 3;
192 }
193
194 catch ( ... ) {
195 std::cerr << "Exception caught (...)" << std::endl;
196 return 4;
197 }
198
199 return 0;
200}
void get_schema(OksKernel &kernel, coral::ISessionProxy *session, const std::string &schema, int version, bool read_methods, int verbose_level)
Get schema by version number.
coral::ISessionProxy * start_coral_session(const std::string &connect_string, int mode, std::unique_ptr< coral::ConnectionService > &connection, int verbose_level)
Start coral session.
int get_head_schema_version(coral::ISessionProxy *session, const std::string &schema, const char *release=0, int verbose_level=1, const char *dx="")
Get HEAD schema version number.
int main(int argc, char *argv[])
static void usage()
static void no_param(const char *s)
#define VerboseMsg2(MSG)
Definition ral.hpp:20
#define VerboseMsg5(MSG)
Definition ral.hpp:23
#define VerboseMsg3(MSG)
Definition ral.hpp:21