Skip to content

Commit ca66bb2

Browse files
authored
Fix integer enumeration parsing (#14)
Schema parsing failed with integer enumeration, because we tried to get field size from the enumeration elements `len`, which does not work for `int`
1 parent f4e6fd5 commit ca66bb2

21 files changed

Lines changed: 67 additions & 5 deletions

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "xml2db"
7-
version = "0.12.2"
7+
version = "0.12.3"
88
authors = [
99
{ name="Commission de régulation de l'énergie", email="opensource@cre.fr" },
1010
]

src/xml2db/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def recurse_parse_simple_type(elem_type):
384384
else None
385385
)
386386
ae = ae and bt_ae if ae is not None and bt_ae is not None else None
387-
if elem_type.enumeration is not None:
387+
if elem_type.enumeration is not None and dt in ["string", "NMTOKEN", "duration", "token"]:
388388
mil = min([len(val) for val in elem_type.enumeration])
389389
mal = max([len(val) for val in elem_type.enumeration])
390390
return dt, mil, mal, ae

src/xml2db/table/column.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def types_mapping_default(temp: bool, col: "DataModelColumn") -> Any:
3232
"""
3333
if col.occurs[1] != 1:
3434
return String(8000)
35-
if col.data_type in ["decimal", "float"]:
35+
if col.data_type in ["decimal", "float", "double"]:
3636
return Double
3737
if col.data_type == "dateTime":
3838
return DateTime(timezone=True)
@@ -77,7 +77,7 @@ def types_mapping_mssql(temp: bool, col: "DataModelColumn") -> Any:
7777
"""
7878
if col.occurs[1] != 1:
7979
return mssql.VARCHAR(8000)
80-
if col.data_type in ["decimal", "float"]:
80+
if col.data_type in ["decimal", "float", "double"]:
8181
return Double
8282
if col.data_type == "dateTime":
8383
# using the DATETIMEOFFSET directly in the temporary table caused issues when inserting data in the target

tests/sample_models/orders/base_types.xsd

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@
88
<xs:simpleType name="inttype">
99
<xs:restriction base="xs:integer"/>
1010
</xs:simpleType>
11+
<xs:simpleType name="quantitytype">
12+
<xs:restriction base="xs:integer">
13+
<xs:maxInclusive value="99" />
14+
<xs:minInclusive value="1" />
15+
</xs:restriction>
16+
</xs:simpleType>
17+
<xs:simpleType name="currencytype">
18+
<xs:restriction base="xs:string">
19+
<xs:enumeration value="EUR" />
20+
<xs:enumeration value="CHF" />
21+
<xs:enumeration value="GBP" />
22+
<xs:enumeration value="USD" />
23+
</xs:restriction>
24+
</xs:simpleType>
25+
<xs:simpleType name="versiontype">
26+
<xs:restriction base="xs:int">
27+
<xs:enumeration value="1" />
28+
<xs:enumeration value="2" />
29+
<xs:enumeration value="3" />
30+
</xs:restriction>
31+
</xs:simpleType>
1132
<xs:simpleType name="dectype">
1233
<xs:restriction base="xs:decimal"/>
1334
</xs:simpleType>

tests/sample_models/orders/invalid_xml/malformed_recover.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version='1.0' encoding='utf-8'?>
22
<orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" batch_id="5" xsi:noNamespaceSchemaLocation="../orders.xsd">
3+
<version>3</version>
34
<shiporder orderid="1" processed_at="2023-09-10T09:37:00.000+02:00">
45
<orderperson>
56
<name>Bob</name>
@@ -20,6 +21,7 @@
2021
</product>
2122
<quantity>13</quantity>
2223
<price>340.23</price>
24+
<currency>CHF</currency>
2325
</item>
2426
<item>
2527
<product>
@@ -29,6 +31,7 @@
2931
<note>string</note>
3032
<quantity>8</quantity>
3133
<price>56.2</price>
34+
<currency>CHF</currency>
3235
</item>
3336
<item>
3437
<product>
@@ -37,6 +40,7 @@
3740
</product>
3841
<quantity>25</quantity>
3942
<price>21.7</price>
43+
<currency>CHF</currency>
4044
</item>
4145
</shiporder>
4246
</orders>

tests/sample_models/orders/orders.xsd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@
4848
<xs:sequence>
4949
<xs:element name="product" type="producttype" minOccurs="1" maxOccurs="1"/>
5050
<xs:element name="note" type="bt:stringtype" minOccurs="0"/>
51-
<xs:element name="quantity" type="bt:inttype"/>
51+
<xs:element name="quantity" type="bt:quantitytype"/>
5252
<xs:element name="price" type="bt:dectype"/>
53+
<xs:element name="currency" type="bt:currencytype"/>
5354
</xs:sequence>
5455
</xs:complexType>
5556

@@ -65,6 +66,7 @@
6566

6667
<xs:complexType name="orderstype">
6768
<xs:sequence>
69+
<xs:element name="version" type="bt:versiontype" />
6870
<xs:element name="shiporder" type="shipordertype" minOccurs="0" maxOccurs="unbounded" />
6971
</xs:sequence>
7072
<xs:attribute name="batch_id" type="bt:stringtype" />

tests/sample_models/orders/orders_ddl_mssql_version0.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CREATE TABLE item (
2323
note VARCHAR(1000) NULL,
2424
quantity INTEGER NULL,
2525
price DOUBLE PRECISION NULL,
26+
currency CHAR(3) NULL,
2627
record_hash BINARY(20) NULL,
2728
CONSTRAINT cx_pk_item PRIMARY KEY CLUSTERED (pk_item),
2829
CONSTRAINT item_xml2db_record_hash UNIQUE (record_hash)
@@ -54,6 +55,7 @@ CREATE TABLE shiporder_item (
5455
CREATE TABLE orders (
5556
pk_orders INTEGER NOT NULL IDENTITY,
5657
batch_id VARCHAR(1000) NULL,
58+
version INTEGER NULL,
5759
input_file_path VARCHAR(256) NULL,
5860
record_hash BINARY(20) NULL,
5961
CONSTRAINT cx_pk_orders PRIMARY KEY CLUSTERED (pk_orders),

tests/sample_models/orders/orders_ddl_mssql_version1.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ CREATE TABLE shiporder (
3434
CREATE TABLE orders (
3535
pk_orders INTEGER NOT NULL IDENTITY,
3636
batch_id VARCHAR(1000) NULL,
37+
version INTEGER NULL,
3738
xml2db_processed_at DATETIMEOFFSET NULL,
3839
input_file_path VARCHAR(256) NULL,
3940
record_hash BINARY(16) NULL,
@@ -60,6 +61,7 @@ CREATE TABLE item (
6061
note VARCHAR(1000) NULL,
6162
quantity INTEGER NULL,
6263
price DOUBLE PRECISION NULL,
64+
currency CHAR(3) NULL,
6365
CONSTRAINT cx_pk_item PRIMARY KEY CLUSTERED (pk_item),
6466
FOREIGN KEY(fk_parent_shiporder) REFERENCES shiporder (pk_shiporder)
6567
)

tests/sample_models/orders/orders_ddl_mssql_version2.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
CREATE TABLE orders (
33
pk_orders INTEGER NOT NULL IDENTITY,
44
batch_id VARCHAR(1000) NULL,
5+
version INTEGER NULL,
56
input_file_path VARCHAR(256) NULL,
67
xml2db_record_hash BINARY(20) NULL,
78
CONSTRAINT cx_pk_orders PRIMARY KEY CLUSTERED (pk_orders),
@@ -42,6 +43,7 @@ CREATE TABLE item (
4243
note VARCHAR(1000) NULL,
4344
quantity INTEGER NULL,
4445
price DOUBLE PRECISION NULL,
46+
currency CHAR(3) NULL,
4547
xml2db_record_hash BINARY(20) NULL,
4648
CONSTRAINT cx_pk_item PRIMARY KEY CLUSTERED (pk_item),
4749
CONSTRAINT item_xml2db_record_hash UNIQUE (xml2db_record_hash),

tests/sample_models/orders/orders_ddl_mysql_version0.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CREATE TABLE item (
2323
note VARCHAR(255),
2424
quantity INTEGER,
2525
price DOUBLE,
26+
currency VARCHAR(3),
2627
record_hash BINARY(20),
2728
CONSTRAINT cx_pk_item PRIMARY KEY (pk_item),
2829
CONSTRAINT item_xml2db_record_hash UNIQUE (record_hash)
@@ -54,6 +55,7 @@ CREATE TABLE shiporder_item (
5455
CREATE TABLE orders (
5556
pk_orders INTEGER NOT NULL AUTO_INCREMENT,
5657
batch_id VARCHAR(255),
58+
version INTEGER,
5759
input_file_path VARCHAR(256),
5860
record_hash BINARY(20),
5961
CONSTRAINT cx_pk_orders PRIMARY KEY (pk_orders),

0 commit comments

Comments
 (0)