using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.Core
{
///
/// Represents an SQL data source.
///
public class DataSource
{
//private static bool _has_provider;
///
/// Gets or sets the type of source.
///
public DataSourceType Type { get; set; }
///
/// Gets or sets the address.
///
public String Address { get; set; }
///
/// Gets or sets the catalog name.
///
public String Catalog { get; set; }
///
/// Gets or sets a value indicating whether to use integrated security.
///
public bool IntegratedSecurity { get; set; }
///
/// Gets or sets the user name to use if is false.
///
public String UserName { get; set; }
///
/// Gets or sets the password to use if is false.
///
public String Password { get; set; }
///
/// Gets or sets the access token Token.
///
public String AccessToken { get; set; }
///
/// Gets or sets the access token expiration.
///
public DateTime AccessTokenExpiration { get; set; }
///
/// Initializes a new instance of the class.
///
public DataSource()
{
Type = DataSourceType.SQLServer;
Address = "localhost\\SQLEXPRESS";
Catalog = "Tango";
IntegratedSecurity = true;
UserName = String.Empty;
Password = String.Empty;
}
///
/// Creates a DBConnection instance which represents this data source.
///
///
public DbConnection ToConnection()
{
switch (Type)
{
case DataSourceType.SQLite:
var connection = new SQLiteConnection()
{
ConnectionString = new SQLiteConnectionStringBuilder() { DataSource = Path.GetFullPath(Address), ForeignKeys = true }.ConnectionString
};
return connection;
case DataSourceType.MDF:
if (IntegratedSecurity)
{
return new SqlConnection(String.Format("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFileName={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework", Path.GetFullPath(Address), Catalog));
}
else
{
return new SqlConnection(String.Format("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFileName={0};Initial Catalog={1};Integrated Security=False;User Id={2};Password={3};MultipleActiveResultSets=True;App=EntityFramework", Path.GetFullPath(Address), Catalog, UserName, Password));
}
case DataSourceType.SQLServer:
if (IntegratedSecurity)
{
return new SqlConnection(String.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework", Address, Catalog));
}
else
{
return new SqlConnection(String.Format("Data Source={0};Initial Catalog={1};Integrated Security=False;User Id={2};Password={3};MultipleActiveResultSets=True;App=EntityFramework", Address, Catalog, UserName, Password));
}
case DataSourceType.Azure:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = Address;
builder.InitialCatalog = Catalog;
builder.UserID = UserName;
builder.Password = Password;
builder.IntegratedSecurity = false;
builder.MultipleActiveResultSets = true;
builder.ApplicationName = "EntityFramework";
builder.Authentication = SqlAuthenticationMethod.ActiveDirectoryPassword;
SqlConnection sqlConnection = new SqlConnection(builder.ConnectionString);
return sqlConnection;
case DataSourceType.AccessToken:
SqlConnectionStringBuilder b = new SqlConnectionStringBuilder();
b.DataSource = Address;
b.InitialCatalog = Catalog;
b.IntegratedSecurity = false;
b.MultipleActiveResultSets = true;
b.ApplicationName = "EntityFramework";
SqlConnection sqlCon = new SqlConnection(b.ConnectionString);
sqlCon.AccessToken = AccessToken;
return sqlCon;
default:
return null;
}
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return $"{Address}\\{Catalog}";
}
}
}