diff --git a/src/SPARQLIngest.ts b/src/SPARQLIngest.ts index ce4d76b..40989b1 100644 --- a/src/SPARQLIngest.ts +++ b/src/SPARQLIngest.ts @@ -3,7 +3,7 @@ import { SDS } from "@treecg/types"; import { Store, Parser, DataFactory } from "n3"; import { CREATE, UPDATE, DELETE } from "./SPARQLQueries"; import { Quad_Subject, Term } from "@rdfjs/types"; -import { doSPARQLRequest } from "./Utils"; +import { doSPARQLRequest, sanitizeQuads } from "./Utils"; const { quad, namedNode } = DataFactory; @@ -123,6 +123,8 @@ export async function sparqlIngest( const ctv = store.getQuads(null, config.changeSemantics!.changeTypePath, null, null)[0]; // Remove change type quad from store store.removeQuad(ctv); + // Sanitize quads to prevent issues on SPARQL queries + sanitizeQuads(store); // Assemble corresponding SPARQL UPDATE query if (ctv.object.value === config.changeSemantics.createValue) { query = CREATE(store, ng); diff --git a/src/Utils.ts b/src/Utils.ts index 24e15f0..63fe0fe 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -1,3 +1,16 @@ +import { XSD } from "@treecg/types"; +import { Store, DataFactory as DF } from "n3"; + +export function sanitizeQuads(store: Store): void { + for (const q of store.getQuads(null, null, null, null)) { + if (q.object.termType === "Literal") { + if (/\+\d+/.test(q.object.value)) { + store.removeQuad(q); + store.addQuad(q.subject, q.predicate, DF.literal(q.object.value.substring(1), DF.namedNode(XSD.integer)), q.graph); + } + } + } +} export async function doSPARQLRequest(query: string, url: string): Promise { const res = await fetch(url, { diff --git a/tests/SPARQLIngest.test.ts b/tests/SPARQLIngest.test.ts index a322293..341ac08 100644 --- a/tests/SPARQLIngest.test.ts +++ b/tests/SPARQLIngest.test.ts @@ -537,7 +537,8 @@ function dataGenerator( a ex:NestedEntity; ex:nestedProp "some other value" ]; - ex:prop3 ex:SomeNamedNode. + ex:prop3 ex:SomeNamedNode; + ex:propNum +30. ` : "."} `; } else {