DB_File-DB_Database
view release on metacpan or search on metacpan
lib/DB_File/DB_Database.pm view on Meta::CPAN
=item get_record_hash
the same
=back
=head2 Writing the data
=over 4
Basically like above, but do not specify the ID, leave it to DB_File::DB_Database.
The ID will be sequent numbers.
On success they return true -- the record ID. Index file is automatical updated, if needed.
=item set_record
the same, recommand to use for updating data
=item set_record_hash
the same, recommand to use for updating data
=item update_record_hash
the same
=item delete_record
the same
=item append_record
As parameters, takes the list of values of the fields. It append the record to the file.
Unspecified fields (if you pass less than you should) are set to undef/empty.
ID will be returned.
=item append_record_hash
Unspecified fields (if you pass less than you should) are set to undef/empty.
ID will be returned.
=back
Examples:
$table->append_record("Caroline", "20", "sister");
$table->append_record_hash('jimmy', "age" => 25,
"msg" => 'Nothing is easy!');
=head2 Using Index
=over 4
Index file is stored in DB_File BTREE. Once created, all index files will be automatically
opened when open the database file, and updated automatically when writing the database file.
=item create_index
Create index file for one field. Default permits of the index file is 0640. 'name' is the index
tag name, 'key' is the formula for indexing. For example:
'key' => 'Age' # index by the age, from young to old
'key' => '-Age' # index by the age, from old to young
'key' => '-Age(3)+Name' # index by the age(up to 999),then name; from old to young,then from A to Z
'key' => '-Age(3)+-Name' # index by the age(up to 999),then name; from old to young,then from Z to A
'Age(3)' is similar to 'substr(Age,0,3)', only the length of the last field name appeared in
the 'key' can be ommited. '+-' CAN'T be subsituded by '-'.
# Index File name will be dbexample_indexA
print $table->create_index( 'name' => 'indexA' ,
'key' => 'Age' , # '-Age' means reverse sort,
'permits'=> 0640 );
=item recreate_index
Recreate the index file. Parameter is the index name(s).
=item drop_index
Delete the index file. Parameter is the index name(s).
# delete Index indexA
print $table->drop_index('indexA');
=back
=head2 Select records
=over 4
Select matched records, using index will speed up the searching.
=item prepare_select
As parameters, pass a hash as parameters. Almost each value is a hash reference. Eg:
# find people aged form 10 to 25, select the first 10 people. their 'msg' must content 'hi'
$table->prepare_select( "seek" => {'index'=> 'indexA',
'from' => 10,
'to' => 25},
"where" => {'msg'=> 'hi'},
"top" => 10);
If no "seek" specified(do not use index), it will search from the first record to the last(or up to the record numbers you needed)."top" means select the first ? records. You may use "cut" instead, "cut" => [2,6] means select from the secord matched ...
for "seek", "from" is needed, "to" can be omitted(till the last).
To fetch the selected record. Use get_record, get_record_nf, get_record_hash, leave the ID undef.
=back
Examples of selecting record:
use DB_File::DB_Database;
my $table = new DB_File::DB_Database "dbexample" or die DB_File::DB_Database->errstr;
my $table = new XBase "names.dbf" or die XBase->errstr;
# find people aged form 10 to 25, select the first 10 people. their 'msg' must content 'hi'
$table->prepare_select( "seek" => {'index'=> 'indexA',
'from' => 10,
'to' => 25},
( run in 2.262 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )