Skip to content
Permalink
Browse files

[feature] allow generating default values on postgres just in time

The expression used to generate default values for a field on the
postgres database are currently being printed on the feature form.

    nextval('serial')

A new option in the properties allows evaluating the expression before
usage and will directly print the new value in the feature form.

    23
  • Loading branch information
m-kuhn committed Apr 4, 2016
1 parent 48943d4 commit d1c79d817c2527367de48a06a235a24c41979068
Showing with 61 additions and 34 deletions.
  1. +2 −0 src/app/qgsoptions.cpp
  2. +16 −1 src/providers/postgres/qgspostgresprovider.cpp
  3. +43 −33 src/ui/qgsoptionsbase.ui
@@ -601,6 +601,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
cbxSnappingOptionsDocked->setChecked( mSettings->value( "/qgis/dockSnapping", false ).toBool() );
cbxAddPostgisDC->setChecked( mSettings->value( "/qgis/addPostgisDC", false ).toBool() );
cbxAddOracleDC->setChecked( mSettings->value( "/qgis/addOracleDC", false ).toBool() );
cbxEvaluateDefaultValues->setChecked( mSettings->value( "/qgis/evaluateDefaultValues", false ).toBool() );
cbxCompileExpressions->setChecked( mSettings->value( "/qgis/compileExpressions", true ).toBool() );
cbxCreateRasterLegendIcons->setChecked( mSettings->value( "/qgis/createRasterLegendIcons", false ).toBool() );
cbxAutoTransaction->setChecked( QgisApp::instance()->autoTransaction() );
@@ -1153,6 +1154,7 @@ void QgsOptions::saveOptions()
mSettings->setValue( "/qgis/addPostgisDC", cbxAddPostgisDC->isChecked() );
mSettings->setValue( "/qgis/addOracleDC", cbxAddOracleDC->isChecked() );
mSettings->setValue( "/qgis/compileExpressions", cbxCompileExpressions->isChecked() );
mSettings->setValue( "/qgis/evaluateDefaultValues", cbxEvaluateDefaultValues->isChecked() );
mSettings->setValue( "/qgis/defaultLegendGraphicResolution", mLegendGraphicResolutionSpinBox->value() );
bool createRasterLegendIcons = mSettings->value( "/qgis/createRasterLegendIcons", false ).toBool();
mSettings->setValue( "/qgis/createRasterLegendIcons", cbxCreateRasterLegendIcons->isChecked() );
@@ -25,6 +25,7 @@
#include <qgscoordinatereferencesystem.h>

#include <QMessageBox>
#include <QSettings>

#include "qgsvectorlayerimport.h"
#include "qgsprovidercountcalcevent.h"
@@ -1609,6 +1610,7 @@ QVariant QgsPostgresProvider::maximumValue( int index )
sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ), sql );

QgsPostgresResult rmax( connectionRO()->PQexec( sql ) );

return convertValue( fld.type(), rmax.PQgetvalue( 0, 0 ) );
}
catch ( PGFieldNotFound )
@@ -1625,7 +1627,20 @@ bool QgsPostgresProvider::isValid()

QVariant QgsPostgresProvider::defaultValue( int fieldId )
{
return mDefaultValues.value( fieldId, QString::null );
QSettings settings;

QVariant defVal = mDefaultValues.value( fieldId, QString::null );

if ( settings.value( "/qgis/evaluateDefaultValues", false ).toBool() && !defVal.isNull() )
{
const QgsField& fld = field( fieldId );

QgsPostgresResult res( connectionRO()->PQexec( QString( "SELECT %1" ).arg( defVal.toString() ) ) );

return convertValue( fld.type(), res.PQgetvalue( 0, 0 ) );
}

return defVal;
}

QString QgsPostgresProvider::paramValue( const QString& fieldValue, const QString &defaultValue ) const
@@ -308,7 +308,7 @@
<item>
<widget class="QStackedWidget" name="mOptionsStackedWidget">
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<widget class="QWidget" name="mOptionsPageGeneral">
<layout class="QVBoxLayout" name="verticalLayout_3">
@@ -337,8 +337,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>723</width>
<height>670</height>
<width>720</width>
<height>718</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_28">
@@ -1010,8 +1010,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>663</width>
<height>862</height>
<width>658</width>
<height>877</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_22">
@@ -1394,8 +1394,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>720</height>
<width>604</width>
<height>788</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_27">
@@ -1651,6 +1651,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbxEvaluateDefaultValues">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;When digitizing a new feature, default values are retrieved from the database. With this option turned on, the default values will be evaluated at the time of digitizing. With this option turned off, the default values will be evaluated at the time of saving.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Evaluate default values</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@@ -1749,8 +1759,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>747</width>
<height>802</height>
<width>736</width>
<height>846</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_29">
@@ -2469,8 +2479,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>168</width>
<height>276</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_46">
@@ -2574,8 +2584,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>521</width>
<height>354</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_25">
@@ -2912,8 +2922,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>650</width>
<height>645</height>
<width>647</width>
<height>710</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_30">
@@ -3374,8 +3384,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>537</width>
<height>591</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_39">
@@ -3629,8 +3639,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>668</height>
<width>566</width>
<height>707</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_31">
@@ -4174,8 +4184,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>462</width>
<height>382</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
@@ -4313,8 +4323,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>647</height>
<width>560</width>
<height>681</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_15">
@@ -4559,8 +4569,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>303</width>
<height>236</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_32">
@@ -4668,8 +4678,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>740</height>
<width>552</width>
<height>791</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_33">
@@ -5147,18 +5157,18 @@
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsColorButtonV2</class>
<extends>QToolButton</extends>
<header>qgscolorbuttonv2.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsColorButtonV2</class>
<extends>QToolButton</extends>
<header>qgscolorbuttonv2.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsColorSchemeList</class>
<extends>QWidget</extends>

15 comments on commit d1c79d8

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson replied Apr 4, 2016

@m-kuhn no test case? ;)

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Apr 4, 2016

I'm not yet totally convinced that the API via QSettings is the best of all ways

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson replied Apr 4, 2016

What about making it a postgres connection setting?

@3nids

This comment has been minimized.

Copy link
Member

@3nids 3nids replied Apr 4, 2016

shouldn't it be a field property rather than a QGIS global?

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Apr 4, 2016

What about making it a postgres connection setting?

Does that allow upgrading an existing layer?

Shouldn't it be a field property rather than a QGIS global?

That would allow maximum flexibility but I fear the configuration effort of this approach. Good ideas?

@3nids

This comment has been minimized.

Copy link
Member

@3nids 3nids replied Apr 4, 2016

What is the typical use case for this? Isn't this used only in specific case?

@3nids

This comment has been minimized.

Copy link
Member

@3nids 3nids replied Apr 4, 2016

If you don't move it at the field level, I would make it a project setting rather than a global.
Project sounds like the perfect balance, no?

as for transactions ;)

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Apr 4, 2016

The use cases are things like

  • allow creating linked entries from the add feature dialog
  • allow adding new entries from the relation reference widget

On the other hand, I'm not sure there are a lot of cases where this behavior is really problematic, but I'm sure there are some.

@3nids

This comment has been minimized.

Copy link
Member

@3nids 3nids replied Apr 4, 2016

if we people want to stay strict with a serial (no cancelled input for instance)...?

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Apr 4, 2016

That's why I have put the "really" there ;)

@3nids

This comment has been minimized.

Copy link
Member

@3nids 3nids replied Apr 4, 2016

if you move transaction setting to the project, it will be quite coherent to have this next to it!

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Apr 4, 2016

or more finegrained

@3nids

This comment has been minimized.

Copy link
Member

@3nids 3nids replied Apr 6, 2016

so what's your decision?

@3nids

This comment has been minimized.

Copy link
Member

@3nids 3nids replied May 11, 2016

@m-kuhn it might have been forgotten....although I'd still be in favor of having this in the project properties. Thoughts?

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied May 11, 2016

Please sign in to comment.
You can’t perform that action at this time.