Skip to content

Commit

Permalink
Merge branch '48-improve-soft-delete' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dhumphreys committed May 25, 2012
2 parents 14af423 + b5b89fd commit 6dcbbbe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/mappers/CFWheels.cfc
Expand Up @@ -3,6 +3,7 @@
<cffunction name="buildMapping" returntype="void" access="public">
<cfargument name="table" type="any" required="true" />
<cfargument name="relation" type="any" required="true" />
<cfargument name="includeSoftDeletes" type="boolean" default="#variables.includeSoftDeletes#" />
<cfscript>
var loc = {};

Expand Down Expand Up @@ -57,7 +58,7 @@
}

// if the option is set, and the model has soft delete, consider it in the WHERE clause
if (NOT variables.includeSoftDeletes AND loc.model.$softDeletion())
if (NOT arguments.includeSoftDeletes AND loc.model.$softDeletion())
arguments.relation.where(loc.tableAlias & "." & loc.model.$softDeleteColumn() & " IS NULL");
</cfscript>
</cffunction>
Expand Down Expand Up @@ -148,9 +149,9 @@
if (NOT StructKeyExists(loc.includeStack[1], loc.key)) {
loc.includeStack[1][loc.key] = javaHash();

// build mapping for current model
// build mapping for current model, but do not map soft deletes
loc.associationTable = sqlTable(model=loc.associationModel);
buildMapping(loc.associationTable, arguments.relation);
buildMapping(loc.associationTable, arguments.relation, true);

// determine table aliases to use
loc.modelAlias = loc.aliasStack[1];
Expand Down Expand Up @@ -211,6 +212,10 @@

// use the passed in join type, or the default for this association
loc.joinType = (arguments.joinType EQ "") ? loc.assoc.joinType : arguments.joinType;

// if the option is set, and the model has soft delete, append it to the ON clause
if (NOT variables.includeSoftDeletes AND loc.associationModel.$softDeletion())
loc.condition &= " AND " & loc.includeStack[1][loc.key]['_alias'] & "." & loc.associationModel.$softDeleteColumn() & " IS NULL";

// call join on relation
relation.join(loc.associationTable, loc.condition, [], loc.joinType, true);
Expand Down
1 change: 1 addition & 0 deletions src/relation/initialization.cfm
Expand Up @@ -53,6 +53,7 @@
variables.qoq = arguments.qoq;
variables.paged = false;
variables.paginationData = false;
variables.includeSoftDeletes = arguments.includeSoftDeletes;
return this;
</cfscript>
Expand Down
14 changes: 11 additions & 3 deletions src/relation/query.cfm
Expand Up @@ -137,9 +137,17 @@
case "model":
loc.table = sqlTable(model=arguments.target);
// map the models using the mapper
if (NOT arguments.$skipMapping)
this.mapper.buildMapping(loc.table, this);
// map the models using the mapper, but place soft delete condition on the join
if (NOT arguments.$skipMapping) {
this.mapper.buildMapping(loc.table, this, true);
// if we have an ON clause and soft deletions are available, put them in the ON clause
// TODO: remove this code from join()! it belongs in a mapper!
if (NOT variables.includeSoftDeletes AND loc.table.model.$softDeletion() AND typeOf(loc.condition) NEQ "simple") {
loc.softDeleteCondition = loc.table.alias & "." & loc.table.model.$softDeleteColumn() & " IS NULL";
loc.condition = sqlBinaryOp(left=loc.condition, op="AND", right=loc.softDeleteCondition);
}
}
break;
// use another relation as a subquery
Expand Down

0 comments on commit 6dcbbbe

Please sign in to comment.