8b7e2a3c2f2d8517e20c90c1f866c740abe7569e
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Firebird / Common.pm
1 package DBIx::Class::Storage::DBI::Firebird::Common;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class::Storage::DBI/;
6 use mro 'c3';
7 use List::Util 'first';
8 use namespace::clean;
9
10 =head1 NAME
11
12 DBIx::Class::Storage::DBI::Firebird::Common - Driver Base Class for the Firebird RDBMS
13
14 =head1 DESCRIPTION
15
16 This class implements autoincrements for Firebird using C<RETURNING> as well as
17 L<auto_nextval|DBIx::Class::ResultSource/auto_nextval>, savepoints and server
18 version detection.
19
20 =cut
21
22 # set default
23 __PACKAGE__->_use_insert_returning (1);
24 __PACKAGE__->sql_limit_dialect ('FirstSkip');
25 __PACKAGE__->sql_quote_char ('"');
26
27 sub _sequence_fetch {
28   my ($self, $nextval, $sequence) = @_;
29
30   $self->throw_exception("Can only fetch 'nextval' for a sequence")
31     if $nextval !~ /^nextval$/i;
32
33   $self->throw_exception('No sequence to fetch') unless $sequence;
34
35   my ($val) = $self->_get_dbh->selectrow_array(sprintf
36     'SELECT GEN_ID(%s, 1) FROM rdb$database',
37     $self->sql_maker->_quote($sequence)
38   );
39
40   return $val;
41 }
42
43 sub _dbh_get_autoinc_seq {
44   my ($self, $dbh, $source, $col) = @_;
45
46   my $table_name = $source->from;
47   $table_name    = $$table_name if ref $table_name;
48   $table_name    = $self->sql_maker->quote_char ? $table_name : uc($table_name);
49
50   local $dbh->{LongReadLen} = 100000;
51   local $dbh->{LongTruncOk} = 1;
52
53   my $sth = $dbh->prepare(<<'EOF');
54 SELECT t.rdb$trigger_source
55 FROM rdb$triggers t
56 WHERE t.rdb$relation_name = ?
57 AND t.rdb$system_flag = 0 -- user defined
58 AND t.rdb$trigger_type = 1 -- BEFORE INSERT
59 EOF
60   $sth->execute($table_name);
61
62   while (my ($trigger) = $sth->fetchrow_array) {
63     my @trig_cols = map {
64       /^"([^"]+)/ ? $1 : uc($1)
65     } $trigger =~ /new\.("?\w+"?)/ig;
66
67     my ($quoted, $generator) = $trigger =~
68 /(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
69
70     if ($generator) {
71       $generator = uc $generator unless $quoted;
72
73       return $generator
74         if first {
75           $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
76         } @trig_cols;
77     }
78   }
79
80   return undef;
81 }
82
83 sub _exec_svp_begin {
84   my ($self, $name) = @_;
85
86   $self->_dbh->do("SAVEPOINT $name");
87 }
88
89 sub _exec_svp_release {
90   my ($self, $name) = @_;
91
92   $self->_dbh->do("RELEASE SAVEPOINT $name");
93 }
94
95 sub _exec_svp_rollback {
96   my ($self, $name) = @_;
97
98   $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
99 }
100
101 # http://www.firebirdfaq.org/faq223/
102 sub _get_server_version {
103   my $self = shift;
104
105   return $self->_get_dbh->selectrow_array(q{
106 SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION') FROM rdb$database
107   });
108 }
109
110 1;
111
112 =head1 CAVEATS
113
114 =over 4
115
116 =item *
117
118 C<last_insert_id> support by default only works for Firebird versions 2 or
119 greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should
120 work with earlier versions.
121
122 =back
123
124 =head1 AUTHOR
125
126 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
127
128 =head1 LICENSE
129
130 You may distribute this code under the same terms as Perl itself.
131
132 =cut
133 # vim:sts=2 sw=2: