Skip to content

Instantly share code, notes, and snippets.

@robjshaw
Created June 19, 2013 02:42
Show Gist options
  • Select an option

  • Save robjshaw/5811317 to your computer and use it in GitHub Desktop.

Select an option

Save robjshaw/5811317 to your computer and use it in GitHub Desktop.
<cfcomponent output="false">
<cffunction name="get" access="remote">
<cfargument name="type" required="true" />
<cfargument name="stArguments" required="true" />
<cfscript>
//var listToArray(arguments.type, '_');
var qGet = "";
var aColumns = arrayNew(1);
var lWant = '';
var result = '';
if (structKeyExists(stArguments, 'want')){
lWant = stArguments.want;
}
if (not structKeyExists(stArguments, 'limitBy')){
stArguments.limitBy = 10;
}
if (not structKeyExists(stArguments, 'orderBy')){
stArguments.orderBy = 'ASC';
}
</cfscript>
<cfloop collection="#arguments.stArguments#" item="i">
<cfscript>
if ((i neq 'want') and (i neq 'limitBy') and (i neq 'orderBy')){
arrayAppend(aColumns, i);
}
</cfscript>
</cfloop>
<cfscript>
if (listLen(lWant) eq 0){
lWant = '*';
}
</cfscript>
<cfquery datasource="skylevel_2" name="qGet">
SELECT #lWant#
FROM #arguments.type#
WHERE 1 = 1
<cfloop from="1" to="#arrayLen(aColumns)#" index="k">
<cfif isNumeric(stArguments[aColumns[k]])>
AND #aColumns[k]# = <cfqueryparam value="#stArguments[aColumns[k]]#" cfsqltype="cf_sql_integer" />
<cfelse>
AND #aColumns[k]# = <cfqueryparam value="#stArguments[aColumns[k]]#" cfsqltype="cf_sql_varchar" />
</cfif>
</cfloop>
<!--- ORDER BY #stArguments.orderBy# --->
LIMIT #stArguments.limitBy#
</cfquery>
<cfscript>
result = qGet;
return result;
</cfscript>
</cffunction>
<cffunction name="create" access="remote">
<cfargument name="type" required="true" />
<cfargument name="stArguments" required="true" />
<cfscript>
var aColumns = arrayNew(1);
//var lWant = stArguments.want;
var rInserted = '';
var stGetArguments = structNew();
</cfscript>
<cfloop collection="#arguments.stArguments#" item="i">
<cfscript>
if (i neq 'want'){
arrayAppend(aColumns, i);
}
</cfscript>
</cfloop>
<cfquery datasource="skylevel_2" result="rInserted">
INSERT INTO #arguments.type# (#arrayToList(aColumns)#, dtInserted)
VALUES ( <cfloop from="1" to="#arrayLen(aColumns)#" index="k">
<cfif isNumeric(stArguments[aColumns[k]])>
<cfqueryparam value="#stArguments[aColumns[k]]#" cfsqltype="cf_sql_integer" />
<cfelse>
<cfqueryparam value="#stArguments[aColumns[k]]#" cfsqltype="cf_sql_varchar" />
</cfif>
,
</cfloop>
now()
)
</cfquery>
<cfscript>
stGetArguments['#type#id'] = rInserted.generatedKey;
result = get(type=arguments.type,stArguments=stGetArguments);
return result;
</cfscript>
</cffunction>
<cffunction name="update" access="remote">
<cfargument name="type" required="true" />
<cfargument name="stArguments" required="true" />
</cffunction>
<cffunction name="delete" access="remote">
<cfargument name="type" required="true" />
<cfargument name="stArguments" required="true" />
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment