DBD::DBM (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
DBD::DBM - a DBI driver for DBM & MLDBM filesSYNOPSIS
use DBI; $dbh = DBI->connect('dbi:DBM:'); # defaults to SDBM_File $dbh = DBI->connect('DBI:DBM(RaiseError=1):'); # defaults to SDBM_File $dbh = DBI->connect('dbi:DBM:dbm_type=DB_File'); # defaults to DB_File $dbh = DBI->connect('dbi:DBM:dbm_mldbm=Storable'); # MLDBM with SDBM_File # or $dbh = DBI->connect('dbi:DBM:', undef, undef); $dbh = DBI->connect('dbi:DBM:', undef, undef, { f_ext => '.db/r', f_dir => '/path/to/dbfiles/', f_lockfile => '.lck', dbm_type => 'BerkeleyDB', dbm_mldbm => 'FreezeThaw', dbm_store_metadata => 1, dbm_berkeley_flags => { '-Cachesize' => 1000, # set a ::Hash flag }, });
and other variations on connect() as shown in the
Use standard
DESCRIPTION
The module uses a
QUICK START
You should be aware that there are several options for the
But here's a sample to get you started.
use DBI; my $dbh = DBI->connect('dbi:DBM:'); $dbh->{RaiseError} = 1; for my $sql( split /;\n+/," CREATE TABLE user ( user_name TEXT, phone TEXT ); INSERT INTO user VALUES ('Fred Bloggs','233-7777'); INSERT INTO user VALUES ('Sanjay Patel','777-3333'); INSERT INTO user VALUES ('Junk','xxx-xxxx'); DELETE FROM user WHERE user_name = 'Junk'; UPDATE user SET phone = '999-4444' WHERE user_name = 'Sanjay Patel'; SELECT * FROM user "){ my $sth = $dbh->prepare($sql); $sth->execute; $sth->dump_results if $sth->{NUM_OF_FIELDS}; } $dbh->disconnect;
USAGE
This section will explain some usage cases in more detail. To get an overview about the available attributes, see ``Metadata''.Specifying Files and Directories
- .pag/r
-
Chosen for dbm_type "SDBM_File", "ODBM_File" and "NDBM_File"
when an implementation is detected which wraps "-ldbm" for
"NDBM_File" (e.g. Solaris, AIX, ...).
For those types, the ".dir" extension is recognized, too (for being deleted when dropping a table).
- .db/r
-
Chosen for dbm_type "NDBM_File" when an implementation is detected
which wraps BerkeleyDB 1.x for "NDBM_File" (typically BSD's, Darwin).
"GDBM_File", "DB_File" and "BerkeleyDB" don't usually use a file extension.
If your
$dbh = DBI->connect('dbi:DBM:f_ext=.db'); # .db extension is used $dbh = DBI->connect('dbi:DBM:f_ext='); # no extension is used # or $dbh->{f_ext}='.db'; # global setting $dbh->{f_meta}->{'qux'}->{f_ext}='.db'; # setting for table 'qux'
By default files are assumed to be in the current working directory. To use other directories specify the f_dir attribute in either the connect string or by setting the database handle attribute.
For example, this will look for the file /foo/bar/fruit (or /foo/bar/fruit.pag for
my $dbh = DBI->connect('dbi:DBM:f_dir=/foo/bar'); # and this will too: my $dbh = DBI->connect('dbi:DBM:'); $dbh->{f_dir} = '/foo/bar'; # but this is recommended my $dbh = DBI->connect('dbi:DBM:', undef, undef, { f_dir => '/foo/bar' } ); # now you can do my $ary = $dbh->selectall_arrayref(q{ SELECT x FROM fruit });
You can also use delimited identifiers to specify paths directly in
my $dbh = DBI->connect('dbi:DBM:'); my $ary = $dbh->selectall_arrayref(q{ SELECT x FROM "/foo/bar/fruit" });
You can also tell
$dbh->{dbm_tables}->{f}->{file} = q(/foo/bar/fruit);
Please be aware that you cannot specify this during connection.
If you have SQL::Statement installed, you can use table aliases:
my $dbh = DBI->connect('dbi:DBM:'); my $ary = $dbh->selectall_arrayref(q{ SELECT f.x FROM "/foo/bar/fruit" AS f });
See the ``
Table locking and flock()
Table locking is accomplished using a lockfile which has the same basename as the table's file but with the file extension '.lck' (or a lockfile extension that you supply, see below). This lock file is created with the table during aSince the locking depends on flock(), it only works on operating systems that support flock(). In cases where flock() is not implemented,
Even on those systems that do support flock(), locking is only advisory - as is always the case with flock(). This means that if another program tries to access the table file while
Specifying the DBM type
Each ``flavor'' of By default,
You can specify the
In the connection string, just set "dbm_type=TYPENAME" where "TYPENAME" is any
my $dbh=DBI->connect('dbi:DBM:'); # uses the default SDBM_File my $dbh=DBI->connect('dbi:DBM:dbm_type=GDBM_File'); # uses the GDBM_File # You can also use $dbh->{dbm_type} to set the DBM type for the connection: $dbh->{dbm_type} = 'DB_File'; # set the global DBM type print $dbh->{dbm_type}; # display the global DBM type
If you have several tables in your script that use different
# sets global default of GDBM_File my $dbh->('dbi:DBM:type=GDBM_File'); # overrides the global setting, but only for the tables called # I<foo> and I<bar> my $dbh->{f_meta}->{foo}->{dbm_type} = 'DB_File'; my $dbh->{f_meta}->{bar}->{dbm_type} = 'BerkeleyDB'; # prints the dbm_type for the table "foo" print $dbh->{f_meta}->{foo}->{dbm_type};
Note that you must change the dbm_type of a table before you access it for first time.
Adding multi-column support with MLDBM
Most of the If you want more than two columns, you must install
Some examples:
$dbh=DBI->connect('dbi:DBM:dbm_mldbm=Storable'); # use MLDBM with Storable $dbh=DBI->connect( 'dbi:DBM:dbm_mldbm=MySerializer' # use MLDBM with a user defined module ); $dbh=DBI->connect('dbi::dbm:', undef, undef, { dbm_mldbm => 'YAML' }); # use 3rd party serializer $dbh->{dbm_mldbm} = 'YAML'; # same as above print $dbh->{dbm_mldbm} # show the MLDBM serializer $dbh->{f_meta}->{foo}->{dbm_mldbm}='Data::Dumper'; # set Data::Dumper for table "foo" print $dbh->{f_meta}->{foo}->{mldbm}; # show serializer for table "foo"
# uses DB_File with MLDBM and Storable $dbh = DBI->connect('dbi:DBM:', undef, undef, { dbm_type => 'DB_File', dbm_mldbm => 'Storable', });
SDBM_File, the default dbm_type is quite limited, so if you are going to use
See below for some ``
Support for Berkeley DB
The Berkeley The ``BerkeleyDB'' dbm_type is experimental and it's interface is likely to change. It currently defaults to BerkeleyDB::Hash and does not currently support ::Btree or ::Recno.
With BerkeleyDB, you can specify initialization flags by setting them in your script like this:
use BerkeleyDB; my $env = new BerkeleyDB::Env -Home => $dir; # and/or other Env flags $dbh = DBI->connect('dbi:DBM:', undef, undef, { dbm_type => 'BerkeleyDB', dbm_mldbm => 'Storable', dbm_berkeley_flags => { 'DB_CREATE' => DB_CREATE, # pass in constants 'DB_RDONLY' => DB_RDONLY, # pass in constants '-Cachesize' => 1000, # set a ::Hash flag '-Env' => $env, # pass in an environment }, });
Do not set the -Flags or -Filename flags as those are determined and overwritten by the
Time has not permitted us to provide support in this release of
See DB_File and BerkeleyDB for further details.
Optimizing the use of key fields
Most ``flavors'' ofIn
CREATE TABLE user ( user_name TEXT, phone TEXT); INSERT INTO user VALUES ('Fred Bloggs','233-7777'); # ... many more inserts SELECT phone FROM user WHERE user_name='Fred Bloggs';
The ``user_name'' column is the key column since it is the first column. The
In contrast, these searches on the same table are not optimized:
1. SELECT phone FROM user WHERE user_name < 'Fred'; 2. SELECT user_name FROM user WHERE phone = '233-7777';
In #1, the operation uses a less-than (<) comparison rather than an equals comparison, so it will not be optimized for key searching. In #2, the key field ``user_name'' is not specified in the
Note that the underlying
Even if lookups are faster by around 50%, DBI::SQL::Nano and SQL::Statement can benefit from the key field optimizations on updating and deleting rows - and here the improved where clause evaluation of SQL::Statement might beat DBI::SQL::Nano every time the where clause contains not only the key field (or more than one).
Supported SQL syntax
Option #1: By default, this module inherits its
Option #2: If you install the pure Perl
To find out which
print $dbh->{sql_handler}, "\n";
That will print out either ``SQL::Statement'' or ``DBI::SQL::Nano''.
Baring the section about optimized access to the
# DBI::SQL::Nano is faster $sth = $dbh->prepare( "update foo set value='new' where key=15" ); $sth->execute(); $sth = $dbh->prepare( "delete from foo where key=27" ); $sth->execute(); $sth = $dbh->prepare( "select * from foo where key='abc'" ); # SQL::Statement might faster (depending on DB size) $sth = $dbh->prepare( "update foo set value='new' where key=?" ); $sth->execute(15); $sth = $dbh->prepare( "update foo set value=? where key=15" ); $sth->execute('new'); $sth = $dbh->prepare( "delete from foo where key=?" ); $sth->execute(27); # SQL::Statement is faster $sth = $dbh->prepare( "update foo set value='new' where value='old'" ); $sth->execute(); # must be expressed using "where key = 15 or key = 27 or key = 42 or key = 'abc'" # in DBI::SQL::Nano $sth = $dbh->prepare( "delete from foo where key in (15,27,42,'abc')" ); $sth->execute(); # must be expressed using "where key > 10 and key < 90" in DBI::SQL::Nano $sth = $dbh->prepare( "select * from foo where key between (10,90)" ); $sth->execute(); # only SQL::Statement can handle $sth->prepare( "select * from foo,bar where foo.name = bar.name" ); $sth->execute(); $sth->prepare( "insert into foo values ( 1, 'foo' ), ( 2, 'bar' )" ); $sth->execute();
Specifying Column Names
my $dbh = DBI->connect('dbi:DBM:'); $dbh->do("CREATE TABLE baz (foo CHAR(10), bar INTEGER)"); $dbh->do("INSERT INTO baz (foo,bar) VALUES ('zippy',1)");
Will create a file that has a structure something like this:
_metadata \0 | <dbd_metadata><schema></schema><col_names>foo,bar</col_names></dbd_metadata> zippy | 1
The next time you access this table with
If you do not want the column names stored as a data row in the table you can set the dbm_store_metadata attribute to 0.
my $dbh = DBI->connect('dbi:DBM:', undef, undef, { dbm_store_metadata => 0 }); # or $dbh->{dbm_store_metadata} = 0; # or for per-table setting $dbh->{f_meta}->{qux}->{dbm_store_metadata} = 0;
By default,
my $dbh = DBI->connect('dbi:DBM:', undef, undef, { dbm_store_metadata => 0, dbm_cols => [ qw(foo bar) ], }); # or $dbh->{dbm_store_metadata} = 0; $dbh->{dbm_cols} = 'foo,bar'; # or to set the column names on per-table basis, do this: # sets the column names only for table "qux" $dbh->{f_meta}->{qux}->{dbm_store_metadata} = 0; $dbh->{f_meta}->{qux}->{col_names} = [qw(foo bar)];
If you have a file that was created by another
DBI database handle attributes
Metadata
Statement handle ($sth) attributes and methodsMost statement handle attributes such as
Driver handle ($dbh) attributes
It is not supported anymore to use dbm-attributes without the dbm_-prefix. Currently, if an
dbm_cols
Contains a comma separated list of column names or an array reference to the column names.
dbm_type
Contains the
dbm_mldbm
Contains the serializer for
- Data::Dumper
- Default serializer. Deployed with Perl core.
- Storable
- Faster serializer. Deployed with Perl core.
- FreezeThaw
- Pure Perl serializer, requires FreezeThaw to be installed.
- YAML
-
Portable serializer (between languages but not architectures).
Requires YAML::MLDBMinstallation.
- JSON
- Portable, fast serializer (between languages but not architectures). Requires MLDBM::Serializer::JSON installation.
dbm_store_metadata
Boolean value which determines if the metadata in
dbm_berkeley_flags
Hash reference with additional flags for BerkeleyDB::Hash instantiation.
dbm_version
Readonly attribute containing the version of
f_meta
In addition to the attributes DBD::File recognizes,
dbm_tables
This attribute provides restricted access to the table meta data. See f_meta and ``f_meta'' in DBD::File for attribute details.
dbm_tables is a tied hash providing the internal table names as keys (accessing unknown tables might create an entry) and their meta data as another tied hash. The table meta storage is obtained via the "get_table_meta" method from the table implementation (see DBD::File::Developers). Attribute setting and getting within the table meta data is handled via the methods "set_table_meta_attr" and "get_table_meta_attr".
Following attributes are no longer handled by
dbm_ext
This attribute is silently mapped to DBD::File's attribute f_ext. Later versions of
dbm_lockfile
This attribute is silently mapped to DBD::File's attribute f_lockfile. Later versions of
DBI database handle methods
The $dbh->dbm_versions() method
The private method dbm_versions() returns a summary of what other modules are being used at any given time.
print $dbh->dbm_versions; # displays global settings print $dbh->dbm_versions($table_name); # displays per table settings
An important thing to note about this method is that when it called with no arguments, it displays the *global* settings. If you override these by setting per-table attributes, these will not be shown unless you specify a table name as an argument to the method call.
Storing Objects
If you are usingEXTENSIBILITY
- SQL::Statement
-
Improved SQLengine compared to the built-in DBI::SQL::Nano - see ``SupportedSQLsyntax''.
- DB_File
-
Berkeley DBversion 1. This database library is available on many systems without additional installation and most systems are supported.
- GDBM_File
-
Simple dbm type (comparable to "DB_File") under the GNUlicense. Typically not available (or requires extra installation) on non-GNU operating systems.
- BerkeleyDB
-
Berkeley DBversion up to v4 (and maybe higher) - requires additional installation but is easier than GDBM_File on non-GNU systems.
db4 comes with a many tools which allow repairing and migrating databases. This is the recommended dbm type for production use.
- MLDBM
- Serializer wrapper to support more than one column for the files. Comes with serializers using "Data::Dumper", "FreezeThaw" and "Storable".
- YAML::MLDBM
-
Additional serializer for MLDBM. YAMLis very portable between languages.
- MLDBM::Serializer::JSON
-
Additional serializer for MLDBM. JSONis very portable between languages, probably more thanYAML.
GOTCHAS AND WARNINGS
Using the
$dbh->do(qq{DROP TABLE "/path/to/any/file"});
Each
Different
When using
See the entire section on ``Table locking and flock()'' for gotchas and warnings about the use of flock().
BUGS AND LIMITATIONS
This module uses hash interfaces of two column file databases. While none of supported
$sth->do( "insert into foo values (1, 'hello')" ); # this statement does ... $sth->do( "update foo set v='world' where k=1" ); # ... the same as this statement $sth->do( "insert into foo values (1, 'world')" );
This is considered to be a bug and might change in a future release.
Known affected dbm types are "ODBM_File" and "NDBM_File". We highly recommended you use a more modern dbm type such as "DB_File".
GETTING HELP, MAKING SUGGESTIONS, AND REPORTING BUGS
If you need help installing or using
If you have suggestions, ideas for improvements, or bugs to report, please report a bug as described in
When reporting bugs, please send the output of $dbh->dbm_versions($table) for a table that exhibits the bug and as small a sample as you can make of the code that produces the bug. And of course, patches are welcome, too :-).
If you need enhancements quickly, you can get commercial support as described at <dbi.perl.org/support> or you can contact Jens Rehsack at rehsack@cpan.org for commercial support in Germany.
Please don't bother Jochen Wiedmann or Jeff Zucker for support - they handed over further maintenance to H.Merijn Brand and Jens Rehsack.
ACKNOWLEDGEMENTS
Many, many thanks to Tim Bunce for prodding me to write this, and for copious, wise, and patient suggestions all along the way. (Jeff Zucker)I send my thanks and acknowledgements to H.Merijn Brand for his initial refactoring of DBD::File and his strong and ongoing support of SQL::Statement. Without him, the current progress would never have been made. And I have to name Martin J. Evans for each laugh (and correction) of all those funny word creations I (as non-native speaker) made to the documentation. And - of course - I have to thank all those unnamed contributors and testers from the Perl community. (Jens Rehsack)
AUTHOR AND COPYRIGHT
This module is written by Jeff Zucker < jzucker
Copyright (c) 2004 by Jeff Zucker, all rights reserved. Copyright (c) 2010-2013 by Jens Rehsack & H.Merijn Brand, all rights reserved.
You may freely distribute and/or modify this module under the terms of either the