File: DBD.pm

package info (click to toggle)
libdata-objectdriver-perl 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 740 kB
  • ctags: 472
  • sloc: perl: 3,529; sql: 60; makefile: 7
file content (203 lines) | stat: -rw-r--r-- 6,160 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# $Id$

package Data::ObjectDriver::Driver::DBD;
use strict;
use warnings;


sub new {
    my $class = shift;
    my($name) = @_;
    die "No Driver" unless $name;
    my $subclass = join '::', $class, $name;
    no strict 'refs';
    unless (%{"${subclass}::"}) {
        eval "use $subclass"; ## no critic
        die $@ if $@;
    }
    bless {}, $subclass;
}

sub init_dbh { }
sub bind_param_attributes { }
sub db_column_name { $_[2] }
sub fetch_id { }
sub offset_implemented { 1 }
sub map_error_code { }

# SQL doesn't define a function to ask a machine of its time in
# unixtime form.  MySQL does, so we override this in the subclass.
# but for sqlite and others, we assume "remote" time is same as local
# machine's time, which is especially true for sqlite.
sub sql_for_unixtime { return time() }

# by default, LIMIT isn't supported on a DELETE.  MySql overrides.
sub can_delete_with_limit { 0 }

# searches are case sensitive by default.  MySql overrides.
sub is_case_insensitive { 0 }

sub can_replace { 0 }

# Some drivers have problems with prepared caches
sub can_prepare_cached_statements { 1 };

sub sql_class { 'Data::ObjectDriver::SQL' }


1;

__END__

=head1 NAME

Data::ObjectDriver::Driver::DBD - base class for database drivers

=head1 SYNOPSIS

    package SomeObject;
    use base qw( Data::ObjectDriver::BaseObject );

    __PACKAGE__->install_properties({
        ...
        driver => Data::ObjectDriver::Driver::DBI->new(
            ...
            dbd => Data::ObjectDriver::Driver::DBD->new('mysql'),
        ),
    });

=head1 DESCRIPTION

I<Data::ObjectDriver::Driver::DBD> is the base class for I<database> drivers.
Database drivers handle the peculiarities of specific database servers to
provide an abstract API to the I<object> drivers. While database drivers
operate on queries and database concepts like the last insert ID and binding
attributes for a query, object drivers operate on objects higher level concepts
like caching and partitioning.

Database drivers are used with C<Data::ObjectDriver::Driver::DBI> object
drivers. If you are making an object driver that doesn't use C<DBI>, you do not
need a database driver; implement your custom behavior in your
C<Data::ObjectDriver> subclass directly.

=head1 USAGE

=head2 C<Data::ObjectDriver::Driver::DBD-E<gt>new($name)>

Creates a new database driver of the given subclass type. That is,
C<new('mysql')> would create a new instance of
C<Data::ObjectDriver::Driver::DBD::mysql>.

=head2 C<$dbd-E<gt>init_dbh($dbh)>

Initializes the given database handle connected to this driver's type of
database.

By default, no operation is performed. Override this method if your type of
database must do further initialization that other database servers don't need,
such as setting an operative time zone.

=head2 C<$dbd-E<gt>bind_param_attributes($type)>

Returns a hashref to pass to the C<DBI> statement method C<bind_param>, to
describe the SQL type of a column defined as C<$type> in an object class's
C<column_defs> mapping.

By default, no operation is performed. Override this method if your type of
database needs such type hinting for some fields, such as specifying a
parameter is a C<BLOB>.

Note that object classes must define your custom types in their C<column_defs>
mappings for those classes to be compatible with your C<DBD>. Make sure to
document any custom types you implement.

=head2 C<$dbd-E<gt>db_column_name($datasource, $column)>

Returns a decorated column name. For example, if your database requires column
names to be prepended with table names, that concatenation would occur in this
method.

By default, C<db_column_name> returns the column name unmodified.

=head2 C<$dbd-E<gt>fetch_id()>

Returns the autogenerated ID of the most recently inserted record, or C<undef>
if this feature is not supported.

By default, C<fetch_id> returns C<undef>.

=head2 C<$dbd-E<gt>map_error_code($code, $msg)>

Returns a C<Data::ObjectDriver::Errors> code for the given database error, or
C<undef> if no equivalent has been defined.

By default, C<map_error_code> returns C<undef> for every error.

=head2 C<$dbd-E<gt>sql_for_unixtime()>

Returns the SQL for querying the current UNIX time on the database server, or a
UNIX time to use as the remote time.

By default, C<sql_for_unixtime> returns the value of perl C<time()> on the
local machine.

=head2 C<$dbd-E<gt>offset_implemented()>

Returns true if the database this driver represents supports C<OFFSET> clauses.

By default, C<offset_implemented> returns true.

=head2 C<$dbd-E<gt>can_delete_with_limit()>

Returns true if the database this driver represents supports C<LIMIT> clauses
on C<DELETE> statements.

By default, C<can_delete_with_limit> returns false.

=head2 C<$dbd-E<gt>can_prepare_cached_statements()>

Returns true if the database this driver can cope with preparing a cached statement.

By default, C<can_delete_with_limit> returns true.

=head2 C<$dbd-E<gt>is_case_insensitive()>

Returns true if the database this driver represents normally compares string
fields case insensitively.

By default, C<is_case_insensitive> returns false.

=head2 C<$dbd-E<gt>can_replace()>

Returns true if the database this driver represents supports C<REPLACE INTO>
statements.

By default, C<can_replace> returns false.

=head2 C<$dbd-E<gt>force_no_prepared_cache()>

Returns false if the database this driver represents supports the
C<prepare_cached> method on its DBI database handles.

By default, C<force_no_prepared_cache> returns false.

=head2 C<$dbd-E<gt>sql_class()>

Provides the package name of the class responsible for representing SQL
queries. This method returns 'Data::ObjectDriver::SQL', but may be
overridden to return a package that has a similar interface but produces
SQL that is compatible with that DBD driver. The package provided must
already be loaded and available for use.

=head1 LICENSE

I<Data::ObjectDriver> is free software; you may redistribute it and/or modify
it under the same terms as Perl itself.

=head1 AUTHOR & COPYRIGHT

Except where otherwise noted, I<Data::ObjectDriver> is Copyright 2005-2006
Six Apart, cpan@sixapart.com. All rights reserved.

=cut