1 package DBIx::Class::Storage::DBI::MSSQL;
6 use base qw/DBIx::Class::Storage::DBI::AmbiguousGlob DBIx::Class::Storage::DBI/;
11 __PACKAGE__->mk_group_accessors(simple => qw/
12 _identity _identity_method
15 __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::MSSQL');
19 my ($source, $cols, $data) = @_;
21 my $identity_insert = 0;
24 foreach my $col (@{$cols}) {
25 if ($source->column_info($col)->{is_auto_increment}) {
31 if ($identity_insert) {
32 my $table = $source->from;
33 $self->dbh->do("SET IDENTITY_INSERT $table ON");
36 $self->next::method(@_);
38 if ($identity_insert) {
39 my $table = $source->from;
40 $self->dbh->do("SET IDENTITY_INSERT $table OFF");
44 sub _prep_for_execute {
46 my ($op, $extra_bind, $ident, $args) = @_;
48 # cast MONEY values properly
49 if ($op eq 'insert' || $op eq 'update') {
50 my $fields = $args->[0];
51 my $col_info = $self->_resolve_column_info($ident, [keys %$fields]);
53 for my $col (keys %$fields) {
54 if ($col_info->{$col}{data_type} =~ /^money\z/i) {
55 my $val = $fields->{$col};
56 $fields->{$col} = \['CAST(? AS MONEY)', [ $col => $val ]];
61 my ($sql, $bind) = $self->next::method (@_);
63 if ($op eq 'insert') {
64 $sql .= ';SELECT SCOPE_IDENTITY()';
66 my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
67 if (List::Util::first { $_->{is_auto_increment} } (values %$col_info) ) {
69 my $table = $ident->from;
70 my $identity_insert_on = "SET IDENTITY_INSERT $table ON";
71 my $identity_insert_off = "SET IDENTITY_INSERT $table OFF";
72 $sql = "$identity_insert_on; $sql; $identity_insert_off";
83 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
84 if ($op eq 'insert') {
85 $self->_identity($self->_fetch_identity($sth));
88 return wantarray ? ($rv, $sth, @bind) : $rv;
92 my ($self, $sth) = @_;
93 my ($identity) = $sth->fetchrow_array;
96 if ((not defined $identity) && $self->_identity_method &&
97 $self->_identity_method eq '@@identity') {
98 ($identity) = $self->_dbh->selectrow_array('select @@identity');
104 sub last_insert_id { shift->_identity }
106 # savepoint syntax is the same as in Sybase ASE
109 my ($self, $name) = @_;
111 $self->dbh->do("SAVE TRANSACTION $name");
114 # A new SAVE TRANSACTION with the same name releases the previous one.
115 sub _svp_release { 1 }
118 my ($self, $name) = @_;
120 $self->dbh->do("ROLLBACK TRANSACTION $name");
123 sub build_datetime_parser {
125 my $type = "DateTime::Format::Strptime";
127 $self->throw_exception("Couldn't load ${type}: $@") if $@;
128 return $type->new( pattern => '%Y-%m-%d %H:%M:%S' ); # %F %T
131 sub sqlt_type { 'SQLServer' }
133 sub _sql_maker_opts {
134 my ( $self, $opts ) = @_;
137 $self->{_sql_maker_opts} = { %$opts };
140 return { limit_dialect => 'Top', %{$self->{_sql_maker_opts}||{}} };
147 DBIx::Class::Storage::DBI::MSSQL - Base Class for Microsoft SQL Server support
152 This is the base class for Microsoft SQL Server support, used by
153 L<DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server> and
154 L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>.
156 =head1 IMPLEMENTATION NOTES
158 Microsoft SQL Server supports three methods of retrieving the IDENTITY
159 value for inserted row: IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY().
160 SCOPE_IDENTITY is used here because it is the safest. However, it must
161 be called is the same execute statement, not just the same connection.
163 So, this implementation appends a SELECT SCOPE_IDENTITY() statement
164 onto each INSERT to accommodate that requirement.
166 C<SELECT @@IDENTITY> can also be used by issuing:
168 $self->_identity_method('@@identity');
170 it will only be used if SCOPE_IDENTITY() fails.
172 This is more dangerous, as inserting into a table with an on insert trigger that
173 inserts into another table with an identity will give erroneous results on
174 recent versions of SQL Server.
178 See L<DBIx::Class/CONTRIBUTORS>.
182 You may distribute this code under the same terms as Perl itself.