Support customized SQL formater for SchemaExport


The default SQL formatter that comes with NHibernate adds extraneous indentation. IFormatter interface is public but SchemaExport doesn't expose an interface to set its formatter. Working around with reflection is definitely not elegant.

Hope that a formatter class can be specified in addition to the format_sql boolean configuration setting, so that customized formatter can be used.




Yufan Lou
August 8, 2017, 1:01 PM

The affected configuration interfaces are:

  • format_sql in XML

  • db.LogFormattedSql in loquacious

I think it is fine to add an additional interface called respectively

  • sql_formatter

  • db.SqlFormatter<IFormatter>()

I am new to NHibernate so please help me by pointing out other potential impact this may have.

Frédéric Delaporte
August 8, 2017, 3:18 PM

Why not adding the possibility of configuring a custom formatting class, of course implementing IFormatter.

Your proposition would mean defining a unique formatter, whatever the SQL, be it DDL or DML. Currently, when formatting SQL is enabled, it uses the basic formatter for DML and the DDL formatter for DDL. (Otherwise it uses the no format formatter, which as shown here does not do anything.)

There are many places where this choice is done, so introducing the new sql_formatter setting would have many impacts, while lacking the ability to specify specialized formatter for DDL/DML.

So maybe a way to override FormatStyle.Basic, FormatStyle.Ddl and FormatStyle.None Formatter should be added instead. This would let format_sql logic still pick the appropriate formatter depending on the context and setting.

But there is no configuration point for such static properties, so it would be up to the user to set its custom formatters (maybe directly by switching the FormatStyle.Formatter setter to public) before the application starts using NHibernate.

A "complete" configurable solution would likely require quite more work, like moving the formatter choice handling to the session factory and allowing to customize it there.

Yufan Lou
August 8, 2017, 5:31 PM

I see. I definitely welcome a way to override the formatters. That's how I initially wanted to do it. Currently in SchemaExport the format style is hardcoded in Initialize() function without a formatter property to set.




Yufan Lou



Affects versions