Skip to content

Commit

Permalink
Importing a layer, create an autoincremental integer pk if either no …
Browse files Browse the repository at this point in the history
…pk was passed or it's an integer field
  • Loading branch information
brushtyler committed Sep 2, 2015
1 parent 71afc9a commit 7d79a73
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
25 changes: 16 additions & 9 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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";
Expand All @@ -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
{
Expand Down
19 changes: 11 additions & 8 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 7d79a73

Please sign in to comment.