=for comment POD_DERIVED_INDEX_GENERATED
The following documentation is automatically generated.  Please do not edit
this file, but rather the original, inline with DBIx::Class::Storage::DBI::MSSQL
at lib/DBIx/Class/Storage/DBI/MSSQL.pm
(on the system that originally ran this).
If you do edit this file, and don't want your changes to be removed, make
sure you change the first line.

=cut

=head1 NAME

DBIx::Class::Storage::DBI::MSSQL - Base Class for Microsoft SQL Server support
in DBIx::Class

=head1 SYNOPSIS

This is the base class for Microsoft SQL Server support, used by
L<DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server> and
L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>.

=head1 IMPLEMENTATION NOTES

=head2 IDENTITY information

Microsoft SQL Server supports three methods of retrieving the IDENTITY
value for inserted row: IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY().
SCOPE_IDENTITY is used here because it is the safest.  However, it must
be called is the same execute statement, not just the same connection.

So, this implementation appends a SELECT SCOPE_IDENTITY() statement
onto each INSERT to accommodate that requirement.

C<SELECT @@IDENTITY> can also be used by issuing:

  $self->_identity_method('@@identity');

it will only be used if SCOPE_IDENTITY() fails.

This is more dangerous, as inserting into a table with an on insert trigger that
inserts into another table with an identity will give erroneous results on
recent versions of SQL Server.

=head2 identity insert

Be aware that we have tried to make things as simple as possible for our users.
For MSSQL that means that when a user tries to create a row, while supplying an
explicit value for an autoincrementing column, we will try to issue the
appropriate database call to make this possible, namely C<SET IDENTITY_INSERT
$table_name ON>. Unfortunately this operation in MSSQL requires the
C<db_ddladmin> privilege, which is normally not included in the standard
write-permissions.

=head2 Ordered Subselects

If you attempted the following query (among many others) in Microsoft SQL
Server

 $rs->search ({}, {
  prefetch => 'relation',
  rows => 2,
  offset => 3,
 });

You may be surprised to receive an exception. The reason for this is a quirk
in the MSSQL engine itself, and sadly doesn't have a sensible workaround due
to the way DBIC is built. DBIC can do truly wonderful things with the aid of
subselects, and does so automatically when necessary. The list of situations
when a subselect is necessary is long and still changes often, so it can not
be exhaustively enumerated here. The general rule of thumb is a joined
L<has_many|DBIx::Class::Relationship/has_many> relationship with limit/group
applied to the left part of the join.

In its "pursuit of standards" Microsft SQL Server goes to great lengths to
forbid the use of ordered subselects. This breaks a very useful group of
searches like "Give me things number 4 to 6 (ordered by name), and prefetch
all their relations, no matter how many". While there is a hack which fools
the syntax checker, the optimizer may B<still elect to break the subselect>.
Testing has determined that while such breakage does occur (the test suite
contains an explicit test which demonstrates the problem), it is relative
rare. The benefits of ordered subselects are on the other hand too great to be
outright disabled for MSSQL.

Thus compromise between usability and perfection is the MSSQL-specific
L<resultset attribute|DBIx::Class::ResultSet/ATTRIBUTES> C<unsafe_subselect_ok>.
It is deliberately not possible to set this on the Storage level, as the user
should inspect (and preferably regression-test) the return of every such
ResultSet individually. The example above would work if written like:

 $rs->search ({}, {
  unsafe_subselect_ok => 1,
  prefetch => 'relation',
  rows => 2,
  offset => 3,
 });

If it is possible to rewrite the search() in a way that will avoid the need
for this flag - you are urged to do so. If DBIC internals insist that an
ordered subselect is necessary for an operation, and you believe there is a
different/better way to get the same result - please file a bugreport.

=head1 INHERITED METHODS

=over 4

=item L<DBIx::Class::Storage::DBI::UniqueIdentifier>

L<new_guid|DBIx::Class::Storage::DBI::UniqueIdentifier/new_guid>

=item L<DBIx::Class::Storage::DBI>

L<auto_savepoint|DBIx::Class::Storage::DBI/auto_savepoint>, L<bind_attribute_by_data_type|DBIx::Class::Storage::DBI/bind_attribute_by_data_type>, L<build_datetime_parser|DBIx::Class::Storage::DBI/build_datetime_parser>, L<columns_info_for|DBIx::Class::Storage::DBI/columns_info_for>, L<connect_call_datetime_setup|DBIx::Class::Storage::DBI/connect_call_datetime_setup>, L<connect_call_do_sql|DBIx::Class::Storage::DBI/connect_call_do_sql>, L<connect_info|DBIx::Class::Storage::DBI/connect_info>, L<connected|DBIx::Class::Storage::DBI/connected>, L<create_ddl_dir|DBIx::Class::Storage::DBI/create_ddl_dir>, L<datetime_parser|DBIx::Class::Storage::DBI/datetime_parser>, L<datetime_parser_type|DBIx::Class::Storage::DBI/datetime_parser_type>, L<dbh|DBIx::Class::Storage::DBI/dbh>, L<dbh_do|DBIx::Class::Storage::DBI/dbh_do>, L<delete|DBIx::Class::Storage::DBI/delete>, L<deploy|DBIx::Class::Storage::DBI/deploy>, L<deployment_statements|DBIx::Class::Storage::DBI/deployment_statements>, L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching>, L<disconnect|DBIx::Class::Storage::DBI/disconnect>, L<disconnect_call_do_sql|DBIx::Class::Storage::DBI/disconnect_call_do_sql>, L<ensure_connected|DBIx::Class::Storage::DBI/ensure_connected>, L<get_dbms_capability|DBIx::Class::Storage::DBI/get_dbms_capability>, L<get_use_dbms_capability|DBIx::Class::Storage::DBI/get_use_dbms_capability>, L<insert|DBIx::Class::Storage::DBI/insert>, L<insert_bulk|DBIx::Class::Storage::DBI/insert_bulk>, L<is_datatype_numeric|DBIx::Class::Storage::DBI/is_datatype_numeric>, L<is_replicating|DBIx::Class::Storage::DBI/is_replicating>, L<lag_behind_master|DBIx::Class::Storage::DBI/lag_behind_master>, L<new|DBIx::Class::Storage::DBI/new>, L<on_connect_call|DBIx::Class::Storage::DBI/on_connect_call>, L<on_connect_do|DBIx::Class::Storage::DBI/on_connect_do>, L<on_disconnect_call|DBIx::Class::Storage::DBI/on_disconnect_call>, L<on_disconnect_do|DBIx::Class::Storage::DBI/on_disconnect_do>, L<relname_to_table_alias|DBIx::Class::Storage::DBI/relname_to_table_alias>, L<select|DBIx::Class::Storage::DBI/select>, L<select_single|DBIx::Class::Storage::DBI/select_single>, L<set_dbms_capability|DBIx::Class::Storage::DBI/set_dbms_capability>, L<set_use_dbms_capability|DBIx::Class::Storage::DBI/set_use_dbms_capability>, L<source_bind_attributes|DBIx::Class::Storage::DBI/source_bind_attributes>, L<sql_maker|DBIx::Class::Storage::DBI/sql_maker>, L<sql_maker_class|DBIx::Class::Storage::DBI/sql_maker_class>, L<sql_name_sep|DBIx::Class::Storage::DBI/sql_name_sep>, L<sql_quote_char|DBIx::Class::Storage::DBI/sql_quote_char>, L<sth|DBIx::Class::Storage::DBI/sth>, L<svp_begin|DBIx::Class::Storage::DBI/svp_begin>, L<svp_release|DBIx::Class::Storage::DBI/svp_release>, L<svp_rollback|DBIx::Class::Storage::DBI/svp_rollback>, L<txn_begin|DBIx::Class::Storage::DBI/txn_begin>, L<txn_commit|DBIx::Class::Storage::DBI/txn_commit>, L<txn_do|DBIx::Class::Storage::DBI/txn_do>, L<txn_rollback|DBIx::Class::Storage::DBI/txn_rollback>, L<unsafe|DBIx::Class::Storage::DBI/unsafe>, L<update|DBIx::Class::Storage::DBI/update>, L<with_deferred_fk_checks|DBIx::Class::Storage::DBI/with_deferred_fk_checks>

=item L<DBIx::Class::Storage>

L<cursor|DBIx::Class::Storage/cursor>, L<cursor_class|DBIx::Class::Storage/cursor_class>, L<debug|DBIx::Class::Storage/debug>, L<debugcb|DBIx::Class::Storage/debugcb>, L<debugfh|DBIx::Class::Storage/debugfh>, L<debugobj|DBIx::Class::Storage/debugobj>, L<savepoints|DBIx::Class::Storage/savepoints>, L<schema|DBIx::Class::Storage/schema>, L<set_schema|DBIx::Class::Storage/set_schema>, L<throw_exception|DBIx::Class::Storage/throw_exception>, L<transaction_depth|DBIx::Class::Storage/transaction_depth>, L<txn_scope_guard|DBIx::Class::Storage/txn_scope_guard>

=back

=head1 AUTHOR

See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.

=head1 LICENSE

You may distribute this code under the same terms as Perl itself.

