Ricardo SIGNES 😄


Class::DBI::MSSQL - Class::DBI for MSSQL


version 0.122

 $Id: /my/cs/projects/cdbi-mssql/trunk/lib/Class/DBI/MSSQL.pm 27829 2006-11-11T04:02:42.956483Z rjbs  $


        use base qw(Class::DBI::MSSQL);

        # lots of normal-looking CDBI code


This is just a simple subclass of Class::DBI; it makes Class::DBI play nicely with MSSQL, at least if DBD::ODBC is providing the connection.

Here are the things it changes:

  • use SELECT @@IDENTITY to get last autonumber value

  • use INSERT INTO table DEFAULT VALUES for create({})

It also implements some metadata methods, described below.



This method sets up the columns from the named table by querying MSSQL's information_schema metadata tables. It will set up the key(s) as Primary and all other columns as Essential.


This returns the named column's datatype.

autoinflate($type => $class)

This will automatically set up has_a() relationships for all columns of the specified type to the given class. If the type is "dates" it will apply to both datetime and smalldatetime columns. If the class is Time::Piece, Time::Piece::MSSQL will be required.

We currently assume that all classess passed will be able to inflate and deflate without needing extra has_a arguments.


For one thing, there are no useful tests in this distribution. I'll take care of that, but right now this is all taken care of in the tests I've written for subclasses of this class, and I don't have a lot of motivation to write new tests just for this package.

Class::DBI's _init sub has a line that reads as follows:

 if (@primary_columns == grep defined, @{$data}{@primary_columns}) {     

This will cause the primary key columns to autovivify as undef, which will make inserts fail under MSSQL. You should change that line to the following, which will fix the behavior.

 if (@$data{@primary_columns}
        and @primary_columns == grep defined, @{$data}{@primary_columns}
 ) {

I can't easily subclass that routine, as it relies on lexical variables above its scope. I've sent a patch to Tony, which I expect to be in the next Class::DBI release.


...to James O'Sullivan, for graciously sending me his own solution to this problem, which I've happily included.

...to Michael Schwern and Tony Bowden for creating and maintaining, respectively, the excellent Class::DBI system.

...to Casey West, for his crash course on Class::DBI at OSCON '04, which finally convinced me to just use the darn thing.


Ricardo SIGNES, <rjbs@cpan.org>

set_up_table and column_type from James O'Sullivan.


(C) 2004-2006, Ricardo SIGNES. Class::DBI::MSSQL is available under the same terms as Perl itself.