Skip to content

Commit 7ede039

Browse files
authored
Merge pull request #3 from rrad0812/develop
rrad0812 - utils/admin_exporter
2 parents 8763fda + eb59acc commit 7ede039

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

utils/admin_exporter/admin.sqlite

138 KB
Binary file not shown.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from sqlalchemy import create_engine, inspect, select
2+
from sqlalchemy.orm import Session
3+
from models import Base
4+
5+
DB_PATH = "admin.sqlite"
6+
7+
engine = create_engine(f"sqlite:///{DB_PATH}", echo=False, future=True)
8+
9+
def main():
10+
print("Povezujem se na:", DB_PATH)
11+
Base.metadata.create_all(engine)
12+
13+
inspector = inspect(engine)
14+
tables = inspector.get_table_names()
15+
16+
print("\n📋 Pronađene tabele u bazi:")
17+
for t in tables:
18+
print(f" • {t}")
19+
20+
with Session(engine) as session:
21+
print("\n🔍 Brz pregled SYS_ tabela:")
22+
for cls in Base.registry.mappers:
23+
model = cls.class_
24+
table_name = model.__tablename__
25+
try:
26+
count = session.execute(select(model)).fetchall()
27+
print(f" {table_name:<25}{len(count)} redova")
28+
except Exception as e:
29+
print(f" {table_name:<25} {e.__class__.__name__}: {e}")
30+
31+
print("\nORM povezivanje završeno.")
32+
33+
if __name__ == "__main__":
34+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python3 admin_exporter.py
2+
python3 admin_loader.py

0 commit comments

Comments
 (0)