[ic] cleanse option for import_table.html

Stefan Hornburg Racke interchange-users@interchange.redhat.com
Thu Apr 18 08:37:03 2002


Here some fast patches for a "cleanse" option on import_table.

This allows to upload a table and any records not in this table
get removed after updating the records in the upload (to avoid
an empty table during the update).

It is largely untested. I'm planning to add to DEVEL, if no one
objects.

Ciao
        Racke

diff -u -u -r2.0 import_fields.tag
--- import_fields.tag	18 Jul 2001 02:22:15 -0000	2.0
+++ import_fields.tag	18 Apr 2002 11:58:23 -0000
@@ -193,7 +193,7 @@
 	my $k;
 	my @f;
 	@names = split /\s+/, $fields;
-	shift @names;
+	my $key = shift @names;
 	my @set;
 	my $i = 0;
 	my $idx = 0;
@@ -201,6 +201,12 @@
 		$db->column_index($_);
 		$set[$idx++] = $db->field_settor($_);
 	}
+	my %keys;
+	if ($opt->{cleanse}) {
+		# record existing columns
+		my $recs = $db->query("select $key from $table");
+		$keys{$_->[0]} = 1 for @$recs;
+	}
 	my $count = 0;
 	my $totcount = 0;
 	my $delcount = 0;
@@ -246,8 +252,18 @@
 		}
 		for ($i = 0; $i < $idx; $i++) {
 			$set[$i]->($k, $f[$i]);
+			if ($opt->{cleanse}) {
+				delete $keys{$k};
+			}
 		}
 		$count++;
+	}
+	if ($opt->{cleanse}) {
+		# remove any record which hasn't updated
+		for (keys(%keys)) {
+			$db->delete_record($_);
+			$delcount++;
+		}
 	}
 	$out .= "${tmsg}$count records processed of $totcount input lines.\n";
 	$out .= "${tmsg}$delcount records deleted.\n" if $delcount;

diff -u -u -r2.0.2.2 import_table.html
--- import_table.html	19 Sep 2001 19:08:04 -0000	2.0.2.2
+++ import_table.html	18 Apr 2002 12:29:17 -0000
@@ -93,6 +93,20 @@
 					table="[cgi mv_data_table]"
 	]
 	[/elsif]
+	[elsif cgi ui_upload_ext eq cleanse]
+	[import-fields
+	                add=1
+					convert="[cgi ui_import_convert]"
+					cleanse=1
+					delete="[cgi ui_allow_delete]"
+					dir=backup
+					file="upload/[cgi ui_upload_filename]"
+					move=1
+					multiple="[cgi ui_allow_multiple]"
+					quiet="[cgi ui_import_quiet]"
+					table="[cgi mv_data_table]"
+	]
+	[/elsif]
 	[/if]
 [/if]
 
@@ -119,6 +133,7 @@
 <hr>
 <SELECT NAME=ui_upload_ext>
 	<OPTION VALUE=add> [L]Add new items if present[/L]
+	<OPTION VALUE=cleanse> [L]Replace existing items[/L]
 	<OPTION VALUE=update> [L]Ignore new items[/L]
 </SELECT>&nbsp;&nbsp;&nbsp;<SMALL><INPUT TYPE=checkbox NAME=ui_allow_delete VALUE=1> [L]Allow delete[/L]
 &nbsp;&nbsp;<INPUT TYPE=checkbox NAME=ui_autonumber VALUE=1 CHECKED> [L]Autonumber if no key[/L]</SMALL>

-- 
Think of it !

For projects and other business stuff please refer to COBOLT NetServices
(URL: http://www.cobolt.net; Email: info@cobolt.net; Phone: 0041-1-3884400)