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

Microsoft.Data.Sqlite: Handle quotes in table name in GetSchemaTable #30632

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Microsoft.Data.Sqlite.Core/SqliteDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ public override DataTable GetSchemaTable()
var columnType = "typeof(\"" + columnName.Replace("\"", "\"\"") + "\")";
command.CommandText = new StringBuilder()
.AppendLine($"SELECT {columnType}")
.AppendLine($"FROM \"{tableName}\"")
.AppendLine($"FROM \"{tableName.Replace("\"", "\"\"")}\"")
.AppendLine($"WHERE {columnType} != 'null'")
.AppendLine($"GROUP BY {columnType}")
.AppendLine("ORDER BY count() DESC")
Expand Down
18 changes: 18 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,24 @@ public void GetSchemaTable_works()
}
}

[Fact]
public void GetSchemaTable_works_when_quotes()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.Open();
connection.ExecuteNonQuery(
@"CREATE TABLE ""Bad""""Table""(Value);");

using (var reader = connection.ExecuteReader(@"SELECT * FROM ""Bad""""Table"";"))
{
var schemaTable = reader.GetSchemaTable();
Assert.Equal(1, schemaTable.Rows.Count);
Assert.Equal(@"Bad""Table", schemaTable.Rows[0][SchemaTableColumn.BaseTableName]);
}
}
}

[Fact]
public void GetSchemaTable_works_when_view()
{
Expand Down