087f0c2f8990dd67687bd9f422bc59a567ffd669
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / ACCESS.pm
1 package DBIx::Class::Storage::DBI::ODBC::ACCESS;
2 use strict;
3 use warnings;
4
5 use base qw/DBIx::Class::Storage::DBI/;
6 use mro 'c3';
7
8 use DBI;
9
10 my $ERR_MSG_START = __PACKAGE__ . ' failed: ';
11
12 __PACKAGE__->sql_limit_dialect ('Top');
13 __PACKAGE__->sql_quote_char ([qw/[ ]/]);
14
15 sub insert {
16     my $self = shift;
17     my ( $source, $to_insert ) = @_;
18
19     my $bind_attributes = $self->source_bind_attributes( $source );
20     my ( undef, $sth ) = $self->_execute( 'insert' => [], $source, $bind_attributes, $to_insert );
21
22     #store the identity here since @@IDENTITY is connection global and this prevents
23     #possibility that another insert to a different table overwrites it for this resultsource
24     my $identity = 'SELECT @@IDENTITY';
25     my $max_sth  = $self->{ _dbh }->prepare( $identity )
26         or $self->throw_exception( $ERR_MSG_START . $self->{ _dbh }->errstr() );
27     $max_sth->execute() or $self->throw_exception( $ERR_MSG_START . $max_sth->errstr );
28
29     my $row = $max_sth->fetchrow_arrayref()
30         or $self->throw_exception( $ERR_MSG_START . "$identity did not return any result." );
31
32     $self->{ last_pk }->{ $source->name() } = $row;
33
34     return $to_insert;
35 }
36
37 sub last_insert_id {
38     my $self = shift;
39     my ( $result_source ) = @_;
40
41     return @{ $self->{ last_pk }->{ $result_source->name() } };
42 }
43
44 sub bind_attribute_by_data_type {
45     my $self = shift;
46
47     my ( $data_type ) = @_;
48
49     return { TYPE => $data_type } if $data_type == DBI::SQL_LONGVARCHAR;
50
51     return;
52 }
53
54 sub sqlt_type { 'ACCESS' }
55
56 1;
57
58 =head1 NAME
59
60 DBIx::Class::Storage::DBI::ODBC::ACCESS - Support specific to MS Access over ODBC
61
62 =head1 WARNING
63
64 I am not a DBI, DBIx::Class or MS Access guru. Use this module with that in
65 mind.
66
67 This module is currently considered alpha software and can change without notice.
68
69 =head1 DESCRIPTION
70
71 This class implements support specific to Microsoft Access over ODBC.
72
73 It is loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it
74 detects a MS Access back-end.
75
76 =head1 SUPPORTED VERSIONS
77
78 This module have currently only been tested on MS Access 2003 using the Jet 4.0 engine.
79
80 As far as my knowledge it should work on MS Access 2000 or later, but that have not been tested.
81 Information about support for different version of MS Access is welcome.
82
83 =head1 IMPLEMENTATION NOTES
84
85 MS Access supports the @@IDENTITY function for retrieving the id of the latest inserted row.
86 @@IDENTITY is global to the connection, so to support the possibility of getting the last inserted
87 id for different tables, the insert() function stores the inserted id on a per table basis.
88 last_insert_id() then just returns the stored value.
89
90 =head1 KNOWN ACCESS PROBLEMS
91
92 =over
93
94 =item Invalid precision value
95
96 This error message is received when trying to store more than 255 characters in a MEMO field.
97 The problem is (to my knowledge) an error in the MS Access ODBC driver. The problem is fixed
98 by setting the C<data_type> of the column to C<SQL_LONGVARCHAR> in C<add_columns>. 
99 C<SQL_LONGVARCHAR> is a constant in the C<DBI> module.
100
101 =back
102
103 =head1 IMPLEMENTED FUNCTIONS
104
105 =head2 bind_attribute_by_data_type
106
107 This function currently supports the SQL_LONGVARCHAR column type.
108
109 =head2 insert
110
111 =head2 last_insert_id
112
113 =head2 sqlt_type
114
115 =head1 BUGS
116
117 Most likely. Bug reports are welcome.
118
119 =head1 AUTHORS
120
121 Øystein Torget C<< <oystein.torget@dnv.com> >>
122
123 =head1 COPYRIGHT
124
125 You may distribute this code under the same terms as Perl itself.
126
127 Det Norske Veritas AS (DNV)
128
129 http://www.dnv.com
130
131 =cut
132