[interchange-cvs] interchange - heins modified 2 files

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Mon Oct 7 11:36:01 2002


User:      heins
Date:      2002-10-07 15:35:57 GMT
Modified:  lib/Vend Config.pm
Modified:  lib/Vend/Table DBI.pm
Log:
* Add DatabaseAuto directive to automatically suck in all tables
  from a DSN.

    DatabaseAuto   "dbi:driver:info"  "User name" "Password string"

  For example:

    DatabaseAuto   dbi:mysql:test_foundation interch pass

  will do the equivlent of:

    NoImport  TABLENAME
    Database  TABLENAME TABLENAME.txt dbi:mysql:test_foundation
    Database  TABLENAME USER interch
    Database  TABLENAME PASS pass

  for every table (not view) found in the DSN.

  If the module DBIx::DBSchema is found, it may also do (should for
  mysql and Pg):

    Database  TABLENAME  CREATE_SQL  create table TABLENAME ( ...)

  At that point, the information is in $Vend:Cfg and it is trivial
  to write it to the screen or a file so you can save and modify
  it.

Revision  Changes    Path
2.73      +16 -2     interchange/lib/Vend/Config.pm


rev 2.73, prev_rev 2.72
Index: Config.pm
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /var/cvs/interchange/lib/Vend/Config.pm,v
retrieving revision 2.72
retrieving revision 2.73
diff -u -r2.72 -r2.73
--- Config.pm	26 Sep 2002 16:39:15 -0000	2.72
+++ Config.pm	7 Oct 2002 15:35:57 -0000	2.73
@@ -1,6 +1,6 @@
 # Vend::Config - Configure Interchange
 #
-# $Id: Config.pm,v 2.72 2002/09/26 16:39:15 racke Exp $
+# $Id: Config.pm,v 2.73 2002/10/07 15:35:57 mheins Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 #
@@ -45,7 +45,7 @@
 use Vend::Util;
 use Vend::Data;
=20
-$VERSION =3D substr(q$Revision: 2.72 $, 10);
+$VERSION =3D substr(q$Revision: 2.73 $, 10);
=20
 my %CDname;
=20
@@ -376,6 +376,7 @@
 	['SessionDatabase',  'relative_dir',     'session'],
 	['SessionLockFile',  undef,     		 'etc/session.lock'],
 	['DatabaseDefault',  'hash',	     	 ''],
+	['DatabaseAuto',	 'dbauto',	     	 ''],
 	['Database',  		 'database',     	 ''],
 	['Autoload',		 undef,		     	 ''],
 	['AutoEnd',			 undef,		     	 ''],
@@ -2907,6 +2908,19 @@
=20
 	return $d;
=20=09
+}
+
+sub parse_dbauto {
+	my ($var, $value) =3D @_;
+	return '' unless $value;
+	my @inc =3D Vend::Table::DBI::auto_config($value);
+	my %noed;
+	for(@inc) {
+		my ($t, $thing) =3D @$_;
+		parse_boolean('NoImport', $t) unless $noed{$t}++;
+		parse_database('Database', "$t $thing");
+	}
+	return 1;
 }
=20
 sub parse_database {



2.34      +46 -2     interchange/lib/Vend/Table/DBI.pm


rev 2.34, prev_rev 2.33
Index: DBI.pm
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /var/cvs/interchange/lib/Vend/Table/DBI.pm,v
retrieving revision 2.33
retrieving revision 2.34
diff -u -r2.33 -r2.34
--- DBI.pm	6 Oct 2002 00:03:34 -0000	2.33
+++ DBI.pm	7 Oct 2002 15:35:57 -0000	2.34
@@ -1,6 +1,6 @@
 # Vend::Table::DBI - Access a table stored in an DBI/DBD database
 #
-# $Id: DBI.pm,v 2.33 2002/10/06 00:03:34 mheins Exp $
+# $Id: DBI.pm,v 2.34 2002/10/07 15:35:57 mheins Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 #
@@ -20,7 +20,7 @@
 # MA  02111-1307  USA.
=20
 package Vend::Table::DBI;
-$VERSION =3D substr(q$Revision: 2.33 $, 10);
+$VERSION =3D substr(q$Revision: 2.34 $, 10);
=20
 use strict;
=20
@@ -1850,6 +1850,50 @@
 	return Vend::Util::uneval($ref)
 		if $opt->{textref};
 	return wantarray ? ($ref, \%nh, \@na) : $ref;
+}
+
+sub auto_config {
+	my $string =3D shift;
+	my ($dsn, $user, $pass) =3D Text::ParseWords::shellwords($string);
+	my $handle =3D DBI->connect($dsn, $user, $pass)
+		or ::logDebug(errmsg("DatabaseAuto DSN '%s' does not connect.", $dsn));
+	my $schema;
+	my @tabs;
+	my @out;
+	eval {
+		require DBIx::DBSchema;
+		$schema =3D new_native DBIx::DBSchema $handle;
+	};
+
+	my $sth;
+	eval {
+		$sth =3D $handle->table_info()
+			or die "Table info not enabled for this driver.\n";
+		while(my $ref =3D $sth->fetchrow_arrayref) {
+			next unless $ref->[3] eq 'TABLE';
+			push @tabs, $ref->[2];
+		}
+	};
+
+	my %found;
+	return undef unless @tabs;
+	for my $t (@tabs) {
+		$found{$t} =3D 1;
+		push @out, [$t, "$t.txt $dsn"];
+		push @out, [$t, "USER $user"] if $user;
+		push @out, [$t, "PASS $pass"] if $pass;
+	}
+
+	if($schema) {
+		for my $create ($schema->sql($handle)) {
+			$create =3D~ /^CREATE\s+TABLE\s+(\w+)\s+/
+				or next;
+			my $t =3D $1;
+			next unless $found{$t};
+			push @out, [ $t, "CREATE_SQL $create"];
+		}
+	}
+	return @out;
 }
=20
 *reset =3D \&Vend::Table::Common::reset;