Last active
June 14, 2022 11:15
-
-
Save bennadel/18c0050d3ed996441a359bb626bc7ad5 to your computer and use it in GitHub Desktop.
It's OK To Be Super Explicit In Your Data Access Method Names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
public void function updateThing( | |
required numeric id, | |
string valueOne, | |
string valueTwo, | |
string valueThree | |
) { | |
``` | |
<cfquery name="local.results" result="local.metaResults"> | |
UPDATE | |
thing | |
SET | |
<cfif arguments.keyExists( "valueOne" )> | |
valueOne = <cfqueryparam value="#valueOne#" sqltype="varchar" />, | |
</cfif> | |
<cfif arguments.keyExists( "valueTwo" )> | |
valueTwo = <cfqueryparam value="#valueTwo#" sqltype="varchar" />, | |
</cfif> | |
<cfif arguments.keyExists( "valueThree" )> | |
valueThree = <cfqueryparam value="#valueThree#" sqltype="varchar" />, | |
</cfif> | |
/* No-op assignment here to make the trailing commas consistent. */ | |
id = <cfqueryparam value="#id#" sqltype="bigint" /> | |
WHERE | |
id = <cfqueryparam value="#id#" sqltype="bigint" /> | |
</cfquery> | |
``` | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
public void function upsertThing( | |
required numeric ownerID, | |
required string valueOne, | |
required string valueTwo, | |
required string valueThree | |
) { | |
``` | |
<cfquery name="local.results" result="local.metaResults"> | |
INSERT INTO | |
thing | |
SET | |
/* !!! Owner ID column might cause a key-conflict. !!! */ | |
ownerID = <cfqueryparam value="#ownerID#" sqltype="bigint" />, | |
valueOne = <cfqueryparam value="#valueOne#" sqltype="varchar" />, | |
valueTwo = <cfqueryparam value="#valueTwo#" sqltype="varchar" />, | |
valueThree = <cfqueryparam value="#valueThree#" sqltype="varchar" /> | |
ON DUPLICATE KEY UPDATE | |
valueOne = <cfqueryparam value="#valueOne#" sqltype="varchar" />, | |
valueTwo = <cfqueryparam value="#valueTwo#" sqltype="varchar" />, | |
valueThree = <cfqueryparam value="#valueThree#" sqltype="varchar" /> | |
</cfquery> | |
``` | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
public numeric function createThing( required string value ) { | |
``` | |
<cfquery name="local.results" result="local.metaResults"> | |
INSERT INTO | |
thing | |
SET | |
value = <cfqueryparam value="#value#" sqltype="varchar" /> | |
ON DUPLICATE KEY UPDATE | |
id = LAST_INSERT_ID( id ) -- Store existing ID into "generatedKey". | |
; | |
</cfquery> | |
``` | |
return( metaResults.generatedKey ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment