1+ import os
2+ import sqlite3
3+
4+ OUTPUT_DIR = "models"
5+
6+ def ensure_dir (path ):
7+ if not os .path .exists (path ):
8+ os .makedirs (path )
9+
10+ def map_sqlite_type (sqlite_type : str ):
11+ if not sqlite_type :
12+ return "String"
13+ t = sqlite_type .upper ()
14+ if "INT" in t :
15+ return "Integer"
16+ if "CHAR" in t or "CLOB" in t or "TEXT" in t :
17+ return "String"
18+ if "BLOB" in t :
19+ return "LargeBinary"
20+ if "REAL" in t or "FLOA" in t or "DOUB" in t :
21+ return "Float"
22+ if "DATE" in t or "TIME" in t :
23+ return "DateTime"
24+ return "String"
25+
26+ def export_sys_models (db_path = "admin.sqlite" ):
27+ ensure_dir (OUTPUT_DIR )
28+ conn = sqlite3 .connect (db_path )
29+ cur = conn .cursor ()
30+
31+ # Sve SYS_ tabele iz admin.sqlite baze podataka
32+ cur .execute ("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'SYS_%' ORDER BY name" )
33+ tables = [r [0 ] for r in cur .fetchall ()]
34+
35+ generated = []
36+
37+ for table_name in tables :
38+ cur .execute (f"PRAGMA table_info({ table_name } )" )
39+ columns = cur .fetchall ()
40+ if not columns :
41+ continue
42+
43+ class_name = "" .join ([w .capitalize () for w in table_name .lower ().split ("_" )])
44+ generated .append ((table_name , class_name ))
45+
46+ lines = [
47+ "from sqlalchemy import String, Integer, Float, LargeBinary, DateTime, Boolean, Text" ,
48+ "from sqlalchemy.orm import Mapped, mapped_column" ,
49+ "from . import Base" ,
50+ "" ,
51+ f"class { class_name } (Base):" ,
52+ f" __tablename__ = '{ table_name } '" ,
53+ "" ,
54+ ]
55+
56+ for cid , name , col_type , notnull , default_value , pk in columns :
57+ sa_type = map_sqlite_type (col_type )
58+ pk_flag = ", primary_key=True" if pk else ""
59+ nullable = "" if notnull else ", nullable=True"
60+ default = f", default={ default_value } " if default_value else ""
61+ lines .append (f" { name } : Mapped[{ sa_type } ] = mapped_column({ sa_type } { pk_flag } { nullable } { default } )" )
62+
63+ lines .append ("" )
64+ lines .append (" def __repr__(self):" )
65+ lines .append (f" return f\" <{ class_name } (id={{getattr(self, 'ID', '?')}})>\" " )
66+ lines .append ("" )
67+
68+ out_path = os .path .join (OUTPUT_DIR , f"{ table_name .lower ()} .py" )
69+ with open (out_path , "w" , encoding = "utf-8" ) as f :
70+ f .write ("\n " .join (lines ))
71+ print (f"Generisano: { out_path } " )
72+
73+ # --- automatski __init__.py ---
74+ init_lines = [
75+ "# Auto-generated: Import All SYS_* Models" ,
76+ "from sqlalchemy.orm import DeclarativeBase" ,
77+ "" ,
78+ "class Base(DeclarativeBase):" ,
79+ " pass" ,
80+ "" ,
81+ ]
82+ for table_name , class_name in generated :
83+ init_lines .append (f"from .{ table_name .lower ()} import { class_name } " )
84+ init_lines .append ("" )
85+ init_lines .append ("__all__ = [" )
86+ for _ , class_name in generated :
87+ init_lines .append (f" '{ class_name } '," )
88+ init_lines .append ("]" )
89+ init_lines .append ("" )
90+
91+ init_path = os .path .join (OUTPUT_DIR , "__init__.py" )
92+ with open (init_path , "w" , encoding = "utf-8" ) as f :
93+ f .write ("\n " .join (init_lines ))
94+
95+ conn .close ()
96+ print ("\n ✅ Gotovo! Sve SYS_* ORM klase koriste zajednički Base iz models/__init__.py." )
97+
98+ if __name__ == "__main__" :
99+ export_sys_models ("admin.sqlite" )
0 commit comments