133{
134 if (argc == 1) {
137 }
138
141
142 try {
143
144 int dump_files_only = 0;
145 const char* class_name = 0;
146 const char* query = 0;
147 const char* object_from = 0;
148 const char* path_query = 0;
149 long recursion_depth = 0;
150 bool print_referenced_by = false;
151 const char* ref_by_rel_name = "*";
152 std::vector<std::string> ref_classes;
153 bool input_from_files = false;
154 bool print_files_stat = false;
155
156 for (int i = 1; i < argc; i++) {
157 const char* cp = argv[i];
158
159 if (!strcmp(cp, "-h") || !strcmp(cp, "--help")) {
162 } else if (!strcmp(cp, "-v") || !strcmp(cp, "--version")) {
165 } else if (!strcmp(cp, "-a") || !strcmp(cp, "--allow-duplicated-objects-via-inheritance")) {
167 } else if (!strcmp(cp, "-f") || !strcmp(cp, "--files-only")) {
168 dump_files_only = 2;
169 } else if (!strcmp(cp, "-F") || !strcmp(cp, "--files-stat-only")) {
170 dump_files_only = 2;
171 print_files_stat = true;
172 } else if (!strcmp(cp, "-s") || !strcmp(cp, "--schema-files-only")) {
173 dump_files_only = 1;
174 } else if (!strcmp(cp, "-S") || !strcmp(cp, "--schema-files-stat-only")) {
175 dump_files_only = 1;
176 print_files_stat = true;
177 } else if (!strcmp(cp, "-d") || !strcmp(cp, "--data-files-only")) {
178 dump_files_only = 12;
179 } else if (!strcmp(cp, "-D") || !strcmp(cp, "--data-files-stat-only")) {
180 dump_files_only = 12;
181 print_files_stat = true;
182 } else if (!strcmp(cp, "-c") || !strcmp(cp, "--class")) {
183 if (++i == argc) {
185 } else {
186 class_name = argv[i];
187 }
188 } else if (!strcmp(cp, "-q") || !strcmp(cp, "--query")) {
189 if (++i == argc) {
191 } else {
192 query = argv[i];
193 }
194 } else if (!strcmp(cp, "-i") || !strcmp(cp, "--input-from-files")) {
195 input_from_files = true;
196 } else if (!strcmp(cp, "-r") || !strcmp(cp, "--print-references")) {
197 if (++i == argc) {
199 } else {
200 recursion_depth = atol(argv[i]);
201 }
202 int j = 0;
203 for (; j < argc - i - 1; ++j) {
204 if (argv[i + 1 + j][0] != '-') {
205 ref_classes.push_back(argv[i + 1 + j]);
206 } else {
207 break;
208 }
209 }
210 i += j;
211 } else if (!strcmp(cp, "-b") || !strcmp(cp, "--print-referenced_by")) {
212 print_referenced_by = true;
213 if ((i + 1) < argc && argv[i + 1][0] != '-') {
214 ref_by_rel_name = argv[++i];
215 }
216 } else if (!strcmp(cp, "-p") || !strcmp(cp, "--path")) {
217 if (++i > argc - 1) {
219 } else {
220 object_from = argv[i];
221 path_query = argv[++i];
222 }
223 } else if (strcmp(cp, "--")) {
224 if (input_from_files) {
227 while (
f.good() && !
f.eof()) {
228 std::string file_name;
229 std::getline(f, file_name);
230 if (!file_name.empty() && kernel.
load_file(file_name) == 0) {
231 Oks::error_msg(
"oks_dump") <<
"\tCan not load file \"" << file_name <<
"\", exiting...\n";
233 }
234 }
235 } else {
236 Oks::error_msg(
"oks_dump") <<
"\tCan not open file \"" << cp <<
"\" for reading, exiting...\n";
238 }
239 } else {
241 Oks::error_msg(
"oks_dump") <<
"\tCan not load file \"" << cp <<
"\", exiting...\n";
243 }
244 }
245 }
246 }
247
249 Oks::error_msg(
"oks_dump") <<
"\tAt least one oks file have to be provided, exiting...\n";
251 }
252
253 if (query && !class_name) {
255 << "\tQuery can only be executed when class name is provided (use -c option), exiting...\n";
257 }
258
259 if (dump_files_only) {
260 long total_size = 0;
261 long total_items = 0;
263 for (int j = (dump_files_only / 10); j < (dump_files_only % 10); ++j) {
264 if (!files[j]->empty()) {
265 for (OksFile::Map::const_iterator i = files[j]->begin(); i != files[j]->end(); ++i) {
266 if (print_files_stat) {
267 total_size += i->second->get_size();
268 total_items += i->second->get_number_of_items();
269 std::cout << *(i->first) << " (" << i->second->get_number_of_items() << " items, "
270 << i->second->get_size() << " bytes)" << std::endl;
271 } else {
272 std::cout << *(i->first) << std::endl;
273 }
274 }
275 }
276 }
277
278 if (print_files_stat) {
279 std::cout << "Total number of items: " << total_items
280 << "\n"
281 "Total size of files: "
282 << total_size << " bytes" << std::endl;
283 }
284 } else if (class_name && *class_name) {
286 if (query && *query) {
290 size_t num = (objs ? objs->size() : 0);
291 std::cout << "Found " << num << " matching query \"" << query << "\" in class \"" << class_name << "\"";
292 if (num) {
293 std::cout << ':' << std::endl;
294 while (!objs->empty()) {
296 objs->pop_front();
297
298 if (recursion_depth > 0 || print_referenced_by) {
299 if (recursion_depth) {
303 o->
references(refs, recursion_depth,
false, &all_ref_classes);
304 std::cout << o << " references " << refs.size() << " object(s)" << std::endl;
305 for (OksObject::FSet::const_iterator i = refs.begin(); i != refs.end(); ++i) {
306 std::cout << " - " << *i << std::endl;
307 }
308 }
309 if (print_referenced_by) {
311 std::cout << o << " is referenced by " << ref_by_objs->size() << " object(s) via relationship \""
312 << ref_by_rel_name << "\":" << std::endl;
313
314 for (OksObject::FList::const_iterator i = ref_by_objs->begin(); i != ref_by_objs->end(); ++i) {
315 std::cout << " - " << *i << std::endl;
316 }
317
318 delete ref_by_objs;
319 } else {
320 std::cout << o << " is not referenced by any object via relationship \"" << ref_by_rel_name
321 << '\"' << std::endl;
322 }
323 }
324 } else {
325 std::cout << *o << std::endl;
326 }
327 }
328 delete objs;
329 } else {
330 std::cout << std::endl;
331 }
332 } else {
333 Oks::error_msg(
"oks_dump") <<
"\tFailed to parse query \"" << query <<
"\" in class \"" << class_name
334 << "\"\n";
336 }
337 delete q;
338 } else {
339 std::cout << *
c << std::endl;
340 }
341 } else {
342 Oks::error_msg(
"oks_dump") <<
"\tCan not find class \"" << class_name <<
"\"\n";
344 }
345 } else if (object_from && *object_from && path_query && *path_query) {
347 try {
350
351 size_t num = (objs ? objs->size() : 0);
352 std::cout << "Found " << num << " objects in the path \"" << q << "\" from object " << obj_from << ":"
353 << std::endl;
354
355 if (num) {
356 while (!objs->empty()) {
358 objs->pop_front();
359 std::cout << *o << std::endl;
360 }
361 delete objs;
362 }
366 }
367 } else {
368 std::cout << kernel;
369 }
370
372 Oks::error_msg(
"oks_dump") <<
"The schema contains dangling references to non-loaded classes:\n"
374 }
375
377 Oks::error_msg(
"oks_dump") <<
"\tThe data contain dangling references to non-loaded objects\n";
378 }
379
382 }
384 std::cerr << "Caught oks exception:\n" << ex << std::endl;
386 } catch (std::exception& e) {
387 std::cerr << "Caught standard C++ exception: " << e.what() << std::endl;
389 } catch (...) {
390 std::cerr << "Caught unknown exception" << std::endl;
392 }
393
395}
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::unordered_set< OksObject *, oks::hash_obj_ptr, oks::equal_obj_ptr > FSet
std::list< OksObject * > List
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.
std::list< OksObject *, boost::fast_pool_allocator< OksObject * > > FList
OksObject::List * find_path(const oks::QueryPath &query) const
OksQuery(bool b, OksQueryExpression *q=0)
virtual const char * what() const noexcept
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)