QIDNLF commited on
Commit
76083b9
ยท
verified ยท
1 Parent(s): 428614d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -12
app.py CHANGED
@@ -56,22 +56,16 @@ def fetch_available_standards():
56
 
57
  return sorted(list(set([d["standard"] for d in db_registry])))
58
 
 
 
 
59
  def fetch_database_records(std, ver, cat, table_type):
60
  try:
61
  db_path = os.path.join(UPLOAD_DIR, f"{std}_{ver}.db")
62
  conn = sqlite3.connect(db_path)
63
  conn.text_factory = decode_sqlite_text
64
 
65
- conn_map = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
66
- config_df = pd.read_sql("SELECT * FROM Table_Config WHERE TRIM(Table_Type)=?", conn_map, params=[table_type.strip()])
67
- conn_map.close()
68
-
69
- if config_df.empty:
70
- return pd.DataFrame({"Error": [f"Table_Config์—์„œ '{table_type}' ์„ค์ •์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."]}), []
71
-
72
- anchors = [x.strip() for x in config_df.iloc[0]['Anchor_Column'].split(',')]
73
- displays = [x.strip() for x in config_df.iloc[0]['Display_Columns'].split(',')] if pd.notna(config_df.iloc[0]['Display_Columns']) else anchors
74
-
75
  tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
76
  valid_tables = [t for t in tables if t.lower() not in ['mapping_registry', 'mapping_table', 'table_config', 'sqlite_sequence']]
77
  main_table = f"{std}_{ver}"
@@ -79,13 +73,37 @@ def fetch_database_records(std, ver, cat, table_type):
79
  main_table = valid_tables[0] if valid_tables else None
80
 
81
  if not main_table:
 
82
  return pd.DataFrame({"Error": ["๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."]}), []
83
 
84
- query = f"SELECT * FROM [{main_table}]"
85
-
86
  cols = pd.read_sql(f"PRAGMA table_info([{main_table}])", conn)['name'].tolist()
87
  lower_cols = [c.lower() for c in cols]
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  conditions = []
90
  if cat and cat != "ALL":
91
  if "." in cat and 'chapter' in lower_cols and 'category' in lower_cols:
@@ -103,6 +121,7 @@ def fetch_database_records(std, ver, cat, table_type):
103
  df = pd.read_sql(query, conn)
104
  conn.close()
105
 
 
106
  final_cols = []
107
  for d in displays:
108
  for c in df.columns:
 
56
 
57
  return sorted(list(set([d["standard"] for d in db_registry])))
58
 
59
+ # ==========================================
60
+ # ๐Ÿ’ก [์Šค๋งˆํŠธ ์„ค์ • ํƒ์ƒ‰ ๊ธฐ๋Šฅ์ด ํƒ‘์žฌ๋œ ๋ฐ์ดํ„ฐ ๋กœ๋”]
61
+ # ==========================================
62
  def fetch_database_records(std, ver, cat, table_type):
63
  try:
64
  db_path = os.path.join(UPLOAD_DIR, f"{std}_{ver}.db")
65
  conn = sqlite3.connect(db_path)
66
  conn.text_factory = decode_sqlite_text
67
 
68
+ # 1. ์‹ค์ œ DB ํ…Œ์ด๋ธ”๊ณผ ์—ด(Column) ์ด๋ฆ„์„ ๋จผ์ € ํ›‘์–ด๋ด…๋‹ˆ๋‹ค.
 
 
 
 
 
 
 
 
 
69
  tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
70
  valid_tables = [t for t in tables if t.lower() not in ['mapping_registry', 'mapping_table', 'table_config', 'sqlite_sequence']]
71
  main_table = f"{std}_{ver}"
 
73
  main_table = valid_tables[0] if valid_tables else None
74
 
75
  if not main_table:
76
+ conn.close()
77
  return pd.DataFrame({"Error": ["๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."]}), []
78
 
 
 
79
  cols = pd.read_sql(f"PRAGMA table_info([{main_table}])", conn)['name'].tolist()
80
  lower_cols = [c.lower() for c in cols]
81
 
82
+ # 2. Table_Config๋ฅผ ์ฝ์–ด์˜ต๋‹ˆ๋‹ค.
83
+ conn_map = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
84
+ config_df = pd.read_sql("SELECT * FROM Table_Config WHERE TRIM(Table_Type)=?", conn_map, params=[table_type.strip()])
85
+ conn_map.close()
86
+
87
+ if config_df.empty:
88
+ return pd.DataFrame({"Error": [f"Table_Config์—์„œ '{table_type}' ์„ค์ •์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."]}), []
89
+
90
+ # ๐Ÿ’ก [ํ•ต์‹ฌ ํ•ด๊ฒฐ] Table_Config์— 'Main'์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ, ์‹ค์ œ DB ์—ด ์ด๋ฆ„๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์„ ๋˜‘๋˜‘ํ•˜๊ฒŒ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค!
91
+ matched_config = None
92
+ for _, row in config_df.iterrows():
93
+ anchors_test = [x.strip().lower() for x in str(row['Anchor_Column']).split(',')]
94
+ if any(a in lower_cols for a in anchors_test):
95
+ matched_config = row
96
+ break
97
+
98
+ if matched_config is None:
99
+ matched_config = config_df.iloc[0] # ๋ชป ์ฐพ์œผ๋ฉด ์–ด์ฉ” ์ˆ˜ ์—†์ด ์ฒซ ๋ฒˆ์งธ ๊ฐ•์ œ ์‚ฌ์šฉ
100
+
101
+ anchors = [x.strip() for x in matched_config['Anchor_Column'].split(',')]
102
+ displays = [x.strip() for x in matched_config['Display_Columns'].split(',')] if pd.notna(matched_config['Display_Columns']) else anchors
103
+
104
+ # 3. ์ฟผ๋ฆฌ ์‹คํ–‰ ๋ฐ ์นดํ…Œ๊ณ ๋ฆฌ ํ•„ํ„ฐ๋ง
105
+ query = f"SELECT * FROM [{main_table}]"
106
+
107
  conditions = []
108
  if cat and cat != "ALL":
109
  if "." in cat and 'chapter' in lower_cols and 'category' in lower_cols:
 
121
  df = pd.read_sql(query, conn)
122
  conn.close()
123
 
124
+ # 4. ํ™”๋ฉด์— ๋ณด์—ฌ์ค„ ์—ด ์ •๋ฆฌ
125
  final_cols = []
126
  for d in displays:
127
  for c in df.columns: