ConfigDatabase — specify database holding definitions usually found in catalog.cfg
The directive allows one to keep the usual catalog.cfg definitions in a
database table.
By using the special option LOAD, it is also possible to
instruct Interchange to fill the database with initial data found in your
catalog.cfg — just make sure to remove that option on subsequent restarts.
Example: Defining ConfigDatabase
You first need to create the table in an SQL database that you will use to store config directives. Here's the SQL code needed:
create table config ( code varchar(32) NOT NULL PRIMARY KEY, directive varchar(64) NOT NULL, value varchar(255), extended text );
Just for the record, once you have the above database table, sample database contents of:
code|directive|value|extended C0001|VariableDatabase|variable C0002|SessionExpire|2 hours| C0003|Variable|foo| bar
will correspond to the usual catalog.cfg definitions:
VariableDatabase variable SessionExpire 2 hours Variable foo <<EOF bar EOF
Example: Automatically populating ConfigDatabase with initial data from catalog.cfg
ConfigDatabase config config.txt dbi:mysql:config ConfigDatabase config LOAD 1
Even though this appears to be both global and catalog configuration directive, it is only implemented on catalog level.
Interchange 5.9.0:
Source: lib/Vend/Config.pm
Line 4284 (context shows lines 4284-4427)
sub parse_config_db {
my($name, $value) = @_;
my ($d, $new);
unless (defined $value && $value) {
$d = {};
return $d;
}
if($C) {
$d = $C->{ConfigDatabase};
}
else {
$d = $Global::ConfigDatabase;
}
my($database,$remain) = split /[\s,]+/, $value, 2;
$d->{'name'} = $database;
if(!defined $d->{'file'}) {
my($file, $type) = split /[\s,]+/, $remain, 2;
$d->{'file'} = $file;
if( $type =~ /^\d+$/ ) {
$d->{'type'} = $type;
}
elsif( $type =~ /^(dbi|sql)\b/i ) {
$d->{'type'} = 8;
if($type =~ /^dbi:/) {
$d->{DSN} = $type;
}
}
# LDAP
elsif( $type =~ /^ldap\b/i) {
$d->{'type'} = 9;
if($type =~ /^ldap:(.*)/i) {
$d->{LDAP_HOST} = $1;
}
}
# END LDAP
elsif( "\U$type" eq 'TAB' ) {
$d->{'type'} = 6;
}
elsif( "\U$type" eq 'PIPE' ) {
$d->{'type'} = 5;
}
elsif( "\U$type" eq 'CSV' ) {
$d->{'type'} = 4;
}
elsif( "\U$type" eq 'DEFAULT' ) {
$d->{'type'} = 1;
}
elsif( $type =~ /[%]{1,3}|percent/i ) {
$d->{'type'} = 3;
}
elsif( $type =~ /line/i ) {
$d->{'type'} = 2;
}
else {
$d->{'type'} = 1;
$d->{DELIMITER} = $type;
}
}
else {
my($p, $val) = split /\s+/, $remain, 2;
$p = uc $p;
if(defined $Explode_ref{$p}) {
my($ak, $v);
my(@v) = Text::ParseWords::shellwords($val);
@v = grep defined $_, @v;
$d->{$p} = {} unless defined $d->{$p};
for(@v) {
my ($sk,$v) = split /\s*=\s*/, $_;
my (@k) = grep /\w/, split /\s*,\s*/, $sk;
for my $k (@k) {
if($d->{$p}->{$k}) {
config_warn(
qq{Database %s explode parameter %s redefined to "%s", was "%s".},
$d->{name},
"$p --> $k",
$v,
$d->{$p}->{$k},
);
}
$d->{$p}->{$k} = $v;
}
}
}
elsif(defined $Hash_ref{$p}) {
my($k, $v);
my(@v) = Vend::Util::quoted_comma_string($val);
@v = grep defined $_, @v;
$d->{$p} = {} unless defined $d->{$p};
for(@v) {
($k,$v) = split /\s*=\s*/, $_;
if($d->{$p}->{$k}) {
config_warn(
qq{Database %s hash parameter %s redefined to "%s", was "%s".},
$d->{name},
"$p --> $k",
$v,
$d->{$p}->{$k},
);
}
$d->{$p}->{$k} = $v;
}
}
elsif(defined $Ary_ref{$p}) {
my(@v) = Text::ParseWords::shellwords($val);
$d->{$p} = [] unless defined $d->{$p};
push @{$d->{$p}}, @v;
}
else {
defined $d->{$p}
and ! defined $C->{DatabaseDefault}{$p}
and config_warn(
qq{Database %s scalar parameter %s redefined to "%s", was "%s".},
$d->{name},
$p,
$val,
$d->{$p},
);
$d->{$p} = $val;
}
}
#::logDebug("d object: " . uneval_it($d));
if($d->{ACTIVE} and ! $d->{OBJECT}) {
my $name = $d->{'name'};
$d->{OBJECT} = Vend::Data::import_database($d)
or config_error("Config database $name failed import.\n");
}
elsif($d->{LOAD} and ! $d->{OBJECT}) {
my $name = $d->{'name'};
$d->{OBJECT} = Vend::Data::import_database($d)
or config_error("Config database $name failed import.\n");
if( $d->{type} == 8 ) {
$d->{OBJECT}->set_query("delete from $name where 1 = 1");
}
}
return $d;
}