fix: Single binding sql query causing prepared statement issue (#7792)

This commit is contained in:
Hetu Nandu 2021-10-01 21:05:05 +05:30 committed by GitHub
parent 1e6b916ff9
commit 633db3068e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 14 deletions

View File

@ -17,11 +17,11 @@ import {
DataTree, DataTree,
DataTreeAction, DataTreeAction,
DataTreeEntity, DataTreeEntity,
DataTreeJSAction,
DataTreeObjectEntity, DataTreeObjectEntity,
DataTreeWidget, DataTreeWidget,
ENTITY_TYPE, ENTITY_TYPE,
EvaluationSubstitutionType, EvaluationSubstitutionType,
DataTreeJSAction,
} from "entities/DataTree/dataTreeFactory"; } from "entities/DataTree/dataTreeFactory";
import { import {
addDependantsOfNestedPropertyPaths, addDependantsOfNestedPropertyPaths,
@ -34,15 +34,15 @@ import {
getEntityNameAndPropertyPath, getEntityNameAndPropertyPath,
getImmediateParentsOfPropertyPaths, getImmediateParentsOfPropertyPaths,
getValidatedTree, getValidatedTree,
isAction,
isDynamicLeaf,
isJSAction,
isWidget,
makeParentsDependOnChildren, makeParentsDependOnChildren,
removeFunctions, removeFunctions,
translateDiffEventToDataTreeDiffEvent, translateDiffEventToDataTreeDiffEvent,
trimDependantChangePaths, trimDependantChangePaths,
validateWidgetProperty, validateWidgetProperty,
isDynamicLeaf,
isWidget,
isAction,
isJSAction,
} from "workers/evaluationUtils"; } from "workers/evaluationUtils";
import _ from "lodash"; import _ from "lodash";
import { applyChange, Diff, diff } from "deep-diff"; import { applyChange, Diff, diff } from "deep-diff";
@ -661,15 +661,39 @@ export default class DataTreeEvaluator {
} }
}); });
// if it is just one binding, return that directly // We dont need to substitute template of the result if only one binding exists
if (stringSegments.length === 1) return values[0]; // But it should not be of prepared statements since that does need a string
// else return a combined value according to the evaluation type if (
return substituteDynamicBindingWithValues( stringSegments.length === 1 &&
dynamicBinding, evaluationSubstitutionType !== EvaluationSubstitutionType.PARAMETER
stringSegments, ) {
values, return values[0];
evaluationSubstitutionType, }
); try {
// else return a combined value according to the evaluation type
return substituteDynamicBindingWithValues(
dynamicBinding,
stringSegments,
values,
evaluationSubstitutionType,
);
} catch (e) {
if (fullPropertyPath) {
addErrorToEntityProperty(
[
{
raw: dynamicBinding,
errorType: PropertyEvaluationErrorType.PARSE,
errorMessage: e.message,
severity: Severity.ERROR,
},
],
data,
fullPropertyPath,
);
}
return undefined;
}
} }
return undefined; return undefined;
} }

View File

@ -334,5 +334,19 @@ describe("substituteDynamicBindingWithValues", () => {
expect(result).toBe(expected); expect(result).toBe(expected);
}); });
it("throws error when only binding is provided in parameter substitution", () => {
const binding = `{{ appsmith }}`;
const subBindings = ["{{appsmith}}"];
const subValues = [{ test: "object" }];
expect(() =>
substituteDynamicBindingWithValues(
binding,
subBindings,
subValues,
EvaluationSubstitutionType.PARAMETER,
),
).toThrowError();
});
}); });
}); });

View File

@ -80,6 +80,13 @@ export const parameterSubstituteDynamicValues = (
subSegments, subSegments,
subSegmentValues, subSegmentValues,
); );
// if only one binding is provided in the whole string, we need to throw an error
if (subSegments.length === 1 && subBindings.length === 1) {
throw Error(
"Dynamic bindings in prepared statements are only used to provide parameters inside SQL query. No SQL query found.",
);
}
let finalBinding = binding; let finalBinding = binding;
const parameters: Record<string, unknown> = {}; const parameters: Record<string, unknown> = {};
subBindings.forEach((b, i) => { subBindings.forEach((b, i) => {