Skip to content

Commit 5fe27a5

Browse files
committed
Generate Restrictions schema.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent e0bf3f0 commit 5fe27a5

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

docs/content/overview/schema-collections.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ weight: 80
3535
* `ReferentialConstraints`
3636
* `ReservedWords`[information about reserved words in the server's SQL syntax](../schema/reservedwords/)
3737
* `ResourceGroups`
38+
* `Restrictions`[information about the restrictions supported when retrieving schemas](../schema/restrictions/)
3839
* `SchemaPrivileges`
3940
* `Tables`
4041
* `TableConstraints`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
date: 2022-07-10
3+
lastmod: 2022-07-11
4+
title: Restrictions Schema
5+
---
6+
7+
# Restrictions Schema
8+
9+
The `Restrictions` schema provides information about the restrictions supported when retrieving schemas.
10+
11+
Column Name | Data Type
12+
--- | ---
13+
CollectionName | string
14+
RestrictionName | string
15+
RestrictionDefault | string
16+
RestrictionNumber | int
17+

src/MySqlConnector/Core/SchemaProvider.g.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public async ValueTask<DataTable> GetSchemaAsync(IOBehavior ioBehavior, string c
5353
await FillReservedWordsAsync(ioBehavior, dataTable, restrictionValues, cancellationToken).ConfigureAwait(false);
5454
else if (string.Equals(collectionName, "ResourceGroups", StringComparison.OrdinalIgnoreCase))
5555
await FillResourceGroupsAsync(ioBehavior, dataTable, restrictionValues, cancellationToken).ConfigureAwait(false);
56+
else if (string.Equals(collectionName, "Restrictions", StringComparison.OrdinalIgnoreCase))
57+
await FillRestrictionsAsync(ioBehavior, dataTable, restrictionValues, cancellationToken).ConfigureAwait(false);
5658
else if (string.Equals(collectionName, "SchemaPrivileges", StringComparison.OrdinalIgnoreCase))
5759
await FillSchemaPrivilegesAsync(ioBehavior, dataTable, restrictionValues, cancellationToken).ConfigureAwait(false);
5860
else if (string.Equals(collectionName, "Tables", StringComparison.OrdinalIgnoreCase))
@@ -107,6 +109,7 @@ private Task FillMetaDataCollectionsAsync(IOBehavior ioBehavior, DataTable dataT
107109
dataTable.Rows.Add("ReferentialConstraints", 0, 0);
108110
dataTable.Rows.Add("ReservedWords", 0, 0);
109111
dataTable.Rows.Add("ResourceGroups", 0, 0);
112+
dataTable.Rows.Add("Restrictions", 0, 0);
110113
dataTable.Rows.Add("SchemaPrivileges", 0, 0);
111114
dataTable.Rows.Add("Tables", 0, 0);
112115
dataTable.Rows.Add("TableConstraints", 0, 0);
@@ -579,6 +582,27 @@ private async Task FillResourceGroupsAsync(IOBehavior ioBehavior, DataTable data
579582
await FillDataTableAsync(ioBehavior, dataTable, "RESOURCE_GROUPS", null, cancellationToken).ConfigureAwait(false);
580583
}
581584

585+
private Task FillRestrictionsAsync(IOBehavior ioBehavior, DataTable dataTable, string?[]? restrictionValues, CancellationToken cancellationToken)
586+
{
587+
if (restrictionValues is not null)
588+
throw new ArgumentException("restrictionValues is not supported for schema 'Restrictions'.", nameof(restrictionValues));
589+
590+
dataTable.Columns.AddRange(new DataColumn[]
591+
{
592+
new("CollectionName", typeof(string)),
593+
new("RestrictionName", typeof(string)),
594+
new("RestrictionDefault", typeof(string)),
595+
new("RestrictionNumber", typeof(int)),
596+
});
597+
598+
dataTable.Rows.Add("Columns", "Catalog", "TABLE_CATALOG", 1);
599+
dataTable.Rows.Add("Columns", "Schema", "TABLE_SCHEMA", 2);
600+
dataTable.Rows.Add("Columns", "Table", "TABLE_NAME", 3);
601+
dataTable.Rows.Add("Columns", "Column", "COLUMN_NAME", 4);
602+
603+
return Utility.CompletedTask;
604+
}
605+
582606
private async Task FillSchemaPrivilegesAsync(IOBehavior ioBehavior, DataTable dataTable, string?[]? restrictionValues, CancellationToken cancellationToken)
583607
{
584608
if (restrictionValues is not null)

tests/SideBySide/SchemaProviderTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ public void ColumnsRestriction()
8484
Assert.Equal("Bit32", table.Rows[0]["COLUMN_NAME"]);
8585
}
8686

87+
[Fact]
88+
public void SchemaRestrictionCount()
89+
{
90+
var metadata = m_database.Connection.GetSchema("MetaDataCollections");
91+
var restrictions = m_database.Connection.GetSchema("Restrictions");
92+
foreach (DataRow row in metadata.Rows)
93+
{
94+
var schema = (string) row["CollectionName"];
95+
#if BASELINE
96+
if (schema is "Views" or "ViewColumns" or "Triggers")
97+
continue;
98+
#endif
99+
100+
var restrictionCount = (int) row["NumberOfRestrictions"];
101+
var actualCount = restrictions.Rows.Cast<DataRow>().Count(x => (string) x["CollectionName"] == schema);
102+
Assert.Equal(restrictionCount, actualCount);
103+
}
104+
}
105+
87106
#if !BASELINE
88107
[Fact]
89108
public void ExcessColumnsRestriction() =>

tools/SchemaCollectionGenerator/SchemaCollectionGenerator.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ public async ValueTask<DataTable> GetSchemaAsync(IOBehavior ioBehavior, string c
108108
");
109109
}
110110
}
111+
else if (schema.Name == "Restrictions")
112+
{
113+
foreach (var schemaCollection in schemaCollections)
114+
{
115+
if (schemaCollection.Restrictions is { Count: > 0 })
116+
{
117+
for (var i = 0; i < schemaCollection.Restrictions.Count; i++)
118+
{
119+
var restriction = schemaCollection.Restrictions[i];
120+
codeWriter.WriteLine($@" dataTable.Rows.Add(""{schemaCollection.Name}"", ""{restriction.Name}"", ""{restriction.Default}"", {i + 1});");
121+
}
122+
}
123+
}
124+
}
111125
else
112126
{
113127
codeWriter.Write(@$" {schema.Custom}(dataTable);

tools/SchemaCollectionGenerator/SchemaCollections.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,18 @@
525525
- name: THREAD_PRIORITY
526526
type: int
527527

528+
- name: Restrictions
529+
description: information about the restrictions supported when retrieving schemas
530+
columns:
531+
- name: CollectionName
532+
type: string
533+
- name: RestrictionName
534+
type: string
535+
- name: RestrictionDefault
536+
type: string
537+
- name: RestrictionNumber
538+
type: int
539+
528540
- name: SchemaPrivileges
529541
table: SCHEMA_PRIVILEGES
530542
columns:

0 commit comments

Comments
 (0)