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