DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
oks_get_schema.cxx File Reference
#include "CoralBase/Exception.h"
#include "CoralKernel/Context.h"
#include "RelationalAccess/ITransaction.h"
#include "RelationalAccess/ConnectionService.h"
#include "RelationalAccess/ISessionProxy.h"
#include <unistd.h>
#include <iostream>
#include <stdexcept>
#include "oks/kernel.hpp"
#include "oks/ral.hpp"
Include dependency graph for oks_get_schema.cxx:

Go to the source code of this file.

Functions

static void usage ()
 
static void no_param (const char *s)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 48 of file oks_get_schema.cxx.

49{
50
51 // parse command line
52
53 int verbose_level = 1;
54 std::string connect_string;
55 std::string working_schema;
56 std::string password;
57 std::string file;
58 std::string release;
59 int schema_version = -1;
60 bool remove_file = false;
61
62 for(int i = 1; i < argc; i++) {
63 const char * cp = argv[i];
64
65 if(!strcmp(cp, "-h") || !strcmp(cp, "--help")) {
66 usage();
67 return (EXIT_SUCCESS);
68 }
69 else if(!strcmp(cp, "-v") || !strcmp(cp, "--verbose-level")) {
70 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { verbose_level = atoi(argv[i]); }
71 }
72 else if(!strcmp(cp, "-n") || !strcmp(cp, "--schema-version")) {
73 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { schema_version = atoi(argv[i]); }
74 }
75 else if(!strcmp(cp, "-e") || !strcmp(cp, "--head-schema-version")) {
76 schema_version = 0;
77 if(argc > (i+1) && argv[i+1][0] != '-') {++i; release = argv[i]; }
78 }
79 else if(!strcmp(cp, "-c") || !strcmp(cp, "--connect-string")) {
80 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { connect_string = argv[i]; }
81 }
82 else if(!strcmp(cp, "-w") || !strcmp(cp, "--working-schema")) {
83 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { working_schema = argv[i]; }
84 }
85 else if(!strcmp(cp, "-f") || !strcmp(cp, "--out-file")) {
86 if(++i == argc) { no_param(cp); return (EXIT_FAILURE); } else { file = argv[i]; }
87 }
88 else {
89 std::cerr << "ERROR: Unexpected parameter: \"" << cp << "\"\n\n";
90 usage();
91 return (EXIT_FAILURE);
92 }
93 }
94
95 if(schema_version == -1) {
96 std::cerr << "ERROR: specify schema version or use -e parameter to get HEAD schema version\n";
97 usage();
98 return EXIT_FAILURE;
99 }
100
101 if(connect_string.empty()) {
102 std::cerr << "ERROR: the connect string is required\n";
103 usage();
104 return EXIT_FAILURE;
105 }
106
107 if(working_schema.empty()) {
108 std::cerr << "ERROR: the working schema is required\n";
109 usage();
110 return EXIT_FAILURE;
111 }
112
113
114 // create oks kernel and schema file
115
116 VerboseMsg2("Creating OKS kernel...");
117
118 ::OksKernel kernel;
119
120
121 try {
122
123
124 if(file.empty()) {
125 file = kernel.get_tmp_file("/tmp/unknown.schema.xml");
126 VerboseMsg2("The name of output schema file was not provided.\n * use file \'" << file << '\'');
127 remove_file = true;
128 }
129
130 VerboseMsg3("Creating OKS schema file...");
131 ::OksFile * fh = kernel.new_schema(file);
132
133
134 {
135 std::unique_ptr<coral::ConnectionService> connection;
136 {
137 std::unique_ptr<coral::ISessionProxy> session (oks::ral::start_coral_session(connect_string, coral::ReadOnly, connection, verbose_level));
138
139 if(schema_version == 0) {
140 schema_version = oks::ral::get_head_schema_version(session.get(), working_schema, release.c_str(), verbose_level);
141 if(schema_version < 0) {
142 throw std::runtime_error( (schema_version == 0) ? "Cannot get head schema" : "Cannot get given schema" );
143 }
144 }
145
146 oks::ral::get_schema(kernel, session.get(), working_schema, schema_version, true, verbose_level);
147
148 VerboseMsg5("Dump schema:\n" << kernel);
149
150 VerboseMsg2("Committing...");
151 session->transaction().commit();
152
153 VerboseMsg2("Ending user session..."); // delete session by unique_ptr<>
154 }
155
156 VerboseMsg2("Disconnecting..."); // delete connection by unique_ptr<>
157 }
158
159 if(remove_file) {
160 VerboseMsg2("Removing temporal schema file \'" << file << "\'...");
161 unlink(file.c_str());
162 }
163 else {
164 VerboseMsg2("Saving oks schema...");
165 kernel.save_schema(fh);
166 }
167
168 VerboseMsg2("Exiting...");
169
170 }
171
172 catch ( coral::Exception& e ) {
173 std::cerr << "CORAL exception: " << e.what() << std::endl;
174 return 1;
175 }
176
177 catch ( oks::exception & ex) {
178 std::cerr << "OKS exception:\n" << ex << std::endl;
179 return 2;
180 }
181
182 catch ( std::exception& e ) {
183 std::cerr << "Standard C++ exception : " << e.what() << std::endl;
184 return 3;
185 }
186
187 catch ( ... ) {
188 std::cerr << "Exception caught (...)" << std::endl;
189 return 4;
190 }
191
192 return 0;
193}
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.
static void usage()
static void no_param(const char *s)
#define VerboseMsg2(MSG)
Definition ral.hpp:13
#define VerboseMsg5(MSG)
Definition ral.hpp:16
#define VerboseMsg3(MSG)
Definition ral.hpp:14

◆ no_param()

static void no_param ( const char * s)
static

Definition at line 42 of file oks_get_schema.cxx.

43{
44 std::cerr << "ERROR: no parameter for " << s << " provided\n\n";
45}

◆ usage()

static void usage ( )
static

Definition at line 15 of file oks_get_schema.cxx.

16{
17 std::cout <<
18 "usage: oks_get_schema\n"
19 " -c | --connect-string connect_string\n"
20 " -w | --working-schema schema_name\n"
21 " [-e | --head-schema-version [release-name]]\n"
22 " [-n | --schema-version schema_version]\n"
23 " [-f | --out-file schema_file]\n"
24 " [-v | --verbose-level verbosity_level]\n"
25 " [-h | --help]\n"
26 "\n"
27 "Options/Arguments:\n"
28 " -c connect_string database connection string\n"
29 " -w schema_name name of working schema\n"
30 " -e [release-name] get head schema version for \'" TDAQ_CMT_RELEASE "\' release (or defined explicitly)\n"
31 " -n schema_version get schema by version\n"
32 " -f out_schema_file name for output schema file\n"
33 " -v verbosity_level set verbose output level (0 - silent, 1 - normal, 2 - extended, 3 - debug, ...)\n"
34 " -h print this message\n"
35 "\n"
36 "Description:\n"
37 " The utility creates oks file for given version of schema stored in relational database.\n\n";
38}