Skip to content

Commit

Permalink
Merge pull request #12 from linq2db/iseries_support
Browse files Browse the repository at this point in the history
Added DB2 iSeries provider support.
  • Loading branch information
sdanyliv authored Sep 27, 2018
2 parents b47d468 + 9a568a9 commit 5b9cba2
Show file tree
Hide file tree
Showing 2,241 changed files with 131 additions and 2,953,096 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ obj/
_ReSharper*/
[Tt]est[Rr]esult*
*.csproj.DotSettings
!packages/**
packages/*/
Packages/*/
.vs/**
Binary file added Redist/IBM/IBM.Data.DB2.iSeries.dll
Binary file not shown.
Binary file not shown.
26 changes: 2 additions & 24 deletions Source/DriverHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ public static bool ShowConnectionDialog(DataContextDriver driver, IConnectionInf
var providerInfo = ProviderHelper.GetProvider(providerName);
cxInfo.DatabaseInfo.Provider = providerInfo.GetConnectionNamespace();

string providerVersion = null;

try
{
var provider = ProviderHelper.GetDataProvider(model.SelectedProvider.Name, model.ConnectionString);
var provider = ProviderHelper.GetProvider(model.SelectedProvider.Name).GetDataProvider(model.ConnectionString);

using (var db = new DataConnection(provider, model.ConnectionString))
{
Expand All @@ -86,32 +84,12 @@ public static bool ShowConnectionDialog(DataContextDriver driver, IConnectionInf
cxInfo.DatabaseInfo.Server = ((DbConnection)db.Connection).DataSource;
cxInfo.DatabaseInfo.Database = db.Connection.Database;
cxInfo.DatabaseInfo.DbVersion = ((DbConnection)db.Connection).ServerVersion;

if (providerName == ProviderName.SqlServer)
{
if (int.TryParse(cxInfo.DatabaseInfo.DbVersion?.Split('.')[0], out var version))
{
switch (version)
{
case 8 : providerVersion = ProviderName.SqlServer2000; break;
case 9 : providerVersion = ProviderName.SqlServer2005; break;
case 10 : providerVersion = ProviderName.SqlServer2008; break;
case 11 : providerVersion = ProviderName.SqlServer2012; break;
case 12 : providerVersion = ProviderName.SqlServer2014; break;
default :
if (version > 12)
providerVersion = ProviderName.SqlServer2014;
break;
}
}
}
}
}
catch
{
}

cxInfo.DriverData.SetElementValue("providerVersion", providerVersion);
cxInfo.DriverData.SetElementValue("customConfiguration", model.CustomConfiguration.IsNullOrWhiteSpace() ? null : model.CustomConfiguration);

cxInfo.CustomTypeInfo.CustomAssemblyPath = model.CustomAssemblyPath;
Expand Down Expand Up @@ -141,7 +119,7 @@ static Exception TestConnection(ConnectionViewModel model)
{
if (model.SelectedProvider != null)
{
var provider = ProviderHelper.GetDataProvider(model.SelectedProvider.Name, model.ConnectionString);
var provider = ProviderHelper.GetProvider(model.SelectedProvider.Name).GetDataProvider(model.ConnectionString);

using (var con = provider.CreateConnection(model.ConnectionString))
{
Expand Down
4 changes: 2 additions & 2 deletions Source/LINQPadDataConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public LINQPadDataConnection()
}

public LINQPadDataConnection(string providerName, string connectionString)
: base(ProviderHelper.GetDataProvider(providerName, connectionString), connectionString)
: base(ProviderHelper.GetProvider(providerName).GetDataProvider(connectionString), connectionString)
{
Init();
}

public LINQPadDataConnection(IConnectionInfo cxInfo)
: this(
(string)(cxInfo.DriverData.Element("providerVersion") ?? cxInfo.DriverData.Element("providerName")),
(string)cxInfo.DriverData.Element("providerName"),
cxInfo.DatabaseInfo.CustomCxString)
{
}
Expand Down
2 changes: 1 addition & 1 deletion Source/LinqToDBDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public override object[] GetContextConstructorArguments(IConnectionInfo cxInfo)
{
return new object[]
{
(string)(cxInfo.DriverData.Element("providerVersion") ?? cxInfo.DriverData.Element("providerName")),
(string)cxInfo.DriverData.Element("providerName"),
cxInfo.DatabaseInfo.CustomCxString,
};
}
Expand Down
34 changes: 28 additions & 6 deletions Source/ProviderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using LinqToDB.Common;
using LinqToDB.Configuration;
Expand Down Expand Up @@ -31,6 +32,11 @@ static ProviderHelper()
InitializeDataProviders();
}

public static class DB2iSeriesProviderName
{
public const string DB2 = "DB2.iSeries";
}

static void InitializeDataProviders()
{
AddDataProvider(new DynamicProviderRecord(ProviderName.Access, "Microsoft Access", "System.Data.OleDb.OleDbConnection"));
Expand All @@ -46,7 +52,6 @@ static void InitializeDataProviders()
AddDataProvider(new DynamicProviderRecord(ProviderName.Sybase, "SAP Sybase ASE", "IBM.Data.Informix.IfxConnection"));
AddDataProvider(new DynamicProviderRecord(ProviderName.SapHana, "SAP HANA", "Sap.Data.Hana.HanaConnection"));


AddDataProvider(new DynamicProviderRecord(ProviderName.PostgreSQL, "PostgreSQL", "Npgsql.NpgsqlConnection"));

AddDataProvider(
Expand All @@ -58,6 +63,12 @@ static void InitializeDataProviders()
AdditionalNamespaces = new[] { "Microsoft.SqlServer.Types" },
ProviderLibraries = "Microsoft.SqlServer.Types.dll"
});

AddDataProvider(new DynamicProviderRecord(DB2iSeriesProviderName.DB2, "DB2 iSeries", "IBM.Data.DB2.iSeries.iDB2Connection")
{
InitalizationClassName = "LinqToDB.DataProvider.DB2iSeries.DB2iSeriesTools, LinqToDB.DataProvider.DB2iSeries",
ProviderLibraries = "LinqToDB.DataProvider.DB2iSeries.dll;IBM.Data.DB2.iSeries.dll"
});
}

public class ProviderRecord
Expand Down Expand Up @@ -85,7 +96,7 @@ public class DynamicProviderRecord
{
public string ProviderName { get; set; }
public string Description { get; set; }
public string ProviderFactoryName { get; set; }
public string InitalizationClassName { get; set; }

public NamedValue[] ProviderNamedValues { get; set; }
public string ProviderLibraries { get; set; }
Expand Down Expand Up @@ -148,10 +159,16 @@ public void Load(string connectionString)
.ToArray();


var provider = GetDataProvider(Provider.ProviderName, connectionString);
var typeName = Provider.InitalizationClassName;
if (!string.IsNullOrEmpty(typeName))
{
var initType = Type.GetType(typeName, true);
RuntimeHelpers.RunClassConstructor(initType.TypeHandle);
}

var provider = ProviderHelper.GetDataProvider(Provider.ProviderName, connectionString);

var connectionAssemblies = new List<Assembly>();
connectionAssemblies.Add(provider.GetType().Assembly);
var connectionAssemblies = new List<Assembly> { provider.GetType().Assembly };
try
{
using (var connection = provider.CreateConnection(connectionString))
Expand Down Expand Up @@ -203,7 +220,12 @@ public string GetConnectionNamespace()
}

return null;
}

public IDataProvider GetDataProvider(string connectionString)
{
Load(connectionString);
return ProviderHelper.GetDataProvider(Provider.ProviderName, connectionString);
}

}
Expand All @@ -222,7 +244,7 @@ public static LoadProviderInfo GetProvider(string providerName)
return info;
}

public static IDataProvider GetDataProvider(string providerName, string connectionString)
static IDataProvider GetDataProvider(string providerName, string connectionString)
{
var provider = DataConnection.GetDataProvider(providerName, connectionString);
if (provider == null)
Expand Down
10 changes: 4 additions & 6 deletions Source/SchemaAndCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ public SchemaAndCodeGenerator(IConnectionInfo cxInfo)
NormalizeJoins = ((string)_cxInfo.DriverData.Element("normalizeNames")) ?.ToLower() == "true";
AllowMultipleQuery = ((string)_cxInfo.DriverData.Element("allowMultipleQuery")) ?.ToLower() == "true";
ProviderName = (string)_cxInfo.DriverData.Element("providerName");
ProviderVersion = (string)_cxInfo.DriverData.Element("providerVersion");
CommandTimeout = _cxInfo.DriverData.ElementValueOrDefault("commandTimeout", str => str.ToInt32() ?? 0, 0);
}

public readonly int CommandTimeout;
public readonly bool UseProviderSpecificTypes;
public readonly string ProviderName;
public readonly string ProviderVersion;
public readonly bool NormalizeJoins;
public readonly bool AllowMultipleQuery;
public readonly List<string> References = new List<string>();
Expand All @@ -51,7 +49,7 @@ public IEnumerable<ExplorerItem> GetItemsAndCode(string nameSpace, string typeNa
{
var connectionString = _cxInfo.DatabaseInfo.CustomCxString;

var provider = ProviderHelper.GetDataProvider(ProviderName, connectionString);
var provider = ProviderHelper.GetProvider(ProviderName).GetDataProvider(connectionString);

using (var db = new DataConnection(provider, connectionString))
{
Expand Down Expand Up @@ -113,7 +111,7 @@ public IEnumerable<ExplorerItem> GetItemsAndCode(string nameSpace, string typeNa
.AppendLine($" CommandTimeout = {CommandTimeout};")
.AppendLine( " }")
.AppendLine($" public @{typeName}()")
.AppendLine($" : base({ToCodeString(ProviderVersion ?? ProviderName)}, {ToCodeString(connectionString)})")
.AppendLine($" : base({ToCodeString(ProviderName)}, {ToCodeString(connectionString)})")
.AppendLine( " {")
.AppendLine($" CommandTimeout = {CommandTimeout};")
.AppendLine( " }")
Expand Down Expand Up @@ -383,7 +381,7 @@ ExplorerItem GetProcedures(string header, ExplorerIcon icon, List<ProcedureSchem
var sprocSqlName = _sqlBuilder.BuildTableName(
new StringBuilder(),
null,
p.SchemaName == null ? null : (string)_sqlBuilder.Convert(p.SchemaName, ConvertType.NameToOwner),
p.SchemaName == null ? null : (string)_sqlBuilder.Convert(p.SchemaName, ConvertType.NameToSchema),
(string)_sqlBuilder.Convert(p.ProcedureName, ConvertType.NameToQueryTable)).ToString();

var memberName = p.MemberName;
Expand Down Expand Up @@ -522,7 +520,7 @@ ExplorerItem GetTables(string header, ExplorerIcon icon, IEnumerable<TableSchema
var tableSqlName = _sqlBuilder.BuildTableName(
new StringBuilder(),
null,
t.SchemaName == null ? null : (string)_sqlBuilder.Convert(t.SchemaName, ConvertType.NameToOwner),
t.SchemaName == null ? null : (string)_sqlBuilder.Convert(t.SchemaName, ConvertType.NameToSchema),
(string)_sqlBuilder.Convert(t.TableName, ConvertType.NameToQueryTable)).ToString();

//Debug.WriteLine($"Table: [{t.SchemaName}].[{t.TableName}] - ${tableSqlName}");
Expand Down
16 changes: 12 additions & 4 deletions Source/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral" />
<bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.122.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Google.Protobuf" publicKeyToken="a7d26565bac4d604" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.6.0.0" newVersion="3.6.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.data>
<DbProviderFactories>
<remove invariant="FirebirdSql.Data.FirebirdClient" />
<add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />


<remove invariant="Oracle.ManagedDataAccess.Client" />
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
<remove invariant="FirebirdSql.Data.FirebirdClient" /><add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" /></DbProviderFactories>
</system.data>
<oracle.manageddataaccess.client>
<version number="*">
Expand All @@ -31,4 +39,4 @@
</dataSources>
</version>
</oracle.manageddataaccess.client>
</configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /></startup></configuration>
Loading

0 comments on commit 5b9cba2

Please sign in to comment.