Skip to content
Permalink
Browse files
MSSQL: Add handling of default values (fixes #5316)
  • Loading branch information
szekerest authored and NathanW2 committed Apr 13, 2012
1 parent 7773ddf commit f9e2df427501f43fefe6d05445fca44bab61a014
Showing with 23 additions and 0 deletions.
  1. +19 −0 src/providers/mssql/qgsmssqlprovider.cpp
  2. +4 −0 src/providers/mssql/qgsmssqlprovider.h
@@ -321,6 +321,7 @@ void QgsMssqlProvider::loadMetadata()
void QgsMssqlProvider::loadFields()
{
mAttributeFields.clear();
mDefaultValues.clear();
// get field spec
mQuery = QSqlQuery( mDatabase );
mQuery.setForwardOnly( true );
@@ -358,6 +359,11 @@ void QgsMssqlProvider::loadFields()
sqlTypeName,
mQuery.value( 7 ).toInt(),
mQuery.value( 6 ).toInt() ) );

if ( !mQuery.value( 12 ).isNull() )
{
mDefaultValues.insert( i, mQuery.value( 12 ) );
}
++i;
}
}
@@ -407,6 +413,13 @@ void QgsMssqlProvider::loadFields()
}
}

QVariant QgsMssqlProvider::defaultValue( int fieldId )
{
if ( mDefaultValues.contains(fieldId) )
return mDefaultValues[fieldId];
else
return QVariant( QString::null );
}

QString QgsMssqlProvider::storageType() const
{
@@ -770,6 +783,9 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList & flist )
if ( fld.name().isEmpty() )
continue; // invalid

if ( mDefaultValues.contains(it2.key()) && mDefaultValues[it2.key()] == *it2 )
continue; // skip fields having default values

if ( !first )
{
statement += ",";
@@ -840,6 +856,9 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList & flist )
if ( fld.name().isEmpty() )
continue; // invalid

if ( mDefaultValues.contains(it2.key()) && mDefaultValues[it2.key()] == *it2 )
continue; // skip fields having default values

QVariant::Type type = fld.type();
if ( it2->isNull() || !it2->isValid() )
{
@@ -270,6 +270,9 @@ class QgsMssqlProvider : public QgsVectorDataProvider
/** convert a QgsField to work with MSSQL */
static bool convertField( QgsField &field );

/**Returns the default value for field specified by @c fieldId */
QVariant defaultValue( int fieldId );

/** Import a vector layer into the database */
static QgsVectorLayerImport::ImportError createEmptyLayer(
const QString& uri,
@@ -294,6 +297,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider

//! Fields
QgsFieldMap mAttributeFields;
QMap<int, QVariant> mDefaultValues;

QgsMssqlGeometryParser parser;

0 comments on commit f9e2df4

Please sign in to comment.