• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

terminusdb / terminusdb-client-js / 18384952635

09 Oct 2025 06:08PM UTC coverage: 30.876%. Remained the same
18384952635

push

github

web-flow
Merge pull request #328 from hoijnet/bugfixes/from-test-implementation

Fix variable name mismatches in from() and length() methods

923 of 4678 branches covered (19.73%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

2545 of 6554 relevant lines covered (38.83%)

5.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.03
/lib/query/woqlQuery.js
1
/* eslint-disable class-methods-use-this */
2
/* eslint-disable no-redeclare */
3
/* eslint-disable block-scoped-var */
4
/* eslint-disable no-var */
5
/* eslint-disable vars-on-top */
6
/* eslint-disable no-param-reassign */
7
/* eslint-disable no-unused-vars */
8
/* eslint-disable camelcase */
9
/* eslint-disable no-plusplus */
10
/* eslint-disable prefer-destructuring */
11
/* eslint-disable guard-for-in */
12
/* eslint-disable no-restricted-syntax */
13

14
/// /@ts-check
15
// WOQLQuery
16
/**
17
 * defines the internal functions of the woql query object - the
18
 * language API is defined in WOQLQuery
19
 * @module WOQLQuery
20
 * @constructor
21
 * @param {object} [query] json-ld query for initialisation
22
 * @returns {WOQLQuery}
23
 */
24

25
const WOQLCore = require('./woqlCore');
1✔
26
const { Var, Vars, Doc } = require('./woqlDoc');
1✔
27
// eslint-disable-next-line no-unused-vars
28
const typedef = require('../typedef');
1✔
29

30
// I HAVE TO REVIEW THE Inheritance and the prototype chain
31
class WOQLQuery extends WOQLCore {
2✔
32
  /**
33
   * defines the internal functions of the woql query object - the
34
   * language API is defined in WOQLQuery
35
   * @module WOQLQuery
36
   * @constructor
37
   * @param {object} [query] json-ld query for initialisation
38
   * @returns {WOQLQuery}
39
   */
40

41
  /**
42
 * Update a pattern matching rule for the triple (Subject, Predicate, oldObjValue) with the
43
 * new one (Subject, Predicate, newObjValue)
44
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
45
 * @param {string|Var} predicate - The IRI of a property or a variable
46
 * @param {string|Var} newObjValue - The value to update or a literal
47
 * @param {string|Var} oldObjValue - The old value of the object
48
 * @returns {WOQLQuery} A WOQLQuery which contains the a Update Triple Statement
49
 */
50
  update_triple(subject, predicate, newObjValue, oldObjValue) { return this; }
×
51

52
  /**
53
 * Generates a query that by default matches all triples in a graph identified by "graph"
54
 * or in all the current terminusDB's graph
55
 * @param {string | boolean} [graph] - false or the resource identifier of a graph possible
56
 * value are schema/{main - myschema - *} | instance/{main - myschema - *}  |
57
 * inference/{main - myschema - *}
58
 * @param {string|Var} [subject] - The IRI of a triple’s subject or a variable,
59
 * default value "v:Subject"
60
 * @param {string|Var} [predicate] - The IRI of a property or a variable,
61
 *  default value "v:Predicate"
62
 * @param {string|Var} [object] - The IRI of a node or a variable, or a literal,
63
 * default value "v:Object"
64
 * @returns {WOQLQuery} A WOQLQuery which contains the pattern matching expression
65
 */
66
  star(graph, subject, predicate, object) { return this; }
×
67

68
  /**
69
  * Update a pattern matching rule for the quad [S, P, O, G] (Subject, Predicate, Object, Graph)
70
  * @param {string|Var} subject - The IRI of a triple’s subject or a variable
71
  * @param {string|Var} predicate - The IRI of a property or a variable
72
  * @param {string|Var} newObject - The value to update or a literal
73
  * @param {typedef.GraphRef} graphRef - A valid graph resource identifier string
74
  * @returns {WOQLQuery} A WOQLQuery which contains the a Update Quad Statement
75
  */
76
  update_quad(subject, predicate, newObject, graphRef) { return this; }
×
77

78
  /**
79
   * @param {string|Var} id - IRI string or variable containing
80
   * @param {string|Var} type  -  IRI string or variable containing the IRI of the
81
   * @param {typedef.GraphRef} [refGraph] - Optional Graph resource identifier
82
   * @returns {WOQLQuery} A WOQLQuery which contains the insert expression
83
   */
84
  insert(id, type, refGraph) { return this; }
×
85

86
  /**
87
  * Sets the graph resource ID that will be used for subsequent chained function calls
88
  * @param {typedef.GraphRef} [graphRef] Resource String identifying the graph which will
89
  * be used for subsequent chained schema calls
90
  * @returns {WOQLQuery} A WOQLQuery which contains the partial Graph pattern matching expression
91
  * @example
92
  */
93
  graph(graphRef) { return this; }
×
94

95
  /**
96
   * Specifies the identity of a node that can then be used in subsequent builder functions.
97
   * Note that node() requires subsequent chained functions to complete the triples / quads
98
   * that it produces - by itself it only generates the subject.
99
   * @param {string|Var} nodeid -  The IRI of a node or a variable containing an IRI which will
100
   * be the subject of the builder functions
101
   * @param {typedef.FuntionType} [chainType] - Optional type of builder function to build
102
   * (default is triple)
103
   * @returns {WOQLQuery} - A WOQLQuery which contains the partial Node pattern matching expression
104
   */
105
  node(nodeid, chainType) { return this; }
×
106

107
  /**
108
   * Deletes all triples in the passed graph (defaults to instance/main)
109
   * @param {typedef.GraphRef} [graphRef] - Resource String identifying the graph from
110
   * which all triples will be removed
111
   * @returns {WOQLQuery} - A WOQLQuery which contains the deletion expression
112
   * @example
113
   * nuke("schema/main")
114
   * //will delete everything from the schema/main graph
115
   */
116
  nuke(graphRef) { return this; }
×
117

118
  /**
119
   * @param {string|Var} [Subj] - The IRI of a triple’s subject or a variable
120
   * @param {string|Var} [Pred] - The IRI of a property or a variable
121
   * @param {string|Var} [Obj] - The IRI of a node or a variable, or a literal
122
   * @param {typedef.GraphRef} [Graph] - the resource identifier of a graph possible
123
   * @returns {WOQLQuery} - A WOQLQuery which contains the pattern matching expression
124
   */
125
  all(Subj, Pred, Obj, Graph) { return this; }
×
126

127
  /**
128
   * @param {boolean} tf
129
   * @returns {object}
130
   * @example
131
   */
132
  boolean(tf) { return {}; }
×
133

134
  /**
135
   * @param {string} s
136
   * @returns {object}
137
   * @example
138
   */
139
  string(s) { return {}; }
×
140

141
  /**
142
 * @param {any} s
143
 * @param {string} t
144
 * @returns {object}
145
 * @example
146
 */
147
  literal(s, t) { return {}; }
×
148

149
  /**
150
  * @param {string} s
151
  * @returns {object}
152
  * @example
153
  */
154

155
  iri(s) { return {}; }
×
156

157
  // eslint-disable-next-line no-underscore-dangle
158
  _set_context(ctxt) { return this; }
×
159

160
  /**
161
   * @param {WOQLQuery} Subq
162
   * @returns {WOQLQuery}
163
   */
164
  addSubQuery(Subq) {
165
    super.addSubQuery(Subq);
30✔
166
    return this;
30✔
167
  }
168

169
  /**
170
   * @param {string} msg
171
   * @returns {WOQLQuery}
172
   */
173
  parameterError(msg) {
174
    super.parameterError(msg);
×
175
    return this;
×
176
  }
177

178
  /**
179
   * @returns {WOQLQuery}
180
   */
181
  updated() {
182
    super.updated();
9✔
183
    return this;
9✔
184
  }
1✔
185

186
  // eslint-disable-next-line no-useless-constructor
187
  constructor(query) {
120✔
188
    super(query);
189
  }
1✔
190
}
191

192
/**
193
 * Read a node identified by an IRI as a JSON-LD document
194
 * @param {string} IRI -  The document id  or a variable to read
195
 * @param {string} output - Variable which will be bound to the document.
196
 * @return {WOQLQuery} WOQLQuery
197
 */
198
WOQLQuery.prototype.read_document = function (IRI, output) {
1✔
199
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
200
  this.cursor['@type'] = 'ReadDocument';
1✔
201
  this.cursor.identifier = this.cleanNodeValue(IRI);
1✔
202
  this.cursor.document = this.expandValueVariable(output);
1✔
203
  return this;
1✔
204
};
205

206
/**
207
 * Insert a document in the graph.
208
 * @param {object} docjson -  The document to insert. Must either have an '@id' or
209
 * have a class specified key.
210
 * @param {string} [IRI] - An optional identifier specifying the document location.
211
 * @return {WOQLQuery} WOQLQuery
212
 */
213

214
WOQLQuery.prototype.insert_document = function (docjson, IRI) {
1✔
215
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
216
  this.cursor['@type'] = 'InsertDocument';
1✔
217
  if (typeof IRI !== 'undefined') this.cursor.identifier = this.cleanNodeValue(IRI);
1!
218

219
  this.cursor.document = this.cleanObject(docjson);
1✔
220

221
  return this.updated();
1✔
222
};
223

224
/**
225
 * Update a document identified by an IRI
226
 * @param {object} docjson -  The document to update. Must either have an '@id' or
227
 * have a class specified key.
228
 * @param {string} [IRI] - An optional identifier specifying the document location.
229
 * @return {WOQLQuery} WOQLQuery
230
 */
231

232
WOQLQuery.prototype.update_document = function (docjson, IRI) {
1✔
233
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
234
  this.cursor['@type'] = 'UpdateDocument';
1✔
235
  if (typeof IRI !== 'undefined') this.cursor.identifier = this.cleanNodeValue(IRI);
1!
236

237
  this.cursor.document = this.cleanObject(docjson);
1✔
238

239
  return this.updated();
1✔
240
};
241

242
/**
243
 * Delete a document from the graph.
244
 * @param {string} IRI -  The document id  or a variable
245
 * @return {WOQLQuery} WOQLQuery
246
 */
247

248
WOQLQuery.prototype.delete_document = function (IRI) {
1✔
249
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
250
  this.cursor['@type'] = 'DeleteDocument';
1✔
251
  this.cursor.identifier = this.cleanNodeValue(IRI);
1✔
252
  return this.updated();
1✔
253
};
254

255
/**
256
 * Contains definitions of the WOQL functions which map directly to JSON-LD types
257
 * All other calls and queries can be composed from these
258
 */
259

260
// moved from woqlCore
261
WOQLQuery.prototype.wrapCursorWithAnd = function () {
1✔
262
  if (this.cursor && this.cursor['@type'] === 'And') {
2✔
263
    const newby = this.cursor.and.length;
1✔
264
    this.and({});
1✔
265
    this.cursor = this.cursor.and[newby];
1✔
266
  } else {
267
    const nj = new WOQLQuery().json(this.cursor);
1✔
268
    for (const k in this.cursor) delete this.cursor[k];
4✔
269
    // create an empty json for the new query
270
    this.and(nj, {});
1✔
271
    this.cursor = this.cursor.and[1];
1✔
272
  }
273
};
274

275
/**
276
 * Query running against any specific commit Id
277
 * @param {string}  refPath  - path to specific reference Id or commit Id
278
 * @param {WOQLQuery} [subquery] - subquery for the specific commit point
279
 * @returns {WOQLQuery}
280
 */
281

282
WOQLQuery.prototype.using = function (refPath, subquery) {
1✔
283
  if (this.cursor['@type']) this.wrapCursorWithAnd();
4!
284
  this.cursor['@type'] = 'Using';
4✔
285
  if (!refPath || typeof refPath !== 'string') {
4!
286
    return this.parameterError('The first parameter to using must be a Collection ID (string)');
×
287
  }
288
  this.cursor.collection = refPath;
4✔
289
  return this.addSubQuery(subquery);
4✔
290
};
291

292
/**
293
 * Adds a text comment to a query - can also be used to wrap any part of a query to turn it off
294
 * @param {string} comment - text comment
295
 * @param {WOQLQuery} [subquery]  - query that is "commented out"
296
 * @returns {WOQLQuery}
297
 */
298

299
WOQLQuery.prototype.comment = function (comment, subquery) {
1✔
300
  // if (comment && comment === 'args')
301
  // return ['comment', 'query']
302
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
303
  this.cursor['@type'] = 'Comment';
×
304
  this.cursor.comment = this.jlt(comment);
×
305
  return this.addSubQuery(subquery);
×
306
};
307

308
/**
309
 * Filters the query so that only the variables included in [V1...Vn] are returned in the bindings
310
 * @param {...string|...Var} varNames - only these variables are returned
311
 * @returns {WOQLQuery}
312
 */
313

314
WOQLQuery.prototype.select = function (...varNames) {
8✔
315
  if (this.cursor['@type']) this.wrapCursorWithAnd();
4!
316
  this.cursor['@type'] = 'Select';
4✔
317
  if (!varNames || varNames.length <= 0) {
4!
318
    return this.parameterError('Select must be given a list of variable names');
×
319
  }
320
  const last = varNames[varNames.length - 1];
4✔
321
  /**
322
  *@type {any}
323
  */
324
  let embedquery = false;
4✔
325
  if (typeof last === 'object' && !(last instanceof Var) && last.json) {
4✔
326
    embedquery = varNames.pop();
2✔
327
  } // else var embedquery = false
328
  this.cursor.variables = this.rawVarList(varNames);
4✔
329
  return this.addSubQuery(embedquery);
4✔
330
};
331

332
/**
333
 * Filter the query to return only results that are distinct in the given variables
334
 * @param {...string|...Var} varNames - these variables are guaranteed to be unique as a tuple
335
 * @returns {WOQLQuery}
336
 */
337

338
WOQLQuery.prototype.distinct = function (...varNames) {
1✔
339
  // if (list && list[0] === 'args')
340
  // return ['variable_list', 'query']
341
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
342
  this.cursor['@type'] = 'Distinct';
×
343
  if (!varNames || varNames.length <= 0) {
×
344
    return this.parameterError('Distinct must be given a list of variable names');
×
345
  }
346
  const last = varNames[varNames.length - 1];
×
347
  /**
348
     * @type {any}
349
     */
350
  let embedquery = false;
×
351
  if (typeof last === 'object' && !(last instanceof Var) && last.json) {
×
352
    embedquery = varNames.pop();
×
353
  } // else var embedquery = false
354
  this.cursor.variables = this.rawVarList(varNames);
×
355
  return this.addSubQuery(embedquery);
×
356
};
357

358
/**
359
* Logical conjunction of the contained queries - all queries must match or the entire clause fails
360
* @param {...WOQLQuery} subqueries - A list of one or more woql queries to execute as a conjunction
361
* @returns {WOQLQuery} - A WOQLQuery object containing the conjunction of queries
362
*/
363

364
WOQLQuery.prototype.and = function (...subqueries) {
22✔
365
  if (this.cursor['@type'] && this.cursor['@type'] !== 'And') {
11✔
366
    const nj = new WOQLQuery().json(this.cursor);
2✔
367
    for (const k in this.cursor) delete this.cursor[k];
8✔
368
    subqueries.unshift(nj);
2✔
369
  }
370
  this.cursor['@type'] = 'And';
11✔
371
  if (typeof this.cursor.and === 'undefined') this.cursor.and = [];
11✔
372
  for (let i = 0; i < subqueries.length; i++) {
11✔
373
    const onevar = this.jobj(subqueries[i]);
19✔
374
    if (
19!
375
      onevar['@type'] === 'And'
19!
376
      && onevar.and
377
    ) {
378
      for (let j = 0; j < onevar.and.length; j++) {
×
379
        const qjson = onevar.and[j];
×
380
        if (qjson) {
×
381
          const subvar = this.jobj(qjson);
×
382
          this.cursor.and.push(subvar);
×
383
        }
384
      }
385
    } else {
386
      this.cursor.and.push(onevar);
19✔
387
    }
388
  }
389
  return this;
11✔
390
};
391

392
/**
393
 * Creates a logical OR of the arguments
394
 * @param {...WOQLQuery} subqueries - A list of one or more woql queries
395
 * to execute as alternatives
396
 * @returns {WOQLQuery} - A WOQLQuery object containing the logical Or of the subqueries
397
 */
398

399
WOQLQuery.prototype.or = function (...subqueries) {
1✔
400
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
401
  this.cursor['@type'] = 'Or';
1✔
402
  if (typeof this.cursor.or === 'undefined') this.cursor.or = [];
1!
403
  for (let i = 0; i < subqueries.length; i++) {
1✔
404
    const onevar = this.jobj(subqueries[i]);
2!
405
    this.cursor.or.push(onevar);
2✔
406
  }
407
  return this;
1✔
408
};
409
/**
410
 * Specifies the database URL that will be the default database for the enclosed query
411
 * @param {typedef.GraphRef} graphRef- A valid graph resource identifier string
412
 * @param {WOQLQuery} [query] - The query
413
 * @returns {WOQLQuery} A WOQLQuery object containing the from expression
414
 */
415

416
/**
417
 * Specifies the database URL that will be the default database for the enclosed query
418
 * @param {typedef.GraphRef} graphRef- A valid graph resource identifier string
419
 * @param {WOQLQuery} [query] - The query
420
 * @returns {WOQLQuery} A WOQLQuery object containing the from expression
421
 */
422

423
WOQLQuery.prototype.from = function (graphRef, query) {
1✔
424
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
425
  this.cursor['@type'] = 'From';
1✔
426
  if (!graphRef || typeof graphRef !== 'string') {
1!
427
    return this.parameterError(
×
428
      'The first parameter to from must be a Graph Filter Expression (string)',
429
    );
430
  }
431
  this.cursor.graph = this.jlt(graphRef);
1✔
432
  return this.addSubQuery(query);
1✔
433
};
434

435
/**
436
 * Specifies the graph resource to write the contained query into
437
 * @param {typedef.GraphRef} graphRef- A valid graph resource identifier string
438
 * @param {WOQLQuery} [subquery] - The query which will be written into the graph
439
 * @returns {WOQLQuery} A WOQLQuery which will be written into the graph in question
440
 */
441

442
WOQLQuery.prototype.into = function (graphRef, subquery) {
1✔
443
  // if (graph_descriptor && graph_descriptor === 'args')
444
  // return ['graph', 'query']
445
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
446
  this.cursor['@type'] = 'Into';
×
447
  if (!graphRef || typeof graphRef !== 'string') {
×
448
    return this.parameterError(
×
449
      'The first parameter to from must be a Graph Filter Expression (string)',
450
    );
451
  }
452
  this.cursor.graph = this.jlt(graphRef);
×
453
  return this.addSubQuery(subquery);
×
454
};
455

456
/**
457
 * Creates a triple pattern matching rule for the triple [S, P, O] (Subject, Predicate, Object)
458
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
459
 * @param {string|Var} predicate - The IRI of a property or a variable
460
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
461
 * @returns {WOQLQuery}
462
 */
463

464
WOQLQuery.prototype.triple = function (subject, predicate, object) {
1✔
465
  // if (a && a === 'args')
466
  // return ['subject', 'predicate', 'object']
467
  if (this.cursor['@type']) this.wrapCursorWithAnd();
43✔
468
  this.cursor['@type'] = 'Triple';
43✔
469
  this.cursor.subject = this.cleanSubject(subject);
43✔
470
  this.cursor.predicate = this.cleanPredicate(predicate);
43✔
471
  this.cursor.object = this.cleanObject(object);
43✔
472
  return this;
43✔
473
};
474

475
/**
476
 * Creates a triple pattern matching rule for the triple [S, P, O] (Subject, Predicate,
477
 * Object) added in the current layer
478
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
479
 * @param {string|Var} predicate - The IRI of a property or a variable
480
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
481
 * @returns {WOQLQuery}
482
 */
483

484
WOQLQuery.prototype.added_triple = function (subject, predicate, object) {
1✔
485
  // if (a && a === 'args')
486
  // return ['subject', 'predicate', 'object']
487
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
488
  this.cursor['@type'] = 'AddedTriple';
×
489
  this.cursor.subject = this.cleanSubject(subject);
×
490
  this.cursor.predicate = this.cleanPredicate(predicate);
×
491
  this.cursor.object = this.cleanObject(object);
×
492
  return this;
×
493
};
494

495
/**
496
 * Creates a triple pattern matching rule for the triple [S, P, O] (Subject, Predicate,
497
 * Object) added in the current commit
498
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
499
 * @param {string|Var} predicate - The IRI of a property or a variable
500
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
501
 * @returns {WOQLQuery}
502
 */
503

504
WOQLQuery.prototype.removed_triple = function (subject, predicate, object) {
1✔
505
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
506
  this.cursor['@type'] = 'DeletedTriple';
×
507
  this.cursor.subject = this.cleanSubject(subject);
×
508
  this.cursor.predicate = this.cleanPredicate(predicate);
×
509
  this.cursor.object = this.cleanObject(object);
×
510
  return this;
×
511
};
512

513
/**
514
 * Creates a pattern matching rule for triple [Subject, Predicate, Object]
515
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
516
 * @param {string|Var} predicate - The IRI of a property or a variable
517
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
518
 * @returns {WOQLQuery} A WOQLQuery which contains the a quad or a triple Statement
519
 */
520

521
WOQLQuery.prototype.link = function (subject, predicate, object) {
1✔
522
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
523
  this.cursor['@type'] = 'Triple';
×
524
  this.cursor.subject = this.cleanSubject(subject);
×
525
  this.cursor.predicate = this.cleanPredicate(predicate);
×
526
  this.cursor.object = this.cleanSubject(object);
×
527
  return this;
×
528
};
529

530
/**
531
 * Creates a pattern matching rule for triple [Subject, Predicate, Object]
532
 * add extra information about the type of the value object
533
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
534
 * @param {string|Var} predicate - The IRI of a property or a variable
535
 * @param {string | number | boolean | Var} objValue - an specific value
536
 * @returns {WOQLQuery} A WOQLQuery which contains the a quad or a triple Statement
537
 */
538

539
WOQLQuery.prototype.value = function (subject, predicate, objValue) {
1✔
540
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
541
  this.cursor['@type'] = 'Triple';
×
542
  this.cursor.subject = this.cleanSubject(subject);
×
543
  this.cursor.predicate = this.cleanPredicate(predicate);
×
544
  this.cursor.object = this.cleanDataValue(objValue, 'xsd:string');
×
545
  return this;
×
546
};
547

548
/**
549
 * Creates a pattern matching rule for the quad [S, P, O, G] (Subject, Predicate, Object, Graph)
550
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
551
 * @param {string|Var} predicate - The IRI of a property or a variable
552
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
553
 * @param {typedef.GraphRef} graphRef - A valid graph resource identifier string
554
 * @returns {WOQLQuery}
555
 */
556

557
WOQLQuery.prototype.quad = function (subject, predicate, object, graphRef) {
1✔
558
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
559
  const args = this.triple(subject, predicate, object);
1✔
560
  // if (a && a === 'args')
561
  // return args.concat(['graph'])
562
  if (!graphRef) return this.parameterError('Quad takes four parameters, the last should be a graph filter');
1!
563
  this.cursor['@type'] = 'Triple';
1✔
564
  this.cursor.graph = this.cleanGraph(graphRef);
1✔
565
  return this;
1✔
566
};
567

568
/**
569
 * Creates a pattern matching rule for the quad [S, P, O, G] (Subject, Predicate,
570
 * Object, Graph) removed from the current commit
571
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
572
 * @param {string|Var} predicate - The IRI of a property or a variable
573
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
574
 * @param {typedef.GraphRef} graphRef- A valid graph resource identifier string
575
 * @returns {WOQLQuery}
576
 */
577

578
WOQLQuery.prototype.added_quad = function (subject, predicate, object, graphRef) {
1✔
579
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
580
  const args = this.triple(subject, predicate, object);
×
581
  // if (a && a === 'args')
582
  // return args.concat(['graph'])
583
  if (!graphRef) return this.parameterError('Quad takes four parameters, the last should be a graph filter');
×
584
  this.cursor['@type'] = 'AddedQuad';
×
585
  this.cursor.graph = this.cleanGraph(graphRef);
×
586
  return this;
×
587
};
588

589
/**
590
 * Creates a pattern matching rule for the quad [S, P, O, G] (Subject, Predicate,
591
 * Object, Graph) removed from the current commit
592
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
593
 * @param {string|Var} predicate - The IRI of a property or a variable
594
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
595
 * @param {typedef.GraphRef} graphRef- A valid graph resource identifier string
596
 * @returns {WOQLQuery}
597
 */
598

599
WOQLQuery.prototype.removed_quad = function (subject, predicate, object, graphRef) {
1✔
600
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
601
  const args = this.triple(subject, predicate, object);
×
602
  // if (a && a === 'args')
603
  // return args.concat(['graph'])
604
  if (!graphRef) return this.parameterError('Quad takes four parameters, the last should be a graph filter');
×
605
  this.cursor['@type'] = 'DeletedQuad';
×
606
  this.cursor.graph = this.cleanGraph(graphRef);
×
607
  return this;
×
608
};
609

610
/**
611
 * Returns true if ClassA subsumes ClassB, according to the current DB schema
612
 * @param {string} classA - ClassA
613
 * @param {string} classB - ClassB
614
 * @returns {boolean} WOQLQuery
615
 */
616
WOQLQuery.prototype.sub = function (classA, classB) {
1✔
617
  if (!classA || !classB) return this.parameterError('Subsumption takes two parameters, both URIs');
1!
618
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
619
  this.cursor['@type'] = 'Subsumption';
1✔
620
  this.cursor.parent = this.cleanNodeValue(classA);
1✔
621
  this.cursor.child = this.cleanNodeValue(classB);
1✔
622
  return this;
1✔
623
};
624

625
WOQLQuery.prototype.subsumption = WOQLQuery.prototype.sub;
1✔
626

627
/**
628
 * Matches if a is equal to b
629
 * @param {string|Var} varName - literal, variable or id
630
 * @param {string|Var} varValue - literal, variable or id
631
 * @returns {WOQLQuery}
632
 */
633
WOQLQuery.prototype.eq = function (varName, varValue) {
1✔
634
  // if (a && a === 'args') return ['left', 'right']
635
  if (typeof varName === 'undefined' || typeof varValue === 'undefined') return this.parameterError('Equals takes two parameters');
1!
636
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
637
  this.cursor['@type'] = 'Equals';
1✔
638
  this.cursor.left = this.cleanObject(varName);
1✔
639
  this.cursor.right = this.cleanObject(varValue);
1✔
640
  return this;
1✔
641
};
642

643
WOQLQuery.prototype.equals = WOQLQuery.prototype.eq;
1✔
644

645
/**
646
 * Substring
647
 * @param {string|Var} string - String or variable
648
 * @param {number|Var} before - integer or variable (characters from start to begin)
649
 * @param {number|Var} [length] - integer or variable (length of substring)
650
 * @param {number|Var} [after] - integer or variable (number of characters after substring)
651
 * @param {string|Var} [subString] - String or variable
652
 * @returns {WOQLQuery}
653
 */
654
WOQLQuery.prototype.substr = function (string, before, length, after, subString) {
1✔
655
  // if (String && String === 'args')
656
  // return ['string', 'before', 'length', 'after', 'substring']
657
  if (!subString) {
×
658
    subString = after;
×
659
    after = 0;
×
660
  }
661
  if (!subString) {
×
662
    subString = length;
×
663
    length = subString.length + before;
×
664
  }
665
  if (!string || !subString || typeof subString !== 'string') {
×
666
    return this.parameterError(
×
667
      'Substr - the first and last parameters must be strings representing the full and substring variables / literals',
668
    );
669
  }
670
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
671
  this.cursor['@type'] = 'Substring';
×
672
  this.cursor.string = this.cleanDataValue(string, 'xsd:string');
×
673
  this.cursor.before = this.cleanDataValue(before, 'xsd:nonNegativeInteger');
×
674
  this.cursor.length = this.cleanDataValue(length, 'xsd:nonNegativeInteger');
×
675
  this.cursor.after = this.cleanDataValue(after, 'xsd:nonNegativeInteger');
×
676
  this.cursor.substring = this.cleanDataValue(subString, 'xsd:string');
×
677
  return this;
×
678
};
679

680
WOQLQuery.prototype.substring = WOQLQuery.prototype.substr;
1✔
681

682
/**
683
 * Use the document inteface to import documents
684
 * @deprecated
685
 * Retrieves the exernal resource defined by QueryResource and copies values
686
 * from it into variables defined in AsVars
687
 * @param {Vars | array<Var>} asvars - an array of AsVar variable mappings (see as for format below)
688
 * @param {WOQLQuery} queryResource - an external resource (remote, file, post) to query
689
 * @returns {WOQLQuery} A WOQLQuery which contains the get expression
690
 */
691
WOQLQuery.prototype.get = function (asvars, queryResource) {
1✔
692
  this.cursor['@type'] = 'Get';
1✔
693
  this.cursor.columns = asvars.json ? asvars.json() : new WOQLQuery().as(...asvars).json();
1!
694
  if (queryResource) {
1!
695
    this.cursor.resource = this.jobj(queryResource);
1✔
696
  } else {
697
    this.cursor.resource = {};
×
698
  }
699
  this.cursor = this.cursor.resource;
1✔
700
  return this;
1✔
701
};
702

703
/**
704
 * Use the document inteface to import documents
705
 * @deprecated
706
 * @put Outputs the results of a query to a file
707
 * @param {Vars | array<Var>} varsToExp - an array of AsVar variable
708
 * mappings (see as for format below)
709
 * @param {WOQLQuery} query - The query which will be executed to produce the results
710
 * @param {string} fileResource - an file resource local to the server
711
 * @returns {WOQLQuery} A WOQLQuery which contains the put expression
712
 */
713
WOQLQuery.prototype.put = function (varsToExp, query, fileResource) {
1✔
714
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
715
  this.cursor['@type'] = 'Put';
×
716
  if (Array.isArray(varsToExp) && typeof varsToExp[0] !== 'object') {
×
717
    const nasvars = [];
×
718
    for (let i = 0; i < varsToExp.length; i++) {
×
719
      const iasv = this.asv(i, varsToExp[i]);
×
720
      nasvars.push(iasv);
×
721
      this.cursor.columns = nasvars;
×
722
    }
723
  } else {
724
    this.cursor.columns = varsToExp.json
×
725
      ? varsToExp.json()
726
      : new WOQLQuery().as(...varsToExp).json();
727
  }
728
  this.cursor.query = this.jobj(query);
×
729
  if (fileResource) {
×
730
    this.cursor.resource = this.jobj(fileResource);
×
731
  } else {
732
    this.cursor.resource = {};
×
733
  }
734
  this.cursor = this.cursor.resource;
×
735
  return this;
×
736
};
737

738
/**
739
 * @param {...(array|string|Var)} varList variable number of arguments
740
 * @returns WOQLQuery
741
 */
742
WOQLQuery.prototype.as = function (...varList) {
3✔
743
  // if (varList && varList[0] == 'args')
744
  // return [['indexed_as_var', 'named_as_var']]
745
  if (!Array.isArray(this.query)) this.query = [];
1!
746
  if (Array.isArray(varList[0])) {
1!
747
    if (!varList[1]) {
×
748
      // indexed as vars
749
      for (var i = 0; i < varList[0].length; i++) {
×
750
        const iasv = this.asv(i, varList[0][i]);
×
751
        this.query.push(iasv);
×
752
      }
753
    } else {
754
      for (var i = 0; i < varList.length; i++) {
×
755
        const onemap = varList[i];
×
756
        if (Array.isArray(onemap) && onemap.length >= 2) {
×
757
          const type = onemap && onemap.length > 2 ? onemap[2] : false;
×
758
          const oasv = this.asv(onemap[0], onemap[1], type);
×
759
          this.query.push(oasv);
×
760
        }
761
      }
762
    }
763
  } else if (typeof varList[0] === 'number' || typeof varList[0] === 'string') {
1!
764
    if (varList[2] && typeof varList[2] === 'string') {
1!
765
      var oasv = this.asv(varList[0], varList[1], varList[2]);
1✔
766
    } else if (varList[1] && varList[1] instanceof Var) {
×
767
      var oasv = this.asv(varList[0], varList[1]);
×
768
    } else if (varList[1] && typeof varList[1] === 'string') {
×
769
      if (varList[1].substring(0, 4) === 'xsd:' || varList[1].substring(0, 4) === 'xdd:') {
×
770
        var oasv = this.asv(this.query.length, varList[0], varList[1]);
×
771
      } else {
772
        var oasv = this.asv(varList[0], varList[1]);
×
773
      }
774
    } else {
775
      var oasv = this.asv(this.query.length, varList[0]);
×
776
    }
777
    this.query.push(oasv);
1✔
778
  } else if (typeof varList[0] === 'object') {
×
779
    // check if it is an class object with an json method
780
    this.query.push(varList[0].json ? varList[0].json() : varList[0]);
×
781
  }
782
  return this;
1✔
783
};
784

785
/**
786
 * Identifies a remote resource by URL and specifies the format of the resource through the options
787
 * @param {object} remoteObj - The URL at which the remote resource can be accessed
788
 * @param {typedef.DataFormatObj} [formatObj] - The format of the resource data {}
789
 * @returns {WOQLQuery} A WOQLQuery which contains the remote resource identifier
790
 */
791

792
WOQLQuery.prototype.remote = function (remoteObj, formatObj) {
1✔
793
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
794
  this.cursor['@type'] = 'QueryResource';
1✔
795
  this.cursor.source = { '@type': 'Source', url: remoteObj };
1✔
796
  this.cursor.format = 'csv'; // hard coded for now
1✔
797
  if (typeof opts !== 'undefined') this.cursor.options = formatObj;
1!
798
  return this;
1✔
799
};
800

801
/**
802
 * Identifies a resource as a local path on the client, to be sent to the server through a
803
 * HTTP POST request, with the format defined through the options
804
 * @param {string} url - The Path on the server at which the file resource can be accessed
805
 * @param {typedef.DataFormatObj} [formatObj] - imput options, optional
806
 * @param {string} [source] - It defines the source of the file, it can be 'url','post'
807
 * @returns {WOQLQuery} A WOQLQuery which contains the Post resource identifier
808
 */
809

810
WOQLQuery.prototype.post = function (url, formatObj, source = 'post') {
1!
811
  // if (fpath && fpath == 'args') return ['file', 'format']
812
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
813
  this.cursor['@type'] = 'QueryResource';
×
814
  this.cursor.source = { '@type': 'Source', [source]: url };
×
815
  this.cursor.format = 'csv'; // hard coded for now
×
816
  this.cursor.options = formatObj;
×
817
  if (typeof formatObj !== 'undefined') this.cursor.options = formatObj;
×
818
  return this;
×
819
};
820

821
/**
822
 * Deletes a single triple from the default graph of the database
823
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
824
 * @param {string|Var} predicate - The IRI of a property or a variable
825
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
826
 * @returns {WOQLQuery} - A WOQLQuery which contains the Triple Deletion statement
827
 */
828

829
WOQLQuery.prototype.delete_triple = function (subject, predicate, object) {
1✔
830
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
831
  const args = this.triple(subject, predicate, object);
1✔
832
  this.cursor['@type'] = 'DeleteTriple';
1✔
833
  return this.updated();
1✔
834
};
835

836
/**
837
 * Adds triples according to the the pattern [subject,predicate,object]
838
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
839
 * @param {string|Var} predicate - The IRI of a property or a variable
840
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
841
 * @returns {WOQLQuery}
842
 */
843

844
WOQLQuery.prototype.add_triple = function (subject, predicate, object) {
1✔
845
  if (this.cursor['@type']) this.wrapCursorWithAnd();
2!
846
  const args = this.triple(subject, predicate, object);
2✔
847
  this.cursor['@type'] = 'AddTriple';
2✔
848
  return this.updated();
2✔
849
};
850

851
/**
852
 * Deletes a single triple from the graph [Subject, Predicate, Object, Graph]
853
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
854
 * @param {string|Var} predicate - The IRI of a property or a variable
855
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
856
 * @param {typedef.GraphRef} graphRef - A valid graph resource identifier string
857
 * @returns {WOQLQuery} - A WOQLQuery which contains the Delete Quad Statement
858
 */
859
WOQLQuery.prototype.delete_quad = function (subject, predicate, object, graphRef) {
1✔
860
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
861
  const args = this.triple(subject, predicate, object);
1✔
862
  // if (a && a == 'args') return args.concat(['graph'])
863
  if (!graphRef) {
1!
864
    return this.parameterError(
×
865
      'Delete Quad takes four parameters, the last should be a graph id',
866
    );
867
  }
868
  this.cursor['@type'] = 'DeleteTriple';
1✔
869
  this.cursor.graph = this.cleanGraph(graphRef);
1✔
870
  return this.updated();
1✔
871
};
872

873
/**
874
 * Adds quads according to the pattern [S,P,O,G]
875
 * @param {string|Var} subject - The IRI of a triple’s subject or a variable
876
 * @param {string|Var} predicate - The IRI of a property or a variable
877
 * @param {string|Var} object - The IRI of a node or a variable, or a literal
878
 * @param {typedef.GraphRef} graphRef - A valid graph resource identifier string
879
 * @returns {WOQLQuery}
880
 */
881

882
WOQLQuery.prototype.add_quad = function (subject, predicate, object, graphRef) {
1✔
883
  if (this.cursor['@type']) this.wrapCursorWithAnd();
2!
884
  const args = this.triple(subject, predicate, object);
2✔
885
  if (!graphRef) return this.parameterError('Add Quad takes four parameters, the last should be a graph id');
2!
886
  this.cursor['@type'] = 'AddTriple';
2✔
887
  this.cursor.graph = this.cleanGraph(graphRef);
2✔
888
  return this.updated();
2✔
889
};
890

891
/**
892
 * Remove whitespace from both sides of a string:
893
 * @param {string|Var} inputStr - A string or variable containing
894
 * the untrimmed version of the string
895
 * @param {string|Var} resultVarName - A string or variable
896
 * containing the trimmed version of the string
897
 * @returns {WOQLQuery} A WOQLQuery which contains the Trim pattern matching expression
898
 */
899

900
WOQLQuery.prototype.trim = function (inputStr, resultVarName) {
1✔
901
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
902
  this.cursor['@type'] = 'Trim';
1✔
903
  this.cursor.untrimmed = this.cleanDataValue(inputStr);
1✔
904
  this.cursor.trimmed = this.cleanDataValue(resultVarName);
1✔
905
  return this;
1✔
906
};
907

908
/**
909
 * Evaluates the passed arithmetic expression and generates or matches the result value
910
 * @param {object| WOQLQuery | string} arithExp - query or JSON-LD representing the query
911
 * @param {string|Var} resultVarName - output variable
912
 * @returns {WOQLQuery}
913
 */
914

915
WOQLQuery.prototype.eval = function (arithExp, resultVarName) {
1✔
916
  if (this.cursor['@type']) this.wrapCursorWithAnd();
9!
917
  this.cursor['@type'] = 'Eval';
9✔
918
  this.cursor.expression = arithExp.json ? arithExp.json() : arithExp;
9✔
919
  this.cursor.result = this.cleanArithmeticValue(resultVarName);
9✔
920
  return this;
9✔
921
};
922

923
/**
924
 * Evaluates the passed arithmetic expression and generates or matches the result value.
925
 * Alias for eval() to support both naming conventions in fluent/chained style.
926
 * @param {object|WOQLQuery|string} arithExp - A WOQL query containing a valid arithmetic expression
927
 * @param {string|number|Var} resultVarName - Either a variable to store the result, or a numeric
928
 * literal to test against the evaluated expression
929
 * @returns {WOQLQuery}
930
 */
931

932
WOQLQuery.prototype.evaluate = function (arithExp, resultVarName) {
1✔
933
  return this.eval(arithExp, resultVarName);
2✔
934
};
935

936
/**
937
 * Adds the numbers together
938
 * @param {...(string|number|Var)} args - a variable or numeric containing the values to add
939
 * @returns {WOQLQuery} A WOQLQuery which contains the addition expression
940
 */
941

942
WOQLQuery.prototype.plus = function (...args) {
6✔
943
  // if (args && args[0] == 'args') return ['first', 'second']
944
  if (this.cursor['@type']) this.wrapCursorWithAnd();
3!
945
  this.cursor['@type'] = 'Plus';
3✔
946
  this.cursor.left = this.arop(args.shift());
3✔
947
  if (args.length > 1) {
3!
948
    this.cursor.right = this.jobj(new WOQLQuery().plus(...args.map(this.arop)));
×
949
  } else {
950
    this.cursor.right = this.arop(args[0]);
3✔
951
  }
952
  return this;
3✔
953
};
954

955
/**
956
 *
957
 * Subtracts Numbers N1..Nn
958
 * @param {...(string|number|Var)} args - variable or numeric containing the value that will be
959
 * subtracted from
960
 * @returns {WOQLQuery} A WOQLQuery which contains the subtraction expression
961
 */
962
WOQLQuery.prototype.minus = function (...args) {
2✔
963
  // if (args && args[0] === 'args') return ['first', 'right']
964
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
965
  this.cursor['@type'] = 'Minus';
1✔
966
  this.cursor.left = this.arop(args.shift());
1✔
967
  if (args.length > 1) {
1!
968
    this.cursor.right = this.jobj(new WOQLQuery().minus(...args.map(this.arop)));
×
969
  } else {
970
    this.cursor.right = this.arop(args[0]);
1✔
971
  }
972
  return this;
1✔
973
};
974

975
/**
976
 *
977
 * Multiplies numbers N1...Nn together
978
 * @param {...(string|number|Var)} args - a variable or numeric containing the value
979
 * @returns {WOQLQuery} A WOQLQuery which contains the multiplication expression
980
 */
981
WOQLQuery.prototype.times = function (...args) {
12✔
982
  // if (args && args[0] === 'args') return ['first', 'right']
983
  if (this.cursor['@type']) this.wrapCursorWithAnd();
6!
984
  this.cursor['@type'] = 'Times';
6✔
985
  this.cursor.left = this.arop(args.shift());
6✔
986
  if (args.length > 1) {
6!
987
    this.cursor.right = this.jobj(new WOQLQuery().times(...args.map(this.arop)));
×
988
  } else {
989
    this.cursor.right = this.arop(args[0]);
6✔
990
  }
991
  return this;
6✔
992
};
993

994
/**
995
 * Divides numbers N1...Nn by each other left, to right precedence
996
 * @param {...(string|number|Var )} args - numbers to tbe divided
997
 * @returns {WOQLQuery} A WOQLQuery which contains the division expression
998
 */
999
WOQLQuery.prototype.divide = function (...args) {
2✔
1000
  // if (args && args[0] === 'args') return ['left', 'right']
1001
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1002
  this.cursor['@type'] = 'Divide';
1✔
1003
  this.cursor.left = this.arop(args.shift());
1✔
1004
  if (args.length > 1) {
1!
1005
    this.cursor.right = this.jobj(new WOQLQuery().divide(...args.map(this.arop)));
×
1006
  } else {
1007
    this.cursor.right = this.arop(args[0]);
1✔
1008
  }
1009
  return this;
1✔
1010
};
1011

1012
/**
1013
 * Division - integer division - args are divided left to right
1014
 * @param {...(string|number|Var)} args - numbers for division
1015
 * @returns {WOQLQuery} A WOQLQuery which contains the division expression
1016
 */
1017

1018
WOQLQuery.prototype.div = function (...args) {
2✔
1019
  // if (args && args[0] === 'args') return ['left', 'right']
1020
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1021
  this.cursor['@type'] = 'Div';
1✔
1022
  this.cursor.left = this.arop(args.shift());
1✔
1023
  if (args.length > 1) {
1!
1024
    this.cursor.right = this.jobj(new WOQLQuery().div(...args.map(this.arop)));
×
1025
  } else {
1026
    this.cursor.right = this.arop(args[0]);
1✔
1027
  }
1028
  return this;
1✔
1029
};
1030

1031
/**
1032
 * Exponent - raises varNum01 to the power of varNum02
1033
 * @param {string|number|Var} varNum -  a variable or numeric containing the number to be
1034
 * raised to the power of the second number
1035
 * @param {number} expNum -  a variable or numeric containing the exponent
1036
 * @returns {WOQLQuery} A WOQLQuery which contains the exponent expression
1037
 */
1038
WOQLQuery.prototype.exp = function (varNum, expNum) {
1✔
1039
  // if (a && a === 'args') return ['left', 'right']
1040
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1041
  this.cursor['@type'] = 'Exp';
1✔
1042
  this.cursor.left = this.arop(varNum);
1✔
1043
  this.cursor.right = this.arop(expNum);
1✔
1044
  return this;
1✔
1045
};
1046

1047
/**
1048
 * Generates the nearest lower integer to the passed number
1049
 * @param {string|number|Var} varNum - Variable or numeric containing the number to be floored
1050
 * @returns {WOQLQuery} A WOQLQuery which contains the floor expression
1051
 */
1052
WOQLQuery.prototype.floor = function (varNum) {
1✔
1053
  // if (a && a === 'args') return ['argument']
1054
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1055
  this.cursor['@type'] = 'Floor';
×
1056
  this.cursor.argument = this.arop(varNum);
×
1057
  return this;
×
1058
};
1059

1060
/**
1061
 * Tests whether a given instance IRI has type Class, according to the current state of the DB
1062
 * @param {string|Var} instanceIRI - A string IRI or a variable that identify the class instance
1063
 * @param {string|Var} classId - A Class IRI or a variable
1064
 * @returns {WOQLQuery} A WOQLQuery object containing the type test
1065
 */
1066
WOQLQuery.prototype.isa = function (instanceIRI, classId) {
1✔
1067
  // if (a && a === 'args') return ['element', 'of_type']
1068
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1069
  this.cursor['@type'] = 'IsA';
1✔
1070
  this.cursor.element = this.cleanNodeValue(instanceIRI);
1✔
1071
  this.cursor.type = this.cleanNodeValue(classId);
1✔
1072
  return this;
1✔
1073
};
1074

1075
/**
1076
 * Generates a string Leverstein distance measure between stringA and stringB
1077
 * @param {string|Var} stringA - string literal or variable representing a string to be compared
1078
 * @param {string|Var } stringB - string literal or variable
1079
 * representing the other string to be compared
1080
 * @param {number|string|Var} distance - variable representing the distance between the variables
1081
 * @returns {WOQLQuery} A WOQLQuery which contains the Like pattern matching expression
1082
 */
1083
WOQLQuery.prototype.like = function (stringA, stringB, distance) {
1✔
1084
  // if (a && a === 'args')
1085
  // return ['left', 'right', 'like_similarity']
1086
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1087
  this.cursor['@type'] = 'Like';
×
1088
  this.cursor.left = this.cleanDataValue(stringA, 'xsd:string');
×
1089
  this.cursor.right = this.cleanDataValue(stringB, 'xsd:string');
×
1090
  if (distance) {
×
1091
    this.cursor.similarity = this.cleanDataValue(distance, 'xsd:decimal');
×
1092
  }
1093
  return this;
×
1094
};
1095

1096
/**
1097
 * Compares the value of v1 against v2 and returns true if v1 is less than v2
1098
 * @param {string|number|Var} varNum01 - a variable or numeric containing
1099
 * the number to be compared
1100
 * @param {string|number|Var} varNum02 - a variable or numeric containing the second comporator
1101
 * @returns {WOQLQuery} A WOQLQuery which contains the comparison expression
1102
 */
1103
WOQLQuery.prototype.less = function (varNum01, varNum02) {
1✔
1104
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1105
  this.cursor['@type'] = 'Less';
×
1106
  this.cursor.left = this.cleanDataValue(varNum01);
×
1107
  this.cursor.right = this.cleanDataValue(varNum02);
×
1108
  return this;
×
1109
};
1110

1111
/**
1112
 * Compares the value of v1 against v2 and returns true if v1 is greater than v2
1113
 * @param {string|number|Var} varNum01 - a variable or numeric containing the number to be compared
1114
 * @param {string|number|Var} varNum02 - a variable or numeric containing the second comporator
1115
 * @returns {WOQLQuery} A WOQLQuery which contains the comparison expression
1116
 */
1117
WOQLQuery.prototype.greater = function (varNum01, varNum02) {
1✔
1118
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1119
  this.cursor['@type'] = 'Greater';
×
1120
  this.cursor.left = this.cleanDataValue(varNum01);
×
1121
  this.cursor.right = this.cleanDataValue(varNum02);
×
1122
  return this;
×
1123
};
1124

1125
/**
1126
 * Specifies that the Subquery is optional - if it does not match the query will not fail
1127
 * @param {WOQLQuery} [subquery] - A subquery which will be optionally matched
1128
 * @returns {WOQLQuery} A WOQLQuery object containing the optional sub Query
1129
 */
1130
WOQLQuery.prototype.opt = function (subquery) {
1✔
1131
  if (this.cursor['@type']) this.wrapCursorWithAnd();
2!
1132
  this.cursor['@type'] = 'Optional';
2✔
1133
  this.addSubQuery(subquery);
2✔
1134
  return this;
2✔
1135
};
1136

1137
WOQLQuery.prototype.optional = WOQLQuery.prototype.opt;
1✔
1138

1139
/**
1140
 * Generate a new IRI from the prefix and a hash of the variables which will be unique for any
1141
 * given combination of variables
1142
 * @param {string} prefix - A prefix for the IRI - typically formed of the doc prefix and the
1143
 * classtype of the entity (“doc:Person”)
1144
 * @param {array|string|Var} inputVarList - An array of variables and / or strings from which the
1145
 * unique hash will be generated
1146
 * @param {string|Var} resultVarName - Variable in which the unique ID is stored
1147
 * @returns {WOQLQuery} A WOQLQuery object containing the unique ID generating function
1148
 */
1149
WOQLQuery.prototype.unique = function (prefix, inputVarList, resultVarName) {
1✔
1150
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1151
  this.cursor['@type'] = 'HashKey';
×
1152
  this.cursor.base = this.cleanDataValue(prefix, 'xsd:string');
×
1153
  this.cursor.key_list = this.cleanDataValue(inputVarList);
×
1154
  this.cursor.uri = this.cleanNodeValue(resultVarName);
×
1155
  return this;
×
1156
};
1157

1158
/**
1159
 * Generates the node's ID combined the variable list with a specific prefix (URL base).
1160
 * If the input variables's values are the same, the output value will be the same.
1161
 * @param {string} prefix
1162
 * @param {string |array}  inputVarList the variable input list for generate the id
1163
 * @param {string} outputVar  the output variable name
1164
 */
1165

1166
WOQLQuery.prototype.idgen = function (prefix, inputVarList, outputVar) {
1✔
1167
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1168
  this.cursor['@type'] = 'LexicalKey';
1✔
1169
  this.cursor.base = this.cleanDataValue(prefix, 'xsd:string');
1✔
1170
  // this.cursor['base'] = this.cleanObject(this.string(prefix))
1171
  this.cursor.key_list = this.dataValueList(inputVarList);
1✔
1172
  this.cursor.uri = this.cleanNodeValue(outputVar);
1✔
1173
  return this;
1✔
1174
};
1175

1176
WOQLQuery.prototype.idgenerator = WOQLQuery.prototype.idgen;
1✔
1177

1178
/**
1179
 * Changes a string to upper-case
1180
 * @param {string|Var} inputVarName - string or variable representing the uncapitalized string
1181
 * @param {string|Var} resultVarName -  variable that stores the capitalized string output
1182
 * @returns {WOQLQuery} A WOQLQuery which contains the Upper case pattern matching expression
1183
 */
1184
WOQLQuery.prototype.upper = function (inputVarName, resultVarName) {
1✔
1185
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1186
  this.cursor['@type'] = 'Upper';
×
1187
  this.cursor.mixed = this.cleanDataValue(inputVarName);
×
1188
  this.cursor.upper = this.cleanDataValue(resultVarName);
×
1189
  return this;
×
1190
};
1191

1192
/**
1193
 * Changes a string to lower-case
1194
 * @param {string|Var} inputVarName -  string or variable representing the non-lowercased string
1195
 * @param {string|Var} resultVarName - variable that stores the lowercased string output
1196
 * @returns {WOQLQuery} A WOQLQuery which contains the Lower case pattern matching expression
1197
 */
1198

1199
WOQLQuery.prototype.lower = function (inputVarName, resultVarName) {
1✔
1200
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1201
  this.cursor['@type'] = 'Lower';
×
1202
  this.cursor.mixed = this.cleanDataValue(inputVarName);
×
1203
  this.cursor.lower = this.cleanDataValue(resultVarName);
×
1204
  return this;
×
1205
};
1206

1207
/**
1208
 * Pads out the string input to be exactly len long by appending the pad character pad to
1209
 * form output
1210
 * @param {string|Var} inputVarName - The input string or variable in unpadded state
1211
 * @param {string|Var} pad - The characters to use to pad the string or a variable representing them
1212
 * @param {number | string | Var} len - The variable or integer value representing the length of
1213
 * the output string
1214
 * @param {string|Var} resultVarName - stores output
1215
 * @returns {WOQLQuery} A WOQLQuery which contains the Pad pattern matching expression
1216
 */
1217

1218
WOQLQuery.prototype.pad = function (inputVarName, pad, len, resultVarName) {
1✔
1219
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1220
  this.cursor['@type'] = 'Pad';
×
1221
  this.cursor.string = this.cleanDataValue(inputVarName);
×
1222
  this.cursor.char = this.cleanDataValue(pad);
×
1223
  this.cursor.times = this.cleanDataValue(len, 'xsd:integer');
×
1224
  this.cursor.result = this.cleanDataValue(resultVarName);
×
1225
  return this;
×
1226
};
1227

1228
/**
1229
 * Splits a string (Input) into a list strings (Output) by removing separator
1230
 * @param {string|Var} inputVarName - A string or variable representing the unsplit string
1231
 * @param {string|Var} separator - A string or variable containing a sequence of charatcters
1232
 * to use as a separator
1233
 * @param {string|Var} resultVarName - variable that stores output list
1234
 * @returns {WOQLQuery} A WOQLQuery which contains the Split pattern matching expression
1235
 */
1236

1237
WOQLQuery.prototype.split = function (inputVarName, separator, resultVarName) {
1✔
1238
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1239
  this.cursor['@type'] = 'Split';
1✔
1240
  this.cursor.string = this.cleanDataValue(inputVarName);
1✔
1241
  this.cursor.pattern = this.cleanDataValue(separator);
1✔
1242
  this.cursor.list = this.cleanDataValue(resultVarName);
1✔
1243
  return this;
1✔
1244
};
1245

1246
/**
1247
 * Matches if List includes Element
1248
 * @param {string|object|Var} element - Either a variable, IRI or any simple datatype
1249
 * @param {string|array|Var} list - List ([string, literal] or string*) Either a variable
1250
 * representing a list or a list of variables or literals
1251
 * @returns {WOQLQuery} A WOQLQuery which contains the List inclusion pattern matching expression
1252
 */
1253
WOQLQuery.prototype.member = function (element, list) {
1✔
1254
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1255
  this.cursor['@type'] = 'Member';
1✔
1256
  this.cursor.member = this.cleanObject(element);
1✔
1257
  this.cursor.list = this.valueList(list);
1✔
1258
  return this;
1✔
1259
};
1260

1261
/**
1262
 * takes a variable number of string arguments and concatenates them into a single string
1263
 * @param {array|string|Var} varList -  a variable representing a list or a list of variables or
1264
 * strings - variables can be embedded in the string if they do not contain spaces
1265
 * @param {string|Var}  resultVarName - A variable or string containing the output string
1266
 * @returns {WOQLQuery} A WOQLQuery which contains the Concatenation pattern matching expression
1267
 */
1268

1269
WOQLQuery.prototype.concat = function (varList, resultVarName) {
1✔
1270
  if (typeof varList === 'string') {
1!
1271
    const slist = varList.split(/(v:)/);
1✔
1272
    const nlist = [];
1✔
1273
    if (slist[0]) nlist.push(slist[0]);
1!
1274
    for (let i = 1; i < slist.length; i += 2) {
1✔
1275
      if (slist[i]) {
2!
1276
        if (slist[i] === 'v:') {
2!
1277
          const slist2 = slist[i + 1].split(/([^\w_])/);
2✔
1278
          const x = slist2.shift();
2✔
1279
          nlist.push(`v:${x}`);
2✔
1280
          const rest = slist2.join('');
2✔
1281
          if (rest) nlist.push(rest);
2✔
1282
        }
1283
      }
1284
    }
1285
    varList = nlist;
1✔
1286
  }
1287
  if (Array.isArray(varList)) {
1!
1288
    if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1289
    this.cursor['@type'] = 'Concatenate';
1✔
1290
    this.cursor.list = this.cleanDataValue(varList, true);
1✔
1291
    this.cursor.result = this.cleanDataValue(resultVarName);
1✔
1292
  }
1293
  return this;
1✔
1294
};
1295

1296
WOQLQuery.prototype.concatenate = WOQLQuery.prototype.concat;
1✔
1297

1298
/**
1299
 * Joins a list variable together (Input) into a string variable (Output) by glueing the strings
1300
 * together with Glue
1301
 * @param {string|array|Var} varList - a variable representing a list or a list of strings
1302
 * and / or variables
1303
 * @param {string|Var} glue - A variable (v:glue) or (glue) string representing the characters
1304
 * to put in between the joined strings in input
1305
 * @param {string|Var} resultVarName - A variable or string containing the output string
1306
 * @returns {WOQLQuery} A WOQLQuery which contains the Join pattern matching expression
1307
 */
1308
WOQLQuery.prototype.join = function (varList, glue, resultVarName) {
1✔
1309
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1310
  this.cursor['@type'] = 'Join';
1✔
1311
  this.cursor.list = this.cleanDataValue(varList);
1✔
1312
  this.cursor.separator = this.cleanDataValue(glue);
1✔
1313
  this.cursor.result = this.cleanDataValue(resultVarName);
1✔
1314
  return this;
1✔
1315
};
1316

1317
/**
1318
 * computes the sum of the List of values passed. In contrast to other arithmetic functions,
1319
 * sum self-evaluates - it does not have to be passed to evaluate()
1320
 * @param {WOQLQuery} subquery -  a subquery or ([string or numeric]) - a list variable, or a
1321
 * list of variables or numeric literals
1322
 * @param {string|Var} total - the variable name with the sum result of the values in List
1323
 * @returns {WOQLQuery} - A WOQLQuery which contains the Sum expression
1324
 */
1325
WOQLQuery.prototype.sum = function (subquery, total) {
1✔
1326
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1327
  this.cursor['@type'] = 'Sum';
×
1328
  this.cursor.list = this.cleanDataValue(subquery);
×
1329
  this.cursor.result = this.cleanObject(total);
×
1330
  return this;
×
1331
};
1332

1333
/**
1334
 *
1335
 * Specifies an offset position in the results to start listing results from
1336
 * @param {number|string|Var} start - A variable that refers to an interger or an integer literal
1337
 * @param {WOQLQuery} [subquery] - WOQL Query object, you can pass a subquery as an argument
1338
 * or a chained query
1339
 * @returns {WOQLQuery} A WOQLQuery whose results will be returned starting from
1340
 * the specified offset
1341
 */
1342

1343
WOQLQuery.prototype.start = function (start, subquery) {
1✔
1344
  // if (start && start === 'args') return ['start', 'query']
1345
  if (this.cursor['@type']) this.wrapCursorWithAnd();
2!
1346
  this.cursor['@type'] = 'Start';
2✔
1347
  this.cursor.start = start;
2✔
1348
  return this.addSubQuery(subquery);
2✔
1349
};
1350

1351
/**
1352
 * Specifies a maximum number of results that will be returned from the subquery
1353
 * @param {number|string} limit - A variable that refers to an non-negative integer or a
1354
 * non-negative integer
1355
 * @param {WOQLQuery} [subquery] - A subquery whose results will be limited
1356
 * @returns {WOQLQuery} A WOQLQuery whose results will be returned starting from
1357
 * the specified offset
1358
 */
1359

1360
WOQLQuery.prototype.limit = function (limit, subquery) {
1✔
1361
  if (this.cursor['@type']) this.wrapCursorWithAnd();
9!
1362
  this.cursor['@type'] = 'Limit';
9✔
1363
  this.cursor.limit = limit;
9✔
1364
  return this.addSubQuery(subquery);
9✔
1365
};
1366

1367
/**
1368
 * Matches the regular expression defined in Patern against the Test string, to produce
1369
 * the matched patterns in Matches
1370
 * @param {string} pattern - string or variable using normal PCRE regular expression syntax with
1371
 * the exception that special characters have to be escaped twice (to enable transport in JSONLD)
1372
 * @param {string|Var} inputVarName - string or variable containing the string to be tested for
1373
 * patterns with the regex
1374
 * @param {string|array|object|Var} resultVarList - variable representing the list of matches
1375
 * or a list of strings or variables
1376
 * @returns {WOQLQuery} A WOQLQuery which contains the Regular Expression pattern
1377
 * matching expression
1378
 */
1379

1380
WOQLQuery.prototype.re = function (pattern, inputVarName, resultVarList) {
1✔
1381
  if (this.cursor['@type']) this.wrapCursorWithAnd();
1!
1382
  this.cursor['@type'] = 'Regexp';
1✔
1383
  this.cursor.pattern = this.cleanDataValue(pattern);
1✔
1384
  this.cursor.string = this.cleanDataValue(inputVarName);
1✔
1385
  this.cursor.result = this.cleanDataValue(resultVarList);
1✔
1386
  return this;
1✔
1387
};
1388

1389
WOQLQuery.prototype.regexp = WOQLQuery.prototype.re;
1✔
1390

1391
/**
1392
 * Calculates the length of the list in va and stores it in vb
1393
 * @param {string|array} inputVarList - Either a variable representing a list or a list of
1394
 * variables or literals
1395
 * @param {string|Var} resultVarName -  A variable in which the length of the list is stored or
1396
 * the length of the list as a non-negative integer
1397
 * @returns {WOQLQuery} A WOQLQuery which contains the Length pattern matching expression
1398
 */
1399
WOQLQuery.prototype.length = function (inputVarList, resultVarName) {
1✔
1400
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1401
  this.cursor['@type'] = 'Length';
×
1402
  this.cursor.list = this.cleanDataValue(inputVarList);
×
NEW
1403
  if (typeof resultVarName === 'number') {
×
1404
    this.cursor.length = this.cleanObject(resultVarName, 'xsd:nonNegativeInteger');
×
NEW
1405
  } else if (typeof resultVarName === 'string') {
×
1406
    this.cursor.length = this.varj(resultVarName);
×
1407
  }
1408
  return this;
×
1409
};
1410

1411
/**
1412
 *
1413
 * Logical negation of the contained subquery - if the subquery matches, the query
1414
 * will fail to match
1415
 * @param {string | WOQLQuery} [subquery] -  A subquery which will be negated
1416
 * @returns {WOQLQuery} A WOQLQuery object containing the negated sub Query
1417
 */
1418
WOQLQuery.prototype.not = function (subquery) {
1✔
1419
  if (this.cursor['@type']) this.wrapCursorWithAnd();
3!
1420
  this.cursor['@type'] = 'Not';
3✔
1421
  return this.addSubQuery(subquery);
3✔
1422
};
1423

1424
/**
1425
 * Results in one solution of the subqueries
1426
 * @param {string| WOQLQuery } [subquery] - WOQL Query objects
1427
 * @returns {WOQLQuery} A WOQLQuery object containing the once sub Query
1428
 */
1429
WOQLQuery.prototype.once = function (subquery) {
1✔
1430
  // if (query && query === 'args') return ['query']
1431
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1432
  this.cursor['@type'] = 'Once';
×
1433
  return this.addSubQuery(subquery);
×
1434
};
1435

1436
/**
1437
 * Runs the query without backtracking on side-effects
1438
 * @param {string| WOQLQuery } [subquery] - WOQL Query objects
1439
 * @returns {WOQLQuery} A WOQLQuery object containing the immediately sub Query
1440
 */
1441
WOQLQuery.prototype.immediately = function (query) {
1✔
1442
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1443
  this.cursor['@type'] = 'Immediately';
×
1444
  return this.addSubQuery(query);
×
1445
};
1446

1447
/**
1448
 * Creates a count of the results of the query
1449
 * @param {string|number|Var} countVarName - variable or integer count
1450
 * @param {WOQLQuery} [subquery]
1451
 * @returns {WOQLQuery} A WOQLQuery object containing the count sub Query
1452
 */
1453
WOQLQuery.prototype.count = function (countVarName, subquery) {
1✔
1454
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1455
  this.cursor['@type'] = 'Count';
×
1456
  this.cursor.count = this.cleanObject(countVarName);
×
1457
  return this.addSubQuery(subquery);
×
1458
};
1459

1460
/**
1461
 * Casts the value of Input to a new value of type Type and stores the result in CastVar
1462
 * @param {string|number|object|Var} varName - Either a single variable or a
1463
 * literal of any basic type
1464
 * @param {string|Var} varType - Either a variable or a basic datatype (xsd / xdd)
1465
 * @param {string|Var} resultVarName - save the return variable
1466
 * @returns {WOQLQuery} A WOQLQuery which contains the casting expression
1467
 */
1468

1469
WOQLQuery.prototype.typecast = function (varName, varType, resultVarName) {
1✔
1470
  if (this.cursor['@type']) this.wrapCursorWithAnd();
2!
1471
  this.cursor['@type'] = 'Typecast';
2✔
1472
  this.cursor.value = this.cleanObject(varName);
2✔
1473
  this.cursor.type = this.cleanNodeValue(varType);
2✔
1474
  this.cursor.result = this.cleanObject(resultVarName);
2✔
1475
  return this;
2✔
1476
};
1477

1478
WOQLQuery.prototype.cast = WOQLQuery.prototype.typecast;
1✔
1479

1480
/**
1481
 * Orders the results of the contained subquery by a precedence list of variables
1482
 * @param  {...string|...Var|...array} orderedVarlist - A sequence of variables,
1483
 * by which to order the results,
1484
 * each optionally followed by either “asc” or “desc” to represent order as a list, by default
1485
 * it will sort the variable in ascending order
1486
 * @returns  {WOQLQuery} A WOQLQuery which contains the ordering expression
1487
 */
1488
WOQLQuery.prototype.order_by = function (...orderedVarlist) {
6✔
1489
  // if (orderedVarlist && orderedVarlist[0] === 'args')
1490
  // return ['variable_ordering', 'query']
1491
  if (this.cursor['@type']) this.wrapCursorWithAnd();
2!
1492
  this.cursor['@type'] = 'OrderBy';
2✔
1493
  this.cursor.ordering = [];
2✔
1494

1495
  if (!orderedVarlist || orderedVarlist.length === 0) {
2!
1496
    return this.parameterError(
×
1497
      'Order by must be passed at least one variables to order the query',
1498
    );
1499
  }
1500
  const embedquery = typeof orderedVarlist[orderedVarlist.length - 1] === 'object'
2!
1501
    && orderedVarlist[orderedVarlist.length - 1].json
1502
    ? orderedVarlist.pop()
1503
    : false;
1504

1505
  for (let i = 0; i < orderedVarlist.length; i++) {
2✔
1506
    let obj;
6✔
1507
    if ((typeof orderedVarlist[i] === 'string' || orderedVarlist[i] instanceof Var) && orderedVarlist[i] !== '') {
6!
1508
      obj = {
6✔
1509
        '@type': 'OrderTemplate',
1510
        variable: this.rawVar(orderedVarlist[i]),
1511
        order: 'asc',
1512
      };
1513
    } else if (orderedVarlist[i].length === 2 && orderedVarlist[i][1] === 'asc') {
×
1514
      obj = {
×
1515
        '@type': 'OrderTemplate',
1516
        variable: this.rawVar(orderedVarlist[i][0]),
1517
        order: 'asc',
1518
      };
1519
    } else if (orderedVarlist[i].length === 2 && orderedVarlist[i][1] === 'desc') {
×
1520
      obj = {
×
1521
        '@type': 'OrderTemplate',
1522
        variable: this.rawVar(orderedVarlist[i][0]),
1523
        order: 'desc',
1524
      };
1525
    }
1526

1527
    if (obj) this.cursor.ordering.push(obj);
6!
1528
  }
1529
  return this.addSubQuery(embedquery);
2✔
1530
};
1531

1532
/**
1533
 *
1534
 * Groups the results of the contained subquery on the basis of identical values for Groupvars,
1535
 * extracts the patterns defined in PatternVars and stores the results in GroupedVar
1536
 * @param {array|string|Var} gvarlist - Either a single variable or an array of variables
1537
 * @param {array|string|Var} groupedvar - Either a single variable or an array of variables
1538
 * @param {string|Var} output - output variable name
1539
 * @param {WOQLQuery} [groupquery] - The query whose results will be grouped
1540
 * @returns {WOQLQuery} A WOQLQuery which contains the grouping expression
1541
 */
1542

1543
WOQLQuery.prototype.group_by = function (gvarlist, groupedvar, output, groupquery) {
1✔
1544
  // if (gvarlist && gvarlist === 'args')
1545
  // return ['group_by', 'group_template', 'grouped', 'query']
1546
  if (this.cursor['@type']) this.wrapCursorWithAnd();
3!
1547
  this.cursor['@type'] = 'GroupBy';
3✔
1548
  this.cursor.group_by = [];
3✔
1549

1550
  if (typeof gvarlist === 'string' || gvarlist instanceof Var) gvarlist = [gvarlist];
3✔
1551
  this.cursor.group_by = this.rawVarList(gvarlist);
3✔
1552
  if (typeof groupedvar === 'string' || groupedvar instanceof Var) groupedvar = [groupedvar];
3✔
1553
  this.cursor.template = this.rawVarList(groupedvar);
3✔
1554
  this.cursor.grouped = this.varj(output);
3✔
1555
  return this.addSubQuery(groupquery);
3✔
1556
};
1557

1558
/**
1559
 * A function that always matches, always returns true
1560
 * @returns {WOQLQuery} A WOQLQuery object containing the true value that will match any pattern
1561
 */
1562
WOQLQuery.prototype.true = function () {
1✔
1563
  this.cursor['@type'] = 'True';
×
1564
  return this;
×
1565
};
1566

1567
/**
1568
 * Performs a path regular expression match on the graph
1569
 * @param {string|Var} subject -  An IRI or variable that refers to an IRI representing the subject,
1570
 * i.e. the starting point of the path
1571
 * @param {string} pattern -(string) - A path regular expression describing a pattern through
1572
 * multiple edges of the graph (see: https://terminusdb.com/docs/path-query-reference-guide)
1573
 * @param {string|Var} object - An IRI or variable that refers to an IRI representing the object,
1574
 * i.e. ending point of the path
1575
 * @param {string|Var} [resultVarName] - A variable in which the actual paths
1576
 * traversed will be stored
1577
 * @returns {WOQLQuery} - A WOQLQuery which contains the path regular expression matching expression
1578
 */
1579

1580
WOQLQuery.prototype.path = function (subject, pattern, object, resultVarName) {
1✔
1581
  if (this.cursor['@type']) this.wrapCursorWithAnd();
5!
1582
  this.cursor['@type'] = 'Path';
5✔
1583
  this.cursor.subject = this.cleanSubject(subject);
5✔
1584
  if (typeof pattern === 'string') pattern = this.compilePathPattern(pattern);
5!
1585
  this.cursor.pattern = pattern;
5✔
1586
  this.cursor.object = this.cleanObject(object);
5✔
1587
  if (typeof resultVarName !== 'undefined') {
5✔
1588
    this.cursor.path = this.varj(resultVarName);
4✔
1589
  }
1590
  return this;
5✔
1591
};
1592

1593
/**
1594
 * Extract the value of a key in a bound document.
1595
 * @param {string|Var} document - Document which is being accessed.
1596
 * @param {string|Var} field - The field from which the document which is being accessed.
1597
 * @param {string|Var} value - The value for the document and field.
1598
 * @returns {WOQLQuery} A WOQLQuery which contains the a dot Statement
1599
 */
1600

1601
WOQLQuery.prototype.dot = function (document, field, value) {
1✔
1602
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1603
  this.cursor['@type'] = 'Dot';
×
1604
  this.cursor.document = this.expandValueVariable(document);
×
1605
  this.cursor.field = this.cleanDataValue(field, 'xsd:string');
×
1606
  this.cursor.value = this.expandValueVariable(value);
×
1607
  return this;
×
1608
};
1609

1610
/**
1611
 * Calculates the size in bytes of the contents of the resource identified in ResourceID
1612
 * @param {string|Var} resourceId - A valid resource identifier string (can refer to any graph /
1613
 * branch / commit / db)
1614
 * @param {string|Var} resultVarName - The variable name
1615
 */
1616

1617
WOQLQuery.prototype.size = function (resourceId, resultVarName) {
1✔
1618
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1619
  this.cursor['@type'] = 'Size';
×
1620
  this.cursor.resource = this.cleanGraph(resourceId);
×
1621
  this.cursor.size = this.varj(resultVarName);
×
1622
  return this;
×
1623
};
1624

1625
/**
1626
 *
1627
 * Calculates the number of triples of the contents of the resource identified in ResourceID
1628
 * @param {string|Var} resourceId - A valid resource identifier string (can refer to any graph /
1629
 * branch / commit / db)
1630
 * @param {string|number|Var} tripleCount - An integer literal with the size in bytes or a
1631
 * variable containing that integer
1632
 * @returns {WOQLQuery} A WOQLQuery which contains the size expression
1633
 */
1634
WOQLQuery.prototype.triple_count = function (resourceId, TripleCount) {
1✔
1635
  if (this.cursor['@type']) this.wrapCursorWithAnd();
×
1636
  this.cursor['@type'] = 'TripleCount';
×
1637
  this.cursor.resource = this.cleanGraph(resourceId);
×
1638
  this.cursor.count = this.varj(TripleCount);
×
1639
  return this;
×
1640
};
1641

1642
/**
1643
 * Returns true if 'elementId' is of type 'elementType', according to the current DB schema
1644
 * @param {string|Var} elementId - the id of a schema graph element
1645
 * @param {string|Var} elementType - the element type
1646
 * @returns {WOQLQuery} A WOQLQuery object containing the type_of pattern matching rule
1647
 */
1648

1649
WOQLQuery.prototype.type_of = function (elementId, elementType) {
1✔
1650
  if (!elementId || !elementType) return this.parameterError('type_of takes two parameters, both values');
3!
1651
  if (this.cursor['@type']) this.wrapCursorWithAnd();
3!
1652
  this.cursor['@type'] = 'TypeOf';
3✔
1653
  this.cursor.value = this.cleanObject(elementId);
3✔
1654
  this.cursor.type = this.cleanSubject(elementType);
3✔
1655
  return this;
3✔
1656
};
1657

1658
module.exports = WOQLQuery;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc