@@ -1704,8 +1704,9 @@ bool QgsPostgresProvider::addFeature(QgsFeature* f, int primaryKeyHighWater)
1704
1704
<< " ." << std::endl;
1705
1705
#endif
1706
1706
1707
- // add quotes if the field is a character or date type
1708
- if (fieldvalue != " NULL" && fieldvalue != " DEFAULT" )
1707
+ // add quotes if the field is a character or date type and not
1708
+ // the postgres provided default value
1709
+ if (fieldvalue != " NULL" && fieldvalue != getDefaultValue (it->fieldName (), f))
1709
1710
{
1710
1711
for (std::vector<QgsField>::iterator iter=attributeFields.begin ();iter!=attributeFields.end ();++iter)
1711
1712
{
@@ -1720,6 +1721,7 @@ bool QgsPostgresProvider::addFeature(QgsFeature* f, int primaryKeyHighWater)
1720
1721
)
1721
1722
{
1722
1723
charactertype=true ;
1724
+ break ; // no need to continue with this loop
1723
1725
}
1724
1726
}
1725
1727
}
@@ -1746,13 +1748,13 @@ bool QgsPostgresProvider::addFeature(QgsFeature* f, int primaryKeyHighWater)
1746
1748
PGresult* result=PQexec (connection, (const char *)(insert.utf8 ()));
1747
1749
if (result==0 )
1748
1750
{
1749
- QMessageBox::information (0 ," INSERT error" , " An error occured during feature insertion" ,QMessageBox::Ok);
1751
+ QMessageBox::information (0 ,tr ( " INSERT error" ), tr ( " An error occured during feature insertion" ) ,QMessageBox::Ok);
1750
1752
return false ;
1751
1753
}
1752
1754
ExecStatusType message=PQresultStatus (result);
1753
1755
if (message==PGRES_FATAL_ERROR)
1754
1756
{
1755
- QMessageBox::information (0 ," INSERT error" ,QString (PQresultErrorMessage (result)),QMessageBox::Ok);
1757
+ QMessageBox::information (0 ,tr ( " INSERT error" ) ,QString (PQresultErrorMessage (result)),QMessageBox::Ok);
1756
1758
return false ;
1757
1759
}
1758
1760
@@ -1773,7 +1775,29 @@ bool QgsPostgresProvider::addFeature(QgsFeature* f, int primaryKeyHighWater)
1773
1775
1774
1776
QString QgsPostgresProvider::getDefaultValue (const QString& attr, QgsFeature* f)
1775
1777
{
1776
- return " DEFAULT" ;
1778
+ // Get the default column value from the Postgres information
1779
+ // schema. If there is no default we return an empty string.
1780
+
1781
+ // Maintaining a cache of the results of this query would be quite
1782
+ // simple and if this query is called lots, could save some time.
1783
+
1784
+ QString sql (" SELECT column_default FROM "
1785
+ " information_schema.columns WHERE "
1786
+ " column_default IS NOT NULL AND "
1787
+ " table_schema = '" + mSchemaName + " ' AND "
1788
+ " table_name = '" + mTableName + " ' AND "
1789
+ " column_name = '" + attr + " '" );
1790
+
1791
+ QString defaultValue (" " );
1792
+
1793
+ PGresult* result = PQexec (connection, (const char *)(sql.utf8 ()));
1794
+
1795
+ if (PQntuples (result) == 1 )
1796
+ defaultValue = PQgetvalue (result, 0 , 0 );
1797
+
1798
+ PQclear (result);
1799
+
1800
+ return defaultValue;
1777
1801
}
1778
1802
1779
1803
bool QgsPostgresProvider::deleteFeature (int id)
@@ -1787,13 +1811,13 @@ bool QgsPostgresProvider::deleteFeature(int id)
1787
1811
PGresult* result=PQexec (connection, (const char *)(sql.utf8 ()));
1788
1812
if (result==0 )
1789
1813
{
1790
- QMessageBox::information (0 ," DELETE error" , " An error occured during deletion from disk" ,QMessageBox::Ok);
1814
+ QMessageBox::information (0 ,tr ( " DELETE error" ), tr ( " An error occured during deletion from disk" ) ,QMessageBox::Ok);
1791
1815
return false ;
1792
1816
}
1793
1817
ExecStatusType message=PQresultStatus (result);
1794
1818
if (message==PGRES_FATAL_ERROR)
1795
1819
{
1796
- QMessageBox::information (0 ," DELETE error" ,QString (PQresultErrorMessage (result)),QMessageBox::Ok);
1820
+ QMessageBox::information (0 ,tr ( " DELETE error" ) ,QString (PQresultErrorMessage (result)),QMessageBox::Ok);
1797
1821
return false ;
1798
1822
}
1799
1823
@@ -2106,17 +2130,17 @@ bool QgsPostgresProvider::changeGeometryValues(std::map<int, QgsGeometry> & geom
2106
2130
PGresult* result=PQexec (connection, (const char *)(sql.utf8 ()));
2107
2131
if (result==0 )
2108
2132
{
2109
- QMessageBox::critical (0 , " PostGIS error" ,
2110
- " An error occured contacting the PostgreSQL databse" ,
2133
+ QMessageBox::critical (0 , tr ( " PostGIS error" ) ,
2134
+ tr ( " An error occured contacting the PostgreSQL databse" ) ,
2111
2135
QMessageBox::Ok,
2112
2136
Qt::NoButton);
2113
2137
return false ;
2114
2138
}
2115
2139
ExecStatusType message=PQresultStatus (result);
2116
2140
if (message==PGRES_FATAL_ERROR)
2117
2141
{
2118
- QMessageBox::information (0 , " PostGIS error" ,
2119
- " The PostgreSQL databse returned: "
2142
+ QMessageBox::information (0 , tr ( " PostGIS error" ) ,
2143
+ tr ( " The PostgreSQL databse returned: " )
2120
2144
+ QString (PQresultErrorMessage (result)),
2121
2145
QMessageBox::Ok,
2122
2146
Qt::NoButton);
0 commit comments