import os import sys import xml.etree.ElementTree as ET INDENT = " " NEWLINE = "\n" OUTPUT_DIR = "C:/5/docs" # TABLE DOCUMENTATION def write_table_doc(table: ET.Element): table_name = table.get("Name") class_name = table.get("ClassName") comment = table.get("Comment", "") lines = [] # HEADER lines.append(f"# {class_name or table_name}") lines.append("") if comment: lines.append(comment) lines.append("") lines.append(f"**Database table:** `{table_name}`") lines.append(f"**Model class:** `{class_name}`") lines.append("") # COLUMNS lines.append("## Fields") lines.append("") lines.append("| Field | Column | Type | Nullable | Description |") lines.append("|-------|--------|------|----------|-------------|") columns = {} for column in table.findall("Column"): col_name = column.get("Name") field_name = column.get("FieldName") db_type = column.get("DatabaseType") nullable = column.get("Nullable", "false").lower() == "true" comment = column.get("Comment", "") columns[col_name] = column lines.append( f"| {field_name} | {col_name} | {db_type} | {str(nullable)} | {comment} |" ) lines.append("") return NEWLINE.join(lines) # FOREIGN KEYS def write_relations(table: ET.Element, table_files: dict): fks = table.findall("ForeignKey") if not fks: return "" lines = [] lines.append("## Relations") lines.append("") for fk in fks: from_col = fk.get("FromColumn") to_class = fk.get("ToClassName") field_name = fk.get("FieldName") target_file = table_files.get(to_class, f"{to_class}.md") lines.append( f"- `{field_name}` → [{to_class}]({target_file}) via `{from_col}`" ) lines.append("") return NEWLINE.join(lines) # NAMED ROWS def write_named_rows(table: ET.Element): named_rows = table.findall("NamedRow") if not named_rows: return "" lines = [] lines.append("## Named Rows") lines.append("") all_rows = [] columns = [] seen = set() for nr in named_rows: attrib = nr.attrib if not attrib: continue all_rows.append(attrib) for k in attrib.keys(): if k not in seen: seen.add(k) columns.append(k) if not all_rows: return "" lines.append("| " + " | ".join(columns) + " |") lines.append("| " + " | ".join(["---"] * len(columns)) + " |") for r in all_rows: lines.append("| " + " | ".join(r.get(c, "") for c in columns) + " |") lines.append("") return NEWLINE.join(lines) def ensure_dir(path): os.makedirs(path, exist_ok=True) xml_data = sys.stdin.read() tree = ET.ElementTree(ET.fromstring(xml_data)) root = tree.getroot() output_dir = os.path.abspath(OUTPUT_DIR) ensure_dir(output_dir) table_files = {} index_lines = [] index_lines.append("# Database Documentation") index_lines.append("") index_lines.append("## Tables") index_lines.append("") for db in root.findall("Database"): for schema in db.findall("Schema"): for table in schema.findall("Table"): table_name = table.get("Name") class_name = table.get("ClassName") file_name = f"{class_name or table_name}.md" table_files[class_name or table_name] = file_name file_path = os.path.join(output_dir, "entities", file_name) ensure_dir(os.path.dirname(file_path)) md = write_table_doc(table) md += write_relations(table, table_files) md += write_named_rows(table) with open(file_path, "w", encoding="utf-8") as f: f.write(md) index_lines.append(f"- [{class_name or table_name}](entities/{file_name})") # write index with open(os.path.join(output_dir, "index.md"), "w", encoding="utf-8") as f: f.write(NEWLINE.join(index_lines))