danmurphy
04/27/2024, 12:12 AMvariables.params = [
{
'name' : 'team',
'value' : team,
'cfsqltype' : 'cf_sql_varchar'
}
];
I could then just use variables.params in the queryExecute in each method. But at that point in the component above, the team variable doesn’t exist yet. It only exists as it gets passed into each method. Any idea how I would do this?websolete
04/27/2024, 12:59 AMdanmurphy
04/27/2024, 1:00 AMwebsolete
04/27/2024, 1:01 AMdanmurphy
04/27/2024, 1:04 AMwebsolete
04/27/2024, 1:05 AMwebsolete
04/27/2024, 1:06 AMwebsolete
04/27/2024, 1:06 AMwebsolete
04/27/2024, 1:10 AMdanmurphy
04/27/2024, 1:19 AMwebsolete
04/27/2024, 1:23 AMwebsolete
04/27/2024, 1:25 AMdanmurphy
04/27/2024, 1:26 AMwebsolete
04/27/2024, 1:34 AM/**
* Creates a new resource from a resource bean
*
* @bean any a resource bean
*
* @return struct { "id" : newid }
*
*/
public struct function create(
required any bean
) {
var q = "";
var qr = "";
var sql = bean.buildSql("insert","schools");
q = queryExecute(
sql.statement,
sql.parameters,
{ datasource : getDsn(), result : "qr" }
);
doDebug( qr, "create", getDebugFilePath() );
if( !qr.recordcount ) {
throw( type="ResourceCreationFailed", message="The resource could not be created", errorcode="500" );
}
return { "id" : qr.GENERATEDKEY };
}websolete
04/27/2024, 1:34 AMwebsolete
04/27/2024, 1:35 AMwebsolete
04/27/2024, 1:36 AMdanmurphy
04/27/2024, 1:49 AMwebsolete
04/27/2024, 1:58 AMproperty name="sid" type="numeric" sqltype="cf_sql_integer" pk="true";
property name="schoolType" type="string" sqltype="cf_sql_varchar" maxlength="40";
property name="schoolSector" type="string" sqltype="cf_sql_varchar" maxlength="20";
property name="schoolName" type="string" sqltype="cf_sql_varchar" maxlength="255" required="true";websolete
04/27/2024, 1:59 AM/**
* Returns the various parts of sql queries so a DAO can execute them
*
* @operation string insert | update |d delete
* @tablename string the tablename the query targets
*
* @retrun struct
*/
public struct function buildSql( required string operation, required string tablename ) {
// validate requested sql operation
if( !listfindnocase("insert,update,delete", arguments.operation) ) {
throw( type="InvalidOperation", message="The requested operation is not supported" );
}
var sql = {
"statement" : "",
"parameters" : {}
};
var properties = getMetadata(this).properties;
// set any getdate()-like timestamp values we may have
setTimestamps( properties, arguments.operation );
var populated = getProperties(false); // returns only those properties whose values have been set
// filter properties down to only those that are populated
var workingProps = arrayfilter( properties, function( prop ) {
return structkeyexists(populated,prop.name);
});
switch ( arguments.operation ) {
case "insert":
var sqlColumns = [];
var sqlValues = [];
var params = {};
for( var prop in workingProps ) {
arrayappend( sqlColumns, prop.name );
arrayappend( sqlValues, ":" & prop.name );
params[prop.name] = { cfsqltype : prop.sqltype, value : getProperty(prop.name), null : sqlNullifier( prop.type, getProperty(prop.name) ) };
}
sql.statement = "INSERT INTO " & arguments.tablename & "(" & arraytolist(sqlColumns) & ") VALUES (" & arraytolist(sqlValues) & ")";
sql.parameters = params;
break;
case "update":
var sqlColumns = [];
var params = {};
var identifier = getIdentifier( properties );
for( var prop in workingProps ) {
if( prop.name != identifier.column ) {
arrayappend( sqlColumns, prop.name & " = :" & prop.name );
}
params[prop.name] = { cfsqltype : prop.sqltype, value : getProperty(prop.name), null : sqlNullifier( prop.type, getProperty(prop.name) ) };
}
sql.statement = "UPDATE " & arguments.tablename & " SET " & arraytolist(sqlColumns) & " WHERE " & identifier.column & " = :" & identifier.column;
sql.parameters = params;
break;
case "delete":
var params = {};
var identifier = getIdentifier( properties );
for( var prop in workingProps ) {
if( prop.name == identifier.column ) { // we only need the PK column
params[prop.name] = { cfsqltype : prop.sqltype, value : getProperty(prop.name) };
break;
}
}
sql.statement = "DELETE FROM " & arguments.tablename & " WHERE " & identifier.column & " = :" & identifier.column;
sql.parameters = params;
break;
default:
throw( type="InvalidOperation", message="The requested operation is not supported" );
}
return sql;
}websolete
04/27/2024, 2:00 AMwebsolete
04/27/2024, 2:03 AM