SQL-Loader

 view release on metacpan or  search on metacpan

lib/SQL/Loader/MySQL.pm  view on Meta::CPAN

	foreach my $rr (@{$cols}) {
		if ($rr->[2] =~ /^\s*$/ || $rr->[2] =~ /&nbsp\;/) { # cols with no type set yet are skipped -
																												# considered 'non-production' tables
			$notype = 1;
			next;
		}
		if ( $rcount == 0 ) {
			$q = "DROP TABLE IF EXISTS ".$name->[0].";";
			$dbh->do( $q ) || die $dbh->errstr;
 			$q = "CREATE TABLE ".$name->[0]." (";
		}
 		$q .= $rr->[1] . " " . uc( $rr->[2] );
		# set primary keys via 2 possible flags indicating to do so:
		if ( $rr->[1] eq 'id' ) {
			# 1: a field name 'id' is automatically made PK A_I
			$q .= " PRIMARY KEY AUTO_INCREMENT";
		}
		elsif ( $rr->[3] =~ /PK/ ) {
			# 2: if description of a field contains the case sensitive letters 'PK' it will be made a primary key
			$q .= " PRIMARY KEY";
		}
		$q .= ", " unless $rcount == scalar(@{$cols} - 1);
		$rcount++;
	}

	$q .= ");";

	unless ($notype) {
		print "RUNNING QUERY: $q\n" unless $quiet;
		$dbh->do( $q ) || die $dbh->errstr;
		unless ( $quiet ) {
			print "\n";
			print '*' x 100, "\n\n";
			print "OK", "\n\n";
			print '*' x 100, "\n\n";
		}
	}

	return 1;
}

=head2 connect_string

return dbh connect string.

=cut
sub connect_string {
	my $self = shift;
	my $dbname = $self->dbname();
	my $dbuser = $self->dbuser();
	my $dbpass = $self->dbpass();
	return ("dbi:mysql:$dbname","$dbuser","$dbpass");
}

1;

__END__

=head1 AUTHOR

Ben Hare for www.strategicdata.com.au

benhare@gmail.com

=head1 COPYRIGHT

(c) Copyright Strategic Data Pty. Ltd.

This module is free software. You can redistribute it or modify it under the same terms as Perl itself.

=cut



( run in 0.690 second using v1.01-cache-2.11-cpan-e1769b4cff6 )