Skip to content

Commit

Permalink
Metadata intellisense improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Nov 21, 2021
1 parent ba39040 commit cce0318
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions MarkMpn.Sql4Cds/Autocomplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public IEnumerable<SqlAutocompleteItem> GetSuggestions(string text, int pos)

foreach (var table in tables)
{
if (TryParseTableName(table.Value, out var instanceName, out _, out var tableName) && _dataSources.TryGetValue(instanceName, out var instance) && instance.Metadata.TryGetMinimalData(tableName, out var metadata))
if (TryParseTableName(table.Value, out var instanceName, out var schemaName, out var tableName) && _dataSources.TryGetValue(instanceName, out var instance) && instance.Metadata.TryGetMinimalData((schemaName == "metadata" ? "metadata." : "") + tableName, out var metadata))
{
if (metadata.OneToManyRelationships != null)
joinSuggestions.AddRange(metadata.OneToManyRelationships.Select(rel => new JoinAutocompleteItem(rel, $"{rel.ReferencingEntity}{GetUniqueTableAlias(rel.ReferencingEntity, tables)} ON {table.Key}.{rel.ReferencedAttribute} = {GetUniqueTableName(rel.ReferencingEntity, tables)}.{rel.ReferencingAttribute}", true, instance.Entities, instance.Metadata, currentLength)));
Expand Down Expand Up @@ -359,7 +359,7 @@ public IEnumerable<SqlAutocompleteItem> GetSuggestions(string text, int pos)

if (tables.TryGetValue(alias, out var tableName))
{
if (TryParseTableName(tableName, out var instanceName, out _, out tableName) && _dataSources.TryGetValue(instanceName, out var instance) && instance.Metadata.TryGetMinimalData(tableName, out var metadata))
if (TryParseTableName(tableName, out var instanceName, out var schemaName, out tableName) && _dataSources.TryGetValue(instanceName, out var instance) && instance.Metadata.TryGetMinimalData((schemaName == "metadata" ? "metadata." : "") + tableName, out var metadata))
return FilterList(metadata.Attributes.Where(a => a.IsValidForRead != false && a.AttributeOf == null).SelectMany(a => AttributeAutocompleteItem.CreateList(a, currentLength, false)).OrderBy(a => a), currentWord);
}
}
Expand Down Expand Up @@ -422,7 +422,7 @@ public IEnumerable<SqlAutocompleteItem> GetSuggestions(string text, int pos)
if (entity != null)
items.Add(new EntityAutocompleteItem(entity, table.Key, instance.Metadata, currentLength));

if (instance.Metadata.TryGetMinimalData(tableName, out var metadata))
if (instance.Metadata.TryGetMinimalData((schemaName == "metadata" ? "metadata." : "") + tableName, out var metadata))
attributes.AddRange(metadata.Attributes);
}
}
Expand Down Expand Up @@ -816,14 +816,16 @@ private string GetUniqueTableAlias(string name, IDictionary<string,string> table
{
var alias = GetUniqueTableName(name, tables);

if (name == alias)
if (name.Split('.').Last() == alias)
return null;

return " AS " + alias;
}

private string GetUniqueTableName(string name, IDictionary<string,string> tables)
{
name = name.Split('.').Last();

if (!tables.ContainsKey(name))
return name;

Expand Down Expand Up @@ -1183,7 +1185,9 @@ public override string ToolTipText

public override string GetTextForReplace()
{
_metadata.TryGetMinimalData(_rhs.LogicalName, out _);
if (_rhs != null)
_metadata.TryGetMinimalData(_rhs.LogicalName, out _);

_metadata.TryGetMinimalData(_lhs, out _);
return base.GetTextForReplace();
}
Expand Down
4 changes: 2 additions & 2 deletions MarkMpn.Sql4Cds/SqlQueryControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ private Scintilla CreateSqlEditor()
else
entities = entities.Concat(metaEntities).ToArray();

return new AutocompleteDataSource { Name = ds.Name, Entities = entities, Metadata = ds.Metadata };
return new AutocompleteDataSource { Name = ds.Name, Entities = entities, Metadata = new MetaMetadataCache(ds.Metadata) };
})
.ToDictionary(ds => ds.Name, StringComparer.OrdinalIgnoreCase);

Expand Down Expand Up @@ -504,7 +504,7 @@ public IEnumerator<AutocompleteItem> GetEnumerator()
else
entities = entities.Concat(metaEntities).ToArray();

return new AutocompleteDataSource { Name = ds.Name, Entities = entities, Metadata = ds.Metadata };
return new AutocompleteDataSource { Name = ds.Name, Entities = entities, Metadata = new MetaMetadataCache(ds.Metadata) };
})
.ToDictionary(ds => ds.Name, StringComparer.OrdinalIgnoreCase);

Expand Down

0 comments on commit cce0318

Please sign in to comment.