Comprehensive MSAccess support over both DBD::ODBC and DBD::ADO
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / ACCESS.pm
CommitLineData
65c2b042 1package DBIx::Class::Storage::DBI::ODBC::ACCESS;
726c8f65 2
65c2b042 3use strict;
4use warnings;
726c8f65 5use base qw/
6 DBIx::Class::Storage::DBI::ODBC
7 DBIx::Class::Storage::DBI::ACCESS
8/;
2ad62d97 9use mro 'c3';
10
726c8f65 11__PACKAGE__->mk_group_accessors(inherited =>
12 'disable_sth_caching_for_image_insert_or_update'
13);
65c2b042 14
726c8f65 15__PACKAGE__->disable_sth_caching_for_image_insert_or_update(1);
65c2b042 16
726c8f65 17=head1 NAME
65c2b042 18
726c8f65 19DBIx::Class::Storage::DBI::ODBC::ACCESS - Support specific to MS Access over ODBC
65c2b042 20
726c8f65 21=head1 DESCRIPTION
65c2b042 22
726c8f65 23This class implements support specific to Microsoft Access over ODBC.
65c2b042 24
726c8f65 25It is a subclass of L<DBIx::Class::Storage::DBI::ODBC> and
26L<DBIx::Class::Storage::DBI::ACCESS>, see those classes for more
27information.
d4daee7b 28
726c8f65 29It is loaded automatically by by L<DBIx::Class::Storage::DBI::ODBC> when it
30detects a MS Access back-end.
d4daee7b 31
726c8f65 32This driver implements workarounds for C<IMAGE> and C<MEMO> columns, and
33L<DBIx::Class::InflateColumn::DateTime> support for C<DATETIME> columns.
d4daee7b 34
726c8f65 35=head1 EXAMPLE DSN
65c2b042 36
726c8f65 37 dbi:ODBC:driver={Microsoft Access Driver (*.mdb, *.accdb)};dbq=C:\Users\rkitover\Documents\access_sample.accdb
65c2b042 38
726c8f65 39=head1 TEXT/IMAGE/MEMO COLUMNS
65c2b042 40
726c8f65 41Avoid using C<TEXT> columns as they will be truncated to 255 bytes. Some other
42drivers (like L<ADO|DBIx::Class::Storage::DBI::ADO::MS_Jet>) will automatically
43convert C<TEXT> columns to C<MEMO>, but the ODBC driver does not.
65c2b042 44
726c8f65 45C<IMAGE> columns work correctly, but the statements for inserting or updating an
46C<IMAGE> column will not be L<cached|DBI/prepare_cached>, due to a bug in the
47Access ODBC driver.
65c2b042 48
726c8f65 49C<MEMO> columns work correctly as well, but you must take care to set
50L<LongReadLen|DBI/LongReadLen> to C<$max_memo_size * 2 + 1>. This is done for
51you automatically if you pass L<LongReadLen|DBI/LongReadLen> in your
52L<connect_info|DBIx::Class::Storage::DBI/connect_info>; but if you set this
53attribute directly on the C<$dbh>, keep this limitation in mind.
65c2b042 54
726c8f65 55=cut
65c2b042 56
726c8f65 57# set LongReadLen = LongReadLen * 2 + 1 (see docs on MEMO)
58sub _run_connection_actions {
59 my $self = shift;
65c2b042 60
726c8f65 61 my $long_read_len = $self->_dbh->{LongReadLen};
65c2b042 62
726c8f65 63 # 80 is another default (just like 0) on some drivers
64 if ($long_read_len != 0 && $long_read_len != 80) {
65 $self->_dbh->{LongReadLen} = $long_read_len * 2 + 1;
66 }
65c2b042 67
726c8f65 68 return $self->next::method(@_);
69}
65c2b042 70
726c8f65 71sub insert {
72 my $self = shift;
73 my ($source, $to_insert) = @_;
65c2b042 74
726c8f65 75 my $columns_info = $source->columns_info;
65c2b042 76
726c8f65 77 my $is_image_insert = 0;
65c2b042 78
726c8f65 79 for my $col (keys %$to_insert) {
80 if ($self->_is_binary_lob_type($columns_info->{$col}{data_type})) {
81 $is_image_insert = 1;
82 last;
83 }
84 }
65c2b042 85
726c8f65 86 local $self->{disable_sth_caching} = 1 if $is_image_insert
87 && $self->disable_sth_caching_for_image_insert_or_update;
65c2b042 88
726c8f65 89 return $self->next::method(@_);
90}
65c2b042 91
726c8f65 92sub update {
93 my $self = shift;
94 my ($source, $fields) = @_;
65c2b042 95
726c8f65 96 my $columns_info = $source->columns_info;
65c2b042 97
726c8f65 98 my $is_image_insert = 0;
65c2b042 99
726c8f65 100 for my $col (keys %$fields) {
101 if ($self->_is_binary_lob_type($columns_info->{$col}{data_type})) {
102 $is_image_insert = 1;
103 last;
104 }
105 }
65c2b042 106
726c8f65 107 local $self->{disable_sth_caching} = 1 if $is_image_insert
108 && $self->disable_sth_caching_for_image_insert_or_update;
65c2b042 109
726c8f65 110 return $self->next::method(@_);
111}
65c2b042 112
726c8f65 113sub datetime_parser_type {
114 'DBIx::Class::Storage::DBI::ODBC::ACCESS::DateTime::Format'
115}
65c2b042 116
726c8f65 117package # hide from PAUSE
118 DBIx::Class::Storage::DBI::ODBC::ACCESS::DateTime::Format;
65c2b042 119
726c8f65 120my $datetime_format = '%Y-%m-%d %H:%M:%S'; # %F %T, no fractional part
121my $datetime_parser;
65c2b042 122
726c8f65 123sub parse_datetime {
124 shift;
125 require DateTime::Format::Strptime;
126 $datetime_parser ||= DateTime::Format::Strptime->new(
127 pattern => $datetime_format,
128 on_error => 'croak',
129 );
130 return $datetime_parser->parse_datetime(shift);
131}
65c2b042 132
726c8f65 133sub format_datetime {
134 shift;
135 require DateTime::Format::Strptime;
136 $datetime_parser ||= DateTime::Format::Strptime->new(
137 pattern => $datetime_format,
138 on_error => 'croak',
139 );
140 return $datetime_parser->format_datetime(shift);
141}
65c2b042 142
726c8f65 1431;
65c2b042 144
726c8f65 145=head1 AUTHOR
65c2b042 146
726c8f65 147See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
65c2b042 148
726c8f65 149=head1 LICENSE
65c2b042 150
151You may distribute this code under the same terms as Perl itself.
152
65c2b042 153=cut
726c8f65 154# vim:sts=2 sw=2: