Skip to content

Commit

Permalink
extract LHS and RHS for better code re-use
Browse files Browse the repository at this point in the history
  • Loading branch information
TristenHarr committed Nov 5, 2024
1 parent a06845e commit 504b19f
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions ndc-duckduckapi/src/handlers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ function getIntegerType(field_def: any): string | null {
return checkType(field_def.type);
}

function getRhsExpression(type: string | null): string {
if (!type) return "?";
return `CAST(? AS ${type})`;
}

function build_where(
expression: Expression,
collection_relationships: {
Expand Down Expand Up @@ -152,6 +157,9 @@ function build_where(
const field_def = object_type?.fields[expression.column.name];
const isTimestamp = isTimestampType(field_def);
const integerType = getIntegerType(field_def);
const type = isTimestamp ? "TIMESTAMP" : integerType;
const lhs = escape_double(expression.column.name);
const rhs = getRhsExpression(type);
switch (expression.value.type) {
case "scalar":
args.push(expression.value.value);
Expand All @@ -168,53 +176,29 @@ function build_where(
}
switch (expression.operator) {
case "_eq":
sql = isTimestamp
? `${escape_double(expression.column.name)} = CAST(? AS TIMESTAMP)`
: integerType
? `${escape_double(expression.column.name)} = CAST(? AS ${integerType})`
: `${escape_double(expression.column.name)} = ?`;
sql = `${lhs} = ${rhs}`;
break;
case "_neq":
sql = isTimestamp
? `${escape_double(expression.column.name)} != CAST(? AS TIMESTAMP)`
: integerType
? `${escape_double(expression.column.name)} != CAST(? AS ${integerType})`
: `${escape_double(expression.column.name)} != ?`;
sql = `${lhs} != ${rhs}`;
break;
case "_gt":
sql = isTimestamp
? `${escape_double(expression.column.name)} > CAST(? AS TIMESTAMP)`
: integerType
? `${escape_double(expression.column.name)} > CAST(? AS ${integerType})`
: `${escape_double(expression.column.name)} > ?`;
sql = `${lhs} > ${rhs}`;
break;
case "_lt":
sql = isTimestamp
? `${escape_double(expression.column.name)} < CAST(? AS TIMESTAMP)`
: integerType
? `${escape_double(expression.column.name)} < CAST(? AS ${integerType})`
: `${escape_double(expression.column.name)} < ?`;
sql = `${lhs} < ${rhs}`;
break;
case "_gte":
sql = isTimestamp
? `${escape_double(expression.column.name)} >= CAST(? AS TIMESTAMP)`
: integerType
? `${escape_double(expression.column.name)} >= CAST(? AS ${integerType})`
: `${escape_double(expression.column.name)} >= ?`;
sql = `${lhs} >= ${rhs}`;
break;
case "_lte":
sql = isTimestamp
? `${escape_double(expression.column.name)} <= CAST(? AS TIMESTAMP)`
: integerType
? `${escape_double(expression.column.name)} <= CAST(? AS ${integerType})`
: `${escape_double(expression.column.name)} <= ?`;
sql = `${lhs} <= ${rhs}`;
break;
case "_like":
args[args.length - 1] = `%${args[args.length - 1]}%`;
sql = `${escape_double(expression.column.name)} LIKE ?`;
sql = `${lhs} LIKE ?`;
break;
case "_glob":
sql = `${escape_double(expression.column.name)} GLOB ?`;
sql = `${lhs} GLOB ?`;
break;
default:
throw new Forbidden(
Expand Down

0 comments on commit 504b19f

Please sign in to comment.