Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user defined table name #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Note that the entire process is usually wrapped inside a transaction so that oth
## Command Line

```
Usage: pgdbf [-cCdDeEhnNpPqQtTuU] [-s encoding] [-m memofilename] [-i fieldname1,fieldname2,fieldnameN] filename [indexcolumn ...]
Usage: pgdbf [-cCdDeEhnNpPqQtTuU] [-s encoding] [-m memofilename] [-i fieldname1,fieldname2,fieldnameN] [-o tablename] filename [indexcolumn ...]
```

The only required argument is the filename of the table to be converted. If the table has a memo field, then use the "-m" option to specify the path to the memo file.
Expand All @@ -103,6 +103,8 @@ The "‐n" flag creates NUMERIC fields with type NUMERIC. **This is a new defaul

The "‐N" flag creates NUMERIC fields with type TEXT. This is the historical default setting. Use this if rows contain invalid number data in NUMERIC fields, which are essentially CHARACTER fields behind the scenes, and you receive errors when importing the data into PostgreSQL.

The "-o" flag uses the user-defined table name.

The "‐p" argument shows a progress bar during the conversion process.

The default "-P" argument hides the progress bar.
Expand Down
42 changes: 30 additions & 12 deletions src/pgdbf.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <sys/types.h>

#include "pgdbf.h"
#define STANDARDOPTS "cCdDeEhm:i:nNpPqQrRtTuU"
#define STANDARDOPTS "cCdDeEhm:i:nNo:pPqQrRtTuU"

int main(int argc, char **argv) {
/* Describing the DBF file */
Expand Down Expand Up @@ -121,6 +121,8 @@ int main(int argc, char **argv) {
char basename[MAXCOLUMNNAMESIZE];
int serial;

char *optusetablename = NULL;

#if defined(HAVE_ICONV)
/* Character encoding stuff */
char *optinputcharset = NULL;
Expand Down Expand Up @@ -183,6 +185,9 @@ int main(int argc, char **argv) {
case 'N':
optnumericasnumeric = 0;
break;
case 'o':
optusetablename = optarg;
break;
case 'p':
optshowprogress = 1;
break;
Expand Down Expand Up @@ -238,9 +243,9 @@ int main(int argc, char **argv) {
if(optexitcode != -1) {
printf(
#if defined(HAVE_ICONV)
"Usage: %s [-cCdDeEhtTuU] [-s encoding] [-m memofilename] [-i fieldname1,fieldname2,fieldnameN] filename [indexcolumn ...]\n"
"Usage: %s [-cCdDeEhtTuU] [-s encoding] [-m memofilename] [-i fieldname1,fieldname2,fieldnameN] [-o tablename] filename [indexcolumn ...]\n"
#else
"Usage: %s [-cCdDeEhtTuU] [-m memofilename] [-i fieldname1,fieldname2,fieldnameN] filename [indexcolumn ...]\n"
"Usage: %s [-cCdDeEhtTuU] [-m memofilename] [-i fieldname1,fieldname2,fieldnameN] [-o tablename] filename [indexcolumn ...]\n"
#endif
"Convert the named XBase file into PostgreSQL format\n"
"\n"
Expand All @@ -255,12 +260,13 @@ int main(int argc, char **argv) {
" -m the name of the associated memo file (if necessary)\n"
" -n use type 'NUMERIC' for NUMERIC fields (default)\n"
" -N use type 'TEXT' for NUMERIC fields\n"
" -r remove padding at the end of TEXT and VARCHAR fields (default)\n"
" -R keep padding at the end of TEXT and VARCHAR fields\n"
" -o output table name\n"
" -p show a progress bar during processing\n"
" -P do not show a progress bar\n"
" -q enclose the table name in quotation marks whenever used in statements\n"
" -Q do not enclose the table name in quotation marks (default)\n"
" -r remove padding at the end of TEXT and VARCHAR fields (default)\n"
" -R keep padding at the end of TEXT and VARCHAR fields\n"
#if defined(HAVE_ICONV)
" -s the encoding used in the file, to be converted to UTF-8\n"
#endif
Expand Down Expand Up @@ -307,9 +313,13 @@ int main(int argc, char **argv) {
}
#endif

/* Calculate the table's name based on the DBF filename */
/* Calculate the table's name based on the DBF filename or optusetablename */
dbffilename = argv[optind];
tablename = malloc(strlen(dbffilename) + 1);
if (optusetablename == NULL)
tablename = malloc(strlen(dbffilename) + 1);
else
tablename = malloc(strlen(optusetablename) + 1);

if(tablename == NULL) {
exitwitherror("Unable to allocate the tablename buffer", 1);
}
Expand All @@ -318,19 +328,27 @@ int main(int argc, char **argv) {
* is used for other things, like creating the names of indexes. Despite
* its name, baretablename may be surrounded by quote marks if the "-q"
* option for optusequotedtablename is given. */
baretablename = malloc(strlen(dbffilename) + 1 + optusequotedtablename * 2);
if (optusetablename == NULL)
baretablename = malloc(strlen(dbffilename) + 1 + optusequotedtablename * 2);
else
baretablename = malloc(strlen(optusetablename) + 1 + optusequotedtablename * 2);

if(baretablename == NULL) {
exitwitherror("Unable to allocate the bare tablename buffer", 1);
}
/* Find the first character after the final slash, or the first
* character of the filename if no slash is present, and copy from that
* point to the period in the extension into the tablename string. */
for(s = dbffilename + strlen(dbffilename) - 1; s != dbffilename; s--) {
if(*s == '/') {
s++;
break;
if (optusetablename == NULL) {
for(s = dbffilename + strlen(dbffilename) - 1; s != dbffilename; s--) {
if(*s == '/') {
s++;
break;
}
}
}
else
s = optusetablename;
/* Create tablename and baretablename at the same time. */
t = tablename;
u = baretablename;
Expand Down