Skip to content

Commit 6daef95

Browse files
author
gsherman
committed
Add the option to include a primary key when creating a new spatialite
layer. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13279 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent dd7b187 commit 6daef95

5 files changed

+33
-5
lines changed

resources/context_help/QgsNewSpatialiteLayerDialog-en_US

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Enter a name for the geometry column or accept the default.
1010
Choose the type of layer you want to create.
1111
<h4>EPSG SRID</h4>
1212
Enter the EPSG number for the spatial reference id (SRID). By default the SRID for WGS 84 is filled in for you. Click on <label>Find SRID</label> button to change the coordinate reference system of the layer if needed. The SRID must exist within the spatial_ref_sys in your Spatialite database. You can search for the SRID using partial matches on both name and SRID.
13+
<h4>Create an Autoincrementing Primary Key</h4>
14+
Clicking this checkbox will add a primary key to the new layer. This key field will be autoincrementing, meaning you don't have to enter a value for it when adding features to the attribute table of the layer.
1315
<h4>New attribute</h4>
1416
Add the desired attributes by clicking on the <label>Add to attributes list</label> button after you have specified a name and type for the attribute. Only real, integer, and string attributes are supported.<br/>
1517
Width and precision are irrelevant in a Spatialite database so you do not have to specify these.

src/app/qgisapp.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3131,7 +3131,16 @@ void QgisApp::newSpatialiteLayer()
31313131

31323132
// Build up the sql statement for creating the table
31333133
//
3134-
QString sql = QString( "create table %1(" ).arg( quotedIdentifier( newLayerName ) );
3134+
QString baseSQL;
3135+
if ( spatialiteDialog.includePrimaryKey() )
3136+
{
3137+
baseSQL = "create table %1(pkuid integer primary key autoincrement, ";
3138+
}
3139+
else
3140+
{
3141+
baseSQL = "create table %1(";
3142+
}
3143+
QString sql = baseSQL.arg( quotedIdentifier( newLayerName ) );
31353144
// iterate through the field names and add them to the create statement
31363145
// (use indexed access since this is just as fast as iterators
31373146
for ( int i = 0; i < items->size(); ++i )

src/app/qgsnewspatialitelayerdialog.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ QString QgsNewSpatialiteLayerDialog::geometryColumn() const
232232
return leGeometryColumn->text();
233233
}
234234

235+
bool QgsNewSpatialiteLayerDialog::includePrimaryKey() const
236+
{
237+
return checkBoxPrimaryKey->isChecked();
238+
}
239+
235240
bool QgsNewSpatialiteLayerDialog::createDb()
236241
{
237242
QFile newDb( mDatabaseComboBox->currentText() );

src/app/qgsnewspatialitelayerdialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
4949
QString geometryColumn() const;
5050
/**Returns the selected crs id*/
5151
QString selectedCrsId() const;
52+
/**Returns the state of the primary key checkbox*/
53+
bool includePrimaryKey() const;
5254
/** Create a new database */
5355
bool createDb();
5456

src/ui/qgsnewspatialitelayerdialogbase.ui

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>411</width>
10-
<height>593</height>
9+
<width>431</width>
10+
<height>648</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -227,6 +227,16 @@
227227
</layout>
228228
</item>
229229
<item row="5" column="0">
230+
<widget class="QCheckBox" name="checkBoxPrimaryKey">
231+
<property name="toolTip">
232+
<string>Add an integer id field as the primary key for the new layer</string>
233+
</property>
234+
<property name="text">
235+
<string>Create an autoincrementing primary key</string>
236+
</property>
237+
</widget>
238+
</item>
239+
<item row="6" column="0">
230240
<widget class="QGroupBox" name="groupBox">
231241
<property name="title">
232242
<string>New attribute</string>
@@ -284,7 +294,7 @@
284294
</layout>
285295
</widget>
286296
</item>
287-
<item row="6" column="0">
297+
<item row="7" column="0">
288298
<widget class="QGroupBox" name="groupBox_2">
289299
<property name="title">
290300
<string>Attributes list</string>
@@ -397,7 +407,7 @@
397407
</layout>
398408
</widget>
399409
</item>
400-
<item row="7" column="0">
410+
<item row="8" column="0">
401411
<widget class="QDialogButtonBox" name="buttonBox">
402412
<property name="orientation">
403413
<enum>Qt::Horizontal</enum>

0 commit comments

Comments
 (0)