head	1.4;
access;
symbols
	REL7_4_29:1.2
	REL8_0_25:1.3
	REL7_4_28:1.2
	REL8_0_24:1.3
	REL7_4_27:1.2
	REL8_0_23:1.3
	REL7_4_26:1.2
	REL8_0_22:1.3
	REL7_4_25:1.2
	REL8_0_21:1.3
	REL7_4_24:1.2
	REL8_0_20:1.3
	REL7_4_23:1.2
	REL8_0_19:1.3
	REL7_4_22:1.2
	REL8_0_18:1.3
	REL7_4_21:1.2
	REL8_0_17:1.3
	REL7_4_20:1.2
	REL8_0_16:1.3
	REL7_3_21:1.2
	REL7_4_19:1.2
	REL8_0_15:1.3
	REL7_3_20:1.2
	REL7_4_18:1.2
	REL8_0_14:1.3
	REL7_3_19:1.2
	REL7_4_17:1.2
	REL8_0_13:1.3
	REL8_0_12:1.3
	REL7_3_18:1.2
	REL7_4_16:1.2
	REL8_0_11:1.3
	REL8_0_10:1.3
	REL7_4_15:1.2
	REL7_3_17:1.2
	REL8_0_9:1.3
	REL7_4_14:1.2
	REL7_3_16:1.2
	REL7_3_15:1.2
	REL7_4_13:1.2
	REL8_0_8:1.3
	REL7_3_14:1.2
	REL7_4_12:1.2
	REL8_0_7:1.3
	REL7_3_13:1.2
	REL7_4_11:1.2
	REL8_0_6:1.3
	REL7_3_12:1.2
	REL7_4_10:1.2
	REL8_0_5:1.3
	REL7_3_11:1.2
	REL7_4_9:1.2
	REL8_0_4:1.3
	REL7_2_8:1.2
	REL7_3_10:1.2
	REL7_4_8:1.2
	REL8_0_3:1.3
	REL8_0_2:1.3
	REL7_2_7:1.2
	REL7_3_9:1.2
	REL7_4_7:1.2
	REL8_0_1:1.3
	REL8_0_STABLE:1.3.0.4
	REL8_0_0:1.3.0.2
	REL8_0_0RC5:1.3
	REL8_0_0RC4:1.3
	REL8_0_0RC3:1.3
	REL8_0_0RC2:1.3
	REL8_0_0RC1:1.3
	REL8_0_0BETA5:1.3
	REL8_0_0BETA4:1.2
	REL7_4_6:1.2
	REL7_3_8:1.2
	REL7_2_6:1.2
	REL8_0_0BETA3:1.2
	REL8_0_0BETA2:1.2
	REL7_2_5:1.2
	REL7_4_5:1.2
	REL7_3_7:1.2
	REL7_4_4:1.2
	REL8_0_0BETA1:1.2
	REL7_4_3:1.2
	REL7_4_2:1.2
	REL7_3_6:1.2
	REL7_4_1:1.2
	REL7_3_5:1.2
	REL7_4:1.2
	REL7_4_RC2:1.2
	REL7_4_STABLE:1.2.0.10
	REL7_4_RC1:1.2
	REL7_4_BETA5:1.2
	REL7_4_BETA4:1.2
	REL7_4_BETA3:1.2
	REL7_4_BETA2:1.2
	WIN32_DEV:1.2.0.8
	REL7_4_BETA1:1.2
	REL7_3_4:1.2
	REL7_3_2:1.2
	REL7_2_4:1.2
	REL7_3_STABLE:1.2.0.6
	REL7_2_3:1.2
	REL7_2_STABLE:1.2.0.4
	REL7_2:1.2
	REL7_2_RC2:1.2
	REL7_2_RC1:1.2
	REL7_2_BETA5:1.2
	REL7_2_BETA4:1.2
	REL7_2_BETA3:1.2
	REL7_2_BETA2:1.2
	REL7_2_BETA1:1.2
	REL7_1_2:1.2
	REL7_1_STABLE:1.2.0.2
	REL7_1:1.2
	REL7_1_BETA3:1.1
	REL7_1_BETA2:1.1;
locks; strict;
comment	@# @;


1.4
date	2005.06.22.22.56.26;	author tgl;	state dead;
branches;
next	1.3;

1.3
date	2004.11.04.06.09.21;	author neilc;	state Exp;
branches;
next	1.2;

1.2
date	2001.01.18.07.16.56;	author thomas;	state Exp;
branches;
next	1.1;

1.1
date	2000.12.20.17.20.24;	author thomas;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Remove contrib modules that have been agreed to be obsolete.
(There are more that will be removed once they've been copied to
pgfoundry.org.)
@
text
@#!/usr/bin/perl
# mysql2pgsql
# Take a MySQL schema dump and turn it into SQL92 and PostgreSQL form.
# Thomas Lockhart, (c) 2000, PostgreSQL Inc.
# Thanks to Tim Perdue at SourceForge.Net for testing and feedback.
#

eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

use IO::File;
use Getopt::Long;

my $progname = "mysql2pgsql";
my $version = "0.3";

GetOptions("debug!", "verbose!", "version", "path=s", "help", "data!");

my $debug = $opt_debug || 0;
my $verbose = $opt_verbose || 0;
my $pathfrom = $opt_path || "";
my $nodata = (! $opt_data);

$pathfrom = "$pathfrom/" if ($pathfrom =~ /.*[^\/]$/);

print "$0: $progname version $version\n"
    if ($opt_version || $opt_help);
print "\t(c) 2000 Thomas Lockhart PostgreSQL Inc.\n"
    if ($opt_version && $opt_verbose || $opt_help);

if ($opt_help) {
    print "$0 --verbose --version --help --path=dir --nodata infile ...\n";
    exit;
}

while (@@ARGV) {
    my $ostem;
	my $oname;
	my $pname;
	my @@xargs;

    $iname = shift @@ARGV;
    $ostem = $iname;
    $ostem = $1 if ($ostem =~ /.+\/([^\/]+)$/);
    $ostem = $1 if ($ostem =~ /(.+)[.][^.]*$/);

    $oname = "$ostem.sql92";
    $pname = "$ostem.init";

	@@xargs = ($iname, $oname);
	push @@xargs, $pname unless ($nodata);

    print "@@xargs\n" if ($debug);

    TransformDumpFile($iname, $oname, $pname);
}

exit;

sub TransformDumpFile {
    local ($iname, $oname, $pname) = @@_;
    local @@dlines;
    local @@slines;
    local @@plines;
    local @@tables; # list of tables created
    local %pkeys;
    local %pseqs;
    local %sequences;

    open(IN, "<$iname") || die "Unable to open file $iname";

    while (<IN>) {
		chomp;
		push @@dlines, $_;
    }

    print("Calling CreateSchema with $#dlines lines\n") if ($debug);
    @@slines = CreateSchema(@@dlines);

    open(OUT, ">$oname") || die "Unable to open output file $oname";

    foreach (@@slines) {
		print "> $_" if ($debug);
		print OUT "$_";
    }
    close(OUT);

    return if (! defined($pname));

    @@plines = PopulateSchema(@@tables);

    open(OUT, ">$pname") || die "Unable to open output file $pname";

    foreach (@@plines) {
		print "> $_" if ($debug);
		print OUT "$_";
    }
    close(OUT);
}

sub PopulateSchema {
    local @@tables = @@_;
    local @@out;
    local $pkey;
    local $pseq;

    foreach (@@tables) {
		$table = $_;
		$tpath = "$pathfrom$table";

		print "Table is $table\n" if ($debug);
		push @@out, "\n";
		push @@out, "copy $table from '$tpath.txt';\n";
		if (defined($pkeys{$table})) {
			foreach ($pkeys{$table}) {
				$pkey = $_;
				$pseq = $pseqs{$table};

				print "Key for $table is $pkey on $pseq\n" if ($debug);

#		//push @@out, "\$value = select max($pkey) from $table;\n";
				push @@out, "select setval ('$pseq', (select max($pkey) from $table));\n";
			}
		}
    }

    return @@out;
}

sub CreateSchema {
    local @@lines = @@_;
    local @@out;

#    undef $last;
    local %knames;

    push @@out, "--\n";
    push @@out, "-- Generated from mysql2pgsql\n";
    push @@out, "-- (c) 2000, Thomas Lockhart, PostgreSQL Inc.\n";
    push @@out, "--\n";
    push @@out, "\n";

    while (@@lines) {
		$_ = shift @@lines;
		print "< $_\n" if ($debug);
		# Replace hash comments with SQL9x standard syntax
		$_ = "-- $1" if (/^[\#](.*)/);

		# Found a CREATE TABLE statement?
		if (/(create\s+table)\s+(\w+)\s+([(])\s*$/i) {
			$table = $2;
			$table = "\"$1\"" if ($table =~ /^(user)$/);
			push @@tables, $table;
			push @@tabledef, "create table $table (";
#	push @@out, "$_\n";

			while (@@lines) {
				$_ = shift @@lines;
				print "< $_\n" if ($debug);

				# Replace int(11) with SQL9x standard syntax
				while (/int\(\d*\)/gi) {
					$_ = "$`integer$'";
				}

				# Replace float(10,2) with SQL9x standard syntax
				while (/(float)\((\d+),\s*(\d+)\)/gi) {
					$_ = "$`$1($2)$'";
				}

				# Replace smallinteger with SQL9x syntax
				while (/smallinteger/gi) {
					$_ = "$`integer$'";
				}

				# Replace mediumtext with PostgreSQL syntax
				while (/(longtext|mediumtext|blob|largeblob)/gi) {
					$_ = "$`text$'";
				}

				# Replace integer ... auto_increment with PostgreSQL syntax
				while (/(\s*)(\w+)\s+integer\s+(.*)\s+auto_increment/gi) {
					$serid = $table . "_pk_seq";
					push @@out, "-- serial identifier $serid will likely be truncated\n"
						if (length($serid) >= 32);

					if (length($serid) >= 32) {
						$excess=(length($serid)-31);
						$serid = substr($table,0,-($excess)) . "_pk_seq";
						push @@out, "-- serial identifier $serid was truncated\n";
					}
                    push @@out, "CREATE SEQUENCE $serid;\n\n";
					$pkeys{$table} = $2;
					$pseqs{$table} = $serid;
					push @@out, "-- key is $pkeys{$table}, sequence is $pseqs{$table}\n" if ($debug);
					$_ = "$`$1$2 integer default nextval('$serid') $3$'";
				}

				# Replace date with double-quoted name
#	    while (/^(\s*)(date|time)(\s+)/gi) {
#		$_ = "$1\"$2\"$3$'";
#	    }

				# Found "KEY"? Then remove it from the CREATE TABLE statement
				# and instead write a CREATE INDEX statement.
				if (/^\s*key\s+(\w+)\s*[(](\w[()\w\d,\s]*)[)][,]?/i) {
					$iname = $1;
					$column = $2;
					$iname = $1 if ($iname =~ /^idx_(\w[\_\w\d]+)/);
					# Sheesh, there can be upper bounds on index string sizes?
					# Get rid of the length specifier (e.g. filename(45) -> filename)
					while ($column =~ /(\w[\w\d])[(]\d+[)]/g) {
						$column = "$`$1$'";
					}
#		$column = $1 if ($column =~ /(\w+)[(]\d+[)]/);
#		push @@out, "Index on $table($column) is $iname\n";
					if (defined($knames{$iname})) {
						push @@out, "-- $iname already exists";
#		    sprintf($iname, "idx_%_%s", $table, $iname);
#		    $iname = "idx_" . $table . "_" . $column;
						# Do not bother with more to the name; it will be too big anyway
						$iname = $table . "_" . $column;
						push @@out, "; use $iname instead\n";
					}
					$knames{$iname} = $iname;
					$keydef{$column} = $iname;
#		push @@out, "! $_\n";
#		$last = $tabledef[$#tabledef];
#		push @@out, "? $#tabledef $last\n";
#		push @@out, "match $1\n" if ($last =~ /(.*),\s*$/);
					# Remove the trailing comma from the previous line, if necessary
					$tabledef[$#tabledef] = $1
						if (($#tabledef > 0) && ($tabledef[$#tabledef] =~ /(.*),\s*$/));
#		push @@out, "? $tabledef[$#tabledef]\n";

					# If this is the end of the statement, save it and exit loop
				} elsif (/^\s*[)]\;/) {
				push @@tabledef, $_;
#		push @@out, "< $_\n";
				last;

				# Otherwise, just save the line
			} else {
#		push @@out, "$last\n" if (defined($last));
#		$last = $_;
				push @@tabledef, $_;
#		push @@out, "$_\n";
			}
		}

			foreach $t (@@tabledef) {
				push @@out, "$t\n";
			}
			undef @@tabledef;

			foreach $k (keys %keydef) {
				push @@out, "create index $keydef{$k} on $table ($k);\n";
			}
			undef %keydef;

		} else {
			push @@out, "$_\n";
		}
    }

#    push @@out, "$last\n" if (defined($last));

    foreach (keys %pkeys) {
		my $val = $pkeys{$_};
		print "key is $val\n" if ($debug);
    }

    return @@out;
}

sub StripComma {
    local $line = shift @@_;

    $line = "$1" if ($line =~ /(.*)[,]\s*$/);

    return $line;
}
@


1.3
log
@Contrib build fixes:

- add some additional files to the dbmirror install (approved by
ssinger)

- add a makefile for contrib/mysql, and add mysql to the list of
contribs build by default

- use xml2-config to pickup -I flags for libxml2 in contrib/xml and
contrib/xml2

Original work from Martin Pitt of Debian, minor cleanups by Neil
Conway.
@
text
@@


1.2
log
@Add "--nodata" option to allow schema conversion only.
@
text
@d1 1
a1 1
# -*- perl -*-
@


1.1
log
@Utility to convert MySQL schema dumps to SQL92 and PostgreSQL conventions.
@
text
@d18 1
a18 1
GetOptions("debug!", "verbose!", "version", "path=s", "help");
d23 1
d33 1
a33 1
    print "$0 --verbose --version --help --path=dir infile ...\n";
d39 3
d51 4
a54 1
    print "$iname $oname $pname\n" if ($debug);
d78 1
a78 1
    print "Calling CreateSchema with $#dlines lines\n" if ($debug);
@
