[set page_title]Save Database Configuration[/set] [set ui_class]Admin[/set] [set page_perm]config[/set] [set help_name]dbconfig[/set] [set icon_name]icon_config.gif[/set] [tmp page_title]Save Database Configuration -- [cgi mv_data_table][/tmp] @_UI_STD_HEAD_@ [bounce page=admin/gentable unless="[cgi mv_dbconf]"] [tmpn continue]0[/tmpn] [if cgi mv_dbconf]

    [perl tables="[cgi mv_data_table]"]
    	$Dref = {};
    	my @keys = grep /^mv_dbconf_/, keys %$CGI;
    	my $table = $CGI->{mv_data_table};
    	my $db = $Db{$table};

    	## Develop backup file name for later use with backup-file ITL
    	if($db) {
    		$Tag->tmp('file_to_backup');
    		my $d = $db->config('DIR');
    		my $f = $db->config('file');
    		if($d =~ s:^$Config->{VendRoot}/::) {
    			$Scratch->{file_to_backup} = join "/", $d, $f;
    		}
    	}

    	my $string = '';
    	#$string = join "\n", 'KEYS', @keys, "";
    	for(@keys) {
    		my $key = $_;
    		$key =~ s/^mv_dbconf_//;
    		#$string .= "KEY: $key -- $_\n";
    		$Dref->{$key} = $CGI->{$_};
    	}

    	$Tag->tmp('add_field');
    	$Tag->tmp('del_field');

    	RESOLVEADD: {
    		my @orig = split /[\s,\0]+/, delete $Dref->{orignames};
    		my @curr = grep /\w+/, split /[\s,\0]+/, $Dref->{COLUMN_DEF_name};
    		for(my $i = 0; $i < @curr; $i++ ) {
    			next if $orig[$i] eq $curr[$i];
    			my $add = $curr[$i];
    			$add =~ s/^\s+//;
    			$add =~ s/\s+$//;
    			if($add !~ /^\w+$/) {
    				return errmsg("Improper additional field name '%s', abort.", $add);
    			}
    			$Scratch->{add_field} = $add;
    		}
    	}

    	RESOLVEDEL: {
    		my @del = grep $_, split /[\s,\0]+/, delete $Dref->{delete_field};
    		@del = grep /^\w+$/, @del;
    		last RESOLVEDEL unless @del;
    		if($Scratch->{add_field}) {
    			return errmsg("Cannot add and delete at same time.");
    		}
    		if(@del > 1) {
    			my $s = join " AND ", @del;
    			return errmsg("Cannot delete more than one field, you tried '%s'.", $s);
    		}
    		$Scratch->{add_field} = $del[0];
    		$Scratch->{del_field} = 1;
    	}

    	sub tout {
    		my ($param, $data, $prefix) = @_;
    		$prefix = '' unless $prefix;
    		if($data =~ /\n/) {
    			$data = "<<_EOD\n$data\n_EOD\n";
    		}
    		return sprintf("${prefix}Database %-11s %-15s %s\n", $table, $param, $data);
    	}

    	%Misc_set = (
    		GUESS_NUMERIC => '=No numeric guess, 1=Guess numeric fields',
    		EXCEL => '=No Excel correction, 1=Correct for Excel peculiarities',
    		LARGE   => '=No, 1=Yes (prevents building select list)',
    		HOT   => '=Normal open, 1=Keep table always connected',
    		CONTINUE   => q{=No extra line processing,
    						DITTO="Ditto" line continuation,
    						UNIX=Unix-style \ continuation,
    						NOTES=Lotus notes (only for LINE delimiter)},
    	);

    	%Misc_set_sql = (
    		LONGTRUNCOK => "1=Silently truncate too-long values, 0=Don't truncate",
    	);

    	$string .= tout($Dref->{file}, $str_typemap{ $Dref->{type} } || $Dref->{type});

    	if($Dref->{DSN_default}) {
    		$string .= tout('DSN', "__SQL" . 'DSN' . "__");
    	}
    	elsif($Dref->{DSN}) {
    		$string .= tout('DSN', $Dref->{DSN});
    	}

    	if($Dref->{USER_default}) {
    		$string .= "ifdef SQLUSER\n";
    		for (qw/USER PASS/) {
    			$string .= tout($_,  "__SQL" . $_ . "__");
    		}
    		$string .= "endif\n";
    	}
    	else {
    		for (qw/USER PASS/) {
    			$string .= tout($_, $Dref->{$_}) if length $Dref->{$_};
    		}
    	}

    	for (keys %Misc_set, keys %Misc_set_sql,) {
    		$string .= tout($_, $Dref->{$_}) if length $Dref->{$_};
    	}

    	for (keys %Adv_set, keys %Adv_set_sql, @Adv_set_sql_text) {
    		$string .= tout($_, $Dref->{$_}, '#') if length $Dref->{$_};
    	}

    	if ($Dref->{DEFAULT_TYPE} =~ /\S/) {
    		$string .= tout('DEFAULT_TYPE', $Dref->{DEFAULT_TYPE});
    	}
    	if ($Dref->{CREATE_SQL} =~ /\S/) {
    		$Dref->{CREATE_SQL} =~ s/\s*;\s*$//;
    		$string .= tout('CREATE_SQL', $Dref->{CREATE_SQL});
    	}

    	if($Dref->{COLUMN_DEF_type}) {
    		my %def;
    		for( qw/name type length notnull key default/) {
    			$def{$_} = [ split /\0/, $Dref->{"COLUMN_DEF_$_"} ];
    		}
    		my $count = scalar( @{$def{name}} );
    		for(my $i = 0; $i < $count; $i++) {
    			my $cdef;
    			last if ! ($cdef = $def{name}[$i]);
    			next if ! $def{type}[$i];
    			$cdef = qq{"$cdef=};
    			$cdef .= $def{type}[$i];
    			$cdef .= qq{($def{length}[$i])} if $def{length}[$i] =~ /\S/;
    			$cdef .= qq{ NOT NULL} if $def{notnull}[$i];
    			$cdef .= qq{ PRIMARY KEY} if $def{key}[$i];
    			if ($def{default}[$i]) {
    				$def{default}[$i] = '' if $def{default}[$i] =~ /^['"]+$/;
    				$cdef .= qq{ DEFAULT '$def{default}[$i]'};
    			}
    			$cdef .= qq{"};
    			$string .= tout('COLUMN_DEF', $cdef);
    		}
    	}

    	my @idx = grep /\S/, split /[\0,\s]+/, delete $Dref->{INDEX};
    	for(@idx) {
    		if(s/^UNIQUE://) {
    			my $msg = errmsg("Index for column '%s' needs custom create SQL.", $_);
    			$Tag->warnings($msg);
    		}
    		$string .= tout('INDEX', $_);
    	}

    	if($Dref->{POSTCREATE}) {
    		my @lines = grep /\S/, split /;/, $Dref->{POSTCREATE};
    		for(@lines) {
    			s/^\s+//;
    			s/\s+$//;
    			$string .= tout('POSTCREATE', $_);
    		}
    	}
    	$Scratch->{continue} = 1;
    	$Scratch->{mv_dbconf} = "ParseVariables Yes\n$string";
    [/perl]

[/if]

[if scratch continue]
- Exporting database backup... [if type=explicit compare="[export table='[cgi mv_data_table]']"] [if type=explicit compare=| [backup-file file="[scratch file_to_backup]"] |] SUCCESS [else] Failed to back up [scratch file_to_backup] [set continue]0[/set] [msg]Export succeeded, backup failed.[/msg] [/else] [/if] [else] Failed, bailing out. [set continue]0[/set] [/else] [/if] [/if] [if scratch continue]
- Exporting database [if scratch add_field] [if scratch del_field] deleting [else] adding [/else] [/if] column '[scratch add_field]' [/if] ... [if type=explicit compare="[export table='[cgi mv_data_table]' field='[scratch add_field]' delete='[scratch del_field]' verify=1 ]"] SUCCESS [else] Failed, bailing out. [set continue]0[/set] [/else] [/if] [/if] [if scratch continue]
- Writing configuration ... [if type=explicit compare="[write-relative-file file='[cgi mv_dbconf_included_from]' interpolate=1 ][scratch mv_dbconf][/write-relative-file]"] SUCCESS [else] Failed, bailing out. [set continue]0[/set] [/else] [/if] [/if] [if scratch continue] [and type=file term="products/[cgi mv_dbconf_db_file]"]
- Removing database output file ... [tmp tmp_unlink][unlink-file name='products/[cgi mv_dbconf_db_file]' prefix=products ][/tmp] [if scratch tmp_unlink] SUCCESS [else] Failed, not present? [/else] [/if] [/if] [if scratch continue]
- Reconfiguring catalog, database only ... [if type=explicit compare="[reconfig table='[cgi mv_data_table]' file='[cgi mv_dbconf_included_from]' ]"] SUCCESS [else] Failed, bailing out. [set continue]0[/set] [/else] [/if] [/if]

[if scratch continue] Table configuration should be complete in a matter of seconds, [page href=admin/dbconfig form="mv_data_table=[cgi mv_data_table]"]go here to check. [/if] @_UI_STD_FOOTER_@