Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gsherman committed Apr 8, 2010
1 parent dd7b187 commit 6daef95
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions resources/context_help/QgsNewSpatialiteLayerDialog-en_US
Expand Up @@ -10,6 +10,8 @@ Enter a name for the geometry column or accept the default.
Choose the type of layer you want to create.
<h4>EPSG SRID</h4>
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.
<h4>Create an Autoincrementing Primary Key</h4>
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.
<h4>New attribute</h4>
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/>
Width and precision are irrelevant in a Spatialite database so you do not have to specify these.
Expand Down
11 changes: 10 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -3131,7 +3131,16 @@ void QgisApp::newSpatialiteLayer()

// Build up the sql statement for creating the table
//
QString sql = QString( "create table %1(" ).arg( quotedIdentifier( newLayerName ) );
QString baseSQL;
if ( spatialiteDialog.includePrimaryKey() )
{
baseSQL = "create table %1(pkuid integer primary key autoincrement, ";
}
else
{
baseSQL = "create table %1(";
}
QString sql = baseSQL.arg( quotedIdentifier( newLayerName ) );
// iterate through the field names and add them to the create statement
// (use indexed access since this is just as fast as iterators
for ( int i = 0; i < items->size(); ++i )
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsnewspatialitelayerdialog.cpp
Expand Up @@ -232,6 +232,11 @@ QString QgsNewSpatialiteLayerDialog::geometryColumn() const
return leGeometryColumn->text();
}

bool QgsNewSpatialiteLayerDialog::includePrimaryKey() const
{
return checkBoxPrimaryKey->isChecked();
}

bool QgsNewSpatialiteLayerDialog::createDb()
{
QFile newDb( mDatabaseComboBox->currentText() );
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsnewspatialitelayerdialog.h
Expand Up @@ -49,6 +49,8 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
QString geometryColumn() const;
/**Returns the selected crs id*/
QString selectedCrsId() const;
/**Returns the state of the primary key checkbox*/
bool includePrimaryKey() const;
/** Create a new database */
bool createDb();

Expand Down
18 changes: 14 additions & 4 deletions src/ui/qgsnewspatialitelayerdialogbase.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>411</width>
<height>593</height>
<width>431</width>
<height>648</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -227,6 +227,16 @@
</layout>
</item>
<item row="5" column="0">
<widget class="QCheckBox" name="checkBoxPrimaryKey">
<property name="toolTip">
<string>Add an integer id field as the primary key for the new layer</string>
</property>
<property name="text">
<string>Create an autoincrementing primary key</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>New attribute</string>
Expand Down Expand Up @@ -284,7 +294,7 @@
</layout>
</widget>
</item>
<item row="6" column="0">
<item row="7" column="0">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Attributes list</string>
Expand Down Expand Up @@ -397,7 +407,7 @@
</layout>
</widget>
</item>
<item row="7" column="0">
<item row="8" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand Down

0 comments on commit 6daef95

Please sign in to comment.