Skip to content

Commit

Permalink
Fixed dictionary bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-tkachev committed Apr 1, 2016
1 parent 69a37fb commit 0785cee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 52 deletions.
79 changes: 39 additions & 40 deletions Source/LinqToDBDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,52 +220,51 @@ public Exception TestConnection(ConnectionViewModel model)
public override List<ExplorerItem> GetSchemaAndBuildAssembly(
IConnectionInfo cxInfo, AssemblyName assemblyToBuild, ref string nameSpace, ref string typeName)
{
List<ExplorerItem> items = null;
try
{
var gen = new SchemaAndCodeGenerator(cxInfo);
items = gen.GetItemsAndCode(nameSpace, typeName).ToList();
var text = gen.Code.ToString();
var syntaxTree = CSharpSyntaxTree.ParseText(text);

var references = new List<MetadataReference>
{
MetadataReference.CreateFromFile(typeof(object). Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable). Assembly.Location),
MetadataReference.CreateFromFile(typeof(IDbConnection). Assembly.Location),
MetadataReference.CreateFromFile(typeof(DataConnection). Assembly.Location),
MetadataReference.CreateFromFile(typeof(LINQPadDataConnection).Assembly.Location),
};

references.AddRange(gen.References.Select(r => MetadataReference.CreateFromFile(r)));

var compilation = CSharpCompilation.Create(
assemblyToBuild.Name,
syntaxTrees : new[] { syntaxTree },
references : references,
options : new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

using (var stream = new FileStream(assemblyToBuild.CodeBase, FileMode.Create))
try
{
var result = compilation.Emit(stream);
var gen = new SchemaAndCodeGenerator(cxInfo);
var items = gen.GetItemsAndCode(nameSpace, typeName).ToList();
var text = gen.Code.ToString();
var syntaxTree = CSharpSyntaxTree.ParseText(text);

if (!result.Success)
var references = new List<MetadataReference>
{
var failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
MetadataReference.CreateFromFile(typeof(object). Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable). Assembly.Location),
MetadataReference.CreateFromFile(typeof(IDbConnection). Assembly.Location),
MetadataReference.CreateFromFile(typeof(DataConnection). Assembly.Location),
MetadataReference.CreateFromFile(typeof(LINQPadDataConnection).Assembly.Location),
};

references.AddRange(gen.References.Select(r => MetadataReference.CreateFromFile(r)));

var compilation = CSharpCompilation.Create(
assemblyToBuild.Name,
syntaxTrees : new[] { syntaxTree },
references : references,
options : new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

using (var stream = new FileStream(assemblyToBuild.CodeBase, FileMode.Create))
{
var result = compilation.Emit(stream);

if (!result.Success)
{
var failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);

foreach (var diagnostic in failures)
throw new Exception(diagnostic.ToString());
foreach (var diagnostic in failures)
throw new Exception(diagnostic.ToString());
}
}
}

}
catch(Exception ex)
{
MessageBox.Show($"{ex}\n{ex.StackTrace}");
}

return items;
return items;
}
catch (Exception ex)
{
MessageBox.Show($"{ex}\n{ex.StackTrace}");
throw;
}
}

public override ParameterDescriptor[] GetContextConstructorParameters(IConnectionInfo cxInfo)
Expand Down
22 changes: 10 additions & 12 deletions Source/SchemaAndCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ from p in _schema.Procedures
else
{
yield return new ExplorerItem(
GetUniqueName(_explorerNames, s.Key.SchemaName.IsNullOrEmpty() ? s.Key.IsDefaultSchema ? "(default)" : "empty" : s.Key.SchemaName),
s.Key.SchemaName.IsNullOrEmpty() ? s.Key.IsDefaultSchema ? "(default)" : "empty" : s.Key.SchemaName,
ExplorerItemKind.Schema,
ExplorerIcon.Schema)
{
Expand Down Expand Up @@ -380,15 +380,13 @@ void CodeProcedure(StringBuilder code, ProcedureSchema p, string sprocSqlName)
code.AppendLine(" }");
}

HashSet<string> _explorerNames = new HashSet<string>();

ExplorerItem GetColumnItem(ColumnSchema column)
{
var memberType = UseProviderSpecificTypes ? (column.ProviderSpecificType ?? column.MemberType) : column.MemberType;
var sqlName = (string)_sqlBuilder.Convert(column.ColumnName, ConvertType.NameToQueryField);

return new ExplorerItem(
GetUniqueName(_explorerNames, column.MemberName),
column.MemberName,
ExplorerItemKind.Property,
column.IsPrimaryKey ? ExplorerIcon.Key : ExplorerIcon.Column)
{
Expand All @@ -404,7 +402,7 @@ ExplorerItem GetProcedures(string header, ExplorerIcon icon, List<ProcedureSchem
{
var results = new HashSet<TableSchema>();

var items = new ExplorerItem(GetUniqueName(_explorerNames, header), ExplorerItemKind.Category, icon)
var items = new ExplorerItem(header, ExplorerItemKind.Category, icon)
{
Children = procedures
.Select(p =>
Expand All @@ -425,7 +423,7 @@ ExplorerItem GetProcedures(string header, ExplorerIcon icon, List<ProcedureSchem
memberName += $" -> {res.ParameterType}";
}

var ret = new ExplorerItem(GetUniqueName(_explorerNames, memberName), ExplorerItemKind.QueryableObject, icon)
var ret = new ExplorerItem(memberName, ExplorerItemKind.QueryableObject, icon)
{
DragText = $"{p.MemberName}(" +
p.Parameters
Expand All @@ -439,7 +437,7 @@ ExplorerItem GetProcedures(string header, ExplorerIcon icon, List<ProcedureSchem
.Where (pr => !pr.IsResult)
.Select(pr =>
new ExplorerItem(
GetUniqueName(_explorerNames, $"{pr.ParameterName} ({pr.ParameterType})"),
$"{pr.ParameterName} ({pr.ParameterType})",
ExplorerItemKind.Parameter,
ExplorerIcon.Parameter))
.Union(p.ResultTable?.Columns.Select(GetColumnItem) ?? new ExplorerItem[0])
Expand Down Expand Up @@ -536,7 +534,7 @@ ExplorerItem GetTables(string header, ExplorerIcon icon, IEnumerable<TableSchema
var tables = tableSource.ToList();
var dic = new Dictionary<TableSchema,ExplorerItem>();

var items = new ExplorerItem(GetUniqueName(_explorerNames, header), ExplorerItemKind.Category, icon)
var items = new ExplorerItem(header, ExplorerItemKind.Category, icon)
{
Children = tables
.Select(t =>
Expand All @@ -555,7 +553,7 @@ ExplorerItem GetTables(string header, ExplorerIcon icon, IEnumerable<TableSchema

//Debug.WriteLine($"Table: [{t.SchemaName}].[{t.TableName}] - ${tableSqlName}");

var ret = new ExplorerItem(GetUniqueName(_explorerNames, memberName), ExplorerItemKind.QueryableObject, icon)
var ret = new ExplorerItem(memberName, ExplorerItemKind.QueryableObject, icon)
{
DragText = memberName,
ToolTipText = $"ITable<{t.TypeName}>",
Expand All @@ -571,7 +569,7 @@ ExplorerItem GetTables(string header, ExplorerIcon icon, IEnumerable<TableSchema
.ToList()
};

foreach (var table in tables)
foreach (var table in tables.Where(t => dic.ContainsKey(t)))
{
var entry = dic[table];

Expand All @@ -583,7 +581,7 @@ ExplorerItem GetTables(string header, ExplorerIcon icon, IEnumerable<TableSchema

entry.Children.Add(
new ExplorerItem(
GetUniqueName(_explorerNames, key.MemberName),
key.MemberName,
key.AssociationType == AssociationType.OneToMany
? ExplorerItemKind.CollectionLink
: ExplorerItemKind.ReferenceLink,
Expand All @@ -597,7 +595,7 @@ ExplorerItem GetTables(string header, ExplorerIcon icon, IEnumerable<TableSchema
ToolTipText = typeName + (key.BackReference == null ? " // Back Reference" : ""),
SqlName = key.KeyName,
IsEnumerable = key.AssociationType == AssociationType.OneToMany,
HyperlinkTarget = dic[key.OtherTable],
HyperlinkTarget = dic.ContainsKey(key.OtherTable) ? dic[key.OtherTable] : null,
});
}
}
Expand Down

0 comments on commit 0785cee

Please sign in to comment.