* Parse the current line into separate attributes (fields), * performing de-escaping as needed. This has exactly the same API as * CopyReadAttributesText, except we parse the fields according to * "standard" (i.e. common) CSV usage. */
| 1551 | * "standard" (i.e. common) CSV usage. |
| 1552 | */ |
| 1553 | int |
| 1554 | CopyReadAttributesCSV(CopyFromState cstate, int stop_processing_at_field) |
| 1555 | { |
| 1556 | char delimc = cstate->opts.delim[0]; |
| 1557 | bool delim_off = cstate->opts.delim_off; |
| 1558 | char quotec = cstate->opts.quote[0]; |
| 1559 | char escapec = cstate->opts.escape[0]; |
| 1560 | int fieldno; |
| 1561 | char *output_ptr; |
| 1562 | char *cur_ptr; |
| 1563 | char *line_end_ptr; |
| 1564 | |
| 1565 | /* |
| 1566 | * We need a special case for zero-column tables: check that the input |
| 1567 | * line is empty, and return. |
| 1568 | */ |
| 1569 | if (cstate->max_fields <= 0) |
| 1570 | { |
| 1571 | if (cstate->line_buf.len != 0) |
| 1572 | ereport(ERROR, |
| 1573 | (errcode(ERRCODE_BAD_COPY_FILE_FORMAT), |
| 1574 | errmsg("extra data after last expected column"))); |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
| 1578 | resetStringInfo(&cstate->attribute_buf); |
| 1579 | |
| 1580 | /* |
| 1581 | * The de-escaped attributes will certainly not be longer than the input |
| 1582 | * data line, so we can just force attribute_buf to be large enough and |
| 1583 | * then transfer data without any checks for enough space. We need to do |
| 1584 | * it this way because enlarging attribute_buf mid-stream would invalidate |
| 1585 | * pointers already stored into cstate->raw_fields[]. |
| 1586 | */ |
| 1587 | if (cstate->attribute_buf.maxlen <= cstate->line_buf.len) |
| 1588 | enlargeStringInfo(&cstate->attribute_buf, cstate->line_buf.len); |
| 1589 | output_ptr = cstate->attribute_buf.data; |
| 1590 | |
| 1591 | /* set pointer variables for loop */ |
| 1592 | cur_ptr = cstate->line_buf.data + cstate->line_buf.cursor; |
| 1593 | line_end_ptr = cstate->line_buf.data + cstate->line_buf.len; |
| 1594 | |
| 1595 | /* Outer loop iterates over fields */ |
| 1596 | fieldno = 0; |
| 1597 | for (;;) |
| 1598 | { |
| 1599 | bool found_delim = false; |
| 1600 | bool saw_quote = false; |
| 1601 | char *start_ptr; |
| 1602 | char *end_ptr; |
| 1603 | int input_len; |
| 1604 | |
| 1605 | /* |
| 1606 | * In QD, stop once we have processed the last field we need in the QD. |
| 1607 | */ |
| 1608 | if (fieldno == stop_processing_at_field) |
| 1609 | { |
| 1610 | cstate->stopped_processing_at_delim = true; |
no test coverage detected