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 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 = !cxInfo.DynamicSchemaOptions.ExcludeRoutines; 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("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()); switch (providerName) { case ProviderName.Access: cxInfo.DatabaseInfo.Provider = typeof(AccessType).Namespace; break; case ProviderName.DB2: case ProviderName.DB2LUW: case ProviderName.DB2zOS: cxInfo.DatabaseInfo.Provider = typeof(DB2Type).Namespace; break; case ProviderName.Informix: cxInfo.DatabaseInfo.Provider = typeof(InformixType).Namespace; break; case ProviderName.Firebird: cxInfo.DatabaseInfo.Provider = typeof(FirebirdType).Namespace; break; case ProviderName.PostgreSQL: cxInfo.DatabaseInfo.Provider = typeof(PostgreSQLType).Namespace; break; case ProviderName.OracleNative: cxInfo.DatabaseInfo.Provider = typeof(OracleNativeType).Namespace; break; case ProviderName.OracleManaged: cxInfo.DatabaseInfo.Provider = typeof(OracleManagedType).Namespace; break; case ProviderName.MySql: cxInfo.DatabaseInfo.Provider = typeof(MySqlType).Namespace; break; case ProviderName.SqlCe: cxInfo.DatabaseInfo.Provider = typeof(SqlCeType).Namespace; break; case ProviderName.SQLite: cxInfo.DatabaseInfo.Provider = typeof(SQLiteType).Namespace; break; case ProviderName.SqlServer: cxInfo.DatabaseInfo.Provider = typeof(SqlServerType).Namespace; break; case ProviderName.Sybase: cxInfo.DatabaseInfo.Provider = typeof(SybaseType).Namespace; break; case ProviderName.SapHana: cxInfo.DatabaseInfo.Provider = typeof(SapHanaType).Namespace; break; } string providerVersion = null; try { using (var db = new LINQPadDataConnection(providerName, 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; if (providerName == ProviderName.SqlServer) { int version; if (int.TryParse(cxInfo.DatabaseInfo.DbVersion?.Split('.')[0], out 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; 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); }