添加一些 xml parse的东西

This commit is contained in:
shenjack 2024-08-29 02:18:50 +08:00
parent adc35e8f37
commit fe28a0b8ba
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 49 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target
config.toml
sr_download.exe
env

View File

@ -0,0 +1,48 @@
from __future__ import annotations
import psycopg2
import tomli
from lib_not_dr import loggers
with open("../../config.toml", "rb") as f:
CONFIG = tomli.load(f)
logger = loggers.config.get_logger("xml_parse")
def get_db():
connect = psycopg2.connect(
CONFIG["db"]["url"]
)
return connect
def main():
db = get_db()
db_cur = db.cursor()
xml_fetch = """
WITH limited_full_data AS (
SELECT save_id, data
FROM public.full_data
WHERE "save_type" != 'none'
AND xml_is_well_formed_document(full_data."data")
LIMIT 20
)
SELECT limited_full_data.save_id, array_agg(x.part_type) AS part_types, array_agg(x.part_id) AS part_ids
FROM limited_full_data,
XMLTABLE (
'//Ship/Parts/Part'
PASSING BY VALUE xmlparse(document limited_full_data."data")
COLUMNS part_type text PATH '@partType',
part_id text PATH '@id'
) AS x
GROUP BY limited_full_data.save_id;
"""
db_cur.execute(xml_fetch)
logger.info(db_cur.fetchall())
...
main()