Last active
November 10, 2015 22:19
-
-
Save elpete/7e6328bac43450681421 to your computer and use it in GitHub Desktop.
AOP approach to converting queries to an array of structs
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 extends='coldbox.system.EventHandler' { | |
property name='dao' inject='models.TestDAO'; | |
function index(event, rc, prc) { | |
var data = dao.getData(); | |
writeDump(data); | |
abort; | |
} | |
} |
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 { | |
this.title = 'QueryToArrayOfStructsConverter'; | |
this.author = 'Eric Peterson'; | |
this.webURL = 'https://github.com/elpete/QueryToArrayOfStructsConverter'; | |
this.description = 'ColdBox/WireBox Aspect to convert queries to arrays of structs'; | |
this.version = '1.0.0'; | |
this.autoMapModels = false; | |
function configure() { | |
addAOPListenerIfNeeded(); | |
binder.mapAspect('QueryToArrayOfStructsConverter') | |
.to('#moduleMapping#.aspects.QueryToArrayOfStructsConverter'); | |
// // Bind by component annotation | |
binder.bindAspect( | |
classes = binder.match().annotatedWith('convertToArrayOfStructs'), | |
methods = binder.match().any(), | |
aspects = 'QueryToArrayOfStructsConverter' | |
); | |
// // Bind by method annotation | |
binder.bindAspect( | |
classes = binder.match().any(), | |
methods = binder.match().annotatedWith('convertToArrayOfStructs'), | |
aspects = 'QueryToArrayOfStructsConverter' | |
); | |
} | |
private function addAOPListenerIfNeeded() { | |
if (AOPListenerIsRegistered()) { | |
return; | |
} | |
var mixer = new coldbox.system.aop.Mixer(); | |
mixer.configure( controller.getWireBox(), {} ); | |
controller.getInterceptorService().registerInterceptor( | |
interceptorObject = mixer, | |
interceptorName = "AOPMixer" | |
); | |
} | |
private function AOPListenerIsRegistered() { | |
var listeners = binder.getListeners(); | |
// I'm using a for loop for cross-platform compatibility | |
var registered = false; | |
for (var listener in listeners) { | |
if (listener.class == 'coldbox.system.aop.Mixer') { | |
registered = true; | |
} | |
} | |
return registered; | |
} | |
} |
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 implements='coldbox.system.aop.MethodInterceptor' { | |
/** | |
* @hint Invoke an AOP method invocation | |
* @invocation.hint The method invocation object: coldbox.system.aop.MethodInvocation | |
*/ | |
public any function invokeMethod(required any invocation) output=false { // The output=false is important for the interface | |
var q = arguments.invocation.proceed(); | |
return queryToArrayOfStructs(q); | |
} | |
private array function queryToArrayOfStructs(required query q) { | |
var results = []; | |
for (var row in arguments.q) { | |
results.append(row); | |
} | |
return results; | |
} | |
} |
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 function getData() { | |
return queryExecute("SELECT * FROM users;", {}, { datasource = 'test' }); | |
} | |
} |
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
/** | |
* @convertToArrayOfStructs | |
*/ | |
component { | |
public function getData() { | |
return queryExecute("SELECT * FROM users;", {}, { datasource = 'test' }); | |
} | |
} |
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 convertToArrayOfStructs=true { | |
public function getData() { | |
return queryExecute("SELECT * FROM users;", {}, { datasource = 'test' }); | |
} | |
} |
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 { | |
/** | |
* @convertToArrayOfStructs | |
*/ | |
public function getData() { | |
return queryExecute("SELECT * FROM users;", {}, { datasource = 'test' }); | |
} | |
} |
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 function getData() convertToArrayOfStructs=true { | |
return queryExecute("SELECT * FROM users;", {}, { datasource = 'test' }); | |
} | |
} |
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 extends='coldbox.system.ioc.config.Binder' { | |
function configure() { | |
wireBox = { | |
scopeRegistration = { | |
enabled = true, | |
scope = 'application', | |
key = 'wireBox' | |
}, | |
listeners = [ | |
// use wirebox.system.aop.Mixer in standalone installations | |
{ class='coldbox.system.aop.Mixer', properties={} } | |
] | |
}; | |
mapAspect('QueryToArrayOfStructsConverter') | |
.to('models.aspects.QueryToArrayOfStructsConverter'); | |
// Bind by *DAO filename | |
bindAspect( | |
classes = match().regex('.*[Dd][Aa][Oo]$'), | |
methods = match().any(), | |
aspects = 'QueryToArrayOfStructsConverter' | |
); | |
// Bind by method annotation | |
bindAspect( | |
classes = match().any(), | |
methods = match().annotatedWith('convertToArrayOfStructs'), | |
aspects = 'QueryToArrayOfStructsConverter' | |
); | |
// Bind by component annotation | |
bindAspect( | |
classes = match().annotatedWith('convertToArrayOfStructs'), | |
methods = match().any(), | |
aspects = 'QueryToArrayOfStructsConverter' | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment