This commit is contained in:
Luc Genetier 2024-08-05 18:13:03 +02:00 коммит произвёл GitHub
Родитель dbffee13ff
Коммит 1ceef60c2f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 82 добавлений и 3 удалений

Просмотреть файл

@ -59,14 +59,14 @@ namespace Microsoft.PowerFx.Connectors
return false;
}
ConnectorType cr = ConnectorType.Fields.First(ct => ct.Name == fieldName);
ConnectorType ct = ConnectorType.Fields.First(ct => ct.Name == fieldName);
if (cr.ExternalTables?.Any() != true)
if (ct.ExternalTables?.Any() != true)
{
return base.TryGetFieldType(fieldName, out type);
}
string tableName = cr.ExternalTables.First();
string tableName = ct.ExternalTables.First();
try
{

Просмотреть файл

@ -49,5 +49,55 @@ namespace Microsoft.PowerFx.Connectors
GetTables tables = await GetObject<GetTables>(httpClient, "Get tables", uri, null, cancellationToken, logger).ConfigureAwait(false);
return tables?.Value?.Select(rt => new CdpTable(DatasetName, rt.Name, DatasetMetadata, tables?.Value) { DisplayName = rt.DisplayName });
}
/// <summary>
/// Retrieves a single CdpTable.
/// </summary>
/// <param name="httpClient">HttpClient.</param>
/// <param name="uriPrefix">Connector Uri prefix.</param>
/// <param name="tableName">Table name to search.</param>
/// <param name="logicalOrDisplay">bool? value: true = logical only, false = display name only, null = logical or display name. All comparisons are case sensitive.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <param name="logger">Logger.</param>
/// <returns>CdpTable class.</returns>
/// <exception cref="InvalidOperationException">When no or more than one tables are identified.</exception>
public async Task<CdpTable> GetTableAsync(HttpClient httpClient, string uriPrefix, string tableName, bool? logicalOrDisplay, CancellationToken cancellationToken, ConnectorLogger logger = null)
{
cancellationToken.ThrowIfCancellationRequested();
IEnumerable<CdpTable> tables = await GetTablesAsync(httpClient, uriPrefix, cancellationToken, logger).ConfigureAwait(false);
IEnumerable<CdpTable> filtered = tables.Where(ct => IsNameMatching(ct.TableName, ct.DisplayName, tableName, logicalOrDisplay));
if (!filtered.Any())
{
throw new InvalidOperationException("Cannot find any table with the specified name");
}
if (filtered.Count() > 1)
{
throw new InvalidOperationException($"Too many tables correspond to the specified name - Found {filtered.Count()} tables");
}
return filtered.First();
}
// logicalOrDisplay
// true = logical only
// false = display name only
// null = any
private bool IsNameMatching(string logicalName, string displayName, string expectedName, bool? logicalOrDisplay)
{
if (logicalOrDisplay != false && logicalName == expectedName)
{
return true;
}
if (logicalOrDisplay != true && displayName == expectedName)
{
return true;
}
return false;
}
}
}

Просмотреть файл

@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
@ -193,6 +194,28 @@ namespace Microsoft.PowerFx.Connectors.Tests
Assert.False(connectorTable.IsInitialized);
Assert.Equal(realTableName, connectorTable.DisplayName);
testConnector.SetResponseFromFile(@"Responses\SQL GetTables SampleDB.json");
CdpTable table2 = await cds.GetTableAsync(client, $"/apim/sql/{connectionId}", realTableName, null /* logical or display name */, CancellationToken.None, logger);
Assert.False(table2.IsInitialized);
Assert.Equal(realTableName, table2.DisplayName);
Assert.Equal("[SalesLT].[Product]", table2.TableName); // Logical Name
testConnector.SetResponseFromFile(@"Responses\SQL GetTables SampleDB.json");
table2 = await cds.GetTableAsync(client, $"/apim/sql/{connectionId}", realTableName, false /* display name only */, CancellationToken.None, logger);
Assert.False(table2.IsInitialized);
Assert.Equal(realTableName, table2.DisplayName);
Assert.Equal("[SalesLT].[Product]", table2.TableName); // Logical Name
testConnector.SetResponseFromFile(@"Responses\SQL GetTables SampleDB.json");
table2 = await cds.GetTableAsync(client, $"/apim/sql/{connectionId}", "[SalesLT].[Product]", true /* logical name only */, CancellationToken.None, logger);
Assert.False(table2.IsInitialized);
Assert.Equal(realTableName, table2.DisplayName);
Assert.Equal("[SalesLT].[Product]", table2.TableName); // Logical Name
testConnector.SetResponseFromFile(@"Responses\SQL GetTables SampleDB.json");
InvalidOperationException ioe = await Assert.ThrowsAsync<InvalidOperationException>(() => cds.GetTableAsync(client, $"/apim/sql/{connectionId}", "[SalesLT].[Product]", false /* display name only */, CancellationToken.None, logger));
Assert.Equal("Cannot find any table with the specified name", ioe.Message);
testConnector.SetResponseFromFiles(@"Responses\SQL GetSchema Products.json", @"Responses\SQL GetRelationships SampleDB.json");
await connectorTable.InitAsync(client, $"/apim/sql/{connectionId}", CancellationToken.None, logger);
Assert.True(connectorTable.IsInitialized);
@ -604,6 +627,12 @@ namespace Microsoft.PowerFx.Connectors.Tests
Assert.Equal("Account", sfTable.TabularRecordType.TableSymbolName);
RecordType rt = sfTable.TabularRecordType;
NamedFormulaType nft = rt.GetFieldTypes().First();
Assert.Equal("AccountSource", nft.Name);
Assert.Equal("Account Source", nft.DisplayName);
HashSet<IExternalTabularDataSource> ads = sfTable.Type._type.AssociatedDataSources;
Assert.NotNull(ads);