Skip to content

Instantly share code, notes, and snippets.

@mix3
Created March 3, 2016 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mix3/b5cc90a552776d8501c5 to your computer and use it in GitHub Desktop.
Save mix3/b5cc90a552776d8501c5 to your computer and use it in GitHub Desktop.
requires "SQL::Translator::Producer::PlantUML";
requires "DBD::mysql";
CREATE TABLE user (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(191) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
CREATE TABLE item (
id INTEGER UNSIGNED NOT NULL,
name VARCHAR(191) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
CREATE TABLE user_item (
user_id INTEGER UNSIGNED NOT NULL,
item_id INTEGER UNSIGNED NOT NULL,
amount INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (user_id, item_id),
FOREIGN KEY (user_id) REFERENCES user (id),
FOREIGN KEY (item_id) REFERENCES item (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
CREATE TABLE user_item_history (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INTEGER UNSIGNED NOT NULL,
item_id INTEGER UNSIGNED NOT NULL,
how_get INTEGER UNSIGNED NOT NULL,
how_out INTEGER UNSIGNED NOT NULL,
amount INTEGER UNSIGNED NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id, item_id) REFERENCES user_item (user_id, item_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use DBI;
use SQL::Translator;
my $dbh = DBI->connect("dbi:mysql:dbname=example", "root", "", {
AutoCommit => 1,
PrintError => 0,
RaiseError => 1,
ShowErrorStatement => 1,
AutoInactiveDestroy => 1,
mysql_enable_utf8 => 1,
mysql_auto_reconnect => 0,
});
my $got = SQL::Translator->new(
parser => 'DBI',
parser_args => { dbh => $dbh },
to => 'PlantUML',
)->translate;
$got =~ s/^\s*\n//mg;
print $got;
exit;
@mix3
Copy link
Author

mix3 commented Mar 3, 2016

$ mysql -u root -e 'create database example'
$ mysql -u root example < ddl
$ carton exec perl example.pl
@startuml
object item {
    - id
    name
}
object user {
    - id
    name
}
object user_item {
    - user_id (FK)
    - item_id (FK)
    amount
}
object user_item_history {
    - id
    user_id (FK)
    item_id (FK)
    how_get
    how_out
    amount
    created_at
}
user --o user_item
item --o user_item
user_item --o user_item_history
@enduml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment