1 /* -------------------------------------------------------------------------
4 * $Header: /cvsroot/pgsql/contrib/pg_dumplo/Attic/main.c,v 1.11 2002/09/04 20:31:07 momjian Exp $
7 * -------------------------------------------------------------------------
10 /* We import postgres_fe.h mostly to get the HAVE_GETOPT_LONG configure result. */
12 #include "postgres_fe.h"
22 #include <libpq/libpq-fs.h>
24 #include "pg_dumplo.h"
26 #ifdef HAVE_GETOPT_LONG
29 #define required_argument 1
34 char *progname = NULL;
36 int main(int argc, char **argv);
37 static void usage(void);
38 static void parse_lolist(LODumpMaster * pgLO);
42 * The mother of all C functions
46 main(int argc, char **argv)
65 pgLO->lolist_start = 0;
77 #ifdef HAVE_GETOPT_LONG
79 static struct option l_opt[] = {
80 {"help", no_argument, 0, 'h'},
81 {"user", required_argument, 0, 'u'},
82 {"pwd", required_argument, 0, 'p'},
83 {"db", required_argument, 0, 'd'},
84 {"host", required_argument, 0, 'h'},
85 {"port", required_argument, 0, 'o'},
86 {"space", required_argument, 0, 's'},
87 {"import", no_argument, 0, 'i'},
88 {"export", no_argument, 0, 'e'},
89 {"remove", no_argument, 0, 'r'},
90 {"quiet", no_argument, 0, 'q'},
91 {"all", no_argument, 0, 'a'},
92 {"show", no_argument, 0, 'w'},
96 while ((arg = getopt_long(argc, argv, "?aeho:u:p:qd:l:t:irs:w", l_opt, &l_index)) != -1)
99 while ((arg = getopt(argc, argv, "?aeho:u:p:qd:l:t:irs:w")) != -1)
109 pgLO->user = strdup(optarg);
112 pgLO->host = strdup(optarg);
115 pgLO->port = strdup(optarg);
118 pwd = strdup(optarg);
121 pgLO->db = strdup(optarg);
124 pgLO->space = strdup(optarg);
127 pgLO->action = ACTION_IMPORT;
130 pgLO->action = ACTION_EXPORT_ATTR;
131 pgLO->lolist_start = optind - 1;
136 pgLO->action = ACTION_EXPORT_ALL;
139 pgLO->action = ACTION_SHOW;
148 fprintf(stderr, "%s: bad arg -%c\n", progname, arg);
163 if (!pgLO->space && !pgLO->action == ACTION_SHOW)
165 if (!(pgLO->space = getenv("PWD")))
167 fprintf(stderr, "%s: not set space for dump-tree (option '-s' or $PWD).\n", progname);
174 fprintf(stderr, "%s: What do you want - export or import?\n", progname);
181 pgLO->conn = PQsetdbLogin(pgLO->host, pgLO->port, NULL, NULL, pgLO->db,
184 if (PQstatus(pgLO->conn) == CONNECTION_BAD)
186 fprintf(stderr, "%s (connection): %s\n", progname, PQerrorMessage(pgLO->conn));
189 pgLO->host = PQhost(pgLO->conn) ? PQhost(pgLO->conn) : "localhost";
190 pgLO->db = PQdb(pgLO->conn);
191 pgLO->user = PQuser(pgLO->conn);
197 if (pgLO->action != ACTION_SHOW)
200 PQexec(pgLO->conn, "BEGIN");
202 switch (pgLO->action)
206 case ACTION_EXPORT_ALL:
210 case ACTION_EXPORT_ATTR:
214 if (pgLO->action == ACTION_SHOW)
215 printf("\nDatabase '%s' contains %d large objects.\n\n", pgLO->db, pgLO->counter);
217 printf("\nExported %d large objects.\n\n", pgLO->counter);
224 printf("\nImported %d large objects.\n\n", pgLO->counter);
228 PQexec(pgLO->conn, "COMMIT");
229 PQfinish(pgLO->conn);
231 if (pgLO->action != ACTION_SHOW)
238 parse_lolist(LODumpMaster * pgLO)
243 buff[MAX_TABLE_NAME + MAX_ATTR_NAME + 1];
245 pgLO->lolist = (LOlist *) malloc(pgLO->argc * sizeof(LOlist));
249 fprintf(stderr, "%s: can't allocate memory\n", progname);
253 for (d = pgLO->argv + pgLO->lolist_start, ll = pgLO->lolist;
258 strncpy(buff, *d, MAX_TABLE_NAME + MAX_ATTR_NAME);
260 if ((loc = strchr(buff, '.')) == NULL)
262 fprintf(stderr, "%s: '%s' is bad 'table.attr'\n", progname, buff);
266 ll->lo_table = strdup(buff);
267 ll->lo_attr = strdup(++loc);
270 ll->lo_table = ll->lo_attr = (char *) NULL;
277 printf("\npg_dumplo %s - PostgreSQL large objects dump\n", VERSION);
278 puts("pg_dumplo [option]\n\n"
280 #ifdef HAVE_GETOPT_LONG
282 "-h --help this help\n"
283 "-u --user=<username> username for connection to server\n"
284 "-p --password=<password> password for connection to server\n"
285 "-d --db=<database> database name\n"
286 "-t --host=<hostname> server hostname\n"
287 "-o --port=<port> database server port (default: 5432)\n"
288 "-s --space=<dir> directory with dump tree (for export/import)\n"
289 "-i --import import large obj dump tree to DB\n"
290 "-e --export export (dump) large obj to dump tree\n"
291 "-l <table.attr ...> dump attribute (columns) with LO to dump tree\n"
292 "-a --all dump all LO in DB (default)\n"
293 "-r --remove if is set '-i' try remove old LO\n"
294 "-q --quiet run quietly\n"
295 "-w --show not dump, but show all LO in DB\n"
300 "-u <username> username for connection to server\n"
301 "-p <password> password for connection to server\n"
302 "-d <database> database name\n"
303 "-t <hostname> server hostname\n"
304 "-o <port> database server port (default: 5432)\n"
305 "-s <dir> directory with dump tree (for export/import)\n"
306 "-i import large obj dump tree to DB\n"
307 "-e export (dump) large obj to dump tree\n"
308 "-l <table.attr ...> dump attribute (columns) with LO to dump tree\n"
309 "-a dump all LO in DB (default)\n"
310 "-r if is set '-i' try remove old LO\n"
312 "-w not dump, but show all LO in DB\n"
317 "Example (dump): pg_dumplo -d my_db -s /my_dump/dir -l t1.a t1.b t2.a\n"
318 " pg_dumplo -a -d my_db -s /my_dump/dir\n"
319 "Example (import): pg_dumplo -i -d my_db -s /my_dump/dir\n"
320 "Example (show): pg_dumplo -w -d my_db\n\n"
321 "Note: * option '-l' must be last option!\n"
322 " * option '-i' without option '-r' make new large obj in DB\n"
323 " not rewrite old, the '-i' UPDATE oid numbers in table.attr only!\n"
324 " * if is not set option -s, the pg_dumplo use $PWD\n"