1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
#!/usr/bin/perl
use warnings;
use strict;
use Alzabo::Create;
use Text::Autoformat qw(autoformat form);
my $name;
unless ( $name = $ARGV[0] )
{
print "Usage: alzabo_to_ascii schema\n";
exit;
}
my $schema = Alzabo::Create::Schema->load_from_file( name => $name );
my @out;
# 60 chars wide
###############################################################################
my $schema_title = <<'EOF';
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-------------------------------------------------------------------------------
EOF
###############################################################################
my $table_title = <<'EOF';
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
-----------------------------------------------------------------------------
\| Name \| Type \| Null? \| Default \| \|
-----------------------------------------------------------------------------
EOF
my $column = <<'EOF';
\| [[[[[[[[[[[[[[[[[[[[[[[[ \| [[[[[[[[[[[[[[[[[[[[[[ \| [[[[[ \| [[[[[[[[ \| [[ \|
EOF
my $column_comment = <<'EOF';
\| - [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ \|
EOF
my $fk_comment = <<'EOF';
\| - [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ \|
EOF
my $lj_table_line = <<'EOF';
\| [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ \|
EOF
render_schema($schema);
print join '', @out;
sub render_schema
{
my $schema = shift;
push @out, form $schema_title,
'Schema: ' . $schema->name . ' (' . $schema->rules->rules_id . ')';
foreach my $t ($schema->tables)
{
render_table($t);
}
}
sub render_table
{
my $t = shift;
# indent 2 spaces
push @out, form $table_title, $t->name;
foreach my $c ($t->columns)
{
render_column($c);
}
push @out, ' ' . '-' x 77;
push @out, "\n";
if ( $t->all_foreign_keys )
{
push @out, form $lj_table_line, 'Foreign keys';
push @out, ' ' . '-' x 77;
push @out, "\n";
foreach my $fk ($t->all_foreign_keys)
{
render_foreign_key($fk);
push @out, ' ' . '-' x 77;
push @out, "\n";
}
}
if ( $t->indexes )
{
push @out, form $lj_table_line, 'Indexes';
push @out, ' ' . '-' x 77;
push @out, "\n";
foreach my $i ($t->indexes)
{
render_index($i);
push @out, ' ' . '-' x 77;
push @out, "\n";
}
}
push @out, "\n";
my $comment = $t->comment;
if ( defined $comment && length $comment )
{
$comment =~ s/\r\n?/\n/g;
$comment =~ s/\n$//;
push @out, autoformat( $comment, { all => 1 } );
push @out, "\n\n";
}
}
sub render_column
{
my $c = shift;
my $type = $c->type;
if ( $c->length )
{
$type .= '(';
$type .= $c->length;
$type .= ', ' . $c->precision if $c->precision;
$type .= ')';
}
if ($c->attributes)
{
$type .= ' ';
$type .= join ' ', sort $c->attributes;
}
push @out, form $column,
( $c->name,
$type,
( $c->nullable ? 'Y' : '' ),
( defined $c->default ? $c->default : ''),
( $c->is_primary_key ? 'PK' : '' )
);
my $comment = $c->comment;
if ( defined $comment && length $comment )
{
push @out, form $column_comment, $comment;
}
}
sub render_foreign_key
{
my $fk = shift;
foreach my $p ( $fk->column_pairs )
{
push @out, form $lj_table_line, $p->[0]->name . ' => ' . $p->[1]->table->name . '.' . $p->[1]->name;
}
my $to = $fk->table_to->name;
my ($amount, $verb);
my $plural = '';
if ( $fk->from_is_dependent )
{
$verb = 'must be';
if ( $fk->is_one_to_many )
{
$amount = 'one or more';
$plural = 's';
}
else
{
$amount = 'one and only one';
}
}
else
{
$verb = 'can be';
if ( $fk->is_one_to_many )
{
$amount = 'zero or more';
$plural = 's';
}
else
{
$amount = 'zero or one';
}
}
push @out, form $lj_table_line, "There $verb $amount corresponding row$plural in the foreign table";
my $comment = $fk->comment;
if ( length $comment )
{
push @out, form $fk_comment, $comment;
}
}
sub render_index
{
my $i = shift;
my @i;
foreach my $c ( $i->columns )
{
my $spec = $c->name;
$spec .= '(' . $i->prefix($c) . ')' if $i->prefix($c);
push @i, $spec;
}
my $out = join ', ', @i;
$out .= ' -- unique' if $i->unique;
$out .= ' -- fulltext' if $i->fulltext;
push @out, form $lj_table_line, $out;
}
|