@@ -443,69 +443,64 @@ private Task DoFillForeignKeysAsync(IOBehavior ioBehavior, DataTable dataTable,
443443 FillDataTableAsync ( IOBehavior . Synchronous , dataTable , command =>
444444 {
445445 command . CommandText = """
446- SELECT rc.constraint_catalog, rc.constraint_schema,
447- rc.constraint_name, kcu.table_catalog, kcu.table_schema, rc.table_name,
448- rc.match_option, rc.update_rule, rc.delete_rule,
449- NULL as referenced_table_catalog,
450- kcu.referenced_table_schema, rc.referenced_table_name
446+ SELECT rc.constraint_catalog, rc.constraint_schema, rc.constraint_name,
447+ kcu.table_catalog, kcu.table_schema,
448+ rc.table_name, rc.match_option, rc.update_rule, rc.delete_rule,
449+ NULL as referenced_table_catalog, kcu.referenced_table_schema, rc.referenced_table_name
451450 FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
452451 LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu ON
453- kcu.constraint_catalog <=> rc.constraint_catalog AND
454- kcu.constraint_schema <=> rc.constraint_schema AND
455- kcu.constraint_name <=> rc.constraint_name
456- WHERE 1=1 AND kcu.ORDINAL_POSITION=1
452+ (
453+ (kcu.constraint_catalog = rc.constraint_catalog OR (kcu.constraint_catalog IS NULL AND rc.constraint_catalog IS NULL)) AND
454+ (kcu.constraint_schema = rc.constraint_schema OR (kcu.constraint_schema IS NULL AND rc.constraint_schema IS NULL)) AND
455+ (kcu.constraint_name = rc.constraint_name OR (kcu.constraint_name IS NULL AND rc.constraint_name IS NULL))
456+ )
457+ WHERE kcu.ORDINAL_POSITION = 1
457458 """ ;
458459
459- if ( restrictionValues is not null )
460+ if ( restrictionValues is [ _ , { Length : > 0 } schema , .. ] )
460461 {
461- var where = "" ;
462- if ( restrictionValues . Length >= 2 && ! string . IsNullOrEmpty ( restrictionValues [ 1 ] ) )
463- {
464- where += " AND rc.constraint_schema LIKE @schema" ;
465- command . Parameters . AddWithValue ( "@schema" , restrictionValues [ 1 ] ) ;
466- }
467- if ( restrictionValues . Length >= 3 && ! string . IsNullOrEmpty ( restrictionValues [ 2 ] ) )
468- {
469- where += " AND rc.table_name LIKE @table" ;
470- command . Parameters . AddWithValue ( "@table" , restrictionValues [ 2 ] ) ;
471- }
472- if ( restrictionValues . Length >= 4 && ! string . IsNullOrEmpty ( restrictionValues [ 3 ] ) )
473- {
474- where += " AND rc.constraint_name LIKE @constraint" ;
475- command . Parameters . AddWithValue ( "@constraint" , restrictionValues [ 3 ] ) ;
476- }
477- command . CommandText += where ;
462+ command . CommandText += " AND rc.constraint_schema LIKE @schema" ;
463+ command . Parameters . AddWithValue ( "@schema" , schema ) ;
464+ }
465+ if ( restrictionValues is [ _, _, { Length : > 0 } table , ..] )
466+ {
467+ command . CommandText += " AND rc.table_name LIKE @table" ;
468+ command . Parameters . AddWithValue ( "@table" , table ) ;
469+ }
470+ if ( restrictionValues is [ _, _, _, { Length : > 0 } constraint , ..] )
471+ {
472+ command . CommandText += " AND rc.constraint_name LIKE @constraint" ;
473+ command . Parameters . AddWithValue ( "@constraint" , constraint ) ;
478474 }
479475 } , cancellationToken ) ;
480476
481477 private Task DoFillIndexesAsync ( IOBehavior ioBehavior , DataTable dataTable , string ? [ ] ? restrictionValues , CancellationToken cancellationToken ) =>
482478 FillDataTableAsync ( ioBehavior , dataTable , command =>
483479 {
484480 command . CommandText = """
485- SELECT DISTINCT null AS INDEX_CATALOG, INDEX_SCHEMA,
481+ SELECT null AS INDEX_CATALOG, INDEX_SCHEMA,
486482 INDEX_NAME, TABLE_NAME,
487483 !NON_UNIQUE as `UNIQUE`,
488484 INDEX_NAME='PRIMARY' as `PRIMARY`,
489485 INDEX_TYPE as TYPE, COMMENT
490486 FROM INFORMATION_SCHEMA.STATISTICS
491- WHERE 1 =1
487+ WHERE SEQ_IN_INDEX =1
492488 """ ;
493489
494- if ( restrictionValues is not null )
490+ if ( restrictionValues is [ _ , { Length : > 0 } schema , .. ] )
495491 {
496- var where = "" ;
497- if ( restrictionValues . Length >= 2 && ! string . IsNullOrEmpty ( restrictionValues [ 1 ] ) )
498- {
499- where += " AND INDEX_SCHEMA LIKE @schema" ;
500- command . Parameters . AddWithValue ( "@schema" , restrictionValues [ 1 ] ) ;
501- }
502- if ( restrictionValues . Length >= 3 && ! string . IsNullOrEmpty ( restrictionValues [ 2 ] ) )
503- {
504- where += " AND TABLE_NAME LIKE @table" ;
505- command . Parameters . AddWithValue ( "@table" , restrictionValues [ 2 ] ) ;
506- }
507-
508- command . CommandText += where ;
492+ command . CommandText += " AND INDEX_SCHEMA LIKE @schema" ;
493+ command . Parameters . AddWithValue ( "@schema" , schema ) ;
494+ }
495+ if ( restrictionValues is [ _, _, { Length : > 0 } table , ..] )
496+ {
497+ command . CommandText += " AND TABLE_NAME LIKE @table" ;
498+ command . Parameters . AddWithValue ( "@table" , table ) ;
499+ }
500+ if ( restrictionValues is [ _, _, _, { Length : > 0 } index , ..] )
501+ {
502+ command . CommandText += " AND INDEX_NAME LIKE @index" ;
503+ command . Parameters . AddWithValue ( "@index" , index ) ;
509504 }
510505 } , cancellationToken ) ;
511506
@@ -522,25 +517,20 @@ FROM INFORMATION_SCHEMA.STATISTICS
522517 WHERE 1=1
523518 """ ;
524519
525- if ( restrictionValues is not null )
520+ if ( restrictionValues is [ _, { Length : > 0 } schema , ..] )
521+ {
522+ command . CommandText += " AND INDEX_SCHEMA LIKE @schema" ;
523+ command . Parameters . AddWithValue ( "@schema" , schema ) ;
524+ }
525+ if ( restrictionValues is [ _, _, { Length : > 0 } table , ..] )
526+ {
527+ command . CommandText += " AND TABLE_NAME LIKE @table" ;
528+ command . Parameters . AddWithValue ( "@table" , table ) ;
529+ }
530+ if ( restrictionValues is [ _, _, _, { Length : > 0 } index , ..] )
526531 {
527- var where = "" ;
528- if ( restrictionValues . Length >= 2 && ! string . IsNullOrEmpty ( restrictionValues [ 1 ] ) )
529- {
530- where += " AND INDEX_SCHEMA LIKE @schema" ;
531- command . Parameters . AddWithValue ( "@schema" , restrictionValues [ 1 ] ) ;
532- }
533- if ( restrictionValues . Length >= 3 && ! string . IsNullOrEmpty ( restrictionValues [ 2 ] ) )
534- {
535- where += " AND TABLE_NAME LIKE @table" ;
536- command . Parameters . AddWithValue ( "@table" , restrictionValues [ 2 ] ) ;
537- }
538- if ( restrictionValues . Length >= 4 && ! string . IsNullOrEmpty ( restrictionValues [ 3 ] ) )
539- {
540- where += " AND INDEX_NAME LIKE @index" ;
541- command . Parameters . AddWithValue ( "@index" , restrictionValues [ 3 ] ) ;
542- }
543- command . CommandText += where ;
532+ command . CommandText += " AND INDEX_NAME LIKE @index" ;
533+ command . Parameters . AddWithValue ( "@index" , index ) ;
544534 }
545535 } , cancellationToken ) ;
546536}
0 commit comments