/* * Copyright (c) 1995 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "t2c.h" #include #include #include #include "Table.h" #include #ifdef BOOTSTRAP #include "bootstrap.h" #else #include "All_Objects.h" #endif static void usage(const char *prog) { cerr << "usage: " << prog << " [-d database] [-o owner] [objects ...]" << endl; exit(1); } int main(int argc, char *argv[]) { char *dbname = "scott/tiger"; char *owner = NULL; char *prog = argv[0]; GetOpt opt(argc, argv, "d:o:?h"); char optch; while((optch = opt()) != FAIL) switch(optch) { case 'd': dbname = opt.optarg; break; case 'o': owner = opt.optarg; break; default: usage(prog); break; } cout << "This is t2c. Copyright (c) 1995 Gunther Schadow. " "All rights reserved." << endl; cout << "using " << dbname << endl; Table::use(dbname); All_Objects aob; /* Construct WHERE clause (two passes) */ #define CLAUSE "WHERE ( object_type = 'TABLE' OR object_type = 'VIEW' )" #define OWNER_SUBCLAUSE " AND owner = '" #define CONJUNCTION_START " AND object_name IN ( '" #define CONJUNCTION_MORE "', '" #define CONJUNCTION_END "' )" /* Pass one: determine length of where clause */ size_t len = sizeof(CLAUSE); if(owner != NULL) len += sizeof(OWNER_SUBCLAUSE) - 1 + strlen(owner) + 1; if(argc > opt.optind) { len += sizeof(CONJUNCTION_START) - 1 + sizeof(CONJUNCTION_END) - 1; for(u_int i = opt.optind; i < argc; i++) { len += strlen(argv[i]); if(i + 1 < argc) len += sizeof(CONJUNCTION_MORE) - 1; } } /* Pass two: construct actual clause */ char clause[len]; strcpy(clause, CLAUSE); if(owner != NULL) { strcat(clause, OWNER_SUBCLAUSE); strcat(clause, owner); strcat(clause, "'"); } if(argc > opt.optind) { strcat(clause, CONJUNCTION_START); for(u_int i = opt.optind; i < argc; i++) { strcat(clause, argv[i]); if(i + 1 < argc) strcat(clause, CONJUNCTION_MORE); } strcat(clause, CONJUNCTION_END); } /* Retrieve and generate code */ for(result r = aob.retrieve(clause); r == SUCCESS; r = aob.next()) { cout << aob.get_owner() << '.' << aob.get_object_name() << endl; struct decl_rec *decl = make_decl(aob.get_owner(), aob.get_object_name()); make_class(aob.get_owner(), aob.get_object_name(), aob.get_object_type(), decl); remove_decl(decl); } return 0; } void make_class(const char *owner, const char* name, const char* type, const struct decl_rec *decl) { char *namebuf = strdup(name); output_interface(owner, namebuf, type, decl); output_inline(owner, namebuf, type, decl); output_definition(owner, namebuf, type, decl); free(namebuf); }