Skip to content

Commit

Permalink
Merge pull request #24 from linq2db/master
Browse files Browse the repository at this point in the history
Release 2.9.1
  • Loading branch information
MaceWindu authored Oct 3, 2019
2 parents 946665f + e68a801 commit 7204381
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 181 deletions.
1 change: 1 addition & 0 deletions Source/ConnectionDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<CheckBox Content="Pluralize Table properties" FontSize="12" IsChecked="{Binding Pluralize}" />
<CheckBox Content="Capitalize property names" FontSize="12" IsChecked="{Binding Capitalize}" Margin="0,5" />
<CheckBox Content="Include Stored Procedures and Functions" FontSize="12" IsChecked="{Binding IncludeRoutines}" />
<CheckBox Content="Include Foreign Keys" FontSize="12" IsChecked="{Binding IncludeFKs}" Margin="0,5" />
</StackPanel>
</GroupBox>

Expand Down
37 changes: 37 additions & 0 deletions Source/ConnectionViewModel.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,43 @@ private void OnIncludeRoutinesChanged()

#endregion

#region IncludeFKs : bool

private bool _includeFKs;
public bool IncludeFKs
{
get { return _includeFKs; }
set
{
if (_includeFKs != value)
{
BeforeIncludeFKsChanged(value);
_includeFKs = value;
AfterIncludeFKsChanged();

OnIncludeFKsChanged();
}
}
}

#region INotifyPropertyChanged support

partial void BeforeIncludeFKsChanged(bool newValue);
partial void AfterIncludeFKsChanged ();

public const string NameOfIncludeFKs = "IncludeFKs";

private static readonly PropertyChangedEventArgs _includeFKsChangedEventArgs = new PropertyChangedEventArgs(NameOfIncludeFKs);

private void OnIncludeFKsChanged()
{
OnPropertyChanged(_includeFKsChangedEventArgs);
}

#endregion

#endregion

#region EncryptConnectionString : bool

private bool _encryptConnectionString;
Expand Down
1 change: 1 addition & 0 deletions Source/ConnectionViewModel.tt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
new NotifyingProperty("bool", "Pluralize"),
new NotifyingProperty("bool", "Capitalize"),
new NotifyingProperty("bool", "IncludeRoutines"),
new NotifyingProperty("bool", "IncludeFKs"),
new NotifyingProperty("bool", "EncryptConnectionString"),
new NotifyingProperty("bool", "UseCustomFormatter"),
new NotifyingProperty("bool", "UseProviderSpecificTypes"),
Expand Down
3 changes: 2 additions & 1 deletion Source/DriverHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static bool ShowConnectionDialog(DataContextDriver driver, IConnectionInf
model.EncryptConnectionString = cxInfo.DatabaseInfo.EncryptCustomCxString;
model.Pluralize = !cxInfo.DynamicSchemaOptions.NoPluralization;
model.Capitalize = !cxInfo.DynamicSchemaOptions.NoCapitalization;
//model.IncludeRoutines = !isNewConnection && !cxInfo.DynamicSchemaOptions.ExcludeRoutines;
model.IncludeRoutines = cxInfo.DriverData.Element("excludeRoutines")?.Value.ToLower() != "true";
model.IncludeFKs = cxInfo.DriverData.Element("excludeFKs")?.Value.ToLower() != "true";
model.ConnectionString = cxInfo.DatabaseInfo.CustomCxString.IsNullOrWhiteSpace() ? (string)cxInfo.DriverData.Element("connectionString") : cxInfo.DatabaseInfo.CustomCxString;
model.IncludeSchemas = cxInfo.DriverData.Element("includeSchemas") ?.Value;
model.ExcludeSchemas = cxInfo.DriverData.Element("excludeSchemas") ?.Value;
Expand All @@ -61,6 +61,7 @@ public static bool ShowConnectionDialog(DataContextDriver driver, IConnectionInf
cxInfo.DriverData.SetElementValue("providerName", providerName);
cxInfo.DriverData.SetElementValue("connectionString", null);
cxInfo.DriverData.SetElementValue("excludeRoutines", !model.IncludeRoutines ? "true" : "false");
cxInfo.DriverData.SetElementValue("excludeFKs", !model.IncludeFKs ? "true" : "false");
cxInfo.DriverData.SetElementValue("includeSchemas", model.IncludeSchemas. IsNullOrWhiteSpace() ? null : model.IncludeSchemas);
cxInfo.DriverData.SetElementValue("excludeSchemas", model.ExcludeSchemas. IsNullOrWhiteSpace() ? null : model.ExcludeSchemas);
cxInfo.DriverData.SetElementValue("includeCatalogs", model.IncludeCatalogs.IsNullOrWhiteSpace() ? null : model.IncludeSchemas);
Expand Down
54 changes: 39 additions & 15 deletions Source/SchemaAndCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public IEnumerable<ExplorerItem> GetItemsAndCode(string nameSpace, string typeNa
db.CommandTimeout = CommandTimeout;

_dataProvider = db.DataProvider;
_sqlBuilder = _dataProvider.CreateSqlBuilder();
_sqlBuilder = _dataProvider.CreateSqlBuilder(_dataProvider.MappingSchema);

var options = new GetSchemaOptions();

Expand All @@ -76,7 +76,8 @@ public IEnumerable<ExplorerItem> GetItemsAndCode(string nameSpace, string typeNa
var excludeCatalogs = (string)_cxInfo.DriverData.Element("excludeCatalogs");
if (excludeCatalogs != null) options.ExcludedCatalogs = excludeCatalogs.Split(',', ';');

options.GetProcedures = (string)_cxInfo.DriverData.Element("excludeRoutines") != "true";
options.GetProcedures = (string)_cxInfo.DriverData.Element("excludeRoutines") != "true";
options.GetForeignKeys = (string)_cxInfo.DriverData.Element("excludeFKs") != "true";

_schema = _dataProvider.GetSchemaProvider().GetSchema(db, options);

Expand Down Expand Up @@ -274,8 +275,8 @@ void CodeProcedure(StringBuilder code, ProcedureSchema p, string sprocSqlName)
code
.Append($" {p.MemberName}(")
.Append(p.Parameters
.Where (pr => !pr.IsResult)
.Select(pr => $"{(pr.IsOut ? pr.IsIn ? "ref " : "out " : "")}{pr.ParameterType} {pr.ParameterName}")
.Where (pr => !pr.IsResult || !p.IsFunction)
.Select(pr => $"{(pr.IsOut || pr.IsResult ? pr.IsIn ? "ref " : "out " : "")}{pr.ParameterType} {pr.ParameterName}")
.Join(", "))
.AppendLine(")")
.AppendLine(" {")
Expand All @@ -293,13 +294,14 @@ void CodeProcedure(StringBuilder code, ProcedureSchema p, string sprocSqlName)
}
else if (p.IsFunction)
{
// aggregate and scalar functions branch
code.AppendLine(" throw new InvalidOperationException();");
}
else
{
var inputParameters = p.Parameters.Where(pp => pp.IsIn) .ToList();
var outputParameters = p.Parameters.Where(pp => pp.IsOut) .ToList();
var inOrOutputParameters = p.Parameters.Where(pp => pp.IsIn || pp.IsOut).ToList();
var inputParameters = p.Parameters.Where(pp => pp.IsIn) .ToList();
var outputParameters = p.Parameters.Where(pp => pp.IsOut || pp.IsResult) .ToList();
var inOrOutputParameters = p.Parameters.Where(pp => pp.IsIn || pp.IsOut || pp.IsResult).ToList();

spName += inOrOutputParameters.Count == 0 ? ");" : ",";

Expand All @@ -309,7 +311,7 @@ void CodeProcedure(StringBuilder code, ProcedureSchema p, string sprocSqlName)
while (p.Parameters.Any(pp => pp.ParameterName == retName))
retName = "__ret__" + ++retNo;

var hasOut = outputParameters.Any(pr => pr.IsOut);
var hasOut = outputParameters.Any(pr => pr.IsOut || pr.IsResult);
var prefix = hasOut ? $"var {retName} =" : "return";

if (p.ResultTable == null)
Expand Down Expand Up @@ -360,16 +362,16 @@ void CodeProcedure(StringBuilder code, ProcedureSchema p, string sprocSqlName)
var pr = inOrOutputParameters[i];

var str = string.Format(
!pr.IsIn && pr.IsOut
!pr.IsIn && (pr.IsOut || pr.IsResult)
? " new DataParameter({0}, null, DataType.{2})"
: " new DataParameter({0}, {1}, DataType.{2})",
CSharpTools.ToStringLiteral(pr.SchemaName),
pr.ParameterName,
pr.DataType);

if (pr.IsOut)
if (pr.IsOut || pr.IsResult)
{
str += " { Direction = " + (pr.IsIn ? "ParameterDirection.InputOutput" : "ParameterDirection.Output");
str += " { Direction = " + (pr.IsIn ? "ParameterDirection.InputOutput" : (pr.IsResult ? "ParameterDirection.ReturnValue" : "ParameterDirection.Output"));

if (pr.Size != null && pr.Size.Value != 0)
str += ", Size = " + pr.Size.Value;
Expand All @@ -388,7 +390,7 @@ void CodeProcedure(StringBuilder code, ProcedureSchema p, string sprocSqlName)
{
code.AppendLine();

foreach (var pr in p.Parameters.Where(_ => _.IsOut))
foreach (var pr in p.Parameters.Where(_ => _.IsOut || _.IsResult))
{
var str = $" {pr.ParameterName} = Converter.ChangeTypeTo<{pr.ParameterType}>(((IDbDataParameter)this.Command.Parameters[\"{pr.SchemaName}\"]).Value);";
code.AppendLine(str);
Expand Down Expand Up @@ -451,14 +453,14 @@ ExplorerItem GetProcedures(string header, ExplorerIcon icon, List<ProcedureSchem
{
DragText = $"{p.MemberName}(" +
p.Parameters
.Where (pr => !pr.IsResult)
.Select(pr => $"{(pr.IsOut ? pr.IsIn ? "ref " : "out " : "")}{pr.ParameterName}")
.Where (pr => !pr.IsResult || !p.IsFunction)
.Select(pr => $"{(pr.IsOut || pr.IsResult ? (pr.IsIn ? "ref " : "out ") : "")}{pr.ParameterName}")
.Join(", ") +
")",
SqlName = sprocSqlName,
IsEnumerable = p.ResultTable != null,
Children = p.Parameters
.Where (pr => !pr.IsResult)
.Where (pr => !pr.IsResult || !p.IsFunction)
.Select(pr =>
new ExplorerItem(
$"{pr.ParameterName} ({pr.ParameterType})",
Expand Down Expand Up @@ -696,6 +698,28 @@ void ConvertSchema(string typeName)

foreach (var procedure in _schema.Procedures)
{
// migrate https://github.com/linq2db/linq2db/pull/1905
if (!procedure.IsFunction && ProviderName == LinqToDB.ProviderName.SqlServer)
{
// sql server procedures always have integer return parameter
var name = "@returnValue";
var cnt = 0;
while (procedure.Parameters.Any(_ => _.ParameterName == name))
name = $"@returnValue{cnt++}";

procedure.Parameters.Add(new ParameterSchema()
{
SchemaName = name,
ParameterName = name,
IsResult = true,
DataType = DataType.Int32,
SystemType = typeof(int),
SchemaType = "int",
ParameterType = "int",
ProviderSpecificType = "int"
});
}

procedure.MemberName = GetName(typeNames, procedure.MemberName);

if (procedure.ResultTable != null && !_contextMembers.ContainsKey(procedure.ResultTable))
Expand Down
151 changes: 0 additions & 151 deletions Source/SchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,156 +184,5 @@ string GetTypeName(Type type)

return type.Name;
}

/*public IEnumerable<ExplorerItem> GetItemsAndCode(string nameSpace, string typeName)
{
var schemas =
(
from t in
(
from t in _schema.Tables
select new { t.IsDefaultSchema, t.SchemaName, Table = t, Procedure = (ProcedureSchema)null }
)
.Union
(
from p in _schema.Procedures
select new { p.IsDefaultSchema, p.SchemaName, Table = (TableSchema)null, Procedure = p }
)
group t by new { t.IsDefaultSchema, t.SchemaName } into gr
orderby !gr.Key.IsDefaultSchema, gr.Key.SchemaName
select new
{
gr.Key,
Tables = gr.Where(t => t.Table != null).Select(t => t.Table). ToList(),
Procedures = gr.Where(t => t.Procedure != null).Select(t => t.Procedure).ToList(),
}
)
.ToList();
foreach (var s in schemas)
{
var items = new List<ExplorerItem>();
if (s.Tables.Any(t => !t.IsView && !t.IsProcedureResult))
items.Add(GetTables("Tables", ExplorerIcon.Table, s.Tables.Where(t => !t.IsView && !t.IsProcedureResult)));
if (s.Tables.Any(t => t.IsView))
items.Add(GetTables("Views", ExplorerIcon.View, s.Tables.Where(t => t.IsView)));
if (!_cxInfo.DynamicSchemaOptions.ExcludeRoutines && s.Procedures.Any(p => p.IsLoaded && !p.IsFunction))
items.Add(GetProcedures(
"Stored Procs",
ExplorerIcon.StoredProc,
s.Procedures.Where(p => p.IsLoaded && !p.IsFunction).ToList()));
if (s.Procedures.Any(p => p.IsLoaded && p.IsTableFunction))
items.Add(GetProcedures(
"Table Functions",
ExplorerIcon.TableFunction,
s.Procedures.Where(p => p.IsLoaded && p.IsTableFunction).ToList()));
if (s.Procedures.Any(p => p.IsFunction && !p.IsTableFunction))
items.Add(GetProcedures(
"Scalar Functions",
ExplorerIcon.ScalarFunction,
s.Procedures.Where(p => p.IsFunction && !p.IsTableFunction).ToList()));
if (schemas.Count == 1)
{
foreach (var item in items)
yield return item;
}
else
{
yield return new ExplorerItem(
s.Key.SchemaName.IsNullOrEmpty() ? s.Key.IsDefaultSchema ? "(default)" : "empty" : s.Key.SchemaName,
ExplorerItemKind.Schema,
ExplorerIcon.Schema)
{
Children = items
};
}
}
}
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(
column.MemberName,
ExplorerItemKind.Property,
column.IsPrimaryKey ? ExplorerIcon.Key : ExplorerIcon.Column)
{
Text = $"{column.MemberName} : {memberType}",
ToolTipText = $"{sqlName} {column.ColumnType} {(column.IsNullable ? "NULL" : "NOT NULL")}{(column.IsIdentity ? " IDENTITY" : "")}",
DragText = column.MemberName,
SqlName = sqlName,
SqlTypeDeclaration = $"{column.ColumnType} {(column.IsNullable ? "NULL" : "NOT NULL")}{(column.IsIdentity ? " IDENTITY" : "")}",
};
}
ExplorerItem GetProcedures(string header, ExplorerIcon icon, List<ProcedureSchema> procedures)
{
var results = new HashSet<TableSchema>();
var items = new ExplorerItem(header, ExplorerItemKind.Category, icon)
{
Children = procedures
.Select(p =>
{
var sprocSqlName = _sqlBuilder.BuildTableName(
new StringBuilder(),
null,
p.SchemaName == null ? null : (string)_sqlBuilder.Convert(p.SchemaName, ConvertType.NameToOwner),
(string)_sqlBuilder.Convert(p.ProcedureName, ConvertType.NameToQueryTable)).ToString();
var memberName = p.MemberName;
if (p.IsFunction && !p.IsTableFunction)
{
var res = p.Parameters.FirstOrDefault(pr => pr.IsResult);
if (res != null)
memberName += $" -> {res.ParameterType}";
}
var ret = new ExplorerItem(memberName, ExplorerItemKind.QueryableObject, icon)
{
DragText = $"{p.MemberName}(" +
p.Parameters
.Where (pr => !pr.IsResult)
.Select(pr => $"{(pr.IsOut ? pr.IsIn ? "ref " : "out " : "")}{pr.ParameterName}")
.Join(", ") +
")",
SqlName = sprocSqlName,
IsEnumerable = p.ResultTable != null,
Children = p.Parameters
.Where (pr => !pr.IsResult)
.Select(pr =>
new ExplorerItem(
$"{pr.ParameterName} ({pr.ParameterType})",
ExplorerItemKind.Parameter,
ExplorerIcon.Parameter))
.Union(p.ResultTable?.Columns.Select(GetColumnItem) ?? new ExplorerItem[0])
.ToList(),
};
if (p.ResultTable != null && !results.Contains(p.ResultTable))
{
results.Add(p.ResultTable);
}
return ret;
})
.OrderBy(p => p.Text)
.ToList(),
};
return items;
}
*/
}
}
20 changes: 10 additions & 10 deletions Source/linq2db.LINQPad.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CodeJam" Version="2.1.1" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="3.5.0" />
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="6.6.0" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.1" />
<PackageReference Include="linq2db" Version="2.7.0" />
<PackageReference Include="linq2db4iSeries" Version="2.7.0" />
<PackageReference Include="CodeJam" Version="2.2.1" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="3.6.0" />
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="7.1.1" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="linq2db" Version="2.9.1" />
<PackageReference Include="linq2db4iSeries" Version="2.9.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="1.3.2" />
<PackageReference Include="Microsoft.SqlServer.Types" Version="14.0.1016.290" />
<PackageReference Include="MySql.Data" Version="8.0.16" />
<PackageReference Include="Npgsql" Version="4.0.6" />
<PackageReference Include="Oracle.ManagedDataAccess" Version="18.6.0" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.110" />
<PackageReference Include="MySql.Data" Version="8.0.17" />
<PackageReference Include="Npgsql" Version="4.0.10" />
<PackageReference Include="Oracle.ManagedDataAccess" Version="19.3.1" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.111" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 7204381

Please sign in to comment.