@@ -1550,6 +1550,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
1550
1550
TableInfo * tbinfo = tdinfo -> tdtable ;
1551
1551
const char * classname = tbinfo -> dobj .name ;
1552
1552
PQExpBuffer q = createPQExpBuffer ();
1553
+ PQExpBuffer insertStmt = NULL ;
1553
1554
PGresult * res ;
1554
1555
int tuple ;
1555
1556
int nfields ;
@@ -1591,34 +1592,57 @@ dumpTableData_insert(Archive *fout, void *dcontext)
1591
1592
nfields = PQnfields (res );
1592
1593
for (tuple = 0 ; tuple < PQntuples (res ); tuple ++ )
1593
1594
{
1594
- archprintf (fout , "INSERT INTO %s " , fmtId (classname ));
1595
- if (nfields == 0 )
1595
+ /*
1596
+ * First time through, we build as much of the INSERT statement as
1597
+ * possible in "insertStmt", which we can then just print for each
1598
+ * line. If the table happens to have zero columns then this will
1599
+ * be a complete statement, otherwise it will end in "VALUES(" and
1600
+ * be ready to have the row's column values appended.
1601
+ */
1602
+ if (insertStmt == NULL )
1596
1603
{
1604
+ insertStmt = createPQExpBuffer ();
1605
+ appendPQExpBuffer (insertStmt , "INSERT INTO %s " ,
1606
+ fmtId (classname ));
1607
+
1597
1608
/* corner case for zero-column table */
1598
- archprintf (fout , "DEFAULT VALUES;\n" );
1599
- continue ;
1600
- }
1601
- if (column_inserts )
1602
- {
1603
- resetPQExpBuffer (q );
1604
- appendPQExpBuffer (q , "(" );
1605
- for (field = 0 ; field < nfields ; field ++ )
1609
+ if (nfields == 0 )
1606
1610
{
1607
- if (field > 0 )
1608
- appendPQExpBuffer (q , ", " );
1609
- appendPQExpBufferStr (q , fmtId (PQfname (res , field )));
1611
+ appendPQExpBufferStr (insertStmt , "DEFAULT VALUES;\n" );
1612
+ }
1613
+ else
1614
+ {
1615
+ /* append the list of column names if required */
1616
+ if (column_inserts )
1617
+ {
1618
+ appendPQExpBufferStr (insertStmt , "(" );
1619
+ for (field = 0 ; field < nfields ; field ++ )
1620
+ {
1621
+ if (field > 0 )
1622
+ appendPQExpBufferStr (insertStmt , ", " );
1623
+ appendPQExpBufferStr (insertStmt ,
1624
+ fmtId (PQfname (res , field )));
1625
+ }
1626
+ appendPQExpBufferStr (insertStmt , ") " );
1627
+ }
1628
+
1629
+ appendPQExpBufferStr (insertStmt , "VALUES (" );
1610
1630
}
1611
- appendPQExpBuffer (q , ") " );
1612
- archputs (q -> data , fout );
1613
1631
}
1614
- archprintf (fout , "VALUES (" );
1632
+
1633
+ archputs (insertStmt -> data , fout );
1634
+
1635
+ /* if it is zero-column table then we're done */
1636
+ if (nfields == 0 )
1637
+ continue ;
1638
+
1615
1639
for (field = 0 ; field < nfields ; field ++ )
1616
1640
{
1617
1641
if (field > 0 )
1618
- archprintf ( fout , ", " );
1642
+ archputs ( " , " , fout );
1619
1643
if (PQgetisnull (res , tuple , field ))
1620
1644
{
1621
- archprintf ( fout , "NULL" );
1645
+ archputs ( "NULL" , fout );
1622
1646
continue ;
1623
1647
}
1624
1648
@@ -1647,7 +1671,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
1647
1671
const char * s = PQgetvalue (res , tuple , field );
1648
1672
1649
1673
if (strspn (s , "0123456789 +-eE." ) == strlen (s ))
1650
- archprintf ( fout , "%s" , s );
1674
+ archputs ( s , fout );
1651
1675
else
1652
1676
archprintf (fout , "'%s'" , s );
1653
1677
}
@@ -1661,9 +1685,9 @@ dumpTableData_insert(Archive *fout, void *dcontext)
1661
1685
1662
1686
case BOOLOID :
1663
1687
if (strcmp (PQgetvalue (res , tuple , field ), "t" ) == 0 )
1664
- archprintf ( fout , "true" );
1688
+ archputs ( "true" , fout );
1665
1689
else
1666
- archprintf ( fout , "false" );
1690
+ archputs ( "false" , fout );
1667
1691
break ;
1668
1692
1669
1693
default :
@@ -1676,7 +1700,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
1676
1700
break ;
1677
1701
}
1678
1702
}
1679
- archprintf ( fout , ");\n" );
1703
+ archputs ( ");\n" , fout );
1680
1704
}
1681
1705
1682
1706
if (PQntuples (res ) <= 0 )
@@ -1687,11 +1711,14 @@ dumpTableData_insert(Archive *fout, void *dcontext)
1687
1711
PQclear (res );
1688
1712
}
1689
1713
1690
- archprintf ( fout , "\n\n" );
1714
+ archputs ( "\n\n" , fout );
1691
1715
1692
1716
ExecuteSqlStatement (fout , "CLOSE _pg_dump_cursor" );
1693
1717
1694
1718
destroyPQExpBuffer (q );
1719
+ if (insertStmt != NULL )
1720
+ destroyPQExpBuffer (insertStmt );
1721
+
1695
1722
return 1 ;
1696
1723
}
1697
1724
0 commit comments