Skip to content

Commit c605721

Browse files
committed
fix(QuickQB): Fix for casts in where statements
1 parent 51545f2 commit c605721

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

models/QuickQB.cfc

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ component
3535
) {
3636
if ( isSimpleValue( arguments.column ) && getEntity().hasAttribute( arguments.column ) ) {
3737
arguments.value = generateQueryParamStruct(
38-
column = arguments.column,
39-
value = isNull( arguments.value ) ? javacast( "null", "" ) : arguments.value,
40-
checkNullValues = false // where's should not be null. `WHERE foo = NULL` will return nothing.
38+
column = arguments.column,
39+
value = isNull( arguments.value ) ? javacast( "null", "" ) : arguments.value,
40+
checkNullValues = false, // where's should not be null. `WHERE foo = NULL` will return nothing.
41+
shouldCastValues = true // we are in a where clause, so the casted values will not be persisted to the entity at this time.
4142
);
4243
}
4344
super.whereBasic( argumentCollection = arguments );
@@ -850,7 +851,8 @@ component
850851
public struct function generateQueryParamStruct(
851852
required string column,
852853
any value,
853-
boolean checkNullValues = true
854+
boolean checkNullValues = true,
855+
boolean shouldCastValues = false
854856
) {
855857
// If that value is already a struct, pass it back unchanged.
856858
if ( !isNull( arguments.value ) && getUtils().isValidQueryParamStruct( arguments.value ) ) {
@@ -859,15 +861,17 @@ component
859861

860862
if ( arguments.checkNullValues ) {
861863
return {
862-
"value" : ( isNull( arguments.value ) || getEntity().isNullValue( arguments.column, arguments.value ) ) ? "" : getEntity().convertToCastedValue(
863-
arguments.column,
864-
arguments.value
864+
"value" : ( isNull( arguments.value ) || getEntity().isNullValue( arguments.column, arguments.value ) ) ? "" : (
865+
arguments.shouldCastValues ? getEntity().convertToCastedValue( arguments.column, arguments.value ) : arguments.value
865866
),
866867
"cfsqltype" : getEntity().attributeHasSqlType( arguments.column ) ? getEntity().retrieveSqlTypeForAttribute(
867868
arguments.column
868869
) : (
869870
isNull( arguments.value ) ? "CF_SQL_VARCHAR" : getUtils().inferSqlType(
870-
getEntity().convertToCastedValue( arguments.column, arguments.value ),
871+
arguments.shouldCastValues ? getEntity().convertToCastedValue(
872+
arguments.column,
873+
arguments.value
874+
) : arguments.value,
871875
variables.grammar
872876
)
873877
),
@@ -886,15 +890,17 @@ component
886890
};
887891
} else {
888892
return {
889-
"value" : isNull( arguments.value ) ? "" : getEntity().convertToCastedValue(
890-
arguments.column,
891-
arguments.value
893+
"value" : isNull( arguments.value ) ? "" : (
894+
arguments.shouldCastValues ? getEntity().convertToCastedValue( arguments.column, arguments.value ) : arguments.value
892895
),
893896
"cfsqltype" : getEntity().attributeHasSqlType( arguments.column ) ? getEntity().retrieveSqlTypeForAttribute(
894897
arguments.column
895898
) : (
896899
isNull( arguments.value ) ? "CF_SQL_VARCHAR" : getUtils().inferSqlType(
897-
getEntity().convertToCastedValue( arguments.column, arguments.value ),
900+
arguments.shouldCastValues ? getEntity().convertToCastedValue(
901+
arguments.column,
902+
arguments.value
903+
) : arguments.value,
898904
variables.grammar
899905
)
900906
),

tests/specs/integration/BaseEntity/AttributeCastsSpec.cfc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,21 @@ component extends="tests.resources.ModuleIntegrationSpec" {
166166
} );
167167

168168
it( "casts values when setting them as a where clause", () => {
169-
// expect( () => {
170-
getInstance( "Theme" ).where( "config", { "message" : "I should be cast to JSON" } ).first()
171-
// } ).notToThrow();
169+
expect( () => {
170+
getInstance( "Theme" ).where( "config", { "message" : "I should be cast to JSON" } ).first()
171+
} ).notToThrow();
172+
} );
173+
174+
it( "does not double cast json strings", () => {
175+
var newTheme = getInstance( "Theme" );
176+
newTheme.setSlug( "theme-new" );
177+
newTheme.setVersion( "0.0.1" );
178+
newTheme.setConfig( {} );
179+
newTheme.save();
180+
181+
var fetchedTheme = getInstance( "Theme" ).where( "slug", "theme-new" ).firstOrFail();
182+
expect( fetchedTheme.getConfig() ).toBe( {} );
183+
expect( fetchedTheme.getConfig() ).toBeStruct();
172184
} );
173185
} );
174186
}

0 commit comments

Comments
 (0)