Skip to content

Commit eaa2247

Browse files
authored
Merge pull request #182 from mk3008:feat_dynamic_condition
fix: correct column validation behavior to ignore non-existent columns
2 parents d8f2f3f + 8c3c28a commit eaa2247

File tree

3 files changed

+15
-30
lines changed

3 files changed

+15
-30
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rawsql-ts",
3-
"version": "0.11.24-beta",
3+
"version": "0.11.25-beta",
44
"description": "[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.",
55
"main": "dist/src/index.js",
66
"module": "dist/esm/index.js",

packages/core/src/transformers/SqlParamInjector.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export interface SqlParamInjectorOptions {
1313
ignoreCaseAndUnderscore?: boolean;
1414
/** Whether to allow injection when all parameters are undefined (defaults to false for safety) */
1515
allowAllUndefined?: boolean;
16-
/** Whether to skip column validation entirely (defaults to false for safety) */
17-
skipColumnValidation?: boolean;
16+
/** Whether to ignore non-existent columns instead of throwing errors (defaults to false for safety) */
17+
ignoreNonExistentColumns?: boolean;
1818
}
1919

2020
// Type for state parameter values - can be simple values, conditions, or complex objects
@@ -512,27 +512,12 @@ export class SqlParamInjector {
512512
injectComplexConditions: Function,
513513
validateOperators: Function
514514
): void {
515-
// Skip column validation if option is enabled
516-
if (this.options.skipColumnValidation) {
517-
// if object, validate its keys
518-
if (this.isValidatableObject(stateValue)) {
519-
validateOperators(stateValue, allowedOps, name);
520-
}
521-
522-
// Create a column reference using the name directly
523-
const columnRef = new ColumnReference(null, name);
524-
525-
// Handle complex conditions if needed
526-
if (this.isValidatableObject(stateValue)) {
527-
injectComplexConditions(query, columnRef, name, stateValue);
528-
} else {
529-
injectSimpleCondition(query, columnRef, name, stateValue);
530-
}
531-
return;
532-
}
533-
534515
const queries = finder.find(query, name);
535516
if (queries.length === 0) {
517+
// Ignore non-existent columns if option is enabled
518+
if (this.options.ignoreNonExistentColumns) {
519+
return;
520+
}
536521
throw new Error(`Column '${name}' not found in query`);
537522
}
538523

packages/core/tests/transformers/SqlParamInjector.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,23 @@ describe('SqlParamInjector', () => {
104104
expect(params).toEqual({ article_id: 100 });
105105
});
106106

107-
test('should skip column validation when skipColumnValidation option is enabled', () => {
107+
test('should ignore non-existent columns when ignoreNonExistentColumns option is enabled', () => {
108108
// Arrange: parse base query with limited columns
109109
const baseQuery = SelectQueryParser.parse('select a.article_id from article as a') as SimpleSelectQuery;
110-
// Arrange: state with non-existent column - should not throw error with skipColumnValidation
111-
const state = { nonExistentColumn: 'value' };
110+
// Arrange: state with both existing and non-existent columns
111+
const state = { nonExistentColumn: 'value', article_id: 100 };
112112

113-
// Act: inject parameters with skipColumnValidation option
114-
const injector = new SqlParamInjector({ skipColumnValidation: true });
113+
// Act: inject parameters with ignoreNonExistentColumns option
114+
const injector = new SqlParamInjector({ ignoreNonExistentColumns: true });
115115
const injectedQuery = injector.inject(baseQuery, state);
116116

117117
// Act: format SQL and extract parameters
118118
const formatter = new SqlFormatter();
119119
const { formattedSql, params } = formatter.format(injectedQuery);
120120

121-
// Assert: WHERE clause should be added even for non-existent column
122-
expect(formattedSql).toBe('select "a"."article_id" from "article" as "a" where "nonExistentColumn" = :nonExistentColumn');
123-
expect(params).toEqual({ nonExistentColumn: 'value' });
121+
// Assert: only existing columns should be in WHERE clause, non-existent columns ignored
122+
expect(formattedSql).toBe('select "a"."article_id" from "article" as "a" where "a"."article_id" = :article_id');
123+
expect(params).toEqual({ article_id: 100 });
124124
});
125125

126126
test('injects state using custom tableColumnResolver', () => {

0 commit comments

Comments
 (0)