Skip to content

Commit

Permalink
feat(QueryUtils): Turn on strictDateDetection by default
Browse files Browse the repository at this point in the history
The flag will remain at least until the next major version.

BREAKING CHANGE: strictDateDetection is now the default.
  • Loading branch information
elpete committed Feb 6, 2023
1 parent 54b65c0 commit 6150975
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 53 deletions.
2 changes: 1 addition & 1 deletion ModuleConfig.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ component {
"defaultGrammar": "AutoDiscover@qb",
"defaultReturnFormat": "array",
"preventDuplicateJoins": false,
"strictDateDetection": false,
"strictDateDetection": true,
"numericSQLType": "CF_SQL_NUMERIC",
"integerSQLType": "CF_SQL_INTEGER",
"decimalSQLType": "CF_SQL_DECIMAL",
Expand Down
4 changes: 2 additions & 2 deletions models/Query/QueryUtils.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ component singleton displayname="QueryUtils" accessors="true" {
/**
* qb strictDateDetection so we can do some conditional behaviour in data type detections
*/
property name="strictDateDetection" default="false";
property name="strictDateDetection" default="true";

/**
* allow overriding default numeric SQL type inferral
Expand Down Expand Up @@ -47,7 +47,7 @@ component singleton displayname="QueryUtils" accessors="true" {
* @return qb.models.Query.QueryUtils
*/
public QueryUtils function init(
boolean strictDateDetection = false,
boolean strictDateDetection = true,
string numericSQLType = "CF_SQL_NUMERIC",
boolean autoAddScale = true,
boolean autoDeriveNumericType = false,
Expand Down
1 change: 1 addition & 0 deletions tests/Application.cfc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
component {

this.enableNullSupport = shouldEnableFullNullSupport();
this.timezone = "UTC";

this.mappings[ "/tests" ] = getDirectoryFromPath( getCurrentTemplatePath() );
this.mappings[ "/qb" ] = expandPath( "/" );
Expand Down
16 changes: 6 additions & 10 deletions tests/resources/AbstractQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -2264,18 +2264,14 @@ component extends="testbox.system.BaseSpec" {

it( "turns a function into a subselect", function() {
testCase( function( builder ) {
var subselect = function( qb ) {
qb.from( "departments" )
.select( "name" )
.whereColumn( "employees.departmentId", "departments.id" );
};
return builder
.table( "employees" )
.update(
values = {
"departmentName": function( qb ) {
qb.from( "departments" )
.select( "name" )
.whereColumn( "employees.departmentId", "departments.id" );
}
},
toSql = true
);
.update( values = { "departmentName": subselect }, toSql = true );
}, updateWithSubselect() );
} );

Expand Down
19 changes: 15 additions & 4 deletions tests/specs/Query/Abstract/QueryDebuggingSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ component extends="testbox.system.BaseSpec" {
var query = getBuilder()
.from( "users" )
.join( "logins", function( j ) {
j.on( "users.id", "logins.user_id" ).where( "logins.created_date", ">", "01 Jun 2019" );
j.on( "users.id", "logins.user_id" )
.where( "logins.created_date", ">", parseDateTime( "01 Jun 2019" ) );
} )
.whereIn( "users.type", [ "admin", "manager" ] )
.whereNotNull( "active" )
.orderBy( "logins.created_date", "desc" );

expect( query.toSQL( showBindings = true ) ).toBe(
"SELECT * FROM ""users"" INNER JOIN ""logins"" ON ""users"".""id"" = ""logins"".""user_id"" AND ""logins"".""created_date"" > {""value"":""01 Jun 2019"",""cfsqltype"":""CF_SQL_TIMESTAMP"",""null"":false} WHERE ""users"".""type"" IN ({""value"":""admin"",""cfsqltype"":""CF_SQL_VARCHAR"",""null"":false}, {""value"":""manager"",""cfsqltype"":""CF_SQL_VARCHAR"",""null"":false}) AND ""active"" IS NOT NULL ORDER BY ""logins"".""created_date"" DESC"
);
if ( isLucee() ) {
expect( query.toSQL( showBindings = true ) ).toBe(
"SELECT * FROM ""users"" INNER JOIN ""logins"" ON ""users"".""id"" = ""logins"".""user_id"" AND ""logins"".""created_date"" > {""value"":""June, 01 2019 00:00:00 +0000"",""cfsqltype"":""CF_SQL_TIMESTAMP"",""null"":false} WHERE ""users"".""type"" IN ({""value"":""admin"",""cfsqltype"":""CF_SQL_VARCHAR"",""null"":false}, {""value"":""manager"",""cfsqltype"":""CF_SQL_VARCHAR"",""null"":false}) AND ""active"" IS NOT NULL ORDER BY ""logins"".""created_date"" DESC"
);
} else {
expect( query.toSQL( showBindings = true ) ).toBe(
"SELECT * FROM ""users"" INNER JOIN ""logins"" ON ""users"".""id"" = ""logins"".""user_id"" AND ""logins"".""created_date"" > {""value"":""June, 01 2019 00:00:00"",""cfsqltype"":""CF_SQL_TIMESTAMP"",""null"":false} WHERE ""users"".""type"" IN ({""value"":""admin"",""cfsqltype"":""CF_SQL_VARCHAR"",""null"":false}, {""value"":""manager"",""cfsqltype"":""CF_SQL_VARCHAR"",""null"":false}) AND ""active"" IS NOT NULL ORDER BY ""logins"".""created_date"" DESC"
);
}
} );

it( "provides a useful error message when calling `from` with a closure", function() {
Expand All @@ -48,4 +55,8 @@ component extends="testbox.system.BaseSpec" {
return builder;
}

private boolean function isLucee() {
return server.keyExists( "lucee" );
}

}
46 changes: 10 additions & 36 deletions tests/specs/Query/Abstract/QueryUtilsSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,22 @@ component displayname="QueryUtilsSpec" extends="testbox.system.BaseSpec" {

describe( "numbers", function() {
it( "integers", function() {
if ( isACF2016() ) {
expect( utils.inferSqlType( 100 ) ).toBe( "CF_SQL_VARCHAR" );
} else {
expect( utils.inferSqlType( 100 ) ).toBe( "CF_SQL_NUMERIC" );
variables.utils.setAutoDeriveNumericType( true );
expect( utils.inferSqlType( 100 ) ).toBe( "CF_SQL_INTEGER" );
variables.utils.setAutoDeriveNumericType( false );
}
expect( utils.inferSqlType( 100 ) ).toBe( "CF_SQL_NUMERIC" );
variables.utils.setAutoDeriveNumericType( true );
expect( utils.inferSqlType( 100 ) ).toBe( "CF_SQL_INTEGER" );
variables.utils.setAutoDeriveNumericType( false );
} );

it( "decimals", function() {
if ( isACF2016() ) {
variables.utils.setStrictDateDetection( true );
expect( utils.inferSqlType( 4.50 ) ).toBe( "CF_SQL_VARCHAR" );
variables.utils.setStrictDateDetection( false );
} else {
expect( utils.inferSqlType( 4.50 ) ).toBe( "CF_SQL_NUMERIC" );
variables.utils.setAutoDeriveNumericType( true );
expect( utils.inferSqlType( 4.50 ) ).toBe( "CF_SQL_DECIMAL" );
variables.utils.setAutoDeriveNumericType( false );
}
expect( utils.inferSqlType( 4.50 ) ).toBe( "CF_SQL_NUMERIC" );
variables.utils.setAutoDeriveNumericType( true );
expect( utils.inferSqlType( 4.50 ) ).toBe( "CF_SQL_DECIMAL" );
variables.utils.setAutoDeriveNumericType( false );
} );
} );

it( "dates", function() {
expect( utils.inferSqlType( now() ) ).toBe( "CF_SQL_TIMESTAMP" );
if ( isLucee() ) {
expect( utils.inferSqlType( "06 12345" ) ).toBe( "CF_SQL_TIMESTAMP" );
}
variables.utils.setStrictDateDetection( true );
expect( utils.inferSqlType( now() ) ).toBe( "CF_SQL_TIMESTAMP" );
expect( utils.inferSqlType( "06 12345" ) ).toBe( "CF_SQL_VARCHAR" );
Expand All @@ -71,11 +58,7 @@ component displayname="QueryUtilsSpec" extends="testbox.system.BaseSpec" {

describe( "it infers the sql type from the members of an array", function() {
it( "if all the members of the array are the same", function() {
if ( isACF2016() ) {
expect( utils.inferSqlType( [ 1, 2 ] ) ).toBe( "CF_SQL_VARCHAR" );
} else {
expect( utils.inferSqlType( [ 1, 2 ] ) ).toBe( "CF_SQL_NUMERIC" );
}
expect( utils.inferSqlType( [ 1, 2 ] ) ).toBe( "CF_SQL_NUMERIC" );
} );

it( "but defaults to CF_SQL_VARCHAR if they are different", function() {
Expand All @@ -93,7 +76,7 @@ component displayname="QueryUtilsSpec" extends="testbox.system.BaseSpec" {

describe( "extractBinding()", function() {
it( "includes sensible defaults", function() {
var binding = utils.extractBinding( "05/10/2016" );
var binding = utils.extractBinding( parseDateTime( "05/10/2016" ) );

expect( binding ).toBeStruct();
expect( binding.value ).toBe( "05/10/2016" );
Expand Down Expand Up @@ -232,13 +215,4 @@ component displayname="QueryUtilsSpec" extends="testbox.system.BaseSpec" {
} );
}

private boolean function isACF2016() {
return server.keyExists( "coldfusion" ) &&
!server.keyExists( "lucee" ) &&
left( server.coldfusion.productversion, 4 ) == "2016";
}
private boolean function isLucee() {
return server.keyExists( "lucee" );
}

}

0 comments on commit 6150975

Please sign in to comment.