MCPcopy Create free account
hub / github.com/andymatuschak/orbit / constructListSQLQuery

Function constructListSQLQuery

packages/store-fs/src/sqlite.ts:418–460  ·  view source on GitHub ↗
({
  tableExpression,
  idKey,
  orderKey,
  columns,
  options,
  predicates,
}: {
  tableExpression: SQLTableName | string;
  idKey: string;
  orderKey: string;
  columns: string[];
  options: DatabaseQueryOptions<any>;
  predicates: DatabaseQueryPredicate[];
})

Source from the content-addressed store, hash-verified

416}
417
418function constructListSQLQuery({
419 tableExpression,
420 idKey,
421 orderKey,
422 columns,
423 options,
424 predicates,
425}: {
426 tableExpression: SQLTableName | string;
427 idKey: string;
428 orderKey: string;
429 columns: string[];
430 options: DatabaseQueryOptions<any>;
431 predicates: DatabaseQueryPredicate[];
432}): { statement: string; args: any[] } {
433 const args: any[] = [];
434
435 const whereExpressions: string[] = [];
436
437 for (const predicate of predicates) {
438 whereExpressions.push(`${predicate[0]} ${predicate[1]} ?`);
439 args.push(predicate[2]);
440 }
441
442 if (options.afterID) {
443 whereExpressions.push(
444 `${orderKey} > (SELECT ${orderKey} FROM ${tableExpression} WHERE ${idKey}=?)`,
445 );
446 args.push(options.afterID);
447 }
448
449 const whereClause =
450 whereExpressions.length > 0
451 ? `WHERE ${whereExpressions.join(" AND ")}`
452 : "";
453 const limitClause =
454 options.limit === undefined ? "" : `LIMIT ${options.limit}`;
455
456 const query = `SELECT ${columns.join(
457 ",",
458 )} FROM ${tableExpression} ${whereClause} ${limitClause}`;
459 return { statement: query, args };
460}
461
462export function constructGetByIDSQLQuery<Column, ID>(
463 tableName: SQLTableName,

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected