Home > PostgreSQL > PgDBF: convert FoxPro to PostgreSQL’s non-Public Schema

PgDBF: convert FoxPro to PostgreSQL’s non-Public Schema

PgDBF: convert FoxPro to PostgreSQL’s non-Public Schema
Recently, I have to migrate Foxpro tables into PostgreSQL. I have downloaded the latest version of PgDBF originally written by Kirk Strauser (http://sourceforge.net/projects/pgdbf/files/latest/download?source=files), and successfully built the package. It run well. All foxpro data can only be stored in public schema, PgDBF does not offer option for other schema.
For that purpose I have created a patch. Once the package rebuilt, I can issue command with “upper-cased S” option -S myschema (assuming I have already had the lowercase-typed schema in my PostgreSQL database):
pgdbf -S myschema myfox.dbf
— pgdbf.c 2013-01-06 10:18:41.268248051 +0700
+++ pgdbf_new.c 2013-01-06 11:14:17.766415632 +0700
@@ -31,7 +31,7 @@

#include “pgdbf.h”

-#define STANDARDOPTS “cCdDeEhm:nNpPqQtTuU”
+#define STANDARDOPTS “cCdDeEhm:nNpPqQtTuUS:”

int main(int argc, char **argv) {
/* Describing the DBF file */
@@ -121,6 +121,8 @@
char *optinputcharset = NULL;
#endif

+ char *optcustomschema= NULL;
+
strcpy(optvalidargs, STANDARDOPTS);
#if defined(HAVE_ICONV)
/* Note that the declaration for optvalidargs currently reserves exactly
@@ -193,6 +195,9 @@
case ‘U’:
optusetruncatetable = 0;
break;
+ case ‘S’:
+ optcustomschema= optarg;
+ break;
case ‘h’:
default:
/* If we got here because someone requested ‘-h’, exit
@@ -211,9 +216,9 @@
if(optexitcode != -1) {
printf(
#if defined(HAVE_ICONV)
- “Usage: %s [-cCdDeEhtTuU] [-s encoding] [-m memofilename] filename [indexcolumn ...]\n”
+ “Usage: %s [-cCdDeEhtTuU] [-s encoding] [-S customschema] [-m memofilename] filename [indexcolumn ...]\n”
#else
- “Usage: %s [-cCdDeEhtTuU] [-m memofilename] filename [indexcolumn ...]\n”
+ “Usage: %s [-cCdDeEhtTuU] [-S customschema] [-m memofilename] filename [indexcolumn ...]\n”
#endif
“Convert the named XBase file into PostgreSQL format\n”
“\n”
@@ -234,6 +239,7 @@
#if defined(HAVE_ICONV)
” -s the encoding used in the file, to be converted to UTF-8\n”
#endif
+ ” -S the custom postgresql schema\n”
” -t wrap a transaction around the entire series of statements (default)\n”
” -T do not use an enclosing transaction\n”
” -u issue a ‘TRUNCATE’ command before inserting data\n”
@@ -288,7 +294,7 @@
* 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);
+ baretablename = malloc( (optcustomschema!=NULL ? (strlen(optcustomschema) + 1/*dot schema separator*/) : 0) + strlen(dbffilename) + 1 + optusequotedtablename * 2);
if(baretablename == NULL) {
exitwitherror(“Unable to allocate the bare tablename buffer”, 1);
}
@@ -304,6 +310,12 @@
/* Create tablename and baretablename at the same time. */
t = tablename;
u = baretablename;
+ if(optcustomschema!=NULL) {
+ for(i=0,j=strlen(optcustomschema);i<j;++i){
+ *u++=tolower(optcustomschema[i]);
+ }
+ *u++ = '.'; //dot schema separator
+ }
if(optusequotedtablename) *u++ = '"';
while(*s) {
if(*s == '.') {

About these ads
Categories: PostgreSQL
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: