Skip to content

Commit

Permalink
Add sanitazion function for integers
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrojas87 committed Mar 11, 2024
1 parent 22c43dc commit e5c9f2e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/SPARQLIngest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/Utils.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const res = await fetch(url, {
Expand Down
3 changes: 2 additions & 1 deletion tests/SPARQLIngest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e5c9f2e

Please sign in to comment.