54 va_list ColumnNameArg;
59 va_start(ColumnNameArg, NumColumns);
65 va_end(ColumnNameArg);
66 ERROR_RETURN(
"sqlCopy malloc")
70 if (BufSize < 1) BufSize = 1;
71 pCopy->DataBuf = calloc(BufSize,
sizeof(
char));
77 if ((!pCopy->DataBuf) || (!pCopy->
TableName))
80 va_end(ColumnNameArg);
81 ERROR_RETURN(
"sqlCopyCreate")
96 for (ColIdx = 0; ColIdx < NumColumns; ColIdx++)
98 ColStr = va_arg(ColumnNameArg,
char *);
99 ColStrLength += strlen(ColStr) + 1;
102 if (ColIdx != 0) strncat(pCopy->
ColumnNames,
",", 2);
108 va_end(ColumnNameArg);
109 ERROR_RETURN(
"pCopy->ColumnNames size too small")
112 va_end(ColumnNameArg);
118 int tmp_printhex(
char * str)
120 while(*str) printf(
"%02x", *str++);
150 char* dptr = DataRow;
151 char* NewRow = 0, * nptr;
159 if (((*dptr ==
'\n') || (*dptr ==
'\r')) && (*(dptr + 1))) rncount++;
168 NewRowLen = strlen(DataRow) + rncount;
169 NewRow = malloc(NewRowLen + 1);
171 ERROR_RETURN(
"fo_sqlCopyAdd: out of memory");
174 while (*dptr && *(dptr + 1))
181 else if (*dptr ==
'\r')
196 NewRowLen = strlen(DataRow);
204 pCopy->DataBuf = realloc(pCopy->DataBuf, NewRowLen +
growby);
206 ERROR_RETURN(
"fo_sqlCopyAdd: Realloc for DataBuf failed");
217 strcpy(pCopy->DataBuf + pCopy->
DataIdx, DataRow);
221 if (DataRow[NewRowLen - 1] !=
'\n')
223 pCopy->DataBuf[pCopy->
DataIdx++] =
'\n';
224 pCopy->DataBuf[pCopy->
DataIdx] = 0;
227 if (NewRow) free(NewRow);
248 ERROR_RETURN(
"Null pCopy");
249 if (pCopy->
DataIdx == 0)
return (1);
252 sprintf(copystmt,
"COPY %s(%s) from stdin",
255 result = PQexec(pCopy->
pGconn, copystmt);
256 if (PGRES_COPY_IN == PQresultStatus(result))
259 if (PQputCopyData(pCopy->
pGconn, pCopy->DataBuf, pCopy->
DataIdx) != 1)
260 ERROR_RETURN(PQresultErrorMessage(result))
266 if (PQputCopyEnd(pCopy->
pGconn, NULL) == 1)
268 result = PQgetResult(pCopy->
pGconn);
298 if (pCopy->DataBuf) free(pCopy->DataBuf);
317 printf(
"========== fo_sqlCopyPrint Start ================\n");
318 printf(
"pCopy: %lx, TableName: %s, BufSize: %d, DataIdx: %d\n",
322 if (PrintBytes == 0) PrintBytes = pCopy->
DataIdx;
323 for (idx = 0; idx < PrintBytes; idx++) putchar(pCopy->DataBuf[idx]);
325 printf(
"========== fo_sqlCopyPrint End ================");
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
int fo_sqlCopyAdd(psqlCopy_t pCopy, char *DataRow)
Add a data row to an sqlCopy Use '\N' to pass in a null.
void fo_sqlCopyPrint(psqlCopy_t pCopy, int PrintBytes)
Print the sqlCopy_struct.
#define growby
Grow DataBuf by this number of bytes.
void fo_sqlCopyDestroy(psqlCopy_t pCopy, int ExecuteFlag)
Destructor for sqlCopy_struct.
psqlCopy_t fo_sqlCopyCreate(PGconn *pGconn, char *TableName, int BufSize, int NumColumns,...)
Constructor for sqlCopy_struct.
int fo_sqlCopyExecute(psqlCopy_t pCopy)
Execute the copy (ie insert the buffered records into the database).