Database — register existing database table for use with Interchange
The directive registers a database table for use with Interchange.
table_name specifies an arbitrary name — name
that will be used to refer to the table within Interchange. Names can be composed
of alphanumeric characters including underscore, and we recommend they're in
all lower- or upper-case.
source_file specifies the initial database text
source file, and type specifies database format.
For more about Interchange and databases, and supported formats, see database glossary entry.
Example: Automatically configure all tables from an existing PostgreSQL database
DatabaseAutoIgnore ^sql_ DatabaseAuto NoImportExternal Yes
For further discussion, see DatabaseAuto.
Example: Definition of a Postgres database table
# # Required for PostgreSQL # Require module DBI Require module DBD::Pg Variable SQLDSN dbi:Pg:dbname=database_name Variable SQLUSER username Variable SQLPASS password Variable SOME_DATABASE 1 <ParseVariables Yes> DatabaseDefault USER DatabaseDefault PASS DatabaseDefault NO_ASCII_INDEX 1 Message -i -n Using PostgreSQL, DSN=... # # Individual database table definitions # Database table_name table_name.txt </ParseVariables> # # Optional table descriptions # Database table_name LENGTH_EXCEPTION_DEFAULT truncate_log Database table_name DEFAULT_TYPE varchar(255) Database table_name KEY sku Database table_name HIDE_FIELD inactive Database table_name COLUMN_DEF "sku=varchar(64) NOT NULL PRIMARY KEY" Database table_name COLUMN_DEF "description=varchar(128)" Database table_name COLUMN_DEF "price=varchar(12)" Database table_name INDEX description Database table_name INDEX price
For the above example to work, you will need file present within table_name.txtProductDir, containing the initial data set for the table. If there's no initial data set and the table should be empty, the file still needs to contain a single line with three column names, separated by a TAB:
sku description price
In Interchange, words table and database
are used to refer to the same thing — database table.
Defining databases on an Interchange (global) level
won't work. If you want to share
databases among catalogs, define them in each catalog.cfg separately (possibly
by including the generic file with Database definitions).
Interchange 5.9.0:
Source: lib/Vend/Config.pm
Line 4442 (context shows lines 4442-4639)
sub parse_database {
my ($var, $value) = @_;
my ($c, $new);
if (! $value) {
$c = {};
return $c;
}
$c = $C ? $C->{Database} : $Global::Database;
my($database,$remain) = split /[\s,]+/, $value, 2;
if( ! defined $c->{$database} ) {
$c->{$database} = { 'name' => $database, included_from => $configfile };
$new = 1;
}
my $d = $c->{$database};
if($new) {
my($file, $type) = split /[\s,]+/, $remain, 2;
$d->{'file'} = $file;
if($file eq 'AUTO_SEQUENCE') {
# database table missing for AUTO_SEQUENCE directive
config_error('Missing database %s for AUTO_SEQUENCE %s.', $database, $type);
return $c;
}
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( $type =~ /^ic:(\w*)(:(.*))?/ ) {
my $class = $1;
my $dir = $3;
$d->{DIR} = $dir if $dir;
if($class =~ /^default$/i) {
# Do nothing
}
elsif($class) {
$class = uc $class;
if(! $Vend::Data::db_config{$class}) {
config_error("unrecognized IC database class: %s (from %s)", $class, $type);
}
$d->{Class} = $class;
}
$d->{'type'} = 6;
}
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;
}
if ($d->{'type'} eq '8') { $d->{Class} = 'DBI' }
elsif ($d->{'type'} eq '9') { $d->{Class} = 'LDAP' }
else { $d->{Class} ||= $Global::Default_database }
if($C and $C->{DatabaseDefault}) {
while ( my($k, $v) = each %{$C->{DatabaseDefault}}) {
$d->{$k} = $v;
}
}
$d->{HOT} = 1 if $d->{Class} eq 'MEMORY';
#::logDebug("parse_database: type $type -> $d->{type}");
}
else {
my($p, $val) = split /\s+/, $remain, 2;
$p = uc $p;
#::logDebug("parse_database: parameter $p = $val");
if(defined $Explode_ref{$p}) {
my($ak, $v);
$val =~ s/,+$//;
$val =~ s/^,+//;
my(@v) = Text::ParseWords::shellwords($val);
@v = grep length $_, @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;
}
elsif ($p eq 'COMPOSITE_KEY') {
## Magic hardcode
if($d->{type} == 8) {
$d->{Class} = 'DBI_CompositeKey';
$d->{$p} = $val;
}
else {
config_warn(
'Database %s parameter in type with no handling. Ignored.',
$p,
);
}
}
elsif ($p eq 'CLASS') {
$d->{Class} = $val;
}
elsif ($p =~ /^(MEMORY|SDBM|GDBM|DB_FILE|LDAP)$/i) {
$d->{Class} = uc $p;
}
elsif ($p eq 'ALIAS') {
if (defined $c->{$val}) {
config_warn("Database '%s' already exists, can't alias.", $val);
}
else {
$c->{$val} = $d;
}
}
elsif ($p =~ /^MAP/) {
Vend::Table::Shadow::_parse_config_line ($d, $p, $val);
}
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;
}
$d->{HOT} = 1 if $d->{Class} eq 'MEMORY';
}
return $c;
}