📄 dbd::dbm.3
字号:
\& $dbh\->{dbm_lockfile} = \*(Aq.foo\*(Aq;\& $dbh\->{dbm_tables}\->{qux}\->{lockfile} = \*(Aq.foo\*(Aq;.Ve.PPIf you wish to disable locking, set the dbm_lockfile equal to 0..PP.Vb 3\& $dbh = DBI\->connect(\*(Aqdbi:DBM:lockfile=0\*(Aq);\& $dbh\->{dbm_lockfile} = 0;\& $dbh\->{dbm_tables}\->{qux}\->{lockfile} = 0;.Ve.Sh "Specifying the \s-1DBM\s0 type".IX Subsection "Specifying the DBM type"Each \*(L"flavor\*(R" of \s-1DBM\s0 stores its files in a different format and has different capabilities and different limitations. See AnyDBM_File for a comparison of \s-1DBM\s0 types..PPBy default, \s-1DBD::DBM\s0 uses the SDBM_File type of storage since SDBM_File comes with Perl itself. But if you have other types of \s-1DBM\s0 storage available, you can use any of them with \s-1DBD::DBM\s0 also..PPYou can specify the \s-1DBM\s0 type using the \*(L"dbm_type\*(R" attribute which can be set in the connection string or with the \f(CW$dbh\fR\->{dbm_type} attribute for global settings or with the \f(CW$dbh\fR\->{dbm_tables}\->{$table_name}\->{type} attribute for per-table settings in cases where a single script is accessing more than one kind of \s-1DBM\s0 file..PPIn the connection string, just set type=TYPENAME where \s-1TYPENAME\s0 is any \s-1DBM\s0 type such as GDBM_File, DB_File, etc. Do \fInot\fR use \s-1MLDBM\s0 as your dbm_type, that is set differently, see below..PP.Vb 2\& my $dbh=DBI\->connect(\*(Aqdbi:DBM:\*(Aq); # uses the default SDBM_File\& my $dbh=DBI\->connect(\*(Aqdbi:DBM:type=GDBM_File\*(Aq); # uses the GDBM_File.Ve.PPYou can also use \f(CW$dbh\fR\->{dbm_type} to set global \s-1DBM\s0 type:.PP.Vb 2\& $dbh\->{dbm_type} = \*(AqGDBM_File\*(Aq; # set the global DBM type\& print $dbh\->{dbm_type}; # display the global DBM type.Ve.PPIf you are going to have several tables in your script that come from different \s-1DBM\s0 types, you can use the \f(CW$dbh\fR\->{dbm_tables} hash to store different settings for the various tables. You can even use this to perform joins on files that have completely different storage mechanisms..PP.Vb 3\& my $dbh\->(\*(Aqdbi:DBM:type=GDBM_File\*(Aq);\& #\& # sets global default of GDBM_File\&\& my $dbh\->{dbm_tables}\->{foo}\->{type} = \*(AqDB_File\*(Aq;\& #\& # over\-rides the global setting, but only for the table called "foo"\&\& print $dbh\->{dbm_tables}\->{foo}\->{type};\& #\& # prints the dbm_type for the table "foo".Ve.Sh "Adding multi-column support with \s-1MLDBM\s0".IX Subsection "Adding multi-column support with MLDBM"Most of the \s-1DBM\s0 types only support two columns. However a \s-1CPAN\s0 module called \s-1MLDBM\s0 overcomes this limitation by allowing more than two columns. It does this by serializing the data \- basically it puts a reference to an array into the second column. It can also put almost any kind of Perl object or even Perl coderefs into columns..PPIf you want more than two columns, you must install \s-1MLDBM\s0. It's available for many platforms and is easy to install..PP\&\s-1MLDBM\s0 can use three different modules to serialize the column \- Data::Dumper, Storable, and FreezeThaw. Data::Dumper is the default, Storable is the fastest. \s-1MLDBM\s0 can also make use of user-defined serialization methods. All of this is available to you through \s-1DBD::DBM\s0 with just one attribute setting..PPTo use \s-1MLDBM\s0 with \s-1DBD::DBM\s0, you need to set the dbm_mldbm attribute to the name of the serialization module..PPSome examples:.PP.Vb 8\& $dbh=DBI\->connect(\*(Aqdbi:DBM:mldbm=Storable\*(Aq); # use MLDBM with Storable\& $dbh=DBI\->connect(\& \*(Aqdbi:DBM:mldbm=MySerializer\*(Aq # use MLDBM with a user defined module\& );\& $dbh\->{dbm_mldbm} = \*(AqMySerializer\*(Aq; # same as above\& print $dbh\->{dbm_mldbm} # show the MLDBM serializer\& $dbh\->{dbm_tables}\->{foo}\->{mldbm}=\*(AqData::Dumper\*(Aq; # set Data::Dumper for table "foo"\& print $dbh\->{dbm_tables}\->{foo}\->{mldbm}; # show serializer for table "foo".Ve.PP\&\s-1MLDBM\s0 works on top of other \s-1DBM\s0 modules so you can also set a \s-1DBM\s0 type along with setting dbm_mldbm. The examples above would default to using SDBM_File with \s-1MLDBM\s0. If you wanted GDBM_File instead, here's how:.PP.Vb 3\& $dbh = DBI\->connect(\*(Aqdbi:DBM:type=GDBM_File;mldbm=Storable\*(Aq);\& #\& # uses GDBM_File with MLDBM and Storable.Ve.PPSDBM_File, the default file type is quite limited, so if you are going to use \s-1MLDBM\s0, you should probably use a different type, see AnyDBM_File..PPSee below for some \*(L"\s-1GOTCHAS\s0 \s-1AND\s0 \s-1WARNINGS\s0\*(R" about \s-1MLDBM\s0..Sh "Support for Berkeley \s-1DB\s0".IX Subsection "Support for Berkeley DB"The Berkeley \s-1DB\s0 storage type is supported through two different Perl modules \- DB_File (which supports only features in old versions of Berkeley \s-1DB\s0) and BerkeleyDB (which supports all versions). \s-1DBD::DBM\s0 supports specifying either \*(L"DB_File\*(R" or \*(L"BerkeleyDB\*(R" as a \fIdbm_type\fR, with or without \s-1MLDBM\s0 support..PPThe \*(L"BerkeleyDB\*(R" dbm_type is experimental and its interface is likely to chagne. It currently defaults to BerkeleyDB::Hash and does not currently support ::Btree or ::Recno..PPWith BerkeleyDB, you can specify initialization flags by setting them in your script like this:.PP.Vb 9\& my $dbh = DBI\->connect(\*(Aqdbi:DBM:type=BerkeleyDB;mldbm=Storable\*(Aq);\& use BerkeleyDB;\& my $env = new BerkeleyDB::Env \-Home => $dir; # and/or other Env flags\& $dbh\->{dbm_berkeley_flags} = {\& \*(AqDB_CREATE\*(Aq => DB_CREATE # pass in constants\& , \*(AqDB_RDONLY\*(Aq => DB_RDONLY # pass in constants\& , \*(Aq\-Cachesize\*(Aq => 1000 # set a ::Hash flag\& , \*(Aq\-Env\*(Aq => $env # pass in an environment\& };.Ve.PPDo \fInot\fR set the \-Flags or \-Filename flags, those are determined by the \s-1SQL\s0 (e.g. \-Flags => \s-1DB_RDONLY\s0 is set automatically when you issue a \s-1SELECT\s0 statement)..PPTime has not permitted me to provide support in this release of \s-1DBD::DBM\s0 for further Berkeley \s-1DB\s0 features such as transactions, concurrency, locking, etc. I will be working on these in the future and would value suggestions, patches, etc..PPSee DB_File and BerkeleyDB for further details..Sh "Supported \s-1SQL\s0 syntax".IX Subsection "Supported SQL syntax"\&\s-1DBD::DBM\s0 uses a subset of \s-1SQL\s0. The robustness of that subset depends on what other modules you have installed. Both options support basic \s-1SQL\s0 operations including \s-1CREATE\s0 \s-1TABLE\s0, \s-1DROP\s0 \s-1TABLE\s0, \s-1INSERT\s0, \s-1DELETE\s0, \s-1UPDATE\s0, and \s-1SELECT\s0..PP\&\fBOption #1:\fR By default, this module inherits its \s-1SQL\s0 support from DBI::SQL::Nano that comes with \s-1DBI\s0. Nano is, as its name implies, a *very* small \s-1SQL\s0 engine. Although limited in scope, it is faster than option #2 for some operations. See DBI::SQL::Nano for a description of the \s-1SQL\s0 it supports and comparisons of it with option #2..PP\&\fBOption #2:\fR If you install the pure Perl \s-1CPAN\s0 module SQL::Statement, \s-1DBD::DBM\s0 will use it instead of Nano. This adds support for table aliases, for functions, for joins, and much more. If you're going to use \s-1DBD::DBM\s0 for anything other than very simple tables and queries, you should install SQL::Statement. You don't have to change \s-1DBD::DBM\s0 or your scripts in any way, simply installing SQL::Statement will give you the more robust \s-1SQL\s0 capabilities without breaking scripts written for DBI::SQL::Nano. See SQL::Statement for a description of the \s-1SQL\s0 it supports..PPTo find out which \s-1SQL\s0 module is working in a given script, you can use the \fIdbm_versions()\fR method or, if you don't need the full output and version numbers, just do this:.PP.Vb 1\& print $dbh\->{sql_handler};.Ve.PPThat will print out either \*(L"SQL::Statement\*(R" or \*(L"DBI::SQL::Nano\*(R"..Sh "Optimizing use of key fields".IX Subsection "Optimizing use of key fields"Most \*(L"flavors\*(R" of \s-1DBM\s0 have only two physical columns (but can contain multiple logical columns as explained below). They work similarly to a Perl hash with the first column serving as the key. Like a Perl hash, \s-1DBM\s0 files permit you to do quick lookups by specifying the key and thus avoid looping through all records. Also like a Perl hash, the keys must be unique. It is impossible to create two records with the same key. To put this all more simply and in \s-1SQL\s0 terms, the key column functions as the \s-1PRIMARY\s0 \s-1KEY\s0..PPIn \s-1DBD::DBM\s0, you can take advantage of the speed of keyed lookups by using a \s-1WHERE\s0 clause with a single equal comparison on the key field. For example, the following \s-1SQL\s0 statements are optimized for keyed lookup:.PP.Vb 4\& CREATE TABLE user ( user_name TEXT, phone TEXT);\& INSERT INTO user VALUES (\*(AqFred Bloggs\*(Aq,\*(Aq233\-7777\*(Aq);\& # ... many more inserts\& SELECT phone FROM user WHERE user_name=\*(AqFred Bloggs\*(Aq;.Ve.PPThe \*(L"user_name\*(R" column is the key column since it is the first column. The \s-1SELECT\s0 statement uses the key column in a single equal comparision \- "user_name='Fred Bloggs' \- so the search will find it very quickly without having to loop through however many names were inserted into the table..PPIn contrast, thes searches on the same table are not optimized:.PP.Vb 2\& 1. SELECT phone FROM user WHERE user_name < \*(AqFred\*(Aq;\& 2. SELECT user_name FROM user WHERE phone = \*(Aq233\-7777\*(Aq;.Ve.PPIn #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 \*(L"user_name\*(R" is not specified in the \s-1WHERE\s0 clause, and therefore the search will need to loop through all rows to find the desired result..Sh "Specifying Column Names".IX Subsection "Specifying Column Names"\&\s-1DBM\s0 files don't have a standard way to store column names. \s-1DBD::DBM\s0 gets around this issue with a \s-1DBD::DBM\s0 specific way of storing the column names. \fBIf you are working only with \s-1DBD::DBM\s0 and not using files created by or accessed with other \s-1DBM\s0 programs, you can ignore this section.\fR.PP\&\s-1DBD::DBM\s0 stores column names as a row in the file with the key \fI_metadata \e0\fR. So this code.PP.Vb 3\& my $dbh = DBI\->connect(\*(Aqdbi:DBM:\*(Aq);\& $dbh\->do("CREATE TABLE baz (foo CHAR(10), bar INTEGER)");\& $dbh\->do("INSERT INTO baz (foo,bar) VALUES (\*(Aqzippy\*(Aq,1)");.Ve.PPWill create a file that has a structure something like this:.PP.Vb 2\& _metadata \e0 | foo,bar\& zippy | 1.Ve.PPThe next time you access this table with \s-1DBD::DBM\s0, it will treat the _metadata row as a header rather than as data and will pull the column names from there. However, if you access the file with something other than \s-1DBD::DBM\s0, the row will be treated as a regular data row..PPIf you do not want the column names stored as a data row in the table you can set the \fIdbm_store_metadata\fR attribute to 0..PP.Vb 1\& my $dbh = DBI\->connect(\*(Aqdbi:DBM:store_metadata=0\*(Aq);.Ve.PPor.PP.Vb 1\& $dbh\->{dbm_store_metadata} = 0;.Ve.PPor, for per-table setting.PP.Vb 1\& $dbh\->{dbm_tables}\->{qux}\->{store_metadata} = 0;.Ve.PPBy default, \s-1DBD::DBM\s0 assumes that you have two columns named \*(L"k\*(R" and \*(L"v\*(R" (short for \*(L"key\*(R" and \*(L"value\*(R"). So if you have \fIdbm_store_metadata\fR set to 1 and you want to use alternate column names, you need to specify the column names like this:.PP.Vb 1\& my $dbh = DBI\->connect(\*(Aqdbi:DBM:store_metadata=0;cols=foo,bar\*(Aq);.Ve.PPor.PP.Vb 2\& $dbh\->{dbm_store_metadata} = 0;\& $dbh\->{dbm_cols} = \*(Aqfoo,bar\*(Aq;.Ve.PPTo set the column names on per-table basis, do this:.PP.Vb 4\& $dbh\->{dbm_tables}\->{qux}\->{store_metadata} = 0;\& $dbh\->{dbm_tables}\->{qux}\->{cols} = \*(Aqfoo,bar\*(Aq;\& #\& # sets the column names only for table "qux".Ve.PPIf you have a file that was created by another \s-1DBM\s0 program or created with \fIdbm_store_metadata\fR set to zero and you want to convert it to using \s-1DBD::DBM\s0's column name storage, just use one of the methods above to name the columns but *without* specifying \fIdbm_store_metadata\fR as zero. You only have to do that once \- thereafter you can get by without setting either \fIdbm_store_metadata\fR or setting \fIdbm_cols\fR because the names will be stored in the file..Sh "Statement handle ($sth) attributes and methods".IX Subsection "Statement handle ($sth) attributes and methods"Most statement handle attributes such as \s-1NAME\s0, \s-1NUM_OF_FIELDS\s0, etc. are available only after an execute. The same is true of \f(CW$sth\fR\->rows which is available after the execute but does \fInot\fR require a fetch..ie n .Sh "The $dbh\fP\->\fIdbm_versions() method".el .Sh "The \f(CW$dbh\fP\->\fIdbm_versions()\fP method".IX Subsection "The $dbh->dbm_versions() method"The private method \fIdbm_versions()\fR presents a summary of what other modules are being used at any given time. \s-1DBD::DBM\s0 can work with or without many other modules \- it can use either SQL::Statement or DBI::SQL::Nano as its \s-1SQL\s0 engine, it can be run with \s-1DBI\s0 or DBI::PurePerl, it can use many kinds of \s-1DBM\s0 modules, and many kinds of serializers when run with \s-1MLDBM\s0. The \fIdbm_versions()\fR method reports on all of that and more..PP.Vb 2\& print $dbh\->dbm_versions; # displays global settings\& print $dbh\->dbm_versions($table_name); # displays per table settings.Ve.PPAn important thing to note about this method is that when called with no arguments, it displays the *global* settings. If you over-ride these by setting per-table attributes, these will \fInot\fR be shown unless you specifiy a table name as an argument to the method call..Sh "Storing Objects".IX Subsection "Storing Objects"If you are using \s-1MLDBM\s0, you can use \s-1DBD::DBM\s0 to take advantage of its serializing abilities to serialize any Perl object that \s-1MLDBM\s0 can handle. To store objects in columns, you should (but don't absolutely need to) declare it as a column of type \s-1BLOB\s0 (the type is *currently* ignored by the \s-1SQL\s0 engine, but heh, it's good form)..PPYou *must* use placeholders to insert or refer to the data..SH "GOTCHAS AND WARNINGS".IX Header "GOTCHAS AND WARNINGS"Using the \s-1SQL\s0 \s-1DROP\s0 command will remove any file that has the name specified in the command with either '.pag' or '.dir' or your {dbm_ext} appended to it. Sothis be dangerous if you aren't sure what file it refers to:.PP.Vb 1\& $dbh\->do(qq{DROP TABLE "/path/to/any/file"});.Ve.PPEach \s-1DBM\s0 type has limitations. SDBM_File, for example, can only store values of less than 1,000 characters. *You* as the script author must ensure that you don't exceed those bounds. If you try to insert a value that is bigger than the \s-1DBM\s0 can store, the results will be unpredictable. See the documentation for whatever \s-1DBM\s0 you are using for details..PPDifferent \s-1DBM\s0 implementations return records in different orders. That means that you can \fInot\fR depend on the order of records unless you use an \s-1ORDER\s0 \s-1BY\s0 statement. DBI::SQL::Nano does not currently support \s-1ORDER\s0 \s-1BY\s0 (though it may soon) so if you need ordering, you'll have to install SQL::Statement..PP\&\s-1DBM\s0 data files are platform-specific. To move them from one platform to another, you'll need to do something along the lines of dumping your data to \s-1CSV\s0 on platform #1 and then dumping from \s-1CSV\s0 to \s-1DBM\s0 on platform #2. DBD::AnyData and \s-1DBD::CSV\s0 can help with that. There may also be \s-1DBM\s0 conversion tools for your platforms which would probably be quickest..PPWhen using \s-1MLDBM\s0, there is a very powerful serializer \- it will allow you to store Perl code or objects in database columns. When these get de-serialized, they may be evaled \- in other words \s-1MLDBM\s0 (or actually Data::Dumper when used by \s-1MLDBM\s0) may take the values and try to execute them in Perl. Obviously, this can present dangers, so if you don't know what's in a file, be careful before you access it with \s-1MLDBM\s0 turned on!.PPSee the entire section on \*(L"Table locking and \fIflock()\fR\*(R" for gotchas and warnings about the use of \fIflock()\fR..SH "GETTING HELP, MAKING SUGGESTIONS, AND REPORTING BUGS".IX Header "GETTING HELP, MAKING SUGGESTIONS, AND REPORTING BUGS"If you need help installing or using \s-1DBD::DBM\s0, please write to the \s-1DBI\s0 users mailing list at dbi\-users@perl.org or to the comp.lang.perl.modules newsgroup on usenet. I'm afraid I can't always answer these kinds of questions quickly and there are many on the mailing list or in the newsgroup who can..PPIf you have suggestions, ideas for improvements, or bugs to report, please write me directly at the email shown below..PPWhen reporting bugs, please send the output of \f(CW$dbh\fR\->dbm_versions($table) for a table that exhibits the bug and, if possible, as small a sample as you can make of the code that produces the bug. And of course, patches are welcome too :\-)..SH "ACKNOWLEDGEMENTS".IX Header "ACKNOWLEDGEMENTS"Many, many thanks to Tim Bunce for prodding me to write this, and for copious, wise, and patient suggestions all along the way..SH "AUTHOR AND COPYRIGHT".IX Header "AUTHOR AND COPYRIGHT"This module is written and maintained by.PPJeff Zucker < jzucker \s-1AT\s0 cpan.org >.PPCopyright (c) 2004 by Jeff Zucker, all rights reserved..PPYou may freely distribute and/or modify this module under the terms of either the \s-1GNU\s0 General Public License (\s-1GPL\s0) or the Artistic License, as specified in the Perl \s-1README\s0 file..SH "SEE ALSO".IX Header "SEE ALSO"\&\s-1DBI\s0, SQL::Statement, DBI::SQL::Nano, AnyDBM_File, \s-1MLDBM\s0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -