示例#1
0
        DataConnectionTransaction?StartLoadTransaction(Query query)
        {
            if (!query.IsAnyPreambles())
            {
                return(null);
            }

            DataConnection?dc = null;

            if (DataContext is DataConnection dataConnection)
            {
                dc = dataConnection;
            }
            else if (DataContext is DataContext dataContext)
            {
                dc = dataContext.GetDataConnection();
            }

            if (dc == null || dc.TransactionAsync != null)
            {
                return(null);
            }

            return(dc.BeginTransaction(dc.DataProvider.SqlProviderFlags.DefaultMultiQueryIsolationLevel));
        }
示例#2
0
        Task <DataConnectionTransaction?> StartLoadTransactionAsync(Query query, CancellationToken cancellationToken)
        {
            if (!query.IsAnyPreambles())
            {
                return(TaskCache.CompletedTransaction);
            }

            DataConnection?dc = null;

            if (DataContext is DataConnection dataConnection)
            {
                dc = dataConnection;
            }
            else if (DataContext is DataContext dataContext)
            {
                dc = dataContext.GetDataConnection();
            }

            if (dc == null || dc.TransactionAsync != null)
            {
                return(TaskCache.CompletedTransaction);
            }

            return(dc.BeginTransactionAsync(dc.DataProvider.SqlProviderFlags.DefaultMultiQueryIsolationLevel, cancellationToken) !);
        }
示例#3
0
        // internal API
        internal static bool TryGetDataConnection <T>(this ITable <T> table, [NotNullWhen(true)] out DataConnection?dataConnection)
            where T : notnull
        {
            if (table.DataContext is DataConnection dc)
            {
                dataConnection = dc;
            }
            else if (table.DataContext is DataContext dataContext)
            {
                dataConnection = dataContext.GetDataConnection();
            }
            else
            {
                dataConnection = null;
            }

            return(dataConnection != null);
        }
示例#4
0
 protected override DataConnection CreateDataConnection(LinqToDBConnectionOptions options)
 {
     return(DataConnection = base.CreateDataConnection(options));
 }
示例#5
0
        private DataConnection?GetConnection(string provider, string?providerLocation, string connectionString, string?additionalConnectionString, out DataConnection?secondaryConnection)
        {
            secondaryConnection = null;
            var returnSecondary = false;

            // general rules:
            // - specify specific provider used by tool if linq2db supports multiple providers for database
            // - if multiple dialects (versions) of db supported - make sure version detection enabled
            // other considerations:
            // - generate error for databases with windows-only support (e.g. access, sqlce)
            // - allow user to specify provider discovery hints (e.g. provider path) for unmanaged providers
            switch (provider)
            {
            case ProviderName.SQLite:
                provider = ProviderName.SQLiteClassic;
                break;

            case ProviderName.SqlServer:
                SqlServerTools.AutoDetectProvider = true;
                SqlServerTools.Provider           = SqlServerProvider.MicrosoftDataSqlClient;
                break;

            case ProviderName.Firebird:
                // TODO: don't forget to add versioning here after Firebird versioning feature merged
                break;

            case ProviderName.MySql:
                // TODO: remove provider hint after MySQL.Data support removed
                provider = ProviderName.MySqlConnector;
                break;

            case ProviderName.Oracle:
                OracleTools.AutoDetectProvider = true;
                provider = ProviderName.OracleManaged;
                break;

            case ProviderName.PostgreSQL:
                PostgreSQLTools.AutoDetectProvider = true;
                break;

            case ProviderName.Sybase:
                provider = ProviderName.SybaseManaged;
                break;

            case ProviderName.SqlCe:
            {
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Console.Error.WriteLine($"SQL Server Compact Edition not supported on non-Windows platforms");
                    return(null);
                }

                var assemblyPath = providerLocation ?? Path.Combine(Environment.GetEnvironmentVariable(IntPtr.Size == 4 ? "ProgramFiles(x86)" : "ProgramFiles") !, @"Microsoft SQL Server Compact Edition\v4.0\Private\System.Data.SqlServerCe.dll");
                if (!File.Exists(assemblyPath))
                {
                    Console.Error.WriteLine(@$ "Cannot locate Server Compact Edition installation.
Probed location: {assemblyPath}.
Possible reasons:
1. SQL Server CE not installed => install SQL CE runtime (e.g. from here https://www.microsoft.com/en-us/download/details.aspx?id=30709)
2. SQL Server CE runtime architecture doesn't match process architecture => add '--architecture x86' or '--architecture x64' scaffold option
3. SQL Server CE runtime has custom location => specify path to System.Data.SqlServerCe.dll using '--provider-location <path_to_assembly>' option");
                    return(null);
                }

                var assembly = Assembly.LoadFrom(assemblyPath);
                DbProviderFactories.RegisterFactory("System.Data.SqlServerCe.4.0", assembly.GetType("System.Data.SqlServerCe.SqlCeProviderFactory") !);
                break;
            }

            case ProviderName.SapHana:
            {
                var isOdbc = connectionString.Contains("HDBODBC", StringComparison.OrdinalIgnoreCase);
                if (!isOdbc && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Console.Error.WriteLine($"Only ODBC provider for SAP HANA supported on non-Windows platforms. Provided connection string doesn't look like HANA ODBC connection string.");
                    return(null);
                }

                provider = isOdbc ? ProviderName.SapHanaOdbc : ProviderName.SapHanaNative;

                if (!isOdbc)
                {
                    var assemblyPath = providerLocation ?? Path.Combine(Environment.GetEnvironmentVariable(IntPtr.Size == 4 ? "ProgramFiles(x86)" : "ProgramFiles") !, @"sap\hdbclient\dotnetcore\v2.1\Sap.Data.Hana.Core.v2.1.dll");
                    if (!File.Exists(assemblyPath))
                    {
                        Console.Error.WriteLine(@$ "Cannot locate SAP HANA native client installation.
Probed location: {assemblyPath}.
Possible reasons:
1. HDB client not installed => install HDB client for .net core
2. HDB architecture doesn't match process architecture => add '--architecture x86' or '--architecture x64' scaffold option
3. HDB client installed at custom location => specify path to Sap.Data.Hana.Core.v2.1.dll using '--provider-location <path_to_assembly>' option");
                        return(null);
                    }

                    var assembly = Assembly.LoadFrom(assemblyPath);
                    DbProviderFactories.RegisterFactory("Sap.Data.Hana", assembly.GetType("Sap.Data.Hana.HanaFactory") !);
                }
                break;
            }

            case ProviderName.Informix:
            case ProviderName.DB2:
            {
                if (provider == ProviderName.Informix)
                {
                    provider = ProviderName.InformixDB2;
                }
                else
                {
                    DB2Tools.AutoDetectProvider = true;
                }

                if (providerLocation == null || !File.Exists(providerLocation))
                {
                    // we cannot add 90 Megabytes (compressed size) of native provider for single db just because we can
                    // note: we use IBM.Data.DB2.Core because Net.IBM.Data.Db2 and Net5.IBM.Data.Db2 require
                    // net6/net5 runtime and fail to load if .net core 3.1 runtime used to run tool (default runtime for tool)
                    Console.Error.WriteLine(@$ "Cannot locate IBM.Data.DB2.Core.dll provider assembly.
Due to huge size of it, we don't include IBM.Data.DB2 provider into installation.
You need to install it manually and specify provider path using '--provider-location <path_to_assembly>' option.
Provider could be downloaded from:
- for Windows: https://www.nuget.org/packages/Net.IBM.Data.DB2.Core
- for Linux: https://www.nuget.org/packages/Net.IBM.Data.DB2.Core-lnx
- for macOS: https://www.nuget.org/packages/Net.IBM.Data.DB2.Core-osx");
                    return(null);
                }

                var assembly = Assembly.LoadFrom(providerLocation);
                DbProviderFactories.RegisterFactory("IBM.Data.DB2", assembly.GetType($"{assembly.GetName().Name}.DB2Factory") !);
                break;
            }

            case ProviderName.Access:
            {
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Console.Error.WriteLine($"MS Access not supported on non-Windows platforms");
                    return(null);
                }

                var isOleDb = connectionString.Contains("Microsoft.Jet.OLEDB", StringComparison.OrdinalIgnoreCase) ||
                              connectionString.Contains("Microsoft.ACE.OLEDB", StringComparison.OrdinalIgnoreCase);

                if (!isOleDb)
                {
                    provider = ProviderName.AccessOdbc;
                }

                if (additionalConnectionString == null)
                {
                    Console.Out.WriteLine($"WARNING: it is recommended to use '--additional-connection <secondary_connection>' option with Access for better results");
                }
                else
                {
                    var isSecondaryOleDb = additionalConnectionString.Contains("Microsoft.Jet.OLEDB", StringComparison.OrdinalIgnoreCase) ||
                                           additionalConnectionString.Contains("Microsoft.ACE.OLEDB", StringComparison.OrdinalIgnoreCase);

                    if (isOleDb == isSecondaryOleDb)
                    {
                        Console.Error.WriteLine($"Main and secondary connection strings must use different providers. One should be OLE DB provider and another ODBC provider.");
                        return(null);
                    }

                    var secondaryProvider = isSecondaryOleDb ? ProviderName.Access : ProviderName.AccessOdbc;

                    var secondaryDataProvider = DataConnection.GetDataProvider(secondaryProvider, additionalConnectionString);
                    if (secondaryDataProvider == null)
                    {
                        Console.Error.WriteLine($"Cannot create database provider '{provider}' for secondary connection");
                        return(null);
                    }

                    secondaryConnection = new DataConnection(secondaryDataProvider, additionalConnectionString);
                    // to simplify things for caller (no need to detect connection type)
                    // returned connection should be OLE DB and additional - ODBC
                    returnSecondary = isSecondaryOleDb;
                }

                break;
            }

            default:
                Console.Error.WriteLine($"Unsupported database provider: {provider}");
                return(null);
            }

            var dataProvider = DataConnection.GetDataProvider(provider, connectionString);

            if (dataProvider == null)
            {
                Console.Error.WriteLine($"Cannot create database provider: {provider}");
                secondaryConnection?.Dispose();
                return(null);
            }

            var dc = new DataConnection(dataProvider, connectionString);

            if (secondaryConnection != null && returnSecondary)
            {
                var tmp = secondaryConnection;
                secondaryConnection = dc;
                return(tmp);
            }

            return(dc);
        }
示例#6
0
 internal DataReaderWrapper(DataConnection dataConnection, DbDataReader dataReader, DbCommand?command)
 {
     _dataConnection = dataConnection;
     DataReader      = dataReader;
     Command         = command;
 }