Skip to content

Commit 97c2b2a

Browse files
authored
Adding rdftab compatibility (#4)
1 parent 9e9ce15 commit 97c2b2a

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

src/rdf_sql_bulkloader/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ def main(verbose: int, quiet: bool):
3434
@click.option(
3535
"--force/--no-force", default=False, show_default=True, help="Recreates db if already present"
3636
)
37+
@click.option(
38+
"--rdftab-compatibility/--no-rdftab-compatibility", default=True, show_default=True,
39+
help="Creates a statements table, compatible with rdftab"
40+
)
3741
@click.argument("files", nargs=-1)
38-
def load_sqlite(files, output, force: bool):
42+
def load_sqlite(files, output, force: bool, rdftab_compatibility: bool):
3943
"""Run the rdf-sql-bulkloader's demo command."""
4044
output_path = Path(output)
4145
if output_path.exists():
@@ -44,6 +48,7 @@ def load_sqlite(files, output, force: bool):
4448
else:
4549
raise ValueError(f"Path exists {output_path}")
4650
loader = SqliteBulkloader(output)
51+
loader.rdftab_compatibility = rdftab_compatibility
4752
if len(files) > 1:
4853
logging.warning("Blank nodes may be shared TODO FIX ME")
4954
for file in files:

src/rdf_sql_bulkloader/loaders/bulkloader.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class BulkLoader(ABC):
8989
prefix_map: PREFIX_MAP = None
9090
converter: Converter = None
9191
index_statements = False
92+
rdftab_compatibility = True
9293

9394
def __post_init__(self):
9495
if self.prefix_map is None:

src/rdf_sql_bulkloader/loaders/sqlite3_bulkloader.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import sqlite3
23
from dataclasses import dataclass
34
from typing import Any
@@ -6,6 +7,17 @@
67

78
COLS = ["subject", "predicate", "object", "value", "datatype", "language"]
89

10+
RDFTAB_INSERT = """
11+
CREATE TABLE statements AS
12+
SELECT
13+
subject AS stanza,
14+
subject,
15+
predicate,
16+
value,
17+
datatype,
18+
language
19+
FROM statement
20+
"""
921

1022
@dataclass
1123
class SqliteBulkloader(BulkLoader):
@@ -18,8 +30,10 @@ def bulkload(self, path: str):
1830
tuples = []
1931
for t in self.statements(path):
2032
tuples.append(t)
21-
print(len(tuples))
33+
logging.info(f"Loaded {len(tuples)} loaded")
2234
colstr = ",".join(COLS)
2335
qs = ",".join(["?" for _ in COLS])
2436
con.executemany(f"insert into statement({colstr}) values ({qs})", tuples)
37+
if self.rdftab_compatibility:
38+
con.execute(RDFTAB_INSERT)
2539
con.commit()

tests/test_sqlite3_bulkloader.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,22 @@ class TestSqlit3BulkLoader(unittest.TestCase):
3636

3737
def test_bulkload(self):
3838
loader = SqliteBulkloader(path=":memory:")
39+
loader.rdftab_compatibility = True
3940
loader.bulkload(TEST_INPUT_OWL)
4041
con = loader.connection
4142
cur = con.cursor()
4243
cur.execute("select * from statement WHERE subject=:subject", {"subject": NUCLEUS})
4344
stmts = list(cur.fetchall())
44-
for s in stmts:
45-
print(s)
45+
#for s in stmts:
46+
# print(s)
4647
for case in CASES:
4748
self.assertIn(case, stmts)
49+
# rdftab
50+
cur.execute("select * from statements WHERE subject=:subject", {"subject": NUCLEUS})
51+
stmts = list(cur.fetchall())
52+
for s in stmts:
53+
print(s)
54+
assert len(stmts) > 1
4855

4956
def test_lang_tags(self):
5057
"""

0 commit comments

Comments
 (0)