Skip to content

Commit

Permalink
[fix] Sorting on 1st level
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Feb 2, 2022
1 parent 28afd85 commit 4f90093
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
20 changes: 0 additions & 20 deletions src/ObjectProviderImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,26 +167,6 @@ function propertyShapeToJsonSchemaProperty(
return undefined;
}

export function makeOrderBy(orderBy: string): any {
let descending = false;
let variable1 = '';
if (orderBy.startsWith('ASC(')) {
variable1 = orderBy.substr(4, orderBy.length - 5);
} else if (orderBy.startsWith('DESC(')) {
variable1 = orderBy.substr(5, orderBy.length - 6);
descending = true;
} else {
variable1 = orderBy;
}
// if not ends with a digit, assume 0
const c = variable1.charAt(variable1.length - 1);
if (c < '0' || c > '9') variable1 += '0';
return {
expression: variable(variable1),
descending,
};
}

export const uiMapping: JsObject = {
'@id': {
'ui:widget': 'UriWithCopyWidget',
Expand Down
4 changes: 2 additions & 2 deletions src/SparqlGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ export interface EntConstrInternal extends EntConstrData {
// internal properties, created and changed within SPARQL generation
prefixes: JsStrObj;
subj: NamedNode | Variable;
props2vars: JsStrObj;
vars2props: JsStrObj;
props2vars: JsStrObj; // MST property to indexed sparql var (e.g. name -> name0)
vars2props: JsStrObj; // indexed sparql var to MST property (e.g. name0 -> name)
// pred-partial
qCond: {
bgps: Quad[];
Expand Down
58 changes: 54 additions & 4 deletions src/SparqlGenSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,51 @@ function analyseProps(entConstrs: EntConstrInternal[]) {
});
}

function convertOrderBy(orderBy: any, entConstrs: any | any[] | undefined): any {
if (isArray(orderBy)) return orderBy.map((e) => convertOrderBy(e, entConstrs));
if (orderBy.expression || !orderBy.variable) return orderBy;
let entConstrVariable = orderBy.variable;
if (entConstrs) {
entConstrVariable =
(isArray(entConstrs) ? entConstrs.find((e) => e.props2vars[entConstrVariable]) : entConstrs)?.props2vars[
entConstrVariable
] || orderBy.variable;
}
return {
expression: variable(entConstrVariable),
descending: orderBy?.descending || false,
};
}

function sortResults(objects: JsObject[], orderBy: any[]) {
return objects.sort((a: JsObject, b: JsObject) => {
const order = orderBy?.find((o) => o.variable && a[o.variable] != b[o.variable]);
if (!order) return 0;
let a1 = a[order.variable];
let b1 = b[order.variable];
if (!a1 && b1) {
if (order.descending) return 1;
return -1;
}
if (a1 && !b1) {
if (order.descending) return -1;
return 1;
}
if (typeof a1 === 'number' && typeof b1 === 'number') {
if (order.descending) return b1 - a1;
return a1 - b1;
}
if (typeof a1 === 'string' && typeof b1 === 'string') {
if (order.descending) return b1.localeCompare(a1);
return a1.localeCompare(b1);
}
a1 = typeof a1 === 'string' ? a1 : a1.toString();
b1 = typeof b1 === 'string' ? b1 : b1.toString();
if (order.descending) return b1.localeCompare(a1);
return a1.localeCompare(b1);
});
}

/**
* SELECT
*/
Expand Down Expand Up @@ -296,7 +341,7 @@ function selectQueryFromEntConstrs(entConstrs: EntConstrInternal[], collConstrJs
});
// options should be the latest in WHERE (SPARQL performance optimizations)
query.where = [...(query.where || []), ...allOptions];
if (collConstrJs.orderBy) query.order = collConstrJs.orderBy;
if (collConstrJs.orderBy) query.order = convertOrderBy(collConstrJs.orderBy, entConstrs);
if (collConstrJs.limit) query.limit = collConstrJs.limit;
if (collConstrJs.offset) query.offset = collConstrJs.offset;
if (collConstrJs.distinct) query.distinct = collConstrJs.distinct;
Expand Down Expand Up @@ -434,7 +479,12 @@ export async function constructObjectsQuery(
const results: JsObject[] = await client.sparqlConstruct(queryStr, collConstrJs.options);
if (!results)
throw new Error('Collection load error, no response for the CollConstr with @id=' + collConstrJs['@id']);
const objects: JsObject[] = await jsonLdToObjects(results, entConstrs);
let objects: JsObject[] = await jsonLdToObjects(results, entConstrs);
if (collConstrJs.orderBy && objects?.length > 1) {
//console.log('before sort', objects, collConstrJs.orderBy);
objects = sortResults(objects, collConstrJs.orderBy);
//console.log('after sort', objects, collConstrJs.orderBy);
}
return objects;
}
//TODO: replace getWhereVar
Expand Down Expand Up @@ -615,7 +665,7 @@ function constructQueryFromEntConstrs(entConstrs: EntConstrInternal[], collConst
variables: variables2,
where: whereAll2,
};
if (entConstrJs.orderBy) subQuery2.order = entConstrJs.orderBy;
if (entConstrJs.orderBy) subQuery2.order = convertOrderBy(entConstrJs.orderBy, entConstr);
if (entConstrJs.limit) subQuery2.limit = entConstrJs.limit;
if (entConstrJs.offset) subQuery2.offset = entConstrJs.offset;
if (entConstrJs.distinct) subQuery2.distinct = entConstrJs.distinct;
Expand Down Expand Up @@ -704,7 +754,7 @@ function constructQueryFromEntConstrs(entConstrs: EntConstrInternal[], collConst

// options should be the latest in WHERE (SPARQL performance optimizations)
query.where = [...(query.where || []), ...allOptions];
if (collConstrJs.orderBy) query.order = collConstrJs.orderBy;
if (collConstrJs.orderBy) query.order = convertOrderBy(collConstrJs.orderBy, entConstrs);
if (collConstrJs.limit) query.limit = collConstrJs.limit;
if (collConstrJs.offset) query.offset = collConstrJs.offset;
if (collConstrJs.distinct) query.distinct = collConstrJs.distinct;
Expand Down

0 comments on commit 4f90093

Please sign in to comment.