Database-Schema-Config

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    Returns:

     (errstr,undef) something failed with the DB
     (undef,HASHREF) on success containing keys: "rev", "timestamp","lock", "user". Each of those point to ARRAYREFs.

    So the revision of the first config in the list (which should be the
    oldest) is $hr->{'rev'}->[0]

  isConfigLocked()
    Check to see if the latest config is currently locked. If it is, return
    information about the lock.

      $cfg->isConfigLocked();

    Returns

      (errstr,undef) on failure
      (undef,HASHREF) locked. see keys for details.
      (undef,0) not locked

  lockConfig()
    Lock the configuration so other people know we are editting it. A note
    will be appended to the "log" for the configuration. The latest
    configuration will be "locked" unless "rev" is specified. This should be
    called from the getConfig() method, not directly.

    Accepts:

      -rev => [int], defaults to 0
      -user => [string],

      $cfg->lockConfig(-rev => $rev, -user => $username);

    Returns:

      (errstr,undef) on failure
      ('lock failed',0) if already locked
      (undef,$rev) on success

  unlockConfig()
    Unlock the configuration. Both parameters are required. Should be called
    by the getConfig() method, not directly.

    Accepts:

      -rev => [int], defaults to 0
      -user => [string],

lib/Database/Schema/Config.pm  view on Meta::CPAN

	    	push @{$hv->{'rev'}},       $row->[0];
	    	push @{$hv->{'timestamp'}}, Time::Timestamp->new(ts => $row->[1]);
	    	push @{$hv->{'lock'}},      $row->[2];
	   	push @{$hv->{'user'}},      $row->[3];
    	}
    	return (undef,$hv);
}

=head2 isConfigLocked()

Check to see if the latest config is currently locked. If it is, return information about the lock.

  $cfg->isConfigLocked();

Returns

  (errstr,undef) on failure
  (undef,HASHREF) locked. see keys for details.
  (undef,0) not locked

=cut

sub isConfigLocked {
    	my $self = shift;

   	my $sql = 'SELECT rev, user FROM config WHERE xlock = 1';
    	my $rv  = $self->dbh->selectall_arrayref($sql);

	return ('db failure: '.$self->dbh->errstr(),undef) unless($rv);
        return ('multiple locks on config detected.',undef) if(@$rv > 1);
    	return ('config is not locked',0) if(@$rv == 0);  # no locks
    	return (undef,{
		'rev'  => $rv->[0]->[0],
             	'user' => $rv->[0]->[1],
	});
}

=head2 lockConfig()

Lock the configuration so other people know we are editting it. A note will be appended to the "log" for the configuration.  The latest configuration will be "locked" unless "rev" is specified. This should be called from the getConfig() method, not d...

Accepts:

  -rev => [int], defaults to 0
  -user => [string],

  $cfg->lockConfig(-rev => $rev, -user => $username);

Returns:

  (errstr,undef) on failure
  ('lock failed',0) if already locked
  (undef,$rev) on success

=cut

sub lockConfig {
    	my $self = shift;

    	my $parms = parse_parms({
		-parms => \@_,
		-required => [qw(-rev -user)],

lib/Database/Schema/Config.pm  view on Meta::CPAN


   	my $sql = 'UPDATE config SET xlock = 1, user = '.$self->dbh->quote($u).' WHERE rev = '.$self->dbh->quote($r);
    	my $rv  = $self->dbh->do($sql);

	return ('db failure: '.$self->dbh->errstr(),undef) unless(defined($rv));

	my $err;
    	($err,$rv) = $self->appendLogToConfig(
		-rev => $r,
		-user => $u,
		-log => ['config locked'],
	);
	return ($err,$rv) unless($rv);
    	return (undef,$r);
}

=head2 unlockConfig()

Unlock the configuration. Both parameters are required. Should be called by the getConfig() method, not directly.

Accepts:

lib/Database/Schema/Config.pm  view on Meta::CPAN

	$r = 0 if(!defined($r));

	return ('invalid parameters (rev)',undef) unless($r >= 0);
	return ('invalid parameters (rev)',undef) unless(defined($u) && $u ne '');
	my ($err,$rv) = $self->isConfigLocked();
	return ($err,$rv) unless($rv);

	($err,$rv) = $self->appendLogToConfig(
		-rev => $r,
		-user => $u,
		-log => ['config unlocked'],
	);
    	return ($err,$rv) unless($rv);

    	my $sql = 'UPDATE config SET xlock = 0 WHERE rev = '.$self->dbh->quote($r).' AND user = '.$self->dbh->quote($u);
    	$rv = $self->dbh->do($sql);
    	return ('db failure: '.$self->dbh->errstr(),undef) unless($rv);
    	return (undef,1);
}

=head2 appendLogToConfig()

lib/Database/Schema/Config.pm  view on Meta::CPAN

	my ($err,$rv,$rev);
	if($i){
		$rv = $self->dbh->do('DELETE FROM `'.$self->table().'`');
		return ('db failure: '.$self->dbh->errstr(),undef) unless($rv);
		$rv = $self->dbh->do('TRUNCATE TABLE `'.$self->table().'`');
		return ('db failure: '.$self->dbh->errstr(),undef) unless($rv);
	}
	else {
		my $hr = $self->isConfigLocked();
		return ('no lock on previous config',undef) unless(ref($hr) eq 'HASH');
		return ('Someone else has already locked this config: user='.$hr->{user}.' rev='.$hr->{rev},undef) if($hr->{user} ne $u);
	}

	my $ts = Time::Timestamp->new(ts => time());
	my $sql = 'INSERT INTO `'.$self->table().'` (rev,dt,user,config) VALUES (?,?,?,?)';
	my $sth = $self->dbh->prepare($sql);
	$rv = $sth->execute($rev,$ts->epoch(),$u,@$c);

	return ('db failure: '.$self->dbh->errstr(),undef) unless($rv);

	$sql = 'SELECT rev FROM config WHERE user = ? AND dt = ?';



( run in 1.493 second using v1.01-cache-2.11-cpan-49f99fa48dc )