Skip to content
Permalink
Browse files

Importing a layer, create an autoincremental integer pk if either no …

…pk was passed or it's an integer field
  • Loading branch information
brushtyler committed Sep 2, 2015
1 parent 71afc9a commit 7d79a73f587af021d7cd35588b2d972cbfec252b
Showing with 27 additions and 17 deletions.
  1. +16 −9 src/providers/postgres/qgspostgresprovider.cpp
  2. +11 −8 src/providers/spatialite/qgsspatialiteprovider.cpp
@@ -2939,14 +2939,7 @@ bool QgsPostgresProvider::convertField( QgsField &field, const QMap<QString, QVa
break;

case QVariant::Int:
if ( fieldPrec < 10 )
{
fieldType = "int4";
}
else
{
fieldType = "numeric";
}
fieldType = "int4";
fieldPrec = 0;
break;

@@ -3054,7 +3047,8 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
}
}

// if the field doesn't not exist yet, create it as a serial field
// if the pk field doesn't exist yet, create a serial pk field
// as it's autoincremental
if ( primaryKeyType.isEmpty() )
{
primaryKeyType = "serial";
@@ -3066,6 +3060,19 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
}
#endif
}
else
{
// if the pk field's type is one of the postgres integer types,
// use the equivalent autoincremental type (serialN)
if ( primaryKeyType == "int2" || primaryKeyType == "int4" )
{
primaryKeyType = "serial";
}
else if ( primaryKeyType == "int8" )
{
primaryKeyType = "serial8";
}
}

try
{
@@ -175,17 +175,20 @@ QgsSpatiaLiteProvider::createEmptyLayer(
}
}

// if the field doesn't not exist yet, create it as a int field
// if the pk field doesn't exist yet, create an integer pk field
// as it's autoincremental
if ( primaryKeyType.isEmpty() )
{
primaryKeyType = "INTEGER";
#if 0 // TODO
// check the feature count to choose if create a bigint pk field
if ( layer->featureCount() > 0xFFFFFF )
{
primaryKeyType = "BIGINT";
}
#endif
}
else
{
// if the pk field's type is bigint, use the autoincremental
// integer type instead
if ( primaryKeyType == "BIGINT" )
{
primaryKeyType = "INTEGER";
}
}

try

0 comments on commit 7d79a73

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