#region Copyright
// Advanced.Data.Provider.AdpProvider
//
// Copyright (C) 2004 Astrein Engenharia de Manuteno S/A
// Copyright (C) 2004 Everaldo Canuto <everaldo_canuto@yahoo.com.br>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Reflection;
using System.Data;
namespace Advanced.Data.Provider{
/// <summary>
/// Represents the data provider and namespace relative to the selected database.
/// </summary>
public class AdpProvider
{
#region Fields
private static AdpProviderCollection providers;
private Assembly assembly;
private string name;
private string description;
private string nameSpace;
private string assemblyName;
private string connection;
private string dataAdapter;
private string paramPrefix;
private string identitySql;
#endregion
#region Constructors and destructors
static AdpProvider()
{
providers = new AdpProviderCollection();
// Add default SQL Server provider.
AddProvider("MsSql",
"Microsoft SQL Server 7.0/2000",
"System.Data",
"System.Data.SqlClient",
"System.Data.SqlClient.SqlConnection",
"System.Data.SqlClient.SqlDataAdapter",
"@",
"");
// Add default OLE DB provider.
AddProvider("OleDb",
"OLE DB Data Provider",
"System.Data",
"System.Data.OleDb",
"System.Data.OleDb.OleDbConnection",
"System.Data.OleDb.OleDbDataAdapter",
"?",
"");
// Add default ODBC provider.
AddProvider("Odbc",
"ODBC Data Provider",
"System.Data",
"System.Data.Odbc",
"System.Data.Odbc.OdbcConnection",
"System.Data.Odbc.OdbcDataAdapter",
"?",
"");
// Add default Oracle (Microsoft version) provider.
AddProvider("OracleClient",
"Microsoft Oracle Provider",
"System.Data.OracleClient",
"System.Data.OracleClient",
"System.Data.OracleClient.OracleConnection",
"System.Data.OracleClient.OracleDataAdapter",
":",
"");
// Add default Oracle provider.
AddProvider("Oracle",
"Oracle 9i",
"Oracle.DataAccess",
"Oracle.DataAccess.Client",
"Oracle.DataAccess.Client.OracleConnection",
"Oracle.DataAccess.Client.OracleDataAdapter",
":",
"");
// Add default TDS provider.
AddProvider("Tds",
"TDS Generic",
"Mono.Data.TdsClient",
"Mono.Data.TdsClient",
"Mono.Data.TdsClient.TdsConnection",
"Mono.Data.TdsClient.TdsDataAdapter",
"@",
"");
// Add default MySql provider.
AddProvider("MySql",
"MySql Data Provider",
"ByteFX.MySqlClient",
"ByteFX.Data.MySqlClient",
"ByteFX.Data.MySqlClient.MySqlConnection",
"ByteFX.Data.MySqlClient.MySqlDataAdapter",
"@",
"");
// Add default PostgreSQL provider.
AddProvider("Npgsql",
"PostgreSQL Data Provider",
"Npgsql",
"Npgsql",
"Npgsql.NpgsqlConnection",
"Npgsql.NpgsqlDataAdapter",
"@",
"");
// Add default SQL Lite provider.
AddProvider("Sqlite",
"SQLite Data Provider",
"Mono.Data.SqliteClient",
"Mono.Data.SqliteClient",
"Mono.Data.SqliteClient.SqliteConnection",
"Mono.Data.SqliteClient.SqliteDataAdapter",
"@",
"");
// Add default Sybase provider.
AddProvider("Sybase",
"Sybase Data Provider",
"Mono.Data.SybaseClient",
"Mono.Data.SybaseClient",
"Mono.Data.SybaseClient.SybaseConnection",
"Mono.Data.SybaseClient.SybaseDataAdapter",
"@",
"");
// Add default Sybase provider.
AddProvider("Firebird",
"Firebird SQL Data Provider",
"FirebirdSql.Data.Firebird",
"FirebirdSql.Data.Firebird",
"FirebirdSql.Data.Firebird.FbConnection",
"FirebirdSql.Data.Firebird.FbDataAdapter",
"@",
"");
// Load provider from "Advanced.Data.Provider.config" file.
//LoadProvidersFrom(System.Reflection.Assembly.GetExecutingAssembly().Location+".config");
// Load providers from App.config file.
//LoadProvidersFrom(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
/// <summary>
/// Initializes a new instance of the <see cref='AdpProvider'/> class.
/// </summary>
public AdpProvider()
{
}
/// <summary>
/// Gets new instance of <see cref='AdpProvider'/> class of a specified the provider name.
/// </summary>
/// <param name="name">The provider name.</param>
/// <returns>A <see cref='AdpProvider'/> object.</returns>
public static AdpProvider CreateProvider(string name)
{
AdpProvider prv = providers[name];
// To connect using NameSpace.
if (prv == null)
{
foreach (AdpProvider myprv in providers)
{
if (myprv.nameSpace == name)
{
prv = myprv;
break;
}
}
}
return prv;
}
#endregion
#region Public properties
/// <summary>
/// Gets a list of avaliable providers.
/// </summary>
public static AdpProviderCollection Providers
{
get
{
return providers;
}
}
/// <summary>
/// Gets the provider name.
/// </summary>
public string Name
{
get
{
return name;
}
}
/// <summary>
/// Gets the provider description.
/// </summary>
public string Description
{
get
{
return description;
}
}
/// <summary>
/// Gets the param prefix of data provider.
/// </summary>
public string ParamPrefix
{
get
{
return paramPrefix;
}
}
/// <summary>
/// Gets the Indentify SQL of data provider.
/// </summary>
internal string IdentitySql
{
get
{
return identitySql;
}
}
#endregion
#region Private methods
/// <summary>
/// Adds a <see cref='AdpProvider'/> with the specified value to the <see cref='AdpProviderCollection'/>.
/// </summary>
/// <param name='name'>The provider name.</param>
/// <param name='description'>The provider description.</param>
/// <param name='assemblyName'>The provider assembly name.</param>
/// <param name='nspace'>The provider namespace</param>
/// <param name='connection'>The provider connection name</param>
/// <param name='dataAdapter'>The provider dataadapter name.</param>
/// <param name='paramPrefix'>The provider parameter prefix.</param>
/// <param name='identitySql'>The provider identity SQL.</param>
private static void AddProvider(string name, string description, string assemblyName, string nspace, string connection, string dataAdapter, string paramPrefix, string identitySql)
{
AdpProvider provider = new AdpProvider();
provider.name = name;
provider.description = description;
provider.nameSpace = nspace;
provider.assemblyName = assemblyName;
provider.connection = connection;
provider.dataAdapter = dataAdapter;
provider.paramPrefix = paramPrefix;
provider.identitySql = identitySql;
if (providers.Contains(provider.name))
{
providers[provider.name] = provider;
}
else
{
providers.Add(provider);
}
}
private static string GetNodeValue(XmlNode node, string attribute)
{
XmlAttribute a = node.Attributes[attribute];
if (a == null)
return "";
else
return a.Value;
}
private static void LoadProvidersFrom(string filename)
{
if (File.Exists(filename))
{
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNodeList ProviderList = doc.SelectNodes("configuration/advanced.data.provider/providers/provider");
foreach (XmlNode node in ProviderList)
{
AddProvider(GetNodeValue(node, "name"),
GetNodeValue(node, "description"),
GetNodeValue(node, "assembly"),
GetNodeValue(node, "namespace"),
GetNodeValue(node, "connection"),
GetNodeValue(node, "adapter"),
GetNodeValue(node, "paramprefix"),
GetNodeValue(node, "identity"));
}
}
}
#endregion
#region Public methods
/// <summary>
/// Creates and retuns a <see cref='IDbConnection'/> object associated with this instance of the <see cref='AdpProvider'/>.
/// </summary>
/// <param name="ConnectionString">The string used to open a database server.</param>
/// <returns>A <see cref='IDbConnection'/> object.</returns>
public IDbConnection CreateConnection(string ConnectionString)
{
if (assembly == null)
assembly = Assembly.LoadWithPartialName(assemblyName);
object [] args = new object [] {ConnectionString};
return (IDbConnection) assembly.CreateInstance(connection, true, 0, null, args, null, null);
}
/// <summary>
/// Creates and retuns a <see cref='IDbDataAdapter'/> object associated with this instance of the <see cref='AdpProvider'/>.
/// </summary>
/// <returns>A <see cref='IDbDataAdapter'/> object.</returns>
public IDbDataAdapter CreateDataAdapter()
{
if (assembly == null)
assembly = Assembly.LoadWithPartialName(assemblyName);
return (IDbDataAdapter) assembly.CreateInstance(dataAdapter);
}
#endregion
}
}
|