Try fast search NHibernate

08 June 2009

From where start to implements IDataBaseSchema

The auto-quote and auto import KeyWords features are now available in NHibernate but only for those dialects are providing an implementation of IDataBaseSchema.

If the NHibernate’s dialect, for your favorite RDBMS, does not provide an implementation of IDataBaseSchema, what can you do ?

Start point

First of all you need an easy way to know some “internals” of your DataProvider/RDBMS. The code to extract all information you are needing to implement a IDataBaseSchema is:

internal class Program
{
private static void Main(string[] args)
{

// Extract metadata for Oracle
CreateMetadataXml("System.Data.OracleClient", "User Id=NH; Password=nh");

// Extract metadata for MsSQL
CreateMetadataXml("System.Data.SqlClient", @"Data Source=localhost\SQLEXPRESS;Initial Catalog=NHTEST;Integrated Security=True");

Console.WriteLine("Work done!");
Console.ReadLine();
}

private static void CreateMetadataXml(string providerName, string connectionString)
{
DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);

using (DbConnection conn = factory.CreateConnection())
{
try
{
conn.ConnectionString = connectionString;
conn.Open();

//Get MetaDataCollections and write to an XML file.
//This is equivalent to GetSchema()
DataTable dtMetadata = conn.GetSchema(DbMetaDataCollectionNames.MetaDataCollections);
dtMetadata.WriteXml(providerName + "_MetaDataCollections.xml");

//Get Restrictions and write to an XML file.
DataTable dtRestrictions = conn.GetSchema(DbMetaDataCollectionNames.Restrictions);
dtRestrictions.WriteXml(providerName + "_Restrictions.xml");

//Get DataSourceInformation and write to an XML file.
DataTable dtDataSrcInfo = conn.GetSchema(DbMetaDataCollectionNames.DataSourceInformation);
dtDataSrcInfo.WriteXml(providerName + "_DataSourceInformation.xml");

//data types and write to an XML file.
DataTable dtDataTypes = conn.GetSchema(DbMetaDataCollectionNames.DataTypes);
dtDataTypes.WriteXml(providerName + "_DataTypes.xml");

//Get ReservedWords and write to an XML file.
DataTable dtReservedWords = conn.GetSchema(DbMetaDataCollectionNames.ReservedWords);
dtReservedWords.WriteXml(providerName + "_ReservedWords.xml");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}

The code above will create an XML file for each “matter” involved with IDataBaseSchema implementation.

What’s next

I don’t want to deep in details because each RDBMS may have different info so the only things I can tell you are:

  • There are some base classes that may work, as is, for your RDBMS : AbstractDataBaseSchema, AbstractTableMetadata, AbstractColumnMetaData, AbstractForeignKeyMetadata, AbstractIndexMetadata.
  • There are some implementations for various RDBMS where you can see which are the difference and where some information, extracted from the code above, was used : FirebirdMetaData.cs, MsSqlCeMetaData.cs, MsSqlMetaData.cs, MySQLMetaData.cs, OracleMetaData.cs, SQLiteMetaData.cs, SybaseAnywhereMetaData.cs.
  • Tests to pass are contained in NHibernate.Test.Tools.hbm2ddl namespace.

What you should do after implement IDataBaseSchema

Create a new JIRA ticket as:

  • Project : NHibernate
  • Issue Type: Improvement
  • Summary : New DataBase schema provider for “your preferred RDBMS”
  • Priority : Minor
  • Component : DataProviders / Dialects
  • Affects Version : “The last released”
  • Description : “Few words”
  • Attachment : this is the most important; send us your implementation!!

Thanks.

No comments:

Post a Comment