using FastReport.Data.ConnectionEditors;
using FastReport.Forms;
using FastReport.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Reflection;
using System.Windows.Forms;
namespace FastReport.Data
{
partial class DataConnectionBase : IHasEditor
{
internal DataSourceCloudInfo CloudInfo = new DataSourceCloudInfo();
[Browsable(false)]
[DefaultValue("")]
public string CloudId { get; internal set; }
#region Public Methods
///
public override void Delete()
{
Dispose();
}
///
/// Gets a string that will identify a connection in the Data Wizard.
///
/// The string that contains the connection type and some meaningful information.
public virtual string GetConnectionId()
{
return "";
}
///
/// Gets the default type for a new parameter.
///
/// The integer representation of a parameter type.
public virtual int GetDefaultParameterType()
{
return 0;
}
///
/// Gets a control that will be used to edit the connection properties.
///
/// The editor's control.
public virtual ConnectionEditorBase GetEditor()
{
return null;
}
///
public bool InvokeEditor()
{
using (DataWizardForm form = new DataWizardForm(Report))
{
form.Connection = this;
form.EditMode = true;
return form.ShowDialog() == DialogResult.OK;
}
}
///
/// Tests the connection.
///
///
/// If test connection is not successful, this method throws an exception. Catch this exception to
/// show an error message.
///
public virtual void TestConnection()
{
DbConnection conn = GetConnection();
if (conn != null)
{
try
{
OpenConnection(conn);
}
finally
{
DisposeConnection(conn);
}
}
}
partial void SetConnectionStringBrowsableAttribute()
{
// Get properties of the descriptor.
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(typeof(DataConnectionBase))["ConnectionString"];
// Get "Browsable" attribute of the descriptor.
BrowsableAttribute browsableAttribute = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo browsable;
browsable = browsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
if (browsable == null)
{
browsable = browsableAttribute.GetType().GetField("k__BackingField", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
}
if (browsable != null)
// Set "Browsable" attribute of the descriptor.
browsable.SetValue(browsableAttribute, Config.ConnectionStringVisible);
}
#endregion Public Methods
#region Internal Methods
internal string GetQuotationChars()
{
DbConnection conn = GetConnection();
try
{
OpenConnection(conn);
return QuoteIdentifier("", conn);
}
finally
{
DisposeConnection(conn);
}
}
#endregion Internal Methods
#region Private Methods
private void FilterTables(List tableNames)
{
// filter tables
for (int i = 0; i < tableNames.Count; i++)
{
Design.FilterConnectionTablesEventArgs e = new Design.FilterConnectionTablesEventArgs(this, tableNames[i]);
Config.DesignerSettings.OnFilterConnectionTables(this, e);
if (e.Skip)
{
tableNames.RemoveAt(i);
i--;
}
}
}
private DbConnection GetDefaultConnection()
{
// if the ApplicationConnection is set, use it
if (Config.DesignerSettings.ApplicationConnectionType == this.GetType())
return Config.DesignerSettings.ApplicationConnection;
return null;
}
private bool ShouldNotDispose(DbConnection connection)
{
// if this is the ApplicationConnection, do not dispose it
return connection == Config.DesignerSettings.ApplicationConnection;
}
private void ShowLoginForm(string lastConnectionString)
{
if (String.IsNullOrEmpty(lastConnectionString))
{
using (AskLoginPasswordForm form = new AskLoginPasswordForm())
{
if (form.ShowDialog() == DialogResult.OK)
lastConnectionString = GetConnectionStringWithLoginInfo(form.Login, form.Password);
}
}
}
#endregion Private Methods
partial void SerializeDesignExt(FRWriter writer)
{
if (!string.IsNullOrEmpty(CloudId))
{
writer.WriteStr("CloudId", CloudId);
}
}
internal class DataSourceCloudInfo
{
internal string Name { get; set; }
internal string ConnectionString { get; set; }
}
}
}