Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of the Connection.GetSchema of the DataSourceInformation collection #190

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions DuckDB.NET.Data/DuckDBSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
using System.Linq;
using System.Text;

using DuckDB.NET.Native;

namespace DuckDB.NET.Data;

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

Check warning on line 13 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'table_catalog' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

Check warning on line 13 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'table_schema' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

Check warning on line 13 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'table_name' 8 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

Check warning on line 13 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'table_catalog' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

Check warning on line 13 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'table_schema' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

Check warning on line 13 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'table_name' 8 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

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

Expand All @@ -20,6 +22,7 @@
collectionName.ToUpperInvariant() switch
{
"METADATACOLLECTIONS" => GetMetaDataCollections(),
"DATASOURCEINFORMATION" => GetDataSourceInformation(connection.ServerVersion),
"RESTRICTIONS" => GetRestrictions(),
"RESERVEDWORDS" => GetReservedWords(connection),
"TABLES" => GetTables(connection, restrictionValues),
Expand All @@ -41,6 +44,7 @@
Rows =
{
{ DbMetaDataCollectionNames.MetaDataCollections, 0, 0 },
{ DbMetaDataCollectionNames.DataSourceInformation, 0, 0 },
{ DbMetaDataCollectionNames.Restrictions, 0, 0 },
{ DbMetaDataCollectionNames.ReservedWords, 0, 0 },
{ DuckDbMetaDataCollectionNames.Tables, TableRestrictions.Length, 3 },
Expand All @@ -50,6 +54,52 @@
}
};

private static DataTable GetDataSourceInformation(string? serverVersion) =>
new(DbMetaDataCollectionNames.DataSourceInformation) {
Columns =
{
{ DbMetaDataColumnNames.CompositeIdentifierSeparatorPattern, typeof(string) },
{ DbMetaDataColumnNames.DataSourceProductName, typeof(string) },
{ DbMetaDataColumnNames.DataSourceProductVersion, typeof(string) },
{ DbMetaDataColumnNames.DataSourceProductVersionNormalized, typeof(string) },
{ DbMetaDataColumnNames.GroupByBehavior, typeof(GroupByBehavior) },
{ DbMetaDataColumnNames.IdentifierPattern, typeof(string) },
{ DbMetaDataColumnNames.IdentifierCase, typeof(IdentifierCase) },
{ DbMetaDataColumnNames.OrderByColumnsInSelect, typeof(bool) },
{ DbMetaDataColumnNames.ParameterMarkerFormat, typeof(string) },
{ DbMetaDataColumnNames.ParameterMarkerPattern, typeof(string) },
{ DbMetaDataColumnNames.ParameterNameMaxLength, typeof(int) },
{ DbMetaDataColumnNames.ParameterNamePattern, typeof(string) },
{ DbMetaDataColumnNames.QuotedIdentifierPattern, typeof(string) },
{ DbMetaDataColumnNames.QuotedIdentifierCase, typeof(IdentifierCase) },
{ DbMetaDataColumnNames.StatementSeparatorPattern, typeof(string) },
{ DbMetaDataColumnNames.StringLiteralPattern, typeof(string) },
{ DbMetaDataColumnNames.SupportedJoinOperators, typeof(SupportedJoinOperators) }
},
Rows =
{
{
"\\.",
"duckdb",
Giorgi marked this conversation as resolved.
Show resolved Hide resolved
serverVersion,
serverVersion,
GroupByBehavior.Unrelated,
"(^\\[\\p{Lo}\\p{Lu}\\p{Ll}_@#][\\p{Lo}\\p{Lu}\\p{Ll}\\p{Nd}@$#_]*$)|(^\\[[^\\]\\0]|\\]\\]+\\]$)|(^\\\"[^\\\"\\0]|\\\"\\\"+\\\"$)",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add comments explaining what these regexes do/mean and where they come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments.

IdentifierCase.Insensitive,
false,
"{0}",
"$[\\p{Lo}\\p{Lu}\\p{Ll}\\p{Lm}_@#][\\p{Lo}\\p{Lu}\\p{Ll}\\p{Lm}\\p{Nd}\\uff3f_@#\\$]*(?=\\s+|$)",
128,
"^[\\p{Lo}\\p{Lu}\\p{Ll}\\p{Lm}_@#][\\p{Lo}\\p{Lu}\\p{Ll}\\p{Lm}\\p{Nd}\\uff3f_@#\\$]*(?=\\s+|$)",
"(([^\\[]|\\]\\])*)",
IdentifierCase.Insensitive,
";",
"'(([^']|'')*)'",
SupportedJoinOperators.Inner | SupportedJoinOperators.LeftOuter | SupportedJoinOperators.RightOuter | SupportedJoinOperators.FullOuter
}
}
};

private static DataTable GetRestrictions() =>
new(DbMetaDataCollectionNames.Restrictions)
{
Expand All @@ -62,9 +112,9 @@
},
Rows =
{
{ DuckDbMetaDataCollectionNames.Tables, "Catalog", "table_catalog", 1 },

Check warning on line 115 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'Catalog' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
{ DuckDbMetaDataCollectionNames.Tables, "Schema", "table_schema", 2 },

Check warning on line 116 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'Schema' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
{ DuckDbMetaDataCollectionNames.Tables, "Table", "table_name", 3 },

Check warning on line 117 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'Table' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
{ DuckDbMetaDataCollectionNames.Tables, "TableType", "table_type", 4 },

{ DuckDbMetaDataCollectionNames.Columns, "Catalog", "table_catalog", 1 },
Expand Down Expand Up @@ -95,7 +145,7 @@
{
if (restrictionValues?.Length > TableRestrictions.Length)
{
throw new ArgumentException("Too many restrictions", nameof(restrictionValues));

Check warning on line 148 in DuckDB.NET.Data/DuckDBSchema.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Define a constant instead of using this literal 'Too many restrictions' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
}

const string query = "SELECT table_catalog, table_schema, table_name, table_type FROM information_schema.tables";
Expand Down
25 changes: 25 additions & 0 deletions DuckDB.NET.Test/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ public void IndexesTooManyRestrictions()
Assert.Throws<ArgumentException>(() => Connection.GetSchema("Indexes", new string [5]));
}

[Fact]
public void DataSourceInformation() {
var schema = Connection.GetSchema(DbMetaDataCollectionNames.DataSourceInformation);
Assert.NotEmpty(schema.Rows);

Assert.Equal(1, schema.Rows.Count);
Assert.Equal("\\.", schema.Rows[0][DbMetaDataColumnNames.CompositeIdentifierSeparatorPattern]);
Assert.Equal("duckdb", schema.Rows[0][DbMetaDataColumnNames.DataSourceProductName]);
Assert.Equal(Connection.ServerVersion, schema.Rows[0][DbMetaDataColumnNames.DataSourceProductVersion]);
Assert.Equal(Connection.ServerVersion, schema.Rows[0][DbMetaDataColumnNames.DataSourceProductVersionNormalized]);
Assert.Equal(GroupByBehavior.Unrelated, (GroupByBehavior)schema.Rows[0][DbMetaDataColumnNames.GroupByBehavior]);
Assert.Equal("(^\\[\\p{Lo}\\p{Lu}\\p{Ll}_@#][\\p{Lo}\\p{Lu}\\p{Ll}\\p{Nd}@$#_]*$)|(^\\[[^\\]\\0]|\\]\\]+\\]$)|(^\\\"[^\\\"\\0]|\\\"\\\"+\\\"$)", schema.Rows[0][DbMetaDataColumnNames.IdentifierPattern]);
Assert.Equal(IdentifierCase.Insensitive, (IdentifierCase)schema.Rows[0][DbMetaDataColumnNames.IdentifierCase]);
Assert.Equal(false, schema.Rows[0][DbMetaDataColumnNames.OrderByColumnsInSelect]);
Assert.Equal("{0}", schema.Rows[0][DbMetaDataColumnNames.ParameterMarkerFormat]);
Assert.Equal("$[\\p{Lo}\\p{Lu}\\p{Ll}\\p{Lm}_@#][\\p{Lo}\\p{Lu}\\p{Ll}\\p{Lm}\\p{Nd}\\uff3f_@#\\$]*(?=\\s+|$)", schema.Rows[0][DbMetaDataColumnNames.ParameterMarkerPattern]);
Assert.Equal(128, schema.Rows[0][DbMetaDataColumnNames.ParameterNameMaxLength]);
Assert.Equal("^[\\p{Lo}\\p{Lu}\\p{Ll}\\p{Lm}_@#][\\p{Lo}\\p{Lu}\\p{Ll}\\p{Lm}\\p{Nd}\\uff3f_@#\\$]*(?=\\s+|$)", schema.Rows[0][DbMetaDataColumnNames.ParameterNamePattern]);
Assert.Equal("(([^\\[]|\\]\\])*)", schema.Rows[0][DbMetaDataColumnNames.QuotedIdentifierPattern]);
Assert.Equal(IdentifierCase.Insensitive, (IdentifierCase)schema.Rows[0][DbMetaDataColumnNames.QuotedIdentifierCase]);
Assert.Equal(";", schema.Rows[0][DbMetaDataColumnNames.StatementSeparatorPattern]);
Assert.Equal("'(([^']|'')*)'", schema.Rows[0][DbMetaDataColumnNames.StringLiteralPattern]);
Assert.Equal(SupportedJoinOperators.Inner | SupportedJoinOperators.LeftOuter | SupportedJoinOperators.RightOuter | SupportedJoinOperators.FullOuter, (SupportedJoinOperators)schema.Rows[0][DbMetaDataColumnNames.SupportedJoinOperators]);
}

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