Workflow::Persister::DBI - Persist workflow and history to DBI database
<persister name="MainDatabase" class="Workflow::Persister::DBI" dsn="DBI:mysql:database=workflows" user="wf" password="mypass"/> <persister name="BackupDatabase" class="Workflow::Persister::DBI" dsn="DBI:Pg:dbname=workflows" user="wf" password="mypass" workflow_table="wf" workflow_sequence="wf_seq" history_table="wf_history" history_sequence="wf_history_seq"/>
Main persistence class for storing the workflow and workflow history records to a DBI-accessible datasource.
A common need to create a subclass is to use a database handle created with other means. For instance, OpenInteract has a central configuration file for defining datasources, and the datasource will be available in a predictable manner. So we can create a subclass to provide the database handle on demand from the CTX object available from everywhere. A sample implementation is below. (Note that in real life we would just use SPOPS for this, but it is still a good example.)
CTX
package Workflow::Persister::DBI::OpenInteractHandle; use strict; use base qw( Workflow::Persister::DBI ); use OpenInteract2::Context qw( CTX ); my @FIELDS = qw( datasource_name ); __PACKAGE__->mk_accessors( @FIELDS ); # override parent method, assuming that we set the 'datasource' # parameter in the persister declaration sub init { my ( $self, $params ) = @_; $self->datasource_name( $params->{datasource} ); my $ds_config = CTX->lookup_datasource_config( $self->datasource_name ); # delegate the other assignment tasks to the parent class $self->assign_generators( $ds_config->{driver_name}, $params ); $self->assign_tables( $params ); } sub handle { my ( $self ) = @_; return CTX->datasource( $self->datasource_name ); }
Earlier versions of Workflow used the field 'user' to record in the history the user making a state change or comment. Unfortunately 'user' is a reserved word in our favorite database, PostgreSQL. (Oops.) So in addition to changing the field to an assuredly-unreserved word (workflow_user), we made the fieldnames customizable by subclasses.
Just override either or both of the methods:
Return list of fields in this order:
workflow_id, type, state, last_update
workflow_hist_id, workflow_id, action, description, state, workflow_user, history_date
Note that we may cache the results, so don't try and do anything weird like change the fieldnames based on the workflow user or something...
All public methods are inherited from Workflow::Persister.
Create a database handle from the given parameters. You are only required to provide 'dsn', which is the full DBI DSN you normally use as the first argument to connect().
connect()
You may also use:
Name of user to login with.
Password for user to login with.
user
Date format to use when working with the database. Accepts a format string that can be processed by the DateTime module. See http://search.cpan.org/~drolsky/DateTime-0.39/lib/DateTime.pm#strftime_Specifiers for the format options.
The default is '%Y-%m-%d %H:%M' for backward compatibility.
Table to use for persisting workflow. Default is 'workflow'.
Table to use for persisting workflow history. Default is 'workflow_history'.
You may also use parameters for the different types of ID generators. See below under the init_*_generator for the necessary parameters for your database.
init_*_generator
In addition to creating a database handle we parse the dsn to see what driver we are using to determine how to generate IDs. We have the ability to use automatically generated IDs for PostgreSQL, MySQL, and SQLite. If your database is not included a randomly generated ID will be used. (Default length of 8 characters, which you can modify with a id_length parameter.)
dsn
id_length
You can also create your own adapter for a different type of database. Just check out the existing Workflow::Persister::DBI::AutoGeneratedId and Workflow::Persister::DBI::SequenceId classes for examples.
Given $driver and the persister parameters in \%params, assign the appropriate ID generators for both the workflow and history tables.
$driver
\%params
Returns: nothing, but assigns the object properties workflow_id_generator and history_id_generator.
workflow_id_generator
history_id_generator
Assign the table names from \%params (using 'workflow_table' and 'history_table') or use the defaults 'workflow' and 'workflow_history'.
Returns: nothing, but assigns the object properties workflow_table and history_table.
workflow_table
history_table
Create ID generators for the workflow and history tables using PostgreSQL sequences. You can specify the sequences used for the workflow and history tables:
Sequence for the workflow table. Default: 'workflow_seq'
Sequence for the workflow history table. Default: 'workflow_history_seq'
Create ID generators for the workflow and history tables using the MySQL 'auto_increment' type. No parameters are necessary.
Create ID generators for the workflow and history tables using the SQLite implicit increment. No parameters are necessary.
Create ID generators for the workflow and history tables using a random set of characters. You can specify:
Length of character sequence to generate. Default: 8.
Create ID generators for the workflow and history tables using the Oracle sequences. No parameters are necessary.
Serializes a workflow into the persistance entity configured by our workflow.
Takes a single parameter: a workflow object
Returns a single value, a id for unique identification of out serialized workflow for possible deserialization.
Deserializes a workflow from the persistance entity configured by our workflow.
Takes a single parameter: the unique id assigned to our workflow upon serialization (see "create_workflow").
Returns a hashref consisting of two keys:
state, the workflows current state
last_update, date indicating last update
Updates a serialized workflow in the persistance entity configured by our workflow.
Returns: Nothing
Serializes history records associated with a workflow object
Takes two parameters: a workflow object and an array of workflow history objects
Returns: provided array of workflow history objects upon success
Deserializes history records associated with a workflow object
Returns an array of workflow history objects upon success
Copyright (c) 2003-2007 Chris Winters. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Jonas B. Nielsen (jonasbn) <jonasbn@cpan.org> is the current maintainer.
Chris Winters <chris@cwinters.com>, original author.
To install Workflow, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Workflow
CPAN shell
perl -MCPAN -e shell install Workflow
For more information on module installation, please visit the detailed CPAN module installation guide.