Quantcast
Channel: ColdFusion
Viewing all articles
Browse latest Browse all 1091

How to get serialized JSON results from a CFC, AJAX and a SQL Stored Procedure

$
0
0

Using a CFC in AJAX is easy. Just make sure to pass your arguments in and use  the arguments scope within the cfc and then use them in a cfproc param within your code. This way you can pass multiple parameters into your sql stored procedure.

The benefit of this I found is that I have greater security on the sql query and can validate the parameters being sent to SQL. I can created indexes and update the stats on the SQL Stored Proc etc which I have not found a way to do in CF yet. My code is compiled per se, and my data is returned much faster. Within my SQL Stored Proc I use SQL CTEs as much as possible so that I work on sets of data and not rows of data. For example here is a sql select that concatenates a full composite address (street number to zip code including apartments), this returns over 12000 rows into a common table expression that is returned. You can pass your parameter to the where clause of the select from CTE statement.

CTE EXAMPLE:

;with cteData as (
  select id as value,
    REPLACE(RTRIM(LTRIM(RTRIM(streetnumber))
    +’ ‘+ LTRIM(RTRIM(REPLACE(RTRIM(ISNULL(streetprefix,”))
    +’ ‘+ RTRIM(ISNULL(street,”))
    +’ ‘+ RTRIM(ISNULL(streettype,”))
    +’ ‘+ RTRIM(ISNULL(streetsuffix,”)),’ ‘,’ ‘)))
    +’ ‘+ LTRIM(RTRIM(ISNULL(apartment,”))))+’,’+
    +’ ‘+ LTRIM(RTRIM(ISNULL(city,”)))+’,’+
    +’ ‘+ LTRIM(RTRIM(ISNULL(state,”)))
    +’ ‘+ LTRIM(RTRIM(ISNULL(postalcode,”))),’ ‘,’ ‘)
    as label from tableName
)
select value, label from cteData

AJAX EXAMPLE:

$.ajax{
            type: ‘post’,
           url: ‘../assets/cfc/getData.cfc’,
           data: {
                      method: “getData”,
                      testing: ‘1’,
                      qry: ‘0’
          },
          dataType: ‘json’,
          success: function(data){
                          console.log(data);
          },
          fail: function(data){
                  console.log(data);
          }
});

The AJAX calls passes two parameters to the cfc (testing & qry).

CFC cffunction:

<cffunction name=”getData” displayname=”Get Data” access=”remote” returntype=”Any” returnformat=”JSON” >
<cfargument name=”testing” required=”true” type=”numeric” default=”1″ />
<cfargument name=”qry” required=”true” type=”numeric” />

<!— Define variables —>
<cfset var q=””>

<!— Get data —>
<cfstoredproc procedure=”usp_SQL_STORED_PROC_NAME” datasource=”DATASOURCE”>
      <cfprocresult name=”q” />
      <cfprocparam type=”in” cfsqltype=”CF_SQL_INTEGER” value=”#arguments.testing#” maxlength=”1″/>
      <cfprocparam type=”in” cfsqltype=”CF_SQL_INTEGER” value=”#arguments.qry#” maxlength=”1″/>
</cfstoredproc>

<!— And return it —>
<cfset q = serializeJSON(q,”struct”)>
<cfreturn q>
</cffunction>

 

I serialize the the JSON results into a struct by adding a <cfset q = serializeJSON(q,”struct”)> and find that my JS / CF front end consumes my data much faster.

Running this with with testing parameter = 0 returns to whole data set whereas testing = 1 returns the top 10.

Here is a sample of the data returned:

  1. [{VALUE: “_0002BB81B2MFR”, LABEL: “3434 S 144 ST C226, TUKWILA, WA 98168”},…]
    1. 0:{VALUE: “_0002BB81B2MFR”, LABEL: “3434 S 144 ST C226, TUKWILA, WA 98168”}
    2. 1:{VALUE: “_0011A4CA8FMFR”, LABEL: “17585 SOUTHCENTER PKY, TUKWILA, WA 98188”}
    3. 2:{VALUE: “_0011D183FFMFR”, LABEL: “4044 S 128 ST, TUKWILA, WA 98168”}
    4. 3:{VALUE: “_0012730599MFR”, LABEL: “3440 S 146 ST B02, TUKWILA, WA 98168”}
    5. 4:{VALUE: “_001296A98FMFR”, LABEL: “4708 S 122 ST, TUKWILA, WA 98178”}
    6. 5:{VALUE: “_00167C7899MFR”, LABEL: “14828 MILITARY RD S 103, TUKWILA, WA 98168”}
    7. 6:{VALUE: “_001A36DE86MFR”, LABEL: “14224 37 AVE S 110, TUKWILA, WA 98168”}
    8. 7:{VALUE: “_001C57BCF8MFR”, LABEL: “14893 INTERURBAN AVE S 07, TUKWILA, WA 98188”}
    9. 8:{VALUE: “_0025B1E52DMFR”, LABEL: “13517 34 AVE S, TUKWILA, WA 98168”}
    10. 9:{VALUE: “_0034175BEEMFR”, LABEL: “14250 53 AVE S, TUKWILA, WA 98168”}

Here are the stats for testing = 0 (all rows)fullDataSet

The result is 12,533 rows of serialized JSON.

Note this in on local laptop so yes it is fast but a laptop is not a server.

Here are the stats for testing = 1 (top 10 rows):

partialDataSet

As you can see using stored procedures in CFCs called from AJAX is really simple and really fast thanks to CF.

 

Jim

 


Viewing all articles
Browse latest Browse all 1091

Latest Images

Trending Articles



Latest Images