Skip to content

Commit

Permalink
Extract restrictions arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik committed May 20, 2024
1 parent 0799598 commit 1033016
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
44 changes: 32 additions & 12 deletions DuckDB.NET.Data/DuckDBSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ namespace DuckDB.NET.Data;

internal static class DuckDBSchema
{
private static readonly string[] TableRestrictions =
["table_catalog", "table_schema", "table_name", "table_type"];

private static readonly string[] ColumnRestrictions =
["table_catalog", "table_schema", "table_name", "column_name"];

private static readonly string[] ForeignKeyRestrictions =
["constraint_catalog", "constraint_schema", "table_name", "constraint_name"];

private static readonly string[] IndexesRestrictions =
["index_catalog", "index_schema", "table_name", "index_name"];

public static DataTable GetSchema(DuckDBConnection connection, string collectionName, string?[]? restrictionValues) =>
collectionName.ToUpperInvariant() switch
{
Expand Down Expand Up @@ -35,10 +47,10 @@ private static DataTable GetMetaDataCollections() =>
{ DbMetaDataCollectionNames.MetaDataCollections, 0, 0 },
{ DbMetaDataCollectionNames.Restrictions, 0, 0 },
{ DbMetaDataCollectionNames.ReservedWords, 0, 0 },
{ DuckDbMetaDataCollectionNames.Tables, 4, 3 },
{ DuckDbMetaDataCollectionNames.Columns, 4, 4 },
{ DuckDbMetaDataCollectionNames.ForeignKeys, 4, 3 },
{ DuckDbMetaDataCollectionNames.Indexes, 4, 3 },
{ DuckDbMetaDataCollectionNames.Tables, TableRestrictions.Length, 3 },
{ DuckDbMetaDataCollectionNames.Columns, ColumnRestrictions.Length, 4 },
{ DuckDbMetaDataCollectionNames.ForeignKeys, ForeignKeyRestrictions.Length, 3 },
{ DuckDbMetaDataCollectionNames.Indexes, IndexesRestrictions.Length, 3 },
}
};

Expand Down Expand Up @@ -86,16 +98,21 @@ private static DataTable GetReservedWords(DuckDBConnection connection)

private static DataTable GetTables(DuckDBConnection connection, string?[]? restrictionValues)
{
if (restrictionValues?.Length > TableRestrictions.Length)
throw new ArgumentException("Too many restrictions", nameof(restrictionValues));

const string query = "SELECT table_catalog, table_schema, table_name, table_type FROM information_schema.tables";

using var command = BuildCommand(connection, query, restrictionValues, true,
["table_catalog", "table_schema", "table_name", "table_type"]);
using var command = BuildCommand(connection, query, restrictionValues, true, TableRestrictions);

return GetDataTable(DuckDbMetaDataCollectionNames.Tables, command);
}

private static DataTable GetColumns(DuckDBConnection connection, string?[]? restrictionValues)
{
if (restrictionValues?.Length > ColumnRestrictions.Length)
throw new ArgumentException("Too many restrictions", nameof(restrictionValues));

const string query =
"""
SELECT
Expand All @@ -107,14 +124,16 @@ private static DataTable GetColumns(DuckDBConnection connection, string?[]? rest
character_set_catalog, character_set_schema, character_set_name, collation_catalog
FROM information_schema.columns
""";
using var command = BuildCommand(connection, query, restrictionValues, true,
["table_catalog", "table_schema", "table_name", "column_name"]);
using var command = BuildCommand(connection, query, restrictionValues, true, ColumnRestrictions);

return GetDataTable(DuckDbMetaDataCollectionNames.Columns, command);
}

private static DataTable GetForeignKeys(DuckDBConnection connection, string?[]? restrictionValues)
{
if (restrictionValues?.Length > ForeignKeyRestrictions.Length)
throw new ArgumentException("Too many restrictions", nameof(restrictionValues));

const string query =
"""
SELECT
Expand All @@ -124,14 +143,16 @@ private static DataTable GetForeignKeys(DuckDBConnection connection, string?[]?
FROM information_schema.table_constraints
WHERE constraint_type = 'FOREIGN KEY'
""";
using var command = BuildCommand(connection, query, restrictionValues, false,
["constraint_catalog", "constraint_schema", "table_name", "constraint_name"]);
using var command = BuildCommand(connection, query, restrictionValues, false, ForeignKeyRestrictions);

return GetDataTable(DuckDbMetaDataCollectionNames.ForeignKeys, command);
}

private static DataTable GetIndexes(DuckDBConnection connection, string?[]? restrictionValues)
{
if (restrictionValues?.Length > IndexesRestrictions.Length)
throw new ArgumentException("Too many restrictions", nameof(restrictionValues));

const string query =
"""
SELECT
Expand All @@ -143,8 +164,7 @@ private static DataTable GetIndexes(DuckDBConnection connection, string?[]? rest
is_primary
FROM duckdb_indexes()
""";
using var command = BuildCommand(connection, query, restrictionValues, true,
["index_catalog", "index_schema", "table_name", "index_name"]);
using var command = BuildCommand(connection, query, restrictionValues, true, IndexesRestrictions);

return GetDataTable(DuckDbMetaDataCollectionNames.Indexes, command);
}
Expand Down
28 changes: 26 additions & 2 deletions DuckDB.NET.Test/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ public void TablesWithRestrictions()
Assert.Equal(1, schema.Rows.Count);
Assert.Equal("bar", schema.Rows[0]["table_name"]);
}


[Fact]
public void TablesTooManyRestrictions()
{
Assert.Throws<ArgumentException>(() => Connection.GetSchema("Tables", new string [5]));
}

[Fact]
public void ColumnsWithRestrictions()
{
Expand All @@ -117,7 +123,13 @@ public void ColumnsWithRestrictions()
Assert.Equal("foo", schema.Rows[0]["table_name"]);
Assert.Equal("foo_id", schema.Rows[0]["column_name"]);
}


[Fact]
public void ColumnsTooManyRestrictions()
{
Assert.Throws<ArgumentException>(() => Connection.GetSchema("Columns", new string [5]));
}

[Fact]
public void ForeignKeysWithRestrictions()
{
Expand All @@ -126,6 +138,12 @@ public void ForeignKeysWithRestrictions()
Assert.Equal("bar", schema.Rows[0]["table_name"]);
}

[Fact]
public void ForeignKeysTooManyRestrictions()
{
Assert.Throws<ArgumentException>(() => Connection.GetSchema("ForeignKeys", new string [5]));
}

[Fact]
public void NonUniqueIndex()
{
Expand All @@ -152,6 +170,12 @@ public void UniqueIndex()
Assert.Equal(false, schema.Rows[0]["is_primary"]);
}

[Fact]
public void IndexesTooManyRestrictions()
{
Assert.Throws<ArgumentException>(() => Connection.GetSchema("Indexes", new string [5]));
}

private static IEnumerable<string> GetValues(DataTable schema, string columnName) =>
schema.Rows.Cast<DataRow>().Select(r => (string)r[columnName]);
}

0 comments on commit 1033016

Please sign in to comment.