void TestQgsField::dataStream() { QgsField original; original.setName( QStringLiteral( "name" ) ); original.setType( QVariant::Int ); original.setLength( 5 ); original.setPrecision( 2 ); original.setTypeName( QStringLiteral( "typename1" ) ); original.setComment( QStringLiteral( "comment1" ) ); original.setAlias( QStringLiteral( "alias" ) ); original.setDefaultValueExpression( QStringLiteral( "default" ) ); QgsFieldConstraints constraints; constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider ); constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginLayer ); constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) ); constraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthSoft ); original.setConstraints( constraints ); QByteArray ba; QDataStream ds( &ba, QIODevice::ReadWrite ); ds << original; QgsField result; ds.device()->seek( 0 ); ds >> result; QCOMPARE( result, original ); QCOMPARE( result.typeName(), original.typeName() ); //typename is NOT required for equality QCOMPARE( result.comment(), original.comment() ); //comment is NOT required for equality }
void TestQgsField::asVariant() { QgsField original( QStringLiteral( "original" ), QVariant::Double, QStringLiteral( "double" ), 5, 2, QStringLiteral( "comment" ) ); QgsFieldConstraints constraints; constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull ); original.setConstraints( constraints ); //convert to and from a QVariant QVariant var = QVariant::fromValue( original ); QVERIFY( var.isValid() ); QgsField fromVar = qvariant_cast<QgsField>( var ); QCOMPARE( fromVar, original ); }
void TestQgsField::copy() { QgsField original( QStringLiteral( "original" ), QVariant::Double, QStringLiteral( "double" ), 5, 2, QStringLiteral( "comment" ) ); QgsFieldConstraints constraints; constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider ); constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) ); constraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthSoft ); original.setConstraints( constraints ); QgsField copy( original ); QVERIFY( copy == original ); copy.setName( QStringLiteral( "copy" ) ); QCOMPARE( original.name(), QString( "original" ) ); QVERIFY( copy != original ); }
void TestQgsField::equality() { QgsField field1; field1.setName( QStringLiteral( "name" ) ); field1.setType( QVariant::Int ); field1.setLength( 5 ); field1.setPrecision( 2 ); field1.setTypeName( QStringLiteral( "typename1" ) ); //typename is NOT required for equality field1.setComment( QStringLiteral( "comment1" ) ); //comment is NOT required for equality QgsFieldConstraints constraints; constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider ); field1.setConstraints( constraints ); QgsField field2; field2.setName( QStringLiteral( "name" ) ); field2.setType( QVariant::Int ); field2.setLength( 5 ); field2.setPrecision( 2 ); field2.setTypeName( QStringLiteral( "typename2" ) ); //typename is NOT required for equality field2.setComment( QStringLiteral( "comment2" ) ); //comment is NOT required for equality constraints = field2.constraints(); constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider ); field2.setConstraints( constraints ); QVERIFY( field1 == field2 ); QVERIFY( !( field1 != field2 ) ); //test that all applicable components contribute to equality field2.setName( QStringLiteral( "name2" ) ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); field2.setName( QStringLiteral( "name" ) ); field2.setType( QVariant::Double ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); field2.setType( QVariant::Int ); field2.setLength( 9 ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); field2.setLength( 5 ); field2.setPrecision( 9 ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); field2.setPrecision( 2 ); field2.setAlias( QStringLiteral( "alias " ) ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); field2.setAlias( QString() ); field2.setDefaultValueExpression( QStringLiteral( "1+2" ) ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); field2.setDefaultValueExpression( QString() ); constraints = field2.constraints(); constraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull ); field2.setConstraints( constraints ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); constraints = field2.constraints(); constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginLayer ); field2.setConstraints( constraints ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); constraints = field2.constraints(); constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider ); constraints.setConstraintExpression( QStringLiteral( "exp" ) ); field2.setConstraints( constraints ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); constraints = field2.constraints(); constraints.setConstraintExpression( QStringLiteral( "exp" ), QStringLiteral( "desc" ) ); field2.setConstraints( constraints ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); constraints = QgsFieldConstraints(); constraints.setConstraint( QgsFieldConstraints::ConstraintUnique ); constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthHard ); field2.setConstraints( constraints ); constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft ); field1.setConstraints( constraints ); QVERIFY( !( field1 == field2 ) ); QVERIFY( field1 != field2 ); }
void TestQgsField::gettersSetters() { QgsField field; field.setName( QStringLiteral( "name" ) ); QCOMPARE( field.name(), QString( "name" ) ); field.setType( QVariant::Int ); QCOMPARE( field.type(), QVariant::Int ); field.setTypeName( QStringLiteral( "typeName" ) ); QCOMPARE( field.typeName(), QString( "typeName" ) ); field.setLength( 5 ); QCOMPARE( field.length(), 5 ); field.setPrecision( 2 ); QCOMPARE( field.precision(), 2 ); field.setComment( QStringLiteral( "comment" ) ); QCOMPARE( field.comment(), QString( "comment" ) ); field.setAlias( QStringLiteral( "alias" ) ); QCOMPARE( field.alias(), QString( "alias" ) ); field.setDefaultValueExpression( QStringLiteral( "1+2" ) ); QCOMPARE( field.defaultValueExpression(), QString( "1+2" ) ); QgsFieldConstraints constraints; constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider ); field.setConstraints( constraints ); QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintNotNull ); QCOMPARE( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ), QgsFieldConstraints::ConstraintOriginProvider ); QCOMPARE( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintOriginNotSet ); constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginNotSet ); field.setConstraints( constraints ); QCOMPARE( field.constraints().constraints(), 0 ); constraints.setConstraint( QgsFieldConstraints::ConstraintUnique ); constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull ); field.setConstraints( constraints ); QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintUnique | QgsFieldConstraints::ConstraintNotNull ); constraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull ); field.setConstraints( constraints ); QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintUnique ); constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) ); field.setConstraints( constraints ); QCOMPARE( field.constraints().constraintExpression(), QStringLiteral( "constraint expression" ) ); QCOMPARE( field.constraints().constraintDescription(), QStringLiteral( "description" ) ); QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintUnique | QgsFieldConstraints::ConstraintExpression ); //setting constraint expression should add constraint constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) ); // check a constraint strength which hasn't been set QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ), QgsFieldConstraints::ConstraintStrengthNotSet ); // check a constraint strength which has not been explicitly set QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthHard ); constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft ); field.setConstraints( constraints ); QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthSoft ); // try overwriting a provider constraint's strength constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider ); constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft ); field.setConstraints( constraints ); QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthHard ); }
// loadFields() gets the type from the field record void QgsDb2Provider::loadFields() { mAttributeFields.clear(); //mDefaultValues.clear(); QString table = QStringLiteral( "%1.%2" ).arg( mSchemaName, mTableName ); // Use the Qt functionality to get the fields and their definitions. QSqlRecord r = mDatabase.record( table ); int fieldCount = r.count(); for ( int i = 0; i < fieldCount; i++ ) { QSqlField f = r.field( i ); int typeID = f.typeID(); // seems to be DB2 numeric type id (standard?) QString sqlTypeName = db2TypeName( typeID ); QVariant::Type sqlType = f.type(); QgsDebugMsg( QString( "name: %1; length: %2; sqlTypeID: %3; sqlTypeName: %4" ) .arg( f.name() ).arg( f.length() ).arg( QString::number( typeID ), sqlTypeName ) ); if ( f.name() == mGeometryColName ) continue; // Got this with uri, just skip if ( sqlType == QVariant::String ) { mAttributeFields.append( QgsField( f.name(), sqlType, sqlTypeName, f.length() ) ); } else if ( sqlType == QVariant::Double ) { mAttributeFields.append( QgsField( f.name(), sqlType, sqlTypeName, f.length(), f.precision() ) ); } else { mAttributeFields.append( QgsField( f.name(), sqlType, sqlTypeName ) ); } if ( !f.defaultValue().isNull() ) { mDefaultValues.insert( i, f.defaultValue() ); } // Hack to get primary key since the primaryIndex function above doesn't work // on z/OS. Pick first integer column. if ( mFidColName.isEmpty() && ( sqlType == QVariant::LongLong || sqlType == QVariant::Int ) ) { mFidColName = f.name(); } } if ( !mFidColName.isEmpty() ) { mFidColIdx = mAttributeFields.indexFromName( mFidColName ); if ( mFidColIdx >= 0 ) { // primary key has not null, unique constraints QgsFieldConstraints constraints = mAttributeFields.at( mFidColIdx ).constraints(); constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider ); constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider ); mAttributeFields[ mFidColIdx ].setConstraints( constraints ); } } }