File: sql2dbf.c

package info (click to toggle)
dbf2sql 2.0-6
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 104 kB
  • ctags: 119
  • sloc: ansic: 1,468; makefile: 108
file content (386 lines) | stat: -rw-r--r-- 8,972 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*	utility to read out an table from mSQL or Postgres95, and store it
    into a DBF-file

	M. Boekhold (M.Boekhold@et.tudelft.nl) April 1996
	oktober 1996 added support for Postgres95
*/

#ifndef POSTGRES95
	#ifndef MSQL
		#error One of POSTGRES95 or MSQL *must* be defined in the Makefile
	#endif
#endif

/*
#ifdef POSTGRES95
	#error Not implemented for Postgres95 yet, sorry, I'm working on it
#endif
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#ifdef POSTGRES95
	#include <libpq-fe.h>
#endif
#ifdef MSQL
	#include <msql.h>
#endif
#include "dbf.h"

int		verbose = 0, upper = 0, lower = 0, create = 0;
long	precision = 6;
char	*host = NULL;
char	*dbase = NULL;
char	*table = NULL;

inline void strtoupper(char *string);
inline void strtolower(char *string);
void usage(void);

inline void strtoupper(char *string) {
        while(*string != '\0') {
                *string = toupper(*string);
				*string++;
        }
}

inline void strtolower(char *string) {
        while(*string != '\0') {
                *string = tolower(*string);
				*string++;
        }
}

#ifdef POSTGRES95
void usage(void) {
	printf("pg2dbf %s\n", VERSION);
	printf("usage:\tpg2dbf [-h host] [-u | -l] [-v[v]]\n");
	printf("\t\t\t[-p precision] [-q query] -d dbase -t table dbf-file\n");
}
#endif
#ifdef MSQL
void usage(void) {
	printf("msql2dbf %s\n", VERSION);
	printf("usage:\tmsql2dbf [-h host] [-u | -l] [-v[v]]\n");
	printf("\t\t\t[-p precision] [-q query] -d dbase -t table dbf-file\n");
}
#endif

int main(int argc, char **argv) {
#ifdef POSTGRES95
	PGconn			*conn;
	PGresult		*qres;
	double			dvalue;
	unsigned int	ivalue;
#endif
#ifdef MSQL
	int             conn;
	m_result		*qres;
	m_row			qrow;
	m_field			*qfield;
#endif
	int				i;
	extern int      optind;
	extern char     *optarg;
	char            *query = NULL;
	dbhead          *dbh;
	field			*rec;
	u_long			numfields, numrows, t;

	while ((i = getopt(argc, argv, "lucvq:h:d:t:p:")) != EOF) {
		switch(i) {
			case 'q':
				query = strdup(optarg);
				break;
			case 'v':
				verbose++;
				break;
			case 'c':
				create = 1;
				break;
			case 'l':
				if (upper) {
					usage();
					printf("Can't use -u and -l at the same time!\n");
					exit(1);
				}
				lower = 1;
				break;
			case 'u':
				if (lower) {
					usage();
					printf("Can't use -u and -l at the same time!\n");
					exit(1);
				}
				upper = 1;
				break;
			case 'h':
				host = (char *)strdup(optarg);
				break;
			case 'd':
				dbase = (char *)strdup(optarg);
				break;
			case 't':
				table = (char *)strdup(optarg);
				break;
			case 'p':
				precision = strtol(optarg, (char **)NULL, 10);
				if ((precision == LONG_MIN) || (precision == LONG_MAX)) {
					usage();
					printf("Invalid precision specified!\n");
					exit(1);
				}
				break;
			case ':':
				usage();
				printf("Missing argument!\n");
				exit(1);
			case '?':
				usage();
				printf("Unknown argument: %s\n", argv[0]);
				exit(1);
			default:
				break;
		}
	}

	argc -= optind;
	argv = &argv[optind];

	if (argc != 1) {
		usage();
		exit(1);
	}

	if ((dbase == NULL) || ((table == NULL) && (query == NULL))) {
		usage();
		printf("Dbase and table *must* be specified!\n");
		exit(1);
	}

	if (query == NULL) {
		query = (char *)malloc(14+strlen(table));
		sprintf(query, "SELECT * FROM %s", table);
	}

	if (verbose > 1) {
		printf("Opening %s for writing (previous contents will be lost)\n",
																	argv[0]);
	}

	if ((dbh = dbf_open_new(argv[0], O_WRONLY | O_CREAT | O_TRUNC)) ==
												(dbhead *)DBF_ERROR) {
		fprintf(stderr, "Couldn't open xbase-file for writing: %s\n", argv[0]);
		exit(1);
	}

	if (verbose > 1) {
		printf("Making connection with SQL-server\n");
	}

#ifdef POSTGRES95
	conn = PQsetdb(host,NULL,NULL,NULL, dbase);
	if (PQstatus(conn) != CONNECTION_OK) {
		fprintf(stderr, "Couldn't get a connection with the ");
		fprintf(stderr, "designated host!\n");
		fprintf(stderr, "Detailed report: %s\n", PQerrorMessage(conn));
		close(dbh->db_fd);
		free(dbh);
		exit(1);
	}
#endif
#ifdef MSQL
    if ((conn = msqlConnect(host)) == -1) {
        fprintf(stderr, "Couldn't get a connection with the ");
        fprintf(stderr, "designated host!\n");
        fprintf(stderr, "Detailed report: %s\n", msqlErrMsg);
        close(dbh->db_fd);
        free(dbh);
        exit(1);
    }

    if (verbose > 1) {
        printf("Selecting database\n");
    }

    if ((msqlSelectDB(conn, dbase)) == -1) {
        fprintf(stderr, "Couldn't select database %s.\n", dbase);
        fprintf(stderr, "Detailed report: %s\n", msqlErrMsg);
        close(dbh->db_fd);
        free(dbh);
        msqlClose(conn);
        exit(1);
    }
#endif

	if (verbose > 1) {
		printf("Sending query\n");
	}

#ifdef POSTGRES95
	qres = PQexec(conn, query);
	if ((!res) || (PGRES_TUPLES_OK != PQresultStatus(qres))) {
		fprintf(stderr, "Error sending query.\nDetailed report: %s\n",
				PQerrorMessage(conn));
		if (verbose > 1) {
			fprintf(stderr, "%s\n", query);
		}
		PQfinish(conn);
		close(dbh->db_fd);
		free(dbh);
		exit(1);
	}

	numfields		= PQnfields(qres);
	numrows			= PQntuples(qres);
#endif
#ifdef MSQL
	if (msqlQuery(conn, query) == -1) {
		fprintf(stderr, "Error sending query.\nDetailed report: %s\n",
				msqlErrMsg);
		if (verbose > 1) {
			fprintf(stderr, "%s\n", query);
		}
		msqlClose(conn);
		close(dbh->db_fd);
		free(dbh);
		exit(1);
	}

    qres            = msqlStoreResult();
    numfields       = msqlNumFields(qres);
	numrows			= msqlNumRows(qres);
#endif

/* this part needs to be heavely rewritten for Postgres95
	Biggest problem is the many many types that Postgres95 supports, and
	the fact that the types are encoded in some internal format.
	These are available from POSTGRESDIR/src/backend/catalog/pg_type.h, but
	it's very cryptic.
	They are also obtainable from the server in text-format, see
	POSTGRESDIR/src/bin/psql/psql.c for an example on how to do this.
*/
#ifdef POSTGRES95
	for (i = 0; i < numfields; i++) {
		switch (PQftype(qres, i)) {
/* assume INT's have a max. of 10 digits (4^32 has 10 digits) */
			case INT_TYPE:
				dbf_add_field(dbh, qfield->name, 'N', 10, 0);
				if (verbose > 1) {
					printf("Adding field: %s, INT_TYPE, %d\n", qfield->name,
															   qfield->length);
				}
				break;
/* do we have to make the same assumption here? */
			case REAL_TYPE:
				dbf_add_field(dbh, qfield->name, 'N', qfield->length,
								(int) precision);
				if (verbose > 1) {
					printf("Adding field: %s, INT_REAL, %d\n", qfield->name,
															   qfield->length);
				}
				break;
			case CHAR_TYPE:
				dbf_add_field(dbh, qfield->name, 'C', qfield->length, 0);
				if (verbose > 1) {
					printf("Adding field: %s, INT_CHAR, %d\n", qfield->name,
															   qfield->length);
				}
				break;
			default:
				break;
		}
	}
#endif
/* mSQL makes this easy, but is limited in datatypes */
#ifdef MSQL
	while ((qfield = msqlFetchField(qres))) {
		strtoupper(qfield->name);
		switch (qfield->type) {
/* assume INT's have a max. of 10 digits (4^32 has 10 digits) */
			case INT_TYPE:
				dbf_add_field(dbh, qfield->name, 'N', 10, 0);
				if (verbose > 1) {
					printf("Adding field: %s, INT_TYPE, %d\n", qfield->name,
															   qfield->length);
				}
				break;
/* do we have to make the same assumption here? */
			case REAL_TYPE:
				dbf_add_field(dbh, qfield->name, 'N', qfield->length,
								(int) precision);
				if (verbose > 1) {
					printf("Adding field: %s, INT_REAL, %d\n", qfield->name,
															   qfield->length);
				}
				break;
			case CHAR_TYPE:
				dbf_add_field(dbh, qfield->name, 'C', qfield->length, 0);
				if (verbose > 1) {
					printf("Adding field: %s, INT_CHAR, %d\n", qfield->name,
															   qfield->length);
				}
				break;
			default:
				break;
		}
	}
#endif

	if (verbose > 1) {
		printf("Writing out header and field-descriptions\n");
	}

	dbf_write_head(dbh);
	dbf_put_fields(dbh);

/* Also needs to be adjusted for use with Postgres95, however, this will
	be easier then the previous part */
	if ((rec = dbf_build_record(dbh)) == (field *)DBF_ERROR) {
		fprintf(stderr, "Error allocating memory for record-contents!\n");
	} else {
		if (numrows) {
#ifdef MSQL
			while ((qrow = msqlFetchRow(qres)) != NULL) {
				for (t = 0; t < numfields; t++) {
					if (qrow[t] != 0) {
						strcpy(rec[t].db_contents, qrow[t]);
					} else {
						rec[t].db_contents[0] = '\0';
					}
					if (upper) {
						strtoupper(rec[t].db_contents);
					}
					if (lower) {
						strtolower(rec[t].db_contents);
					}
				}
				dbf_put_record(dbh, rec, dbh->db_records + 1);
			}
#endif
		}
		dbf_free_record(dbh, rec);
	}

	if (verbose > 1) {
		printf("Writing out header\n");
	}

	dbf_write_head(dbh);

	if (verbose > 1) {
		printf("Closing up\n");
	}
				
#ifdef MSQL
	msqlFreeResult(qres);
	msqlClose(conn);
#endif
	close(dbh->db_fd);
	free(dbh);
	exit(0);
}