Skip to content

Commit

Permalink
feat(raw-update): add raw update
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerfaulkner committed Nov 4, 2023
1 parent 09b8b2d commit ffd3672
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comake/skl-js-engine",
"version": "0.16.3",
"version": "0.16.4",
"description": "Standard Knowledge Language Javascript Engine",
"keywords": [
"skl",
Expand Down
4 changes: 4 additions & 0 deletions src/SklEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export class SKLEngine {
return await this.queryAdapter.executeRawQuery<T>(query);
}

public async executeRawUpdate(query: string): Promise<void> {
return await this.queryAdapter.executeRawUpdate(query);
}

public async executeRawEntityQuery(query: string, frame?: Frame): Promise<GraphObject> {
return await this.queryAdapter.executeRawEntityQuery(query, frame);
}
Expand Down
4 changes: 4 additions & 0 deletions src/storage/query-adapter/QueryAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export interface QueryAdapter {
* Performs a raw query for data matching the query.
*/
executeRawQuery<T extends RawQueryResult>(query: string): Promise<T[]>;
/**
* Performs a raw query for data matching the query.
*/
executeRawUpdate(query: string): Promise<void>;
/**
* Performs a raw query for entities matching the query. The query must be a CONSTRUCT query.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/storage/query-adapter/sparql/SparqlQueryAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export class SparqlQueryAdapter implements QueryAdapter {
return await triplesToJsonldWithFrame(response, frame);
}

public async executeRawUpdate(
query: string,
): Promise<void> {
await this.queryExecutor.executeRawSparqlUpdate(query);
}

public async find(options?: FindOneOptions): Promise<Entity | null> {
const jsonld = await this.findAllAsJsonLd({ ...options, limit: 1 });
if (Array.isArray(jsonld) && !options?.skipFraming) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ export class InMemorySparqlQueryExecutor implements QueryExecutor {
public async executeSparqlUpdate(query: Update): Promise<void> {
if ((query?.updates?.length ?? 0) > 0) {
const generatedQuery = this.sparqlGenerator.stringify(query);
await this.engine.queryVoid(
generatedQuery,
this.queryContext,
);
await this.executeRawSparqlUpdate(generatedQuery);
}
}

public async executeRawSparqlUpdate(query: string): Promise<void> {
await this.engine.queryVoid(
query,
this.queryContext,
);
}

public async executeAskQueryAndGetResponse(query: AskQuery): Promise<boolean> {
const generatedQuery = this.sparqlGenerator.stringify(query);
return await this.engine.queryBoolean(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export class SparqlEndpointQueryExecutor implements QueryExecutor {

public async executeSparqlUpdate(query: Update): Promise<void> {
const generatedQuery = this.sparqlGenerator.stringify(query);
await this.sparqlClient.query.update(generatedQuery);
await this.executeRawSparqlUpdate(generatedQuery);
}

public async executeRawSparqlUpdate(query: string): Promise<void> {
await this.sparqlClient.query.update(query);
}

public async executeAskQueryAndGetResponse(query: AskQuery): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface QueryExecutor {
* Executes a SPARQL update query.
*/
executeSparqlUpdate(query: Update): Promise<void>;
/**
* Executes a raw SPARQL update query.
*/
executeRawSparqlUpdate(query: string,): Promise<void>;
/**
* Executes a SPARQL ask query.
*/
Expand Down
2 changes: 1 addition & 1 deletion test/deploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"main": "./dist/index.js",
"dependencies": {
"@comake/skl-js-engine": "file:./comake-skl-js-engine-0.16.3.tgz",
"@comake/skl-js-engine": "file:./comake-skl-js-engine-0.16.4.tgz",
"jsonld": "^6.0.0"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions test/unit/SklEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ describe('SKLEngine', (): void => {
});
});

describe('executeRawUpdate', (): void => {
it('delegates calls to executeRawUpdate to the query adapter.', async(): Promise<void> => {
const executeRawUpdateSpy = jest.spyOn(SparqlQueryAdapter.prototype, 'executeRawUpdate');
await expect(
sklEngine.executeRawUpdate(`DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }`),
).resolves.toBeUndefined();
expect(executeRawUpdateSpy).toHaveBeenCalledTimes(1);
expect(executeRawUpdateSpy).toHaveBeenCalledWith(`DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }`);
});
});

describe('executeRawEntityQuery', (): void => {
it('delegates calls to executeRawEntityQuery to the query adapter.', async(): Promise<void> => {
const executeRawEntityQuerySpy = jest.spyOn(SparqlQueryAdapter.prototype, 'executeRawEntityQuery');
Expand Down
13 changes: 13 additions & 0 deletions test/unit/storage/query-adapter/sparql/SparqlQueryAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ describe('a SparqlQueryAdapter', (): void => {
});
});

describe('executeRawUpdate', (): void => {
it('executes a an update query.', async(): Promise<void> => {
await expect(
adapter.executeRawUpdate([
`DELETE { GRAPH <https://example.com/data/1> { <https://example.com/data/1> <${SKL.sourceId}> ?c1. } }`,
`INSERT { GRAPH <https://example.com/data/1> { <https://example.com/data/1> <${SKL.sourceId}> "abc123". } }`,
'USING <https://example.com/data/1>',
`WHERE { OPTIONAL { <https://example.com/data/1> <${SKL.sourceId}> ?c1. } }`,
].join('\n')),
).resolves.toBeUndefined();
});
});

describe('executeRawEntityQuery', (): void => {
it('executes a sparql construct query and returns an empty GraphObject if no triples are found.',
async(): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ describe('a MemoryQueryAdapter', (): void => {
});
});

describe('executeRawSparqlUpdate', (): void => {
it('executes a void query and returns undefined.', async(): Promise<void> => {
await expect(
executor.executeRawSparqlUpdate('query'),
).resolves.toBeUndefined();
expect(queryVoid).toHaveBeenCalledTimes(1);
expect(queryVoid).toHaveBeenCalledWith(
'query',
{ sources: [ store ], unionDefaultGraph: true },
);
});
});

describe('executeAskQueryAndGetResponse', (): void => {
it('executes a boolean query and returns a boolean.', async(): Promise<void> => {
await expect(
Expand Down

0 comments on commit ffd3672

Please sign in to comment.