116{
117 if(argc == 1) {
120 }
121
124
125 try {
126
127 int dump_files_only = 0;
128 const char * class_name = 0;
129 const char * query = 0;
130 const char * object_from = 0;
131 const char * path_query = 0;
132 long recursion_depth = 0;
134 const char * ref_by_rel_name = "*";
135 std::vector<std::string> ref_classes;
136 bool input_from_files = false;
137 bool print_files_stat = false;
138
139 for(int i = 1; i < argc; i++) {
140 const char * cp = argv[i];
141
142 if(!strcmp(cp, "-h") || !strcmp(cp, "--help")) {
145 }
146 else if(!strcmp(cp, "-v") || !strcmp(cp, "--version")) {
149 }
150 else if(!strcmp(cp, "-a") || !strcmp(cp, "--allow-duplicated-objects-via-inheritance")) {
152 }
153 else if(!strcmp(cp, "-f") || !strcmp(cp, "--files-only")) {
154 dump_files_only = 2;
155 }
156 else if(!strcmp(cp, "-F") || !strcmp(cp, "--files-stat-only")) {
157 dump_files_only = 2;
158 print_files_stat = true;
159 }
160 else if(!strcmp(cp, "-s") || !strcmp(cp, "--schema-files-only")) {
161 dump_files_only = 1;
162 }
163 else if(!strcmp(cp, "-S") || !strcmp(cp, "--schema-files-stat-only")) {
164 dump_files_only = 1;
165 print_files_stat = true;
166 }
167 else if(!strcmp(cp, "-d") || !strcmp(cp, "--data-files-only")) {
168 dump_files_only = 12;
169 }
170 else if(!strcmp(cp, "-D") || !strcmp(cp, "--data-files-stat-only")) {
171 dump_files_only = 12;
172 print_files_stat = true;
173 }
174 else if(!strcmp(cp, "-c") || !strcmp(cp, "--class")) {
175 if(++i == argc) {
no_param(cp); }
else { class_name = argv[i]; }
176 }
177 else if(!strcmp(cp, "-q") || !strcmp(cp, "--query")) {
178 if(++i == argc) {
no_param(cp); }
else { query = argv[i]; }
179 }
180 else if(!strcmp(cp, "-i") || !strcmp(cp, "--input-from-files")) {
181 input_from_files = true;
182 }
183 else if(!strcmp(cp, "-r") || !strcmp(cp, "--print-references")) {
184 if(++i == argc) {
no_param(cp); }
else { recursion_depth = atol(argv[i]); }
185 int j = 0;
186 for(; j < argc - i - 1; ++j) {
187 if(argv[i+1+j][0] != '-') { ref_classes.push_back(argv[i+1+j]); } else { break; }
188 }
189 i += j;
190 }
191 else if(!strcmp(cp, "-b") || !strcmp(cp, "--print-referenced_by")) {
193 if((i+1) < argc && argv[i+1][0] != '-') {
194 ref_by_rel_name = argv[++i];
195 }
196 }
197 else if(!strcmp(cp, "-p") || !strcmp(cp, "--path")) {
198 if(++i > argc - 1) {
no_param(cp); }
else { object_from = argv[i]; path_query = argv[++i];}
199 }
200 else if(strcmp(cp, "--")) {
201 if(input_from_files) {
204 while(
f.good() && !
f.eof()) {
205 std::string file_name;
206 std::getline(f, file_name);
207 if(!file_name.empty() && kernel.
load_file(file_name) == 0) {
208 Oks::error_msg(
"oks_dump") <<
"\tCan not load file \"" << file_name <<
"\", exiting...\n";
210 }
211 }
212 }
213 else {
214 Oks::error_msg(
"oks_dump") <<
"\tCan not open file \"" << cp <<
"\" for reading, exiting...\n";
216 }
217 }
218 else {
220 Oks::error_msg(
"oks_dump") <<
"\tCan not load file \"" << cp <<
"\", exiting...\n";
222 }
223 }
224 }
225 }
226
228 Oks::error_msg(
"oks_dump") <<
"\tAt least one oks file have to be provided, exiting...\n";
230 }
231
232 if(query && !class_name) {
233 Oks::error_msg(
"oks_dump") <<
"\tQuery can only be executed when class name is provided (use -c option), exiting...\n";
235 }
236
237 if(dump_files_only) {
238 long total_size = 0;
239 long total_items = 0;
241 for(int j = (dump_files_only / 10); j < (dump_files_only % 10); ++j) {
242 if(!files[j]->empty()) {
243 for(OksFile::Map::const_iterator i = files[j]->begin(); i != files[j]->end(); ++i) {
244 if(print_files_stat) {
245 total_size += i->second->get_size();
246 total_items += i->second->get_number_of_items();
247 std::cout << *(i->first) << " (" << i->second->get_number_of_items() << " items, " << i->second->get_size() << " bytes)" << std::endl;
248 }
249 else {
250 std::cout << *(i->first) << std::endl;
251 }
252 }
253 }
254 }
255
256 if(print_files_stat) {
257 std::cout << "Total number of items: " << total_items << "\n"
258 "Total size of files: " << total_size << " bytes" << std::endl;
259 }
260 }
261 else if(class_name && *class_name) {
263 if(query && *query) {
267 size_t num = (objs ? objs->size() : 0);
268 std::cout << "Found " << num << " matching query \"" << query << "\" in class \"" << class_name << "\"";
269 if(num) {
270 std::cout << ':' << std::endl;
271 while(!objs->empty()) {
273 objs->pop_front();
274
276 if(recursion_depth) {
280 o->
references(refs, recursion_depth,
false, &all_ref_classes);
281 std::cout << o << " references " << refs.size() << " object(s)" << std::endl;
282 for(OksObject::FSet::const_iterator i = refs.begin(); i != refs.end(); ++i) {
283 std::cout << " - " << *i << std::endl;
284 }
285 }
288 std::cout << o << " is referenced by " << ref_by_objs->size() << " object(s) via relationship \"" << ref_by_rel_name << "\":" << std::endl;
289
290 for(OksObject::FList::const_iterator i = ref_by_objs->begin(); i != ref_by_objs->end(); ++i) {
291 std::cout << " - " << *i << std::endl;
292 }
293
294 delete ref_by_objs;
295 }
296 else {
297 std::cout << o << " is not referenced by any object via relationship \"" << ref_by_rel_name << '\"' << std::endl;
298 }
299 }
300 }
301 else {
302 std::cout << *o << std::endl;
303 }
304 }
305 delete objs;
306 }
307 else {
308 std::cout << std::endl;
309 }
310 }
311 else {
312 Oks::error_msg(
"oks_dump") <<
"\tFailed to parse query \"" << query <<
"\" in class \"" << class_name <<
"\"\n";
314 }
315 delete q;
316 }
317 else {
318 std::cout << *
c << std::endl;
319 }
320 }
321 else {
322 Oks::error_msg(
"oks_dump") <<
"\tCan not find class \"" << class_name <<
"\"\n";
324 }
325 }
326 else if(object_from && *object_from && path_query && *path_query) {
328 try {
331
332 size_t num = (objs ? objs->size() : 0);
333 std::cout << "Found " << num << " objects in the path \"" << q << "\" from object " << obj_from << ":" << std::endl;
334
335 if(num) {
336 while(!objs->empty()) {
338 objs->pop_front();
339 std::cout << *o << std::endl;
340 }
341 delete objs;
342 }
343 }
347 }
348 }
349 else {
350 std::cout << kernel;
351 }
352
354 {
356 }
357
359 {
360 Oks::error_msg(
"oks_dump") <<
"\tThe data contain dangling references to non-loaded objects\n";
361 }
362
364 {
366 }
367 }
369 std::cerr << "Caught oks exception:\n" << ex << std::endl;
371 }
372 catch (std::exception & e) {
373 std::cerr << "Caught standard C++ exception: " << e.what() << std::endl;
375 }
376 catch (...) {
377 std::cerr << "Caught unknown exception" << std::endl;
379 }
380
382}
std::map< const std::string *, OksFile *, SortByName > Map
Provides interface to the OKS kernel.
OksFile * load_file(const std::string &name, bool bind=true)
Load OKS database file.
const OksFile::Map & data_files() const
Get all data files.
const std::string & get_bind_classes_status() const noexcept
Return status of oks classes binding.
void set_test_duplicated_objects_via_inheritance_mode(const bool b)
Set status of test inherited duplicated objects mode. To switch 'On'/'Off' use the method's parameter...
const std::string & get_bind_objects_status() const noexcept
Return status of oks objects binding.
static const char * GetVersion()
Get OKS version. The method returns string containing CVS tag and date of OKS build.
const OksFile::Map & schema_files() const
Get all schema files.
void get_all_classes(const std::vector< std::string > &names_in, ClassSet &classes_out) const
The method searches all classes including subclasses for given names.
std::list< OksObject * > List
std::unordered_set< OksObject *, oks::hash_obj_ptr, oks::equal_obj_ptr > FSet
void references(OksObject::FSet &refs, unsigned long recursion_depth, bool add_self=false, oks::ClassSet *classes=0) const
FList * get_all_rels(const std::string &name="*") const
Return objects referencing this one via relationship with given name.
OksObject::List * find_path(const oks::QueryPath &query) const
std::list< OksObject *, boost::fast_pool_allocator< OksObject * > > FList
virtual const char * what() const noexcept
static void print_referenced_by(const ConfigObject &obj, const char *prefix)
std::unordered_set< const OksClass *, oks::hash_class_ptr, oks::equal_class_ptr > ClassSet
OksObject * find_object(char *s, OksKernel &k)
static void no_param(const char *s)
static void printUsage(std::ostream &s)