1GraphViz2::DBI(3) User Contributed Perl Documentation GraphViz2::DBI(3)
2
3
4
6 GraphViz2::DBI - Visualize a database schema as a graph
7
9 #!/usr/bin/env perl
10
11 use strict;
12 use warnings;
13
14 use DBI;
15
16 use GraphViz2;
17 use GraphViz2::DBI;
18
19 use Log::Handler;
20
21 # ---------------
22
23 exit 0 if (! $ENV{DBI_DSN});
24
25 my($logger) = Log::Handler -> new;
26
27 $logger -> add
28 (
29 screen =>
30 {
31 maxlevel => 'debug',
32 message_layout => '%m',
33 minlevel => 'error',
34 }
35 );
36
37 my($graph) = GraphViz2 -> new
38 (
39 edge => {color => 'grey'},
40 global => {directed => 1},
41 graph => {rankdir => 'TB'},
42 logger => $logger,
43 node => {color => 'blue', shape => 'oval'},
44 );
45 my($attr) = {};
46 $$attr{sqlite_unicode} = 1 if ($ENV{DBI_DSN} =~ /SQLite/i);
47 my($dbh) = DBI -> connect($ENV{DBI_DSN}, $ENV{DBI_USER}, $ENV{DBI_PASS}, $attr);
48
49 $dbh -> do('PRAGMA foreign_keys = ON') if ($ENV{DBI_DSN} =~ /SQLite/i);
50
51 my($g) = GraphViz2::DBI -> new(dbh => $dbh, graph => $graph);
52
53 $g -> create(name => '');
54
55 my($format) = shift || 'svg';
56 my($output_file) = shift || File::Spec -> catfile('html', "dbi.schema.$format");
57
58 $graph -> run(format => $format, output_file => $output_file);
59
60 See scripts/dbi.schema.pl ("Scripts Shipped with this Module" in
61 GraphViz2).
62
63 The image html/dbi.schema.svg was generated from the database tables of
64 my module App::Office::Contacts.
65
67 Takes a database handle, and graphs the schema.
68
69 You can write the result in any format supported by Graphviz
70 <http://www.graphviz.org/>.
71
72 Here is the list of output formats
73 <http://www.graphviz.org/content/output-formats>.
74
76 This module is available as a Unix-style distro (*.tgz).
77
78 See <http://savage.net.au/Perl-modules/html/installing-a-module.html>
79 for help on unpacking and installing distros.
80
82 Install GraphViz2 as you would for any "Perl" module:
83
84 Run:
85
86 cpanm GraphViz2
87
88 or run:
89
90 sudo cpan GraphViz2
91
92 or unpack the distro, and then either:
93
94 perl Build.PL
95 ./Build
96 ./Build test
97 sudo ./Build install
98
99 or:
100
101 perl Makefile.PL
102 make (or dmake or nmake)
103 make test
104 make install
105
107 Calling new()
108 "new()" is called as "my($obj) = GraphViz2::DBI -> new(k1 => v1, k2 =>
109 v2, ...)".
110
111 It returns a new object of type "GraphViz2::DBI".
112
113 Key-value pairs accepted in the parameter list:
114
115 o dbh => $dbh
116 This options specifies the database handle to use.
117
118 This key is mandatory.
119
120 o graph => $graphviz_object
121 This option specifies the GraphViz2 object to use. This allows you
122 to configure it as desired.
123
124 The default is GraphViz2 -> new. The default attributes are the
125 same as in the synopsis, above, except for the graph label of
126 course.
127
128 This key is optional.
129
131 create(exclude => [], include => [], name => $name)
132 Creates the graph, which is accessible via the graph() method, or via
133 the graph object you passed to new().
134
135 Returns $self to allow method chaining.
136
137 Parameters:
138
139 o exclude
140 An optional arrayref of table names to exclude.
141
142 If none are listed for exclusion, all tables are included.
143
144 o include
145 An optional arrayref of table names to include.
146
147 If none are listed for inclusion, all tables are included.
148
149 o name
150 $name is the string which will be placed in the root node of the
151 tree. It may be omitted, in which case the root node is omitted.
152
153 graph()
154 Returns the graph object, either the one supplied to new() or the one
155 created during the call to new().
156
158 Why did I get an error about 'Unable to find primary key'?
159 For plotting foreign keys, the code has an algorithm to find the
160 primary table/key pair which the foreign table/key pair point to.
161
162 The steps are listed here, in the order they are tested. The first
163 match stops the search.
164
165 o Check a hash for special cases
166 Currently, the only special case is a foreign key of "spouse_id".
167 It is assumed to point to a primary key called "person_id".
168
169 There is no option available, at the moment, to override this
170 check.
171
172 o Ask the database for foreign key information
173 DBIx::Admin::TableInfo is used for this.
174
175 o Take a guess
176 Assume the foreign key points to a table with a column called "id",
177 and use that as the primary key.
178
179 o Die with a detailed error message
180
181 Which versions of the servers did you test?
182 See "FAQ" in DBIx::Admin::TableInfo.
183
184 Does GraphViz2::DBI work with SQLite databases?
185 Yes. See "FAQ" in DBIx::Admin::TableInfo.
186
187 What is returned by SQLite's "pragma foreign_key_list($table_name)"?
188 See "FAQ" in DBIx::Admin::TableInfo.
189
190 How does GraphViz2::DBI draw edges from foreign keys to primary keys?
191 It assumes that the primary table's name is a plural word, and that the
192 foreign key's name is prefixed by the singular of the primary table's
193 name, separated by '_'.
194
195 Thus a (primary) table 'people' with a primary key 'id' will be pointed
196 to by a table 'phone_numbers' using a column 'person_id'.
197
198 Table 'phone_numbers' will probably have a primary key 'id' but that is
199 not used (unless some other table has a foreign key pointing to the
200 'phone_numbers' table).
201
202 The conversion of plural to singular is done with
203 Lingua::EN::PluralToSingular.
204
205 If this naming convention does not hold, then both the source and
206 destination ports default to '1', which is the port of the 1st column
207 (in alphabetical order) in each table. The table name itself is port
208 '0'.
209
210 Are any sample scripts shipped with this module?
211 Yes. See "FAQ" in GraphViz2 and "Scripts Shipped with this Module" in
212 GraphViz2.
213
215 Many thanks to the people who chose to make Graphviz
216 <http://www.graphviz.org/> Open Source.
217
218 And thanks to Leon Brocard <http://search.cpan.org/~lbrocard/>, who
219 wrote GraphViz, and kindly gave me co-maint of the module.
220
222 Version numbers < 1.00 represent development versions. From 1.00 up,
223 they are production versions.
224
226 The file Changes was converted into Changelog.ini by
227 Module::Metadata::Changes.
228
230 Email the author, or log a bug on RT:
231
232 <https://rt.cpan.org/Public/Dist/Display.html?Name=GraphViz2>.
233
235 GraphViz2 was written by Ron Savage <ron@savage.net.au> in 2011.
236
237 Home page: <http://savage.net.au/index.html>.
238
240 Australian copyright (c) 2011, Ron Savage.
241
242 All Programs of mine are 'OSI Certified Open Source Software';
243 you can redistribute them and/or modify them under the terms of
244 The Perl License, a copy of which is available at:
245 http://dev.perl.org/licenses/
246
247
248
249perl v5.30.1 2020-01-30 GraphViz2::DBI(3)