Skip to content

Commit 8748a72

Browse files
committed
Update constraints for recently-added schemas.
1 parent dbccb33 commit 8748a72

File tree

6 files changed

+71
-85
lines changed

6 files changed

+71
-85
lines changed

docs/content/overview/schema/foreign keys.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
date: 2022-07-10
3-
lastmod: 2023-12-15
3+
lastmod: 2023-12-16
44
title: Foreign Keys Schema
55
---
66

@@ -30,5 +30,5 @@ Restriction Name | Restriction Default | Restriction Number
3030
Catalog | TABLE_CATALOG | 1
3131
Schema | TABLE_SCHEMA | 2
3232
Table | TABLE_NAME | 3
33-
Column | COLUMN_NAME | 4
33+
Constraint Name | CONSTRAINT_NAME | 4
3434

docs/content/overview/schema/indexcolumns.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
date: 2022-07-10
3-
lastmod: 2023-12-15
3+
lastmod: 2023-12-16
44
title: IndexColumns Schema
55
---
66

@@ -25,6 +25,5 @@ Restriction Name | Restriction Default | Restriction Number
2525
Catalog | TABLE_CATALOG | 1
2626
Schema | TABLE_SCHEMA | 2
2727
Table | TABLE_NAME | 3
28-
Constraint | CONSTRAINT_NAME | 4
29-
Column | COLUMN_NAME | 5
28+
Name | INDEX_NAME | 4
3029

docs/content/overview/schema/indexes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
date: 2022-07-10
3-
lastmod: 2023-12-15
3+
lastmod: 2023-12-16
44
title: Indexes Schema
55
---
66

@@ -26,5 +26,5 @@ Restriction Name | Restriction Default | Restriction Number
2626
Catalog | TABLE_CATALOG | 1
2727
Schema | TABLE_SCHEMA | 2
2828
Table | TABLE_NAME | 3
29-
Column | COLUMN_NAME | 4
29+
Name | INDEX_NAME | 4
3030

src/MySqlConnector/Core/SchemaProvider.cs

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/MySqlConnector/Core/SchemaProvider.g.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private Task FillMetaDataCollectionsAsync(IOBehavior ioBehavior, DataTable dataT
131131
dataTable.Rows.Add("Views", 0, 3);
132132
dataTable.Rows.Add("Foreign Keys", 4, 0);
133133
dataTable.Rows.Add("Indexes", 4, 0);
134-
dataTable.Rows.Add("IndexColumns", 5, 0);
134+
dataTable.Rows.Add("IndexColumns", 4, 0);
135135

136136
return Task.CompletedTask;
137137
}
@@ -640,16 +640,15 @@ private Task FillRestrictionsAsync(IOBehavior ioBehavior, DataTable dataTable, s
640640
dataTable.Rows.Add("Foreign Keys", "Catalog", "TABLE_CATALOG", 1);
641641
dataTable.Rows.Add("Foreign Keys", "Schema", "TABLE_SCHEMA", 2);
642642
dataTable.Rows.Add("Foreign Keys", "Table", "TABLE_NAME", 3);
643-
dataTable.Rows.Add("Foreign Keys", "Column", "COLUMN_NAME", 4);
643+
dataTable.Rows.Add("Foreign Keys", "Constraint Name", "CONSTRAINT_NAME", 4);
644644
dataTable.Rows.Add("Indexes", "Catalog", "TABLE_CATALOG", 1);
645645
dataTable.Rows.Add("Indexes", "Schema", "TABLE_SCHEMA", 2);
646646
dataTable.Rows.Add("Indexes", "Table", "TABLE_NAME", 3);
647-
dataTable.Rows.Add("Indexes", "Column", "COLUMN_NAME", 4);
647+
dataTable.Rows.Add("Indexes", "Name", "INDEX_NAME", 4);
648648
dataTable.Rows.Add("IndexColumns", "Catalog", "TABLE_CATALOG", 1);
649649
dataTable.Rows.Add("IndexColumns", "Schema", "TABLE_SCHEMA", 2);
650650
dataTable.Rows.Add("IndexColumns", "Table", "TABLE_NAME", 3);
651-
dataTable.Rows.Add("IndexColumns", "Constraint", "CONSTRAINT_NAME", 4);
652-
dataTable.Rows.Add("IndexColumns", "Column", "COLUMN_NAME", 5);
651+
dataTable.Rows.Add("IndexColumns", "Name", "INDEX_NAME", 4);
653652

654653
return Task.CompletedTask;
655654
}
@@ -902,8 +901,8 @@ private async Task FillIndexesAsync(IOBehavior ioBehavior, DataTable dataTable,
902901

903902
private async Task FillIndexColumnsAsync(IOBehavior ioBehavior, DataTable dataTable, string tableName, string?[]? restrictionValues, CancellationToken cancellationToken)
904903
{
905-
if (restrictionValues is { Length: > 5 })
906-
throw new ArgumentException("More than 5 restrictionValues are not supported for schema 'IndexColumns'.", nameof(restrictionValues));
904+
if (restrictionValues is { Length: > 4 })
905+
throw new ArgumentException("More than 4 restrictionValues are not supported for schema 'IndexColumns'.", nameof(restrictionValues));
907906

908907
dataTable.TableName = tableName;
909908
dataTable.Columns.AddRange(

tools/SchemaCollectionGenerator/SchemaCollections.yaml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,8 @@
770770
default: TABLE_SCHEMA
771771
- name: Table
772772
default: TABLE_NAME
773-
- name: Column
774-
default: COLUMN_NAME
773+
- name: Constraint Name
774+
default: CONSTRAINT_NAME
775775
columns:
776776
- name: CONSTRAINT_CATALOG
777777
type: string
@@ -808,10 +808,10 @@
808808
default: TABLE_SCHEMA
809809
- name: Table
810810
default: TABLE_NAME
811-
- name: Column # it is only a guess - might need to be changed, but NHibernate passes 4 values, with last one is null
812-
default: COLUMN_NAME
811+
- name: Name
812+
default: INDEX_NAME
813813
columns:
814-
- name: INDEX_CATALOG # placeholder - will be filled by SQL query
814+
- name: INDEX_CATALOG
815815
type: string
816816
- name: INDEX_SCHEMA # same as TABLE_SCHEMA, see https://mariadb.com/kb/en/information-schema-statistics-table/
817817
type: string
@@ -838,12 +838,10 @@
838838
default: TABLE_SCHEMA
839839
- name: Table
840840
default: TABLE_NAME
841-
- name: Constraint
842-
default: CONSTRAINT_NAME
843-
- name: Column # it comes fro
844-
default: COLUMN_NAME
841+
- name: Name
842+
default: INDEX_NAME
845843
columns:
846-
- name: INDEX_CATALOG # placeholder - will be filled by SQL query
844+
- name: INDEX_CATALOG
847845
type: string
848846
- name: INDEX_SCHEMA # same as TABLE_SCHEMA, see https://mariadb.com/kb/en/information-schema-statistics-table/
849847
type: string

0 commit comments

Comments
 (0)