Skip to content

Commit

Permalink
feat(interpreter): adds possibility to specify interpreter options
Browse files Browse the repository at this point in the history
  • Loading branch information
stalniy committed Dec 2, 2020
1 parent 4fc52c8 commit 701e951
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions packages/sql/src/interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
createInterpreter,
Condition,
InterpretationContext
InterpretationContext,
InterpreterOptions
} from '@ucast/core';
import { DialectOptions } from './dialects';

Expand All @@ -26,13 +27,13 @@ export class Query {
private _sql: string[] = [];
private _joins = new Set<string>();
private _lastPlaceholderIndex: number = 1;
private _targetQuery!: unknown;
private _relationContext!: unknown;
private _rootAlias!: string;

constructor(options: SqlQueryOptions, fieldPrefix: string = '', targetQuery?: unknown) {
constructor(options: SqlQueryOptions, fieldPrefix: string = '', relationContext?: unknown) {
this.options = options;
this._fieldPrefix = fieldPrefix;
this._targetQuery = targetQuery;
this._relationContext = relationContext;
this._rootAlias = options.rootAlias ? `${options.escapeField(options.rootAlias)}.` : '';

if (this.options.foreignField) {
Expand Down Expand Up @@ -60,7 +61,7 @@ export class Query {
const relationName = name.slice(0, relationNameIndex);
const field = name.slice(relationNameIndex + 1);

if (!this.options.joinRelation(relationName, this._targetQuery)) {
if (!this.options.joinRelation(relationName, this._relationContext)) {
return this._rootAlias + this._localField(name);
}

Expand Down Expand Up @@ -96,7 +97,7 @@ export class Query {
canLinkParams = !!linkParams;
}

const query = new Query(queryOptions, this._fieldPrefix, this._targetQuery);
const query = new Query(queryOptions, this._fieldPrefix, this._relationContext);

if (canLinkParams) {
query._params = this._params;
Expand Down Expand Up @@ -157,9 +158,16 @@ export type SqlOperator<C extends Condition> = (
context: InterpretationContext<SqlOperator<C>>,
) => Query;

export function createSqlInterpreter(operators: Record<string, SqlOperator<any>>) {
const interpret = createInterpreter<SqlOperator<any>>(operators);
return (condition: Condition, options: SqlQueryOptions, targetQuery?: unknown) => {
return interpret(condition, new Query(options, '', targetQuery)).toJSON();
interface SqlInterpreterOptions {
getInterpreterName?: InterpreterOptions['getInterpreterName']
}

export function createSqlInterpreter(
operators: Record<string, SqlOperator<any>>,
options?: SqlInterpreterOptions
) {
const interpret = createInterpreter<SqlOperator<any>>(operators, options);
return (condition: Condition, sqlOptions: SqlQueryOptions, relationContext?: unknown) => {
return interpret(condition, new Query(sqlOptions, '', relationContext)).toJSON();
};
}

0 comments on commit 701e951

Please sign in to comment.