[interchange-cvs] interchange - jon modified 2 files

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Sun Jan 12 01:48:00 2003


User:      jon
Date:      2003-01-12 06:47:09 GMT
Modified:  lib/Vend Config.pm
Modified:  lib/Vend/Table DBI.pm
Log:
Add new database configuration option PREFER_NULL. It accepts a list of
field names which should be set to NULL instead of an empty string whenever
possible:

Database  people  PREFER_NULL  age height

Probably most useful with NUMERIC fields that are nullable, to prevent
import errors when trying to store '' into a numeric field. Also for
character fields that you'd rather have contain a NULL than '' to comply
with a CHECK constraint or a foreign key constraint.

I tried to keep this as efficient as possible since these are some
frequently-called routines. If anyone can think of a faster way to handle
the quote sub, let me know.

Revision  Changes    Path
2.93      +3 -2      interchange/lib/Vend/Config.pm


rev 2.93, prev_rev 2.92
Index: Config.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Config.pm,v
retrieving revision 2.92
retrieving revision 2.93
diff -u -u -r2.92 -r2.93
--- Config.pm	4 Jan 2003 23:50:43 -0000	2.92
+++ Config.pm	12 Jan 2003 06:47:09 -0000	2.93
@@ -1,6 +1,6 @@
 # Vend::Config - Configure Interchange
 #
-# $Id: Config.pm,v 2.92 2003/01/04 23:50:43 kwalsh Exp $
+# $Id: Config.pm,v 2.93 2003/01/12 06:47:09 jon Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 # Copyright (C) 2003 ICDEVGROUP <interchange@icdevgroup.org>
@@ -46,7 +46,7 @@
 use Vend::Util;
 use Vend::Data;
 
-$VERSION = substr(q$Revision: 2.92 $, 10);
+$VERSION = substr(q$Revision: 2.93 $, 10);
 
 my %CDname;
 
@@ -2780,6 +2780,7 @@
 							DEFAULT_SESSION       DEFAULT_SESSION
 							FIELD_ALIAS   FIELD_ALIAS
 							NUMERIC       NUMERIC
+							PREFER_NULL   PREFER_NULL
 							WRITE_CATALOG WRITE_CATALOG
 					! );
 



2.39      +26 -3     interchange/lib/Vend/Table/DBI.pm


rev 2.39, prev_rev 2.38
Index: DBI.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Table/DBI.pm,v
retrieving revision 2.38
retrieving revision 2.39
diff -u -u -r2.38 -r2.39
--- DBI.pm	8 Dec 2002 15:55:23 -0000	2.38
+++ DBI.pm	12 Jan 2003 06:47:09 -0000	2.39
@@ -1,6 +1,6 @@
 # Vend::Table::DBI - Access a table stored in an DBI/DBD database
 #
-# $Id: DBI.pm,v 2.38 2002/12/08 15:55:23 racke Exp $
+# $Id: DBI.pm,v 2.39 2003/01/12 06:47:09 jon Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 #
@@ -20,7 +20,7 @@
 # MA  02111-1307  USA.
 
 package Vend::Table::DBI;
-$VERSION = substr(q$Revision: 2.38 $, 10);
+$VERSION = substr(q$Revision: 2.39 $, 10);
 
 use strict;
 
@@ -752,6 +752,8 @@
 sub quote {
 	my($s, $value, $field) = @_;
 	$s = $s->import_db() if ! defined $s->[$DBI];
+	return 'NULL' if $field and ! length($value)
+		and exists $s->[$CONFIG]->{PREFER_NULL}{$field};
 	return $s->[$DBI]->quote($value)
 		unless $field and exists $s->[$CONFIG]->{NUMERIC}{$field};
 	$value = 0 if ! length($value);
@@ -1167,6 +1169,16 @@
 		$fary = [ keys   %$href ];
 	}
 
+	if ($s->[$CONFIG]->{PREFER_NULL}) {
+		my $prefer_null = $s->[$CONFIG]->{PREFER_NULL};
+		my $i = 0;
+		for (@$fary) {
+			undef $vary->[$i]
+				if exists $prefer_null->{$_} and $vary->[$i] eq '';
+			++$i;
+		}
+	}
+
     if($s->[$CONFIG]->{LENGTH_EXCEPTION_DEFAULT}) {
 
 		my $lcfg   = $s->[$CONFIG]{FIELD_LENGTH_DATA}
@@ -1248,7 +1260,15 @@
 
 	$s->filter(\@fields, $s->[$CONFIG]{COLUMN_INDEX}, $s->[$CONFIG]{FILTER_TO})
 		if $cfg->{FILTER_TO};
-	my ($val);
+
+	if ($cfg->{PREFER_NULL}) {
+		for (keys %{$cfg->{PREFER_NULL}}) {
+			my $i = $cfg->{COLUMN_INDEX}{$_};
+			undef $fields[$i] if $fields[$i] eq '';
+		}
+	}
+
+	my $val;
 
 	if(scalar @fields == 1) {
 		 return if $cfg->{AUTO_SEQUENCE};
@@ -1479,11 +1499,14 @@
 
 	$key = $s->autonumber()  if ! length($key);
 
+	undef $value if $value eq '' and exists $s->[$CONFIG]{PREFER_NULL}{$column};
+
 	my $rawkey = $key;
 	my $rawval = $value;
 
 	$key   = $s->quote($key, $s->[$KEY]);
 	$value = $s->quote($value, $column);
+
 	my $query;
 	if(! $s->record_exists($rawkey)) {
 		if( $s->[$CONFIG]{AUTO_SEQUENCE} ) {