Added YAML parser, producer, and basic test. All need more work!
Darren Chamberlain [Wed, 8 Oct 2003 16:33:13 +0000 (16:33 +0000)]
lib/SQL/Translator/Parser/YAML.pm [new file with mode: 0644]
lib/SQL/Translator/Producer/YAML.pm [new file with mode: 0644]
t/24yaml.t [new file with mode: 0644]

diff --git a/lib/SQL/Translator/Parser/YAML.pm b/lib/SQL/Translator/Parser/YAML.pm
new file mode 100644 (file)
index 0000000..fe996cb
--- /dev/null
@@ -0,0 +1,58 @@
+package SQL::Translator::Parser::YAML;
+
+# -------------------------------------------------------------------
+# $Id: YAML.pm,v 1.1 2003-10-08 16:33:13 dlc Exp $
+# -------------------------------------------------------------------
+# Copyright (C) 2003 darren chamberlain <darren@cpan.org>,
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; version 2.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+# 02111-1307  USA
+# -------------------------------------------------------------------
+
+use strict;
+use vars qw($VERSION);
+$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+
+use SQL::Translator::Schema;
+use SQL::Translator::Utils qw(header_comment);
+use YAML;
+
+sub parse {
+    my ($translator, $data) = @_;
+    my $schema = $translator->schema;
+    my $data = Load($data);
+
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+SQL::Translator::Parser::YAML - Parse a YAML representation of a schema
+
+=head1 SYNOPSIS
+
+    use SQL::Translator;
+
+    my $translator = SQL::Translator->new(parser => "YAML");
+
+=head1 DESCRIPTION
+
+C<SQL::Translator::Parser::YAML> parses a schema serialized with YAML.
+
+=head1 AUTHOR
+
+Darren Chamberlain E<lt>darren@cpan.orgE<gt>
diff --git a/lib/SQL/Translator/Producer/YAML.pm b/lib/SQL/Translator/Producer/YAML.pm
new file mode 100644 (file)
index 0000000..12a08c5
--- /dev/null
@@ -0,0 +1,77 @@
+package SQL::Translator::Producer::YAML;
+
+# -------------------------------------------------------------------
+# $Id: YAML.pm,v 1.1 2003-10-08 16:33:13 dlc Exp $
+# -------------------------------------------------------------------
+# Copyright (C) 2003 darren chamberlain <darren@cpan.org>,
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; version 2.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+# 02111-1307  USA
+# -------------------------------------------------------------------
+
+use strict;
+use vars qw($VERSION);
+$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+
+use SQL::Translator::Utils qw(header_comment);
+
+sub produce {
+    my $translator  = shift;
+    my $schema      = $translator->schema;
+
+    return 
+        join "\n" => 
+            '--- #YAML:1.0',
+            #header_comment('', '# '),
+            map { view_table($_) } $schema->get_tables;
+}
+
+sub view_table {
+    my $table = shift;
+
+    return
+        sprintf "%s:\n%s\n",
+            $table->name,
+            join "\n" =>
+                map { "    $_" }
+                map { view_field($_) } $table->get_fields;
+}
+
+sub view_field {
+    my $field = shift;
+
+    return
+        sprintf("%s: %s" => $field->name),
+        map {
+            sprintf "    %s: %s" => $_->[0], view($_->[1])
+        } (
+            [ 'order' =>   $field->order        ],
+            [ 'name'  =>   $field->name         ],
+            [ 'type'  =>   $field->data_type    ],
+            [ 'size'  => [ $field->size  ]      ],
+            [ 'extra' => { $field->extra }      ],
+        );
+}
+
+sub view {
+    my $thingie = shift;
+
+    {   ''       => sub { $_[0] },
+        'SCALAR' => sub { ${$_[0]} },
+        'ARRAY'  => sub { join "\n    - $_", @{$_[0]} },
+        'HASH'   => sub { join "\n    " => map { "$_: $_[0]->{$_}" } keys %{$_[0]} },
+    }->{ref $thingie}->($thingie);
+}
+
+1;
diff --git a/t/24yaml.t b/t/24yaml.t
new file mode 100644 (file)
index 0000000..be6e333
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/local/bin/perl
+# vim: set ft=perl:
+
+use strict;
+use Test::More tests => 2;
+use Test::Differences;
+use SQL::Translator;
+
+my $create = q|
+CREATE TABLE random (
+    id int auto_increment PRIMARY KEY,
+    foo varchar(255) not null default '',
+    updated timestamp
+);
+|;
+
+my $yaml = q|--- #YAML:1.0
+random:
+    id: 
+        order: 1
+        name: id
+        type: int
+        size: 11
+        extra: 
+    foo: 
+        order: 2
+        name: foo
+        type: varchar
+        size: 255
+        extra: 
+    updated: 
+        order: 3
+        name: updated
+        type: timestamp
+        size: 0
+        extra: 
+|;
+
+my $out;
+my $tr = SQL::Translator->new(
+    parser   => "MySQL",
+    producer => "YAML"
+);
+
+
+ok($out = $tr->translate(\$create), 'Translate MySQL to YAML');
+eq_or_diff($out, $yaml, 'YAML matches expected');
+