Skip to content

Commit eefc57a

Browse files
tomfroehlethomasfroehleAniruddh25aaronburtle
authored
Support quoted table names (#2616)
## Why make this change? - Fixes #2385 ## What is this change? - In [SqlMetadataProvider.PopulateColumnDefinitionsWithReadOnlyFlag](https://github.com/Azure/data-api-builder/blob/c9e836b551eb6bc729e88e71b3aa4d1c1b235345/src/Core/Services/MetadataProviders/SqlMetadataProvider.cs#L1355) the QuoteIdentifier-method of the SqlQueryBuilder was not used to add quotes to the table name ## How was this tested? - [ ] Integration Tests -> I'm happy to extend the integration tests, if this fix seems like a good idea - [ ] Unit Tests - [x] manually ## Sample Request(s) Sample configuraiton: ``` "MyTable": { "source": { "object": "schema.\"MyTable\"", "type": "table" } } ``` --------- Co-authored-by: Thomas Fröhle <[email protected]> Co-authored-by: Aniruddh Munde <[email protected]> Co-authored-by: aaronburtle <[email protected]> Co-authored-by: aaron burtle <[email protected]>
1 parent 6c55993 commit eefc57a

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

src/Core/Resolvers/DWSqlQueryBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,5 +432,11 @@ private string BuildAggregationColumns(GroupByMetadata metadata)
432432
{
433433
return string.Join(", ", metadata.Aggregations.Select(aggregation => Build(aggregation.Column, useAlias: true)));
434434
}
435+
436+
public string QuoteTableNameAsDBConnectionParam(string param)
437+
{
438+
// Table names in DWSql should not be quoted when used as DB Connection Params.
439+
return param;
440+
}
435441
}
436442
}

src/Core/Resolvers/IQueryBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,11 @@ public interface IQueryBuilder
8484
/// Adds database specific quotes to string identifier
8585
/// </summary>
8686
public string QuoteIdentifier(string identifier);
87+
88+
/// <summary>
89+
/// Adds database specific quotes to the table name when used as part of a
90+
/// DB Connection Param.
91+
/// </summary>
92+
public string QuoteTableNameAsDBConnectionParam(string param);
8793
}
8894
}

src/Core/Resolvers/MsSqlQueryBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,5 +594,11 @@ public string BuildFetchEnabledTriggersQuery()
594594

595595
return query;
596596
}
597+
598+
public string QuoteTableNameAsDBConnectionParam(string param)
599+
{
600+
// Table names in MSSQL should not be quoted when used as DB Connection Params.
601+
return param;
602+
}
597603
}
598604
}

src/Core/Resolvers/MySqlQueryBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,5 +367,11 @@ public string BuildStoredProcedureResultDetailsQuery(string databaseObjectName)
367367
{
368368
throw new NotImplementedException();
369369
}
370+
371+
public string QuoteTableNameAsDBConnectionParam(string param)
372+
{
373+
// Table names in MySQL should not be quoted when used as DB Connection Params.
374+
return param;
375+
}
370376
}
371377
}

src/Core/Resolvers/PostgresQueryBuilder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,12 @@ public string BuildQueryToGetReadOnlyColumns(string schemaParamName, string tabl
241241
$"WHERE attrelid = ({schemaParamName} || '.' || {tableParamName})::regclass AND attgenerated = 's';";
242242
return query;
243243
}
244+
245+
public string QuoteTableNameAsDBConnectionParam(string param)
246+
{
247+
// PostreSQL uses same quoting for table name as DB Connection Param
248+
// as when used directly in SQL text.
249+
return QuoteIdentifier(param);
250+
}
244251
}
245252
}

src/Core/Services/MetadataProviders/SqlMetadataProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,12 +1355,13 @@ private async Task PopulateSourceDefinitionAsync(
13551355
private async Task PopulateColumnDefinitionsWithReadOnlyFlag(string tableName, string schemaOrDatabaseName, SourceDefinition sourceDefinition)
13561356
{
13571357
string schemaOrDatabaseParamName = $"{BaseQueryStructure.PARAM_NAME_PREFIX}param0";
1358+
string quotedTableName = SqlQueryBuilder.QuoteTableNameAsDBConnectionParam(tableName);
13581359
string tableParamName = $"{BaseQueryStructure.PARAM_NAME_PREFIX}param1";
13591360
string queryToGetReadOnlyColumns = SqlQueryBuilder.BuildQueryToGetReadOnlyColumns(schemaOrDatabaseParamName, tableParamName);
13601361
Dictionary<string, DbConnectionParam> parameters = new()
13611362
{
13621363
{ schemaOrDatabaseParamName, new(schemaOrDatabaseName, DbType.String) },
1363-
{ tableParamName, new(tableName, DbType.String) }
1364+
{ tableParamName, new(quotedTableName, DbType.String) }
13641365
};
13651366

13661367
List<string>? readOnlyFields = await QueryExecutor.ExecuteQueryAsync(

0 commit comments

Comments
 (0)