Workflow::Persister::DBI - Persist workflow and history to DBI database |
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.)
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:
get_workflow_fields()
Return list of fields in this order:
workflow_id, type, state, last_update
get_history_fields()
Return list of fields in this order:
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 the Workflow::Persister manpage.
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()
.
You may also use:
user
to login with.
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.
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.)
You can also create your own adapter for a different type of database. Just check out the existing the Workflow::Persister::DBI::AutoGeneratedId manpage and the Workflow::Persister::DBI::SequenceId manpage classes for examples.
Given $driver
and the persister parameters in \%params
, assign
the appropriate ID generators for both the workflow and history
tables.
Returns: nothing, but assigns the object properties
workflow_id_generator
and 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
.
Create ID generators for the workflow and history tables using PostgreSQL sequences. You can specify the sequences used for the workflow and history tables:
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:
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:
Updates a serialized workflow in the persistance entity configured by our workflow.
Takes a single parameter: a workflow object
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
Takes a single parameter: 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.
Workflow::Persister::DBI - Persist workflow and history to DBI database |