65 va_list ColumnNameArg;
70 va_start(ColumnNameArg, NumColumns);
76 va_end(ColumnNameArg);
77 ERROR_RETURN(
"sqlCopy malloc")
81 if (BufSize < 1) BufSize = 1;
82 pCopy->DataBuf = calloc(BufSize,
sizeof(
char));
88 if ((!pCopy->DataBuf) || (!pCopy->
TableName))
91 va_end(ColumnNameArg);
92 ERROR_RETURN(
"sqlCopyCreate")
107 for (ColIdx = 0; ColIdx < NumColumns; ColIdx++)
109 ColStr = va_arg(ColumnNameArg,
char *);
110 ColStrLength += strlen(ColStr) + 1;
113 if (ColIdx != 0) strncat(pCopy->
ColumnNames,
",", 1);
119 va_end(ColumnNameArg);
120 ERROR_RETURN(
"pCopy->ColumnNames size too small")
123 va_end(ColumnNameArg);
129 int tmp_printhex(
char * str)
131 while(*str) printf(
"%02x", *str++);
161 char* dptr = DataRow;
162 char* NewRow = 0, * nptr;
170 if (((*dptr ==
'\n') || (*dptr ==
'\r')) && (*(dptr + 1))) rncount++;
179 NewRowLen = strlen(DataRow) + rncount;
180 NewRow = malloc(NewRowLen + 1);
182 ERROR_RETURN(
"fo_sqlCopyAdd: out of memory");
185 while (*dptr && *(dptr + 1))
192 else if (*dptr ==
'\r')
207 NewRowLen = strlen(DataRow);
215 pCopy->DataBuf = realloc(pCopy->DataBuf, NewRowLen +
growby);
217 ERROR_RETURN(
"fo_sqlCopyAdd: Realloc for DataBuf failed");
228 strcpy(pCopy->DataBuf + pCopy->
DataIdx, DataRow);
232 if (DataRow[NewRowLen - 1] !=
'\n')
234 pCopy->DataBuf[pCopy->
DataIdx++] =
'\n';
235 pCopy->DataBuf[pCopy->
DataIdx] = 0;
238 if (NewRow) free(NewRow);
259 ERROR_RETURN(
"Null pCopy");
260 if (pCopy->
DataIdx == 0)
return (1);
263 sprintf(copystmt,
"COPY %s(%s) from stdin",
266 result = PQexec(pCopy->
pGconn, copystmt);
267 if (PGRES_COPY_IN == PQresultStatus(result))
270 if (PQputCopyData(pCopy->
pGconn, pCopy->DataBuf, pCopy->
DataIdx) != 1)
271 ERROR_RETURN(PQresultErrorMessage(result))
277 if (PQputCopyEnd(pCopy->
pGconn, NULL) == 1)
279 result = PQgetResult(pCopy->
pGconn);
309 if (pCopy->DataBuf) free(pCopy->DataBuf);
328 printf(
"========== fo_sqlCopyPrint Start ================\n");
329 printf(
"pCopy: %lx, TableName: %s, BufSize: %d, DataIdx: %d\n",
330 (
long) pCopy, pCopy->
TableName, pCopy->BufSize, pCopy->DataIdx);
331 printf(
" ColumnNames: %s\n", pCopy->ColumnNames);
333 if (PrintBytes == 0) PrintBytes = pCopy->DataIdx;
334 for (idx = 0; idx < PrintBytes; idx++) putchar(pCopy->DataBuf[idx]);
336 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...
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).
#define growby
Grow DataBuf by this number of bytes.
void fo_sqlCopyDestroy(psqlCopy_t pCopy, int ExecuteFlag)
Destructor for sqlCopy_struct.
void fo_sqlCopyPrint(psqlCopy_t pCopy, int PrintBytes)
Print the sqlCopy_struct.
int fo_sqlCopyAdd(psqlCopy_t pCopy, char *DataRow)
Add a data row to an sqlCopy Use '' to pass in a null.