The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

Object::Relation::Setup::DB::Pg - Object::Relation Pg data store setup

Synopsis

See Object::Relation::Setup.

Description

This module inherits from Object::Relation::Setup::DB to build a Pg set up store.

Class Interface

Constructors

new

  my $pg_setup = Object::Relation::Setup::DB::Pg->new(\%params);

The constructor inherits from Object::Relation::Setup::DB to set default values for the following attributes:

super_user

Defaults to "postgres".

super_pass

Defaults to "".

user

Defaults to "obj_rel".

dsn

Defaults to "dbi:Pg:dbname=obj_rel".

template_dsn

Defaults to the value of dsn with the value for the dbname changed to "template1".

Class Methods

connect_attrs

  DBI->connect(
      $dsn, $user, $pass,
      { Object::Relation::Setup::DB::Pg->connect_attrs }
  );

Returns a list of arugments to be used in the attributes hash passed to the DBI connect() method. Overrides that provided by Object::Relation::Setup::DB to add pg_enable_utf8 => 1.

Instance Interface

Instance Accessors

In addition to the attribute accessors provided by Object::Relation::Setup::DB, this class provides the following.

super_user

  my $super_user = $setup->super_user;
  $setup->super_user($super_user);

Gets and sets the super_user attribute.

super_pass

  my $super_pass = $setup->super_pass;
  $setup->super_pass($super_pass);

Gets and sets the super_pass attribute.

template_dsn

  my $template_dsn = $setup->template_dsn;
  $setup->template_dsn($template_dsn);

Gets and sets the template_dsn attribute.

Instance Methods

setup

  $setup->setup;

Sets up the data store. This implementation simply constructs a database handle and assigns it to the dbh attribute, and then calls SUPER::setup() to let Object::Relation::Setup::DB handle the bulk of the work.

teardown

  $kbs->teardown;

Tears down the tests data store by disconnecting all database handles currently connected via DBD::Pg, waiting for the connections to be registered by PostgreSQL, and then dropping the test database and user. If there appear to be users connected to the database, test_cleanup will sleep for one second and then try again, sleep and try again, etc. If there are still users connected to the database after five seconds, it will give up and die. But that shouldn't happen unless you have an extremely slow system, or have connected to the database yourself in another process. Either way, you'll then need to drop the database and user manually.

rules

  my $machine = FSA::Rules->new($setup->rules);
  print $machine->graph->as_png;

This method returns a list of values to be passed to the FSA::Rules constructor. When the run() method is called on the resulting FSA::Rules object, the rules handle the actual setup of the PostgreSQL database. Used internally by setup() and therefore not of much use externally (except for testing and graphing).

connect

  my $dbh = $setup->connect;
  $dbh = $setup->connect(@args);

Connects to the database and returns the database handle, which is also stored in the dbh attribute. The implementation is handled by the parent class; this method overrides the parent merely to set the client verbosity in the database handle before returning it. If the setup attribute is true, then the database verbosity is set to "log". Otherwise it's set to "warning".

check_version

  $setup->check_version;

Validates that we're using and connecting to PostgreSQL 8.0 or higher.

create_db

  $setup->create_db($db_name);

Attempts to create a database with the supplied name. It uses Unicode encoding. It will disconnect from the database when done. If the database name is not supplied, it will use that stored in the db_name attribute.

add_plpgsql

  $setup->add_plpgsql($db_name);

Given a database name, this method will attempt to add plpgsql to the database using createlang. If the createlang attribute is not specified, it will attempt to find it using find_createlang(), and if that fails it will throw an exception. The appropriate host and port, if any, will be pulled out of the dsn, and the super_user, if set to a true value, will also be used (recommended).

create_user

  $setup->create_user($user, $pass);

Attempts to create a user with the supplied name. It defaults to the user name returned by db_user() and the password returned by db_pass().

build_db

  $setup->build_db;

Builds the database with all of the tables, sequences, indexes, views, triggers, etc., required to power the application. Overrides the implementation in the parent class to set up the database handle and to silence "NOTICE" output from PostgreSQL.

grant_permissions

  $setup->grant_permissions;

Grants the database user permission to access the objects in the database. Called if the super user creates the database.

find_createlang

  my $createlang = $setup->find_createlang;

Searches for the createlang executable on the file system. Used internally by add_plpgsql() when the createlang attribute is not set. It searches in the following directories:

$ENV{POSTGRES_HOME}/bin (if $ENV{POSTGRES_HOME} exists)
$ENV{POSTGRES_LIB}/../bin (if $ENV{POSTGRES_LIB} exists)
The system path (File::Spec->path)
/usr/local/pgsql/bin
/usr/local/postgres/bin
/opt/pgsql/bin
/usr/local/bin
/usr/local/sbin
/usr/bin
/usr/sbin
/bin
C:\Program Files\PostgreSQL\bin

Copyright and License

Copyright (c) 2004-2006 Kineticode, Inc. <info@obj_relode.com>

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