martes, 12 de mayo de 2009

TableTypeString property of Dialect Class is never used in NHibernate

I was wondering why TableTypeString property of Dialect Class is never used inside the NHibernate library, the property is there, but when I try to find usages of this property, no usages were found.

I needed to use this property because I was writing a special Dialect for MySql to be able to generate InnoDb Tables and to make their charset UTF-8 by default in despite of the default charset of the Database.

I found an example of this in this post (Teamwork, MySQL and UTF-8), the code there is for Hibernate in Java, so instead of use the getTableTypeString getter in NHibernate there is a TableTypeString property.

So, I subclass the MySqlDialect to make my own Mysql5InnoDbUtf8Dialect, but it didn’t works. Looking at the code I found that the Table class in Hibernate differs from the NHibernate Class in the SqlCreateString method. At the end of that method we found in the Java class

if ( comment != null ) { 
	buf.append( dialect.getTableComment( comment ) ); 
}  
return buf.append( dialect.getTableTypeString() ).toString(); 

And at the end of the same method in the NHibernate class we found

if(string.IsNullOrEmpty(comment)==false)
{
	buf.Append(dialect.GetTableComment(comment));
}

return buf.ToString();

So as you can see if the TableTypeString property have to be used somewhere, this is the place, and currently is not used(If anyone knows why please, please let me know!)

so I make the change in the code, in the Table Class at the end of the SqlCreateString method (see code below)

if (string.IsNullOrEmpty(comment) == false)
{
	buf.Append(dialect.GetTableComment(comment));
}
			
return buf.Append(dialect.TableTypeString).ToString();
//return buf.ToString();

and recompile again Nhibernate. After that my new Dialect was working very Nice!

I'm not so sure if that is the best way to acomplish what I was needing to do (create tables with UTF-8 charset by default). Sure, if you have control of the database you can create the database with UTF-8 charset by deafult, but I really want to found a way to do it by code and not in the MySql Administrator.

Well let's work again...

No hay comentarios: