Skip to content

Commit 578eef1

Browse files
hf-kkleinKonstantin
andauthored
feat: join v_ahbesser with newly introduced ahb_expressions table (#125)
* expressions WIP * chore: include AHB PK in materialized AHB view * wip * wip * wip * wip * args * linter pain * Delete src/fundamend/sqlmodels/create_ahb_expressions_table.sql * isort * spell check * formating linter conflict * Update src/fundamend/sqlmodels/expression_view.py * enable type checking again * use order by in snapshot test for reproducible, stable results * linters * wip * wip * don't include pruefi, drop empty rows * arg. this turned out to be more complicated than i thought ;) * feat: join `v_ahbesser` with newly introduced `ahb_expressions` table * linter * ooooooooooooooook * Update src/fundamend/sqlmodels/create_ahbtabellen_view.sql --------- Co-authored-by: Konstantin <[email protected]>
1 parent e8f069d commit 578eef1

File tree

4 files changed

+456
-21
lines changed

4 files changed

+456
-21
lines changed

src/fundamend/sqlmodels/ahbtabellen_view.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pathlib import Path
99
from uuid import UUID
1010

11-
from efoli import EdifactFormatVersion
11+
from efoli import EdifactFormat, EdifactFormatVersion
1212
from sqlalchemy.sql.functions import func
1313
from sqlmodel import Field, Session, SQLModel, select
1414

@@ -42,6 +42,7 @@ class AhbTabellenLine(SQLModel, table=True):
4242
__tablename__ = "v_ahbtabellen"
4343
id: UUID = Field(primary_key=True)
4444
format_version: EdifactFormatVersion = Field()
45+
format: EdifactFormat = Field()
4546
pruefidentifikator: str = Field()
4647
path: str = Field()
4748
id_path: str = Field()
@@ -53,6 +54,8 @@ class AhbTabellenLine(SQLModel, table=True):
5354
qualifier: str | None = Field()
5455
line_ahb_status: str | None = Field()
5556
line_name: str | None = Field()
57+
bedingung: str | None = Field()
58+
bedingungsfehler: str | None = Field()
5659
sort_path: str = Field()
5760

5861

src/fundamend/sqlmodels/create_ahbtabellen_view.sql

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,53 @@
44

55
DROP TABLE IF EXISTS v_ahbtabellen; -- this is because sqlmodel tries to create a table first... it doesn't know that this is just a view. bit dirty but ok.
66
DROP VIEW IF EXISTS v_ahbtabellen;
7-
CREATE VIEW v_ahbtabellen as
8-
SELECT id as id,
9-
edifact_format_version as format_version,
10-
pruefidentifikator as pruefidentifikator,
11-
path,
12-
id_path,
13-
kommunikation_von as direction,
14-
beschreibung as description,
15-
'SG' || segmentgroup_id as segmentgroup_key, -- eg 'SG6'
16-
segment_id as segment_code, -- e.g 'NAD'
17-
dataelement_id as data_element, -- e.g 'D_3035'
7+
8+
CREATE VIEW v_ahbtabellen AS
9+
WITH consolidated_ahm AS (SELECT id,
10+
edifact_format_version,
11+
format,
12+
pruefidentifikator,
13+
path,
14+
id_path,
15+
kommunikation_von,
16+
beschreibung,
17+
segmentgroup_id,
18+
segment_id,
19+
dataelement_id,
20+
code_value,
21+
sort_path,
22+
trim(coalesce(code_ahb_status, coalesce(dataelement_ahb_status,
23+
coalesce(segment_ahb_status, segmentgroup_ahb_status)))) AS line_ahb_status,
24+
coalesce(code_name, coalesce(dataelement_name, coalesce(dataelementgroup_name,
25+
coalesce(segment_name, segmentgroup_name)))) AS line_name
26+
FROM ahb_hierarchy_materialized ahm
27+
WHERE ahm.TYPE != 'dataelementgroup'
28+
AND (ahm.TYPE != 'dataelement' OR ahm.dataelement_ahb_status IS NOT NULL))
29+
30+
SELECT c.id as id,
31+
c.edifact_format_version as format_version,
32+
c.format as format,
33+
c.pruefidentifikator as pruefidentifikator,
34+
c.path,
35+
c.id_path,
36+
c.kommunikation_von as direction,
37+
c.beschreibung as description,
38+
'SG' || c.segmentgroup_id as segmentgroup_key, -- eg 'SG6'
39+
c.segment_id as segment_code, -- e.g 'NAD'
40+
c.dataelement_id as data_element, -- e.g 'D_3035'
1841
--CASE
1942
-- WHEN dataelement_id IS NOT NULL THEN SUBSTR(dataelement_id, 3)
2043
-- END AS dataelement_without_leading_d_, -- e.g '3035'
21-
code_value as qualifier,
44+
c.code_value as qualifier,
45+
c.line_ahb_status as line_ahb_status, -- e.g. 'Muss [28] ∧ [64]'
46+
c.line_name as line_name, -- e.g. 'Datums- oder Uhrzeit- oder Zeitspannen-Format, Code' or 'Produkt-Daten für Lieferant relevant'
47+
c.sort_path as sort_path,
48+
NULLIF(ahe.node_texts, '') as bedingung,
49+
NULLIF(ahe.ahbicht_error_message, '') as bedingungsfehler
50+
FROM consolidated_ahm as c
51+
LEFT JOIN ahb_expressions as ahe -- if this table is missing, call create_and_fill_ahb_expression_table(...) first
52+
ON ahe.edifact_format_version = c.edifact_format_version
53+
AND ahe.format = c.format
54+
AND ahe.expression = c.line_ahb_status;
55+
2256

23-
coalesce(code_ahb_status, coalesce(dataelement_ahb_status,
24-
coalesce(segment_ahb_status, segmentgroup_ahb_status))) as line_ahb_status, -- e.g. 'Muss [28] ∧ [64]'
25-
coalesce(code_name, coalesce(dataelement_name, coalesce(dataelementgroup_name,
26-
coalesce(segment_name, segmentgroup_name)))) as line_name, -- e.g. 'Datums- oder Uhrzeit- oder Zeitspannen-Format, Code' or 'Produkt-Daten für Lieferant relevant'
27-
sort_path as sort_path
28-
-- the bedingung column is still missing, but we'll solve this one separately
29-
FROM ahb_hierarchy_materialized
30-
WHERE TYPE != 'dataelementgroup' AND (TYPE != 'dataelement' OR dataelement_ahb_status IS NOT Null) ;

0 commit comments

Comments
 (0)