JDB/Graph

From J Wiki
< JDB
Jump to: navigation, search
JDB: Layout Columns | Queries Σ | API: Structural Records | Client/Server | Implementation | Examples

graphdb generates schema graph of database tables.

It is a simple script based on graphics/graphviz addon. [{{#file: ""}} Download script: ]

NB. graphdb - schema graph

require 'strings graphics/graphviz'

«template»

«code»

Note 'Examples'  NB. use build/init verbs unless already built
  «example»
)

The script implementation is at the bottom of the page.

The following sections show schema graphs for JDB examples.

sandp

[{{#file: ""}} Download script: ]

  load'data/jdb'
    buildsandp_jdb_ jpath'~temp'
  opensandp_jdb_ jpath'~temp'
  makedb locD
  makegraph makedb locD
  graphdb locD

Sandp.png

sed

[{{#file: ""}} Download script: ]

  load'data/jdb/sed'
    initsed jpath'~temp'
  opensed jpath'~temp'
  graphdb locD

Sed.png

vr

[{{#file: ""}} Download script: ]

  load'data/jdb/vr'
    buildvr 1000;jpath '~temp'
  openvr jpath'~temp'
  graphdb d

Vr.png

northwind

[{{#file: ""}} Download script: ]

  load'data/jdb/northwind'
    db_init jpath '~temp'
  db_open jpath'~temp'
  graphdb D

This particular layout is customized in Graph View by setting rankdir=LR and fontname=Verdana, fontsize=8.5. It was then scaled in image editor.

Northwind.png

Script Implementation

Templates contain the structure and styles of Graphviz markup. [{{#file: ""}} Download script: ]

NODESTYLE=: 0 : 0
  shape=record,
  fontname=Arial,fontsize=9,
  margin="0.05,0.02",
  style=filled,fillcolor="#ffffb0",color="#808080"
)

GRAPH=: 0 : 0
digraph g {
  rankdir=TB;
  portPos="e";
  node [%S];
%T
}
)

The code itself generates Graphviz nodes and edges based on JDB column structure. [{{#file: ""}} Download script: ]

makedb=: 3 : 0
  t=. r=. ''
  for_tn. Tables__y do.
    tm=. >tn
    'tb cl tp un pr pk'=. {:"1 ReadCols__y tm
    t=. t,'  ',tm,' [label="{{',tm,'}|{'
    for_c. cl do.
      cm=. >c
      if. #p=. c_index{::pr do.
        t=. t,'<',cm,'>'
        r=. r,'  ',tm,':',cm,' -> ',p,';',LF end.
      if. c_index{::un do. t=. t,'*' end.
      t=. t,cm,'\l'
      if. c_index < <:#cl do. t=. t,'|' end.
    end.
    t=. t,'}}"];',LF
  end.
  t , r
)

makegraph=: 3 : 0
  GRAPH rplc '%S';NODESTYLE;'%T';y
)

graphdb=: graphview@makegraph@makedb