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/ConnectionService.h"
11#include "RelationalAccess/ISessionProxy.h"
12#include "RelationalAccess/ITransaction.h"
13
14#include <iostream>
15#include <stdexcept>
16#include <unistd.h>
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
38 "\' release (or defined explicitly)\n"
39 " -n schema_version get schema by version\n"
40 " -f out_schema_file name for output schema file\n"
41 " -v verbosity_level set verbose output level (0 - silent, 1 - normal, 2 - extended, 3 - debug, ...)\n"
42 " -h print this message\n"
43 "\n"
44 "Description:\n"
45 " The utility creates oks file for given version of schema stored in relational database.\n\n";
46}
47
48static void
49no_param(const char* s)
50{
51 std::cerr << "ERROR: no parameter for " << s << " provided\n\n";
52}
53
54int
55main(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 } else if (!strcmp(cp, "-v") || !strcmp(cp, "--verbose-level")) {
76 if (++i == argc) {
77 no_param(cp);
78 return (EXIT_FAILURE);
79 } else {
80 verbose_level = atoi(argv[i]);
81 }
82 } else if (!strcmp(cp, "-n") || !strcmp(cp, "--schema-version")) {
83 if (++i == argc) {
84 no_param(cp);
85 return (EXIT_FAILURE);
86 } else {
87 schema_version = atoi(argv[i]);
88 }
89 } else if (!strcmp(cp, "-e") || !strcmp(cp, "--head-schema-version")) {
90 schema_version = 0;
91 if (argc > (i + 1) && argv[i + 1][0] != '-') {
92 ++i;
93 release = argv[i];
94 }
95 } else if (!strcmp(cp, "-c") || !strcmp(cp, "--connect-string")) {
96 if (++i == argc) {
97 no_param(cp);
98 return (EXIT_FAILURE);
99 } else {
100 connect_string = argv[i];
101 }
102 } else if (!strcmp(cp, "-w") || !strcmp(cp, "--working-schema")) {
103 if (++i == argc) {
104 no_param(cp);
105 return (EXIT_FAILURE);
106 } else {
107 working_schema = argv[i];
108 }
109 } else if (!strcmp(cp, "-f") || !strcmp(cp, "--out-file")) {
110 if (++i == argc) {
111 no_param(cp);
112 return (EXIT_FAILURE);
113 } else {
114 file = argv[i];
115 }
116 } else {
117 std::cerr << "ERROR: Unexpected parameter: \"" << cp << "\"\n\n";
118 usage();
119 return (EXIT_FAILURE);
120 }
121 }
122
123 if (schema_version == -1) {
124 std::cerr << "ERROR: specify schema version or use -e parameter to get HEAD schema version\n";
125 usage();
126 return EXIT_FAILURE;
127 }
128
129 if (connect_string.empty()) {
130 std::cerr << "ERROR: the connect string is required\n";
131 usage();
132 return EXIT_FAILURE;
133 }
134
135 if (working_schema.empty()) {
136 std::cerr << "ERROR: the working schema is required\n";
137 usage();
138 return EXIT_FAILURE;
139 }
140
141 // create oks kernel and schema file
142
143 VerboseMsg2("Creating OKS kernel...");
144
145 ::OksKernel kernel;
146
147 try {
148
149 if (file.empty()) {
150 file = kernel.get_tmp_file("/tmp/unknown.schema.xml");
151 VerboseMsg2("The name of output schema file was not provided.\n * use file \'" << file << '\'');
152 remove_file = true;
153 }
154
155 VerboseMsg3("Creating OKS schema file...");
156 ::OksFile* fh = kernel.new_schema(file);
157
158 {
159 std::unique_ptr<coral::ConnectionService> connection;
160 {
161 std::unique_ptr<coral::ISessionProxy> session(
162 oks::ral::start_coral_session(connect_string, coral::ReadOnly, connection, verbose_level));
163
164 if (schema_version == 0) {
165 schema_version =
166 oks::ral::get_head_schema_version(session.get(), working_schema, release.c_str(), verbose_level);
167 if (schema_version < 0) {
168 throw std::runtime_error((schema_version == 0) ? "Cannot get head schema" : "Cannot get given schema");
169 }
170 }
171
172 oks::ral::get_schema(kernel, session.get(), working_schema, schema_version, true, verbose_level);
173
174 VerboseMsg5("Dump schema:\n" << kernel);
175
176 VerboseMsg2("Committing...");
177 session->transaction().commit();
178
179 VerboseMsg2("Ending user session..."); // delete session by unique_ptr<>
180 }
181
182 VerboseMsg2("Disconnecting..."); // delete connection by unique_ptr<>
183 }
184
185 if (remove_file) {
186 VerboseMsg2("Removing temporal schema file \'" << file << "\'...");
187 unlink(file.c_str());
188 } else {
189 VerboseMsg2("Saving oks schema...");
190 kernel.save_schema(fh);
191 }
192
193 VerboseMsg2("Exiting...");
194
195 }
196
197 catch (coral::Exception& e) {
198 std::cerr << "CORAL exception: " << e.what() << std::endl;
199 return 1;
200 }
201
202 catch (oks::exception& ex) {
203 std::cerr << "OKS exception:\n" << ex << std::endl;
204 return 2;
205 }
206
207 catch (std::exception& e) {
208 std::cerr << "Standard C++ exception : " << e.what() << std::endl;
209 return 3;
210 }
211
212 catch (...) {
213 std::cerr << "Exception caught (...)" << std::endl;
214 return 4;
215 }
216
217 return 0;
218}
static void usage(const char *s)
static void no_param(const char *s)
int main(int argc, char *argv[])
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.
#define VerboseMsg2(MSG)
Definition ral.hpp:25
#define VerboseMsg5(MSG)
Definition ral.hpp:43
#define VerboseMsg3(MSG)
Definition ral.hpp:31