Skip to content

Commit

Permalink
Microsoft.Data.Sqlite: Handle quotes in table name in dataReader.GetS…
Browse files Browse the repository at this point in the history
…chemaTable

Fixes dotnet#30441
  • Loading branch information
bricelam committed Apr 4, 2023
1 parent 8841bc4 commit e86b2c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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

0 comments on commit e86b2c9

Please sign in to comment.