Skip to content

Commit

Permalink
feat: complete code drawdb
Browse files Browse the repository at this point in the history
  • Loading branch information
il-dat committed Sep 3, 2024
1 parent f594148 commit 08d367c
Show file tree
Hide file tree
Showing 3 changed files with 1,683 additions and 472 deletions.
44 changes: 29 additions & 15 deletions dbterd/adapters/targets/drawdb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
from typing import Tuple
from typing import List, Tuple

from dbterd.adapters import adapter
from dbterd.adapters.meta import Table
from dbterd.types import Catalog, Manifest


Expand Down Expand Up @@ -36,18 +37,7 @@ def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str:
)

# Build DDB content
graphic_tables = dict()
for idx, x in enumerate(tables):
idx_fields = dict()
graphic_tables[x.name] = dict(
id=idx,
x=0, # TODO
y=0, # TODO
fields=idx_fields,
)
for idc, c in enumerate(x.columns):
idx_fields[c.name] = dict(id=idc)

graphic_tables = get_graphic_tables(tables=tables)
drawdb = dict(
author="Generated by dbterd",
title=f"Project ID: {manifest.metadata.project_id}",
Expand All @@ -60,7 +50,7 @@ def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str:
y=graphic_tables.get(x.name, {}).get("y"),
comment=x.description,
indices=[],
color="#6360f7",
color="#175e7a",
fields=[
dict(
id=idc,
Expand All @@ -82,7 +72,7 @@ def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str:
relationships=[
dict(
id=idx,
name="_".join(x.table_map),
name=f"fk__{x.table_map[1]}_{x.table_map[0]}__{x.column_map[1]}",
cardinality=get_rel_symbol(x.type),
startTableId=graphic_tables.get(x.table_map[1], {}).get("id"),
endTableId=graphic_tables.get(x.table_map[0], {}).get("id"),
Expand Down Expand Up @@ -112,6 +102,30 @@ def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str:
return json.dumps(drawdb)


def get_graphic_tables(tables: List[Table]) -> dict:
"""Return the indexed and pre-layouted tables
Args:
tables (List[Table]): List of parsed tables
Returns:
dict: Indexed and Layouted tables
"""
graphic_tables = dict()
for idx, x in enumerate(tables):
idx_fields = dict()
graphic_tables[x.name] = dict(
id=idx,
x=500 * (idx % 4),
y=0.99 * 50 * (len(tables[idx - 4].columns) + 1) * int(idx / 4),
fields=idx_fields,
)
for idc, c in enumerate(x.columns):
idx_fields[c.name] = dict(id=idc)

return graphic_tables


def get_rel_symbol(relationship_type: str) -> str:
"""Get DDB relationship symbol
Expand Down
Loading

0 comments on commit 08d367c

Please sign in to comment.