"COPY FROM" recognize \xDD sequence - addition to copy.c & idea 4 developers

From: "Igor Georgiev" <gory(at)alphasoft-bg(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: "COPY FROM" recognize \xDD sequence - addition to copy.c & idea 4 developers
Date: 2002-10-16 17:48:05
Message-ID: 003501c2753c$2f660d60$0b0546c0@alpha.bg
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

1. Why i do this:
I try to migrate a database with a 200 tables from Sybase SQL Anywhere to PostgreSQL,
but SQL Anywhere escapes special characters like a HEX values ( like \x0D \x2C ..... ).
PostgreSQL COPY FROM recognize only OCT values ( lie \001 ... )
2. How-to it' easy :)))
2.1 - Open $UrSourceDir/src/backend/commands/copy.c
2.2 - Add #include <ctype.h> in te begining
2.3 find function
static char *
CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_print)
/*----------------*/
/*-------- Add this code before it --------*/
static int
HEXVALUE( int c )
{
if (isdigit(c))
{
c -= '0';
}
else
{
if (islower(c))
c= c-'a'+10;
else
c= c-'A'+10;
}
return(c);
}
2.4 in body of CopyReadAttribute
find this code and modify it like this
if (c == '\\')
{
c = CopyGetChar(fp);
if (c == EOF)
goto endOfFile;
switch (c)
{
/*------ Here is my additional code ------*/
case 'x':
case 'X':
{
int val;
CopyDonePeek(fp, c, true /*pick up*/); /* Get x always */
c = CopyPeekChar(fp); /* Get next */
if (isxdigit(c))
{
val = HEXVALUE(c);
c = CopyPeekChar(fp);
if (isxdigit(c))
{
val = (val << 4) + HEXVALUE(c);
CopyDonePeek(fp, c, true /*pick up*/);
}
else
{
if (c == EOF)
goto endOfFile;
CopyDonePeek(fp, c, false /*put back*/);
}
}
else
{
if (c == EOF)
goto endOfFile;
CopyDonePeek(fp, c, false /*put back*/);
}
c = val;
}
break;
/*------ End of my additional code ------*/
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
int val;
val = OCTVALUE(c);
2.4 he he now make , make install ....
3. An idea to developers : maybe u include this addition to COPY in future releases
10x

P.S. Excuse me for my English ( i'm better in C :)

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Curtis Faith 2002-10-16 18:08:21 Re: Postgresql and multithreading
Previous Message Igor Georgiev 2002-10-16 17:43:27 "COPY FROM" recognize \xDD sequence - addition to copy.c & idea 4 developers