Update app.py
Browse files
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 |
-
|
| 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:
|