H.Merijn Brand > Tie-Hash-DBD > Tie::Array::DBD

Download:
Tie-Hash-DBD-0.13.tgz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.13   Source  

NAME ^

Tie::Array::DBD - tie a plain array to a database table

SYNOPSIS ^

  use DBI;
  use Tie::Array::DBD;

  my $dbh = DBI->connect ("dbi:Pg:", ...);

  tie my @array, "Tie::Array::DBD", "dbi:SQLite:dbname=db.tie";
  tie my @array, "Tie::Array::DBD", $dbh;
  tie my @array, "Tie::Array::DBD", $dbh, {
      tbl => "t_tie_analysis",
      key => "h_key",
      fld => "h_value",
      str => "Storable",
      };

  $array[42] = $value;  # INSERT
  $array[42] = 3;       # UPDATE
  delete $array[42];    # DELETE
  $value = $array[42];  # SELECT
  @array = ();          # CLEAR

  @array = (1..42);
  $array[-2] = 42;
  $_ = pop @array;
  push @array, $_;
  $_ = shift @array;
  unshift @array, $_;
  @a = splice @array, 2, -2, 5..9;
  @k = keys   @array;   # $] >= 5.011
  @v = values @array;   # $] >= 5.011

DESCRIPTION ^

This module ties an array to a database table using only an index and a value field. If no tables specification is passed, this will create a temporary table with h_key for the key field and a h_value for the value field.

I think it would make sense to merge the functionality that this module provides into Tie::DBI.

tie ^

The tie call accepts two arguments:

Database

The first argument is the connection specifier. This is either and open database handle or a DBI_DSN string.

If this argument is a valid handle, this module does not open a database all by itself, but uses the connection provided in the handle.

If the first argument is a scalar, it is used as DSN for DBI->connect ().

Supported DBD drivers include DBD::Pg, DBD::SQLite, DBD::CSV, DBD::mysql, DBD::Oracle, and DBD::Unify.

DBD::Pg and DBD::SQLite have an unexpected great performance when server is the local system. DBD::SQLite is even almost as fast as DB_File.

The current implementation appears to be extremely slow CSV, as expected, mysql, and Unify. For Unify and mysql that is because these do not allow indexing on the key field so they cannot be set to be primary key.

Options

The second argument is optional and should - if passed - be a hashref to options. The following options are recognized:

tbl

Defines the name of the table to be used. If none is passed, a new table is created with a unique name like t_tie_dbda_42253_1. When possible, the table is created as temporary. After the session, this table will be dropped.

If a table name is provided, it will be checked for existence. If found, it will be used with the specified key and fld. Otherwise it will be created with key and fld, but it will not be dropped at the end of the session.

key

Defines the name of the key field in the database table. The default is h_key.

fld

Defines the name of the value field in the database table. The default is h_value.

str

Defines the required persistence module. Currently only supports the use of Storable. The default is undefined. Passing unsupported streamer module names will be silently ignored.

Note that Storable does not support persistence of perl types CODE, REGEXP, IO, FORMAT, and GLOB.

If you want to preserve Encoding on the array values, you should use this feature.

Also note that this module does not yet support dynamic deep structures. See "Nesting and deep structues".

Encoding

Tie::Array::DBD stores values as binary data. This means that all Encoding and magic is lost when the data is stored, and thus is also not available when the data is restored, hence all internal information about the data is also lost, which includes the UTF8 flag.

If you want to preserve the UTF8 flag you will need to store internal flags and use the streamer option:

  tie my @array, "Tie::Array::DBD", { str => "Storable" };

Nesting and deep structures

Tie::Array::DBD stores values as binary data. This means that all structure is lost when the data is stored and not available when the data is restored. To maintain deep structures, use the streamer option:

  tie my @array, "Tie::Array::DBD", { str => "Storable" };

Note that changes inside deep structures do not work. See "TODO".

METHODS ^

drop ()

If a table was used with persistence, the table will not be dropped when the untie is called. Dropping can be forced using the drop method at any moment while the array is tied:

  (tied @array)->drop;

PREREQUISITES ^

The only real prerequisite is DBI but of course that uses the DBD driver of your choice. Some drivers are (very) actively maintained. Be sure to to use recent Modules. DBD::SQLite for example seems to require version 1.29 or up.

RESTRICTIONS and LIMITATIONS ^

TODO ^

Update on deep changes

Currently, nested structures do not get updated when it is an change in a deeper part.

  tie my @array, "Tie::Array::DBD", $dbh, { str => "Storable" };

  @array = (
      [ 1, "foo" ],
      [ 2, "bar" ],
      );

  $array[1][0]++; # No effect :(
Documentation

Better document what the implications are of storing data content in a database and restoring that. It will not be fool proof.

Mixins

Maybe: implement a feature that would enable plugins or mixins to do the streaming or preservation of other data attributes.

AUTHOR ^

H.Merijn Brand <h.m.brand@xs4all.nl>

COPYRIGHT AND LICENSE ^

Copyright (C) 2010-2014 H.Merijn Brand

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO ^

DBI, Tie::DBI, Tie::Hash, Tie::Hash::DBD, DBM::Deep

syntax highlighting: