@@ -364,175 +364,175 @@ void QgsMssqlProvider::loadFields()
364
364
return ;
365
365
}
366
366
367
- if ( query.isActive () )
367
+ while ( query.next () )
368
368
{
369
- while ( query.next () )
370
- {
371
- mComputedColumns .append ( query.value ( 0 ).toString () );
372
- }
369
+ mComputedColumns .append ( query.value ( 0 ).toString () );
373
370
}
374
371
375
372
if ( !query.exec ( QStringLiteral ( " exec sp_columns @table_name = N%1, @table_owner = %2" ).arg ( quotedValue ( mTableName ), quotedValue ( mSchemaName ) ) ) )
376
373
{
377
374
pushError ( query.lastError ().text () );
378
375
return ;
379
376
}
380
- if ( query.isActive () )
377
+
378
+ int i = 0 ;
379
+ QStringList pkCandidates;
380
+ while ( query.next () )
381
381
{
382
- int i = 0 ;
383
- QStringList pkCandidates;
384
- while ( query.next () )
385
- {
386
- const QString colName = query.value ( 3 ).toString ();
387
- const QString sqlTypeName = query.value ( 5 ).toString ();
382
+ const QString colName = query.value ( QStringLiteral ( " COLUMN_NAME" ) ).toString ();
383
+ const QString sqlTypeName = query.value ( QStringLiteral ( " TYPE_NAME" ) ).toString ();
388
384
389
- // if we don't have an explicitly set geometry column name, and this is a geometry column, then use it
390
- // but if we DO have an explicitly set geometry column name, then load the other information if this is that column
391
- if ( ( mGeometryColName .isEmpty () && ( sqlTypeName == QLatin1String ( " geometry" ) || sqlTypeName == QLatin1String ( " geography" ) ) )
392
- || colName == mGeometryColName )
385
+ // if we don't have an explicitly set geometry column name, and this is a geometry column, then use it
386
+ // but if we DO have an explicitly set geometry column name, then load the other information if this is that column
387
+ if ( ( mGeometryColName .isEmpty () && ( sqlTypeName == QLatin1String ( " geometry" ) || sqlTypeName == QLatin1String ( " geography" ) ) )
388
+ || colName == mGeometryColName )
389
+ {
390
+ mGeometryColName = colName;
391
+ mGeometryColType = sqlTypeName;
392
+ mParser .mIsGeography = sqlTypeName == QLatin1String ( " geography" );
393
+ }
394
+ else
395
+ {
396
+ QVariant::Type sqlType = DecodeSqlType ( sqlTypeName );
397
+ if ( sqlTypeName == QLatin1String ( " int identity" ) || sqlTypeName == QLatin1String ( " bigint identity" ) )
393
398
{
394
- mGeometryColName = colName ;
395
- mGeometryColType = sqlTypeName ;
396
- mParser . mIsGeography = sqlTypeName == QLatin1String ( " geography " ) ;
399
+ mPrimaryKeyType = PktInt ;
400
+ mPrimaryKeyAttrs << mAttributeFields . size () ;
401
+ isIdentity = true ;
397
402
}
398
- else
403
+ else if ( sqlTypeName == QLatin1String ( " int " ) || sqlTypeName == QLatin1String ( " bigint " ) )
399
404
{
400
- QVariant::Type sqlType = DecodeSqlType ( sqlTypeName );
401
- if ( sqlTypeName == QLatin1String ( " int identity" ) || sqlTypeName == QLatin1String ( " bigint identity" ) )
402
- {
403
- mPrimaryKeyType = PktInt;
404
- mPrimaryKeyAttrs << mAttributeFields .size ();
405
- isIdentity = true ;
406
- }
407
- else if ( sqlTypeName == QLatin1String ( " int" ) || sqlTypeName == QLatin1String ( " bigint" ) )
408
- {
409
- pkCandidates << query.value ( 3 ).toString ();
410
- }
405
+ pkCandidates << query.value ( 3 ).toString ();
406
+ }
411
407
412
- if ( sqlType == QVariant::String )
413
- {
414
- // Field length in chars is column 7 ("Length") of the sp_columns output,
415
- // except for uniqueidentifiers which must use column 6 ("Precision").
416
- int length = query.value ( sqlTypeName.startsWith ( QLatin1String ( " uniqueidentifier" ), Qt::CaseInsensitive ) ? 6 : 7 ).toInt ();
417
- if ( sqlTypeName.startsWith ( QLatin1Char ( ' n' ) ) )
418
- {
419
- length = length / 2 ;
420
- }
421
- mAttributeFields .append (
422
- QgsField (
423
- colName, sqlType,
424
- sqlTypeName,
425
- length ) );
426
- }
427
- else if ( sqlType == QVariant::Double )
428
- {
429
- mAttributeFields .append (
430
- QgsField (
431
- colName, sqlType,
432
- sqlTypeName,
433
- query.value ( 6 ).toInt (),
434
- sqlTypeName == QLatin1String ( " decimal" ) ? query.value ( 8 ).toInt () : -1 ) );
435
- }
436
- else if ( sqlType == QVariant::Date || sqlType == QVariant::DateTime || sqlType == QVariant::Time )
437
- {
438
- mAttributeFields .append (
439
- QgsField (
440
- colName, sqlType,
441
- sqlTypeName,
442
- -1 ,
443
- -1 ) );
444
- }
445
- else
408
+ QgsField field;
409
+ if ( sqlType == QVariant::String )
410
+ {
411
+ // Field length in chars is column 7 ("Length") of the sp_columns output,
412
+ // except for uniqueidentifiers which must use column 6 ("Precision").
413
+ int length = query.value ( sqlTypeName.startsWith ( QLatin1String ( " uniqueidentifier" ), Qt::CaseInsensitive ) ? 6 : 7 ).toInt ();
414
+ if ( sqlTypeName.startsWith ( QLatin1Char ( ' n' ) ) )
446
415
{
447
- mAttributeFields .append (
448
- QgsField (
449
- colName, sqlType,
450
- sqlTypeName ) );
416
+ length = length / 2 ;
451
417
}
418
+ field = QgsField ( colName,
419
+ sqlType,
420
+ sqlTypeName,
421
+ length );
422
+ }
423
+ else if ( sqlType == QVariant::Double )
424
+ {
425
+ field = QgsField ( colName,
426
+ sqlType,
427
+ sqlTypeName,
428
+ query.value ( 6 ).toInt (),
429
+ sqlTypeName == QLatin1String ( " decimal" ) ? query.value ( 8 ).toInt () : -1 );
430
+ }
431
+ else if ( sqlType == QVariant::Date || sqlType == QVariant::DateTime || sqlType == QVariant::Time )
432
+ {
433
+ field = QgsField ( colName,
434
+ sqlType,
435
+ sqlTypeName,
436
+ -1 ,
437
+ -1 );
438
+ }
439
+ else
440
+ {
441
+ field = QgsField ( colName,
442
+ sqlType,
443
+ sqlTypeName );
444
+ }
452
445
453
- // COLUMN_DEF
454
- if ( !query.value ( 12 ).isNull () )
455
- {
456
- mDefaultValues .insert ( i, query.value ( 12 ).toString () );
457
- }
446
+ // Field nullable
447
+ const bool nullable = query.value ( QStringLiteral ( " NULLABLE" ) ).toBool ();
448
+ QgsFieldConstraints constraints;
449
+ if ( !nullable )
450
+ constraints.setConstraint ( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
451
+ field.setConstraints ( constraints );
458
452
459
- ++i;
453
+ mAttributeFields .append ( field );
454
+
455
+ // COLUMN_DEF
456
+ if ( !query.value ( 12 ).isNull () )
457
+ {
458
+ mDefaultValues .insert ( i, query.value ( 12 ).toString () );
460
459
}
460
+
461
+ ++i;
461
462
}
462
- // get primary key
463
- if ( mPrimaryKeyAttrs .isEmpty () )
463
+ }
464
+ // get primary key
465
+ if ( mPrimaryKeyAttrs .isEmpty () )
466
+ {
467
+ query.clear ();
468
+ query.setForwardOnly ( true );
469
+ if ( !query.exec ( QStringLiteral ( " exec sp_pkeys @table_name = N%1, @table_owner = %2 " ).arg ( quotedValue ( mTableName ), quotedValue ( mSchemaName ) ) ) )
464
470
{
465
- query.clear ();
466
- query.setForwardOnly ( true );
467
- if ( !query.exec ( QStringLiteral ( " exec sp_pkeys @table_name = N%1, @table_owner = %2 " ).arg ( quotedValue ( mTableName ), quotedValue ( mSchemaName ) ) ) )
468
- {
469
- QgsDebugMsg ( QStringLiteral ( " SQL:%1\n Error:%2" ).arg ( query.lastQuery (), query.lastError ().text () ) );
470
- }
471
+ QgsDebugMsg ( QStringLiteral ( " SQL:%1\n Error:%2" ).arg ( query.lastQuery (), query.lastError ().text () ) );
472
+ }
471
473
472
- if ( query.isActive () )
473
- {
474
- mPrimaryKeyType = PktInt;
474
+ if ( query.isActive () )
475
+ {
476
+ mPrimaryKeyType = PktInt;
475
477
476
- while ( query.next () )
477
- {
478
- QString fidColName = query.value ( 3 ).toString ();
479
- int idx = mAttributeFields .indexFromName ( fidColName );
480
- const QgsField &fld = mAttributeFields .at ( idx );
478
+ while ( query.next () )
479
+ {
480
+ QString fidColName = query.value ( 3 ).toString ();
481
+ int idx = mAttributeFields .indexFromName ( fidColName );
482
+ const QgsField &fld = mAttributeFields .at ( idx );
481
483
482
- if ( !mPrimaryKeyAttrs .isEmpty () ||
483
- ( fld.type () != QVariant::Int &&
484
- fld.type () != QVariant::LongLong &&
485
- ( fld.type () != QVariant::Double || fld.precision () != 0 ) ) )
486
- mPrimaryKeyType = PktFidMap;
484
+ if ( !mPrimaryKeyAttrs .isEmpty () ||
485
+ ( fld.type () != QVariant::Int &&
486
+ fld.type () != QVariant::LongLong &&
487
+ ( fld.type () != QVariant::Double || fld.precision () != 0 ) ) )
488
+ mPrimaryKeyType = PktFidMap;
487
489
488
- mPrimaryKeyAttrs << idx;
489
- }
490
+ mPrimaryKeyAttrs << idx;
491
+ }
490
492
491
- if ( mPrimaryKeyAttrs .isEmpty () )
492
- {
493
- mPrimaryKeyType = PktUnknown;
494
- }
493
+ if ( mPrimaryKeyAttrs .isEmpty () )
494
+ {
495
+ mPrimaryKeyType = PktUnknown;
495
496
}
496
497
}
498
+ }
497
499
498
- if ( mPrimaryKeyAttrs .isEmpty () )
500
+ if ( mPrimaryKeyAttrs .isEmpty () )
501
+ {
502
+ const auto constPkCandidates = pkCandidates;
503
+ for ( const QString &pk : constPkCandidates )
499
504
{
500
- const auto constPkCandidates = pkCandidates;
501
- for ( const QString &pk : constPkCandidates )
505
+ query.clear ();
506
+ query.setForwardOnly ( true );
507
+ if ( !query.exec ( QStringLiteral ( " select count(distinct [%1]), count([%1]) from [%2].[%3]" )
508
+ .arg ( pk, mSchemaName , mTableName ) ) )
502
509
{
503
- query.clear ();
504
- query.setForwardOnly ( true );
505
- if ( !query.exec ( QStringLiteral ( " select count(distinct [%1]), count([%1]) from [%2].[%3]" )
506
- .arg ( pk, mSchemaName , mTableName ) ) )
507
- {
508
- QgsDebugMsg ( QStringLiteral ( " SQL:%1\n Error:%2" ).arg ( query.lastQuery (), query.lastError ().text () ) );
509
- }
510
+ QgsDebugMsg ( QStringLiteral ( " SQL:%1\n Error:%2" ).arg ( query.lastQuery (), query.lastError ().text () ) );
511
+ }
510
512
511
- if ( query.isActive () && query.next () && query.value ( 0 ).toInt () == query.value ( 1 ).toInt () )
512
- {
513
- mPrimaryKeyType = PktInt;
514
- mPrimaryKeyAttrs << mAttributeFields .indexFromName ( pk );
515
- return ;
516
- }
513
+ if ( query.isActive () && query.next () && query.value ( 0 ).toInt () == query.value ( 1 ).toInt () )
514
+ {
515
+ mPrimaryKeyType = PktInt;
516
+ mPrimaryKeyAttrs << mAttributeFields .indexFromName ( pk );
517
+ return ;
517
518
}
518
519
}
520
+ }
519
521
520
- if ( mPrimaryKeyAttrs .isEmpty () )
521
- {
522
- QString error = QStringLiteral ( " No primary key could be found on table %1" ).arg ( mTableName );
523
- QgsDebugMsg ( error );
524
- mValid = false ;
525
- setLastError ( error );
526
- }
522
+ if ( mPrimaryKeyAttrs .isEmpty () )
523
+ {
524
+ QString error = QStringLiteral ( " No primary key could be found on table %1" ).arg ( mTableName );
525
+ QgsDebugMsg ( error );
526
+ mValid = false ;
527
+ setLastError ( error );
528
+ }
527
529
528
- if ( mPrimaryKeyAttrs .size () == 1 && !isIdentity )
529
- {
530
- // primary key has not null, unique constraints
531
- QgsFieldConstraints constraints = mAttributeFields .at ( mPrimaryKeyAttrs [0 ] ).constraints ();
532
- constraints.setConstraint ( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
533
- constraints.setConstraint ( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
534
- mAttributeFields [ mPrimaryKeyAttrs [0 ] ].setConstraints ( constraints );
535
- }
530
+ if ( mPrimaryKeyAttrs .size () == 1 && !isIdentity )
531
+ {
532
+ // primary key has unique constraints
533
+ QgsFieldConstraints constraints = mAttributeFields .at ( mPrimaryKeyAttrs [0 ] ).constraints ();
534
+ constraints.setConstraint ( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
535
+ mAttributeFields [ mPrimaryKeyAttrs [0 ] ].setConstraints ( constraints );
536
536
}
537
537
}
538
538
0 commit comments