public IDataProvider GetDataProvider(string connectionString) { Load(connectionString); return(ProviderHelper.GetDataProvider(Provider.ProviderName, connectionString)); }
public void Load(string connectionString) { if (IsLoaded) { return; } if (LoadException != null) { return; } try { var loadLibraries = (Provider.ProviderLibraries?.Split(';') ?? Array <string> .Empty) .Where(l => !string.IsNullOrEmpty(l)) .Select(l => { try { return(Assembly.Load(Path.GetFileNameWithoutExtension(l))); } catch (Exception e) { Debug.WriteLine(e.Message); return(null); } } ) .Where(l => l != null); var providerLibraries = loadLibraries .Concat(new[] { typeof(DataConnection).Assembly }) .ToArray(); 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> { provider.GetType().Assembly }; try { using (var connection = provider.CreateConnection(connectionString)) { connectionAssemblies.Add(connection.GetType().Assembly); } } catch { } LoadedAssemblies = connectionAssemblies .Concat(providerLibraries) .Distinct().ToArray(); IsLoaded = true; } catch (Exception e) { LoadException = e; IsLoaded = false; } }
public LINQPadDataConnection(string providerName, string?providerPath, string connectionString) : base(ProviderHelper.GetProvider(providerName, providerPath).GetDataProvider(connectionString), connectionString) { Init(); InitMappingSchema(); }
public static bool ShowConnectionDialog(IConnectionInfo cxInfo, bool isNewConnection, bool isDynamic) { var model = new ConnectionViewModel(); var providerName = isNewConnection ? ProviderName.SqlServer : (string?)cxInfo.DriverData.Element(CX.ProviderName); if (providerName != null) { model.SelectedProvider = model.Providers.FirstOrDefault(p => p.Name == providerName); } model.Name = cxInfo.DisplayName; model.IsDynamic = isDynamic; model.CustomAssemblyPath = cxInfo.CustomTypeInfo.CustomAssemblyPath; model.CustomTypeName = cxInfo.CustomTypeInfo.CustomTypeName; model.AppConfigPath = cxInfo.AppConfigPath; model.CustomConfiguration = cxInfo.DriverData.Element(CX.CustomConfiguration)?.Value; model.Persist = cxInfo.Persist; model.IsProduction = cxInfo.IsProduction; model.EncryptConnectionString = cxInfo.DatabaseInfo.EncryptCustomCxString; model.Pluralize = !cxInfo.DynamicSchemaOptions.NoPluralization; model.Capitalize = !cxInfo.DynamicSchemaOptions.NoCapitalization; model.IncludeRoutines = cxInfo.DriverData.Element(CX.ExcludeRoutines)?.Value.ToLower() == "false"; model.IncludeFKs = cxInfo.DriverData.Element(CX.ExcludeFKs)?.Value.ToLower() != "true"; model.ConnectionString = cxInfo.DatabaseInfo.CustomCxString.IsNullOrWhiteSpace() ? (string?)cxInfo.DriverData.Element(CX.ConnectionString) : cxInfo.DatabaseInfo.CustomCxString; model.IncludeSchemas = cxInfo.DriverData.Element(CX.IncludeSchemas)?.Value; model.ExcludeSchemas = cxInfo.DriverData.Element(CX.ExcludeSchemas)?.Value; model.IncludeCatalogs = cxInfo.DriverData.Element(CX.IncludeCatalogs)?.Value; model.ExcludeCatalogs = cxInfo.DriverData.Element(CX.ExcludeCatalogs)?.Value; //model.NormalizeNames = cxInfo.DriverData.Element(CX.NormalizeNames) ?.Value.ToLower() == "true"; model.UseProviderSpecificTypes = cxInfo.DriverData.Element(CX.UseProviderSpecificTypes)?.Value.ToLower() == "true"; model.UseCustomFormatter = cxInfo.DriverData.Element(CX.UseCustomFormatter)?.Value.ToLower() == "true"; model.CommandTimeout = cxInfo.DriverData.ElementValueOrDefault(CX.CommandTimeout, str => str.ToInt32() ?? 0, 0); model.OptimizeJoins = cxInfo.DriverData.Element(CX.OptimizeJoins) == null || cxInfo.DriverData.Element(CX.OptimizeJoins)?.Value.ToLower() == "true"; model.ProviderPath = (string?)cxInfo.DriverData.Element(CX.ProviderPath); if (ConnectionDialog.Show(model, isDynamic ? TestConnection : null)) { providerName = model.SelectedProvider?.Name; cxInfo.DriverData.SetElementValue(CX.ProviderName, providerName); cxInfo.DriverData.SetElementValue(CX.ProviderPath, model.ProviderPath); cxInfo.DriverData.SetElementValue(CX.ConnectionString, null); cxInfo.DriverData.SetElementValue(CX.ExcludeRoutines, !model.IncludeRoutines ? "true" : "false"); cxInfo.DriverData.SetElementValue(CX.ExcludeFKs, !model.IncludeFKs ? "true" : "false"); cxInfo.DriverData.SetElementValue(CX.IncludeSchemas, model.IncludeSchemas.IsNullOrWhiteSpace() ? null : model.IncludeSchemas); cxInfo.DriverData.SetElementValue(CX.ExcludeSchemas, model.ExcludeSchemas.IsNullOrWhiteSpace() ? null : model.ExcludeSchemas); cxInfo.DriverData.SetElementValue(CX.IncludeCatalogs, model.IncludeCatalogs.IsNullOrWhiteSpace() ? null : model.IncludeSchemas); cxInfo.DriverData.SetElementValue(CX.ExcludeCatalogs, model.ExcludeCatalogs.IsNullOrWhiteSpace() ? null : model.ExcludeSchemas); cxInfo.DriverData.SetElementValue(CX.OptimizeJoins, model.OptimizeJoins ? "true" : "false"); //cxInfo.DriverData.SetElementValue(CX.NormalizeNames, model.NormalizeNames ? "true" : null); cxInfo.DriverData.SetElementValue(CX.UseProviderSpecificTypes, model.UseProviderSpecificTypes ? "true" : null); cxInfo.DriverData.SetElementValue(CX.UseCustomFormatter, model.UseCustomFormatter ? "true" : null); cxInfo.DriverData.SetElementValue(CX.CommandTimeout, model.CommandTimeout.ToString()); try { if (model.ConnectionString != null) { var providerInfo = ProviderHelper.GetProvider(providerName, model.ProviderPath); cxInfo.DatabaseInfo.Provider = providerInfo.GetConnectionNamespace(); var provider = providerInfo.GetDataProvider(model.ConnectionString); using var db = new DataConnection(provider, model.ConnectionString) { CommandTimeout = model.CommandTimeout }; cxInfo.DatabaseInfo.Provider = db.Connection.GetType().Namespace; cxInfo.DatabaseInfo.Server = ((DbConnection)db.Connection).DataSource; cxInfo.DatabaseInfo.Database = db.Connection.Database; cxInfo.DatabaseInfo.DbVersion = ((DbConnection)db.Connection).ServerVersion; } } catch { } cxInfo.DriverData.SetElementValue(CX.CustomConfiguration, model.CustomConfiguration.IsNullOrWhiteSpace() ? null : model.CustomConfiguration); cxInfo.CustomTypeInfo.CustomAssemblyPath = model.CustomAssemblyPath; cxInfo.CustomTypeInfo.CustomTypeName = model.CustomTypeName; cxInfo.AppConfigPath = model.AppConfigPath; cxInfo.DatabaseInfo.CustomCxString = model.ConnectionString; cxInfo.DatabaseInfo.EncryptCustomCxString = model.EncryptConnectionString; cxInfo.DynamicSchemaOptions.NoPluralization = !model.Pluralize; cxInfo.DynamicSchemaOptions.NoCapitalization = !model.Capitalize; cxInfo.DynamicSchemaOptions.ExcludeRoutines = !model.IncludeRoutines; cxInfo.Persist = model.Persist; cxInfo.IsProduction = model.IsProduction; cxInfo.DisplayName = model.Name.IsNullOrWhiteSpace() ? null : model.Name; return(true); } return(false); }
public static bool ShowConnectionDialog(DataContextDriver driver, IConnectionInfo cxInfo, bool isNewConnection, bool isDynamic) { var model = new ConnectionViewModel(); var providerName = isNewConnection ? ProviderName.SqlServer : (string)cxInfo.DriverData.Element("providerName"); if (providerName != null) { model.SelectedProvider = model.Providers.FirstOrDefault(p => p.Name == providerName); } model.Name = cxInfo.DisplayName; model.IsDynamic = isDynamic; model.CustomAssemblyPath = cxInfo.CustomTypeInfo.CustomAssemblyPath; model.CustomTypeName = cxInfo.CustomTypeInfo.CustomTypeName; model.AppConfigPath = cxInfo.AppConfigPath; model.CustomConfiguration = cxInfo.DriverData.Element("customConfiguration")?.Value; model.Persist = cxInfo.Persist; model.IsProduction = cxInfo.IsProduction; 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.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; model.IncludeCatalogs = cxInfo.DriverData.Element("includeCatalogs")?.Value; model.ExcludeCatalogs = cxInfo.DriverData.Element("excludeCatalogs")?.Value; //model.NormalizeNames = cxInfo.DriverData.Element("normalizeNames") ?.Value.ToLower() == "true"; model.AllowMultipleQuery = cxInfo.DriverData.Element("allowMultipleQuery")?.Value.ToLower() == "true"; model.UseProviderSpecificTypes = cxInfo.DriverData.Element("useProviderSpecificTypes")?.Value.ToLower() == "true"; model.UseCustomFormatter = cxInfo.DriverData.Element("useCustomFormatter")?.Value.ToLower() == "true"; model.CommandTimeout = cxInfo.DriverData.ElementValueOrDefault("commandTimeout", str => str.ToInt32() ?? 0, 0); model.OptimizeJoins = cxInfo.DriverData.Element("optimizeJoins") == null || cxInfo.DriverData.Element("optimizeJoins")?.Value.ToLower() == "true"; if (ConnectionDialog.Show(model, isDynamic ? (Func <ConnectionViewModel, Exception>)TestConnection : null)) { providerName = model.SelectedProvider?.Name; cxInfo.DriverData.SetElementValue("providerName", providerName); cxInfo.DriverData.SetElementValue("connectionString", null); cxInfo.DriverData.SetElementValue("excludeRoutines", !model.IncludeRoutines ? "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); cxInfo.DriverData.SetElementValue("excludeCatalogs", model.ExcludeCatalogs.IsNullOrWhiteSpace() ? null : model.ExcludeSchemas); cxInfo.DriverData.SetElementValue("optimizeJoins", model.OptimizeJoins ? "true" : "false"); cxInfo.DriverData.SetElementValue("allowMultipleQuery", model.AllowMultipleQuery ? "true" : "false"); //cxInfo.DriverData.SetElementValue("normalizeNames", model.NormalizeNames ? "true" : null); cxInfo.DriverData.SetElementValue("useProviderSpecificTypes", model.UseProviderSpecificTypes ? "true" : null); cxInfo.DriverData.SetElementValue("useCustomFormatter", model.UseCustomFormatter ? "true" : null); cxInfo.DriverData.SetElementValue("commandTimeout", model.CommandTimeout.ToString()); var providerInfo = ProviderHelper.GetProvider(providerName); cxInfo.DatabaseInfo.Provider = providerInfo.GetConnectionNamespace(); try { var provider = ProviderHelper.GetProvider(model.SelectedProvider.Name).GetDataProvider(model.ConnectionString); using (var db = new DataConnection(provider, model.ConnectionString)) { db.CommandTimeout = model.CommandTimeout; cxInfo.DatabaseInfo.Provider = db.Connection.GetType().Namespace; cxInfo.DatabaseInfo.Server = ((DbConnection)db.Connection).DataSource; cxInfo.DatabaseInfo.Database = db.Connection.Database; cxInfo.DatabaseInfo.DbVersion = ((DbConnection)db.Connection).ServerVersion; } } catch { } cxInfo.DriverData.SetElementValue("customConfiguration", model.CustomConfiguration.IsNullOrWhiteSpace() ? null : model.CustomConfiguration); cxInfo.CustomTypeInfo.CustomAssemblyPath = model.CustomAssemblyPath; cxInfo.CustomTypeInfo.CustomTypeName = model.CustomTypeName; cxInfo.AppConfigPath = model.AppConfigPath; cxInfo.DatabaseInfo.CustomCxString = model.ConnectionString; cxInfo.DatabaseInfo.EncryptCustomCxString = model.EncryptConnectionString; cxInfo.DynamicSchemaOptions.NoPluralization = !model.Pluralize; cxInfo.DynamicSchemaOptions.NoCapitalization = !model.Capitalize; cxInfo.DynamicSchemaOptions.ExcludeRoutines = !model.IncludeRoutines; cxInfo.Persist = model.Persist; cxInfo.IsProduction = model.IsProduction; cxInfo.DisplayName = model.Name.IsNullOrWhiteSpace() ? null : model.Name; return(true); } return(false); }
public IEnumerable <ExplorerItem> GetItemsAndCode(string nameSpace, string typeName) { typeName = ConvertToCompilable(typeName, false); var connectionString = _cxInfo.DatabaseInfo.CustomCxString; var provider = ProviderHelper.GetProvider(ProviderName).GetDataProvider(connectionString); using (var db = new DataConnection(provider, connectionString)) { db.CommandTimeout = CommandTimeout; _dataProvider = db.DataProvider; _sqlBuilder = _dataProvider.CreateSqlBuilder(); var options = new GetSchemaOptions(); var includeSchemas = (string)_cxInfo.DriverData.Element("includeSchemas"); if (includeSchemas != null) { options.IncludedSchemas = includeSchemas.Split(',', ';'); } var excludeSchemas = (string)_cxInfo.DriverData.Element("excludeSchemas"); if (excludeSchemas != null) { options.ExcludedSchemas = excludeSchemas.Split(',', ';'); } var includeCatalogs = (string)_cxInfo.DriverData.Element("includeCatalogs"); if (includeCatalogs != null) { options.IncludedCatalogs = includeCatalogs.Split(',', ';'); } var excludeCatalogs = (string)_cxInfo.DriverData.Element("excludeCatalogs"); if (excludeCatalogs != null) { options.ExcludedCatalogs = excludeCatalogs.Split(',', ';'); } options.GetProcedures = (string)_cxInfo.DriverData.Element("excludeRoutines") != "true"; _schema = _dataProvider.GetSchemaProvider().GetSchema(db, options); ConvertSchema(typeName); } Code .AppendLine("using System;") .AppendLine("using System.Collections;") .AppendLine("using System.Collections.Generic;") .AppendLine("using System.Data;") .AppendLine("using System.Reflection;") .AppendLine("using System.Linq;") .AppendLine("using LinqToDB;") .AppendLine("using LinqToDB.Common;") .AppendLine("using LinqToDB.Data;") .AppendLine("using LinqToDB.Mapping;") .AppendLine("using System.Net.NetworkInformation;") ; if (_schema.Procedures.Any(_ => _.IsAggregateFunction)) { Code .AppendLine("using System.Linq.Expressions;") ; } if (_schema.ProviderSpecificTypeNamespace.NotNullNorWhiteSpace()) { Code.AppendLine($"using {_schema.ProviderSpecificTypeNamespace};"); } var providerInfo = ProviderHelper.GetProvider(ProviderName); References.AddRange(providerInfo.GetAssemblyLocation(connectionString)); if (providerInfo.Provider.AdditionalNamespaces != null) { foreach (var ns in providerInfo.Provider.AdditionalNamespaces) { Code.AppendLine($"using {ns};"); } } Code .AppendLine($"namespace {nameSpace}") .AppendLine("{") .AppendLine($" public class {typeName} : LinqToDB.LINQPad.LINQPadDataConnection") .AppendLine(" {") .AppendLine($" public {typeName}(string provider, string connectionString)") .AppendLine(" : base(provider, connectionString)") .AppendLine(" {") .AppendLine($" CommandTimeout = {CommandTimeout};") .AppendLine(" }") .AppendLine($" public {typeName}()") .AppendLine($" : base({CSharpTools.ToStringLiteral(ProviderName)}, {CSharpTools.ToStringLiteral(connectionString)})") .AppendLine(" {") .AppendLine($" CommandTimeout = {CommandTimeout};") .AppendLine(" }") ; if (ProviderName == LinqToDB.ProviderName.PostgreSQL) { PreprocessPostgreSQLSchema(); } 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 }); } } Code .AppendLine(" }") .AppendLine(_classCode.ToString()) .AppendLine("}") ; #if DEBUG Debug.WriteLine(Code.ToString()); #endif }