Calendar

««Feb 2010»»
SMTWTFS
  123456
78910111213
14151617181920
21222324252627
28

Alert Email

Get a short email alert whenever a new entry is published.

Confidential, secure it's piece of cake to keep uptodate.

Implicit Array/Struct Creation and new Operators

Today sees the release of some much awaited support for both implicit array/struct creation and a whole host of operators. The cfscript and expression parsing engines have been rewritten, switching from the javacc based parsers to a cleaner and leaner ANTLR based one.

Implicit Array/Struct Creation

For those unfamiliar with the concept of implicit array/struct creation, here's some examples to whet your appetite as you wait for the latest OpenBD nightly build to download.

<!--- create an empty array --->
<cfset myarray = []>

<!--- create an array with 3 numeric elements --->
<cfset myarray = [1,2,3]>

<!--- create an array with mixed element types --->
<cfset myarray = [true,'andy',3]>

<!--- create an array of arrays --->
<cfset myArray = [[1,2],[3]]> 

<!--- create an empty struct --->
<cfset myStruct = {}>

<!--- create a struct with fields --->
<cfset myStruct = { name = "Andy", country = "Scotland" }>

<!--- create structs within a struct --->
<cfset myStruct={ a.c = 1, a.b.c = 3 }>

Those familiar with the JSON format will be pleased to see that we also support the following notation for creating structs:

<!--- create a struct with fields --->
<cfset myStruct = { name : "Andy", country : "Scotland" }>

Operators

You can now make use of all the following operators in your OpenBD apps.

  • Assignment: +=, -=, %=, /=, *=, &=
  • Unary Arithmetic: ++, --
  • Binary Arithmetic: %
  • Comparators (cfscript only): ==, !=, <, >,<=, >=
  • Logical: !, ||, &&

Here's an example of how you could make use of that:

<cfscript>
people = [ { name = 'Andy', country = 'Scotland' },
           { name = 'Matt', country = 'USA' },
           { name = 'Alan', country = 'Scotland' },
           { name = 'John', country = 'Australia' } ];

scottishCount = 0;
for ( i = 1; i < arraylen(people); i++ ){
  if ( people[i].country == 'Scotland' ){
    scottishCount++;
  }
}

</cfscript>

Try it out for yourself now in the nightly build of OpenBD.

Comments (2) . Friday, 29 January 2010

OpenBD gets native JSONP support

We all know the power of web services, whether its SOAP or RESTlet based calls, the ability to consume remote services is key to any service orientated architecture.

CFML has always been blessed by its deep web services support, both in consuming remote resources (CFHTTP, CFINVOKE) and also providing rich end points through the use of CFC's.

Any CFC can be made into a remote service endpoint by simply enabling the access="remote" attribute on a function within a component.

// assume this is saved @ /rpc/mycfc.cfc

<cfcomponent>

  <cffunction name="run" access="remote" returntype="string">
    <cfreturn "from a remote">
  </cffunction>

</cfcomponent>

This method is now available for consumption through a number of different methods:

  • SOAP call: http://myhost/rpc/mycfc.cfc?WSDL
  • RESTlet call: http://myhost/rpc/mycfc.cfc?method=run

The RESTlet call is most useful for Javascript based applications, allowing you to consume and trigger CFC from within any web page running on your server. A CFC called in this manner can have a number of different return formats using the returnformat="json|plain|xml" attribute of a function.

This is all standard CFML and has been available within OpenBD from the start.

Cross-Domain Support

However, what if you wish your services to be consumed by a Javascript function outside of your own domain? Cross-domain scripting will prevent this. So how do the likes of delicious.com offer such Javascript integrations for their services?

You achieve this by using JSONP (JSON with Padding) and this lets you achieve cross-domain data fetching.

JSONP requires a little assistance from both the server and the client inorder for it to work. JSONP works by wrapping the returning JSON packet in () and prepending a name to make it into a Javascript document.

The majority of Javascript libraries support JSON from their native core. JQuery supports it using $.getJSON method, which simply asks that you append the URI parameter "callback=?" to the end of the remote URL and it will do all the marshalling of data for you.

Enabling JSONP in OpenBD

Before you do that, you have to tell OpenBD that this is a JSONP request and allow it the opportunity to post-process the packet for you. You do this using the existing __BDRETURNFORMAT parameter flag and setting it to "jsonp". OpenBD will look for the "callback" parameter and the take the necessary action for you accordingly.

An example of this in action from standard JQuery:

$.getJSON( 
  "http://myhost/rpc/mycfc.cfc?method=run&__BDRETURNFORMAT=jsonp&callback=?", 
  params, 
  function(json){
    // do something with the 'json' object
  }
);

The advantage of this, is that you do not need to modify your existing CFC's inorder for them to be consumed using JSONP methods. There is also huge memory and performance gains to be had, letting the core engine do all the heavy lifting instead of the existing workarounds written in CFML.

Controlling incoming data

OpenBD has enabled a number of flags that the remote REST client can use to help format the data coming back from the server without having to touch your CFC.

  • __BDRETURNFORMAT
    Controls the data format of the data. Valid values: wddx, plain, json, jsonp
  • __BDQUERYFORMAT
    Controls how query data should be formatted. Valid values: column, row
  • __BDNODEBUG
    If present, then if the CFC creates an error, then any stack trace will be not be sent to the client

Through the use of these (case insensitive) flags you can control how you wish the data to be presented by the client instead of having to modify any server-side code, making it easier for you to offer greater flexibility to your web services clients.

JSONP support available now

This functionality has been enabled in the nightly build of OpenBD and will be available in the core release 1.3 once released.

Comments (0) . Saturday, 16 January 2010

New Query Functions released

We've justed finished the a whole new set of functions to help with the manipulation of Query objects and their respective datasources.

The following table highlights all the current functions within the Query category available in the OpenBD (nightly build) release:

DatasourceCreate Adds a new datasource to the system for use with any database functions. This does not persist over server restarts
DatasourceDelete Removes the given datasource. Note, it will not remove any datasource that was registered with the underlying bluedragon.xml file
DatasourceIsValid Checks to see if a given datasource has been previously registered using DataSourceCreate()
QueryAddColumn Adds a new column of data to the exist query object, returning the column number
QueryAddRow Adds the specified the number of rows to the end of the query
QueryColumnArray Returns all the data in a query for a given column
QueryColumnList Returns all the data in a query for a given row but as a structure
QueryDeleteColumn Deletes the column from the query, returning the deleted column data as an array
QueryDeleteRow Deletes the row within a query object. Modifies the original query object
QueryIsEmpty Determines if the query has any rows
QueryNew Creates a new query object with the columns past in of the optional types
QueryOfQueryRun Executes a Query-of-Query against a previous SQL result sets. Function version of CFQUERY
QueryRowStruct Returns all the data in a query for a given row but as a structure
QueryRun Executes the given SQL query against the given datasource, optionally passing in paramters. Function version of CFQUERY
QuerySetCell Sets the given column within a query with the value at the given row, or the last row if not specified
QuerySort Sorts the query based on the column specified and the order criteria given. Modifies the original query object
QuotedValueList Returns a quoted list of all the values, for a given column within the query, delimited by the value given
ToCsv Transforms the query object into a Comma Separated Value (CSV) block
ToHtml Transforms the query object into an HTML TABLE block
ValueList Returns a list of all the values, for a given column within the query, delimited by the value given

These functions will greatly increase the speed and efficiency to which you can work with Query objects.

Many of the functions where available using other means. For example, QueryDeleteColumn could have been achieved by performing a query-of-queries leaving out the column you wanted to remove. This however had a huge overhead, as well as duplicating the data.

You can read more about the DataSource functions over at Alan Williamson's blog.

Thanks to Peter J Farrell for many of his suggestions.

Comments (0) . Friday, 1 January 2010

CFJAVASCRIPT gets a turbo boost

One of the beautiful side benefits of open source is the ability to collate and integrate feedback very quickly. This is probably one of the biggest advantages over commercial software. OpenBD enjoys a 6monthly release cycle (April + October) so when we release our major milestones you can be sure the code has been well baked and tested by the time you build a business around it.

One of the tags that has been given the open source loving treatment has been CFJAVASCRIPT. Released earlier this year it has proven popular to those that are managing lots of Javascript files in their apps. This tags lets you manage your Javascript by optimizing them to make them load and run faster at the browser.

So the usual benefits of minimizing, munging, and heavily caching them all go a long way to help speed up your web application. Instead of downloading multiple javascript files, CFJAVASCRIPT can bring them together as a single unit and force the browser to cache them aggressively.

We've just added a whole host of extras that have been requested by the community:

  • Ability to place Javascript code inside the tag instead of specifying external javascript files
  • Specify a comma-separated list of javascript files as the SRC='' attribute. Arrays of files still supported
  • Include-Only-Once; if you have multiple CFJAVASCRIPT tags, then we make sure you only include a javascript file only once per request. That way the browser won't get two JQuery instances!
  • Javascript placement; you can now have the resulting javascript placed at either the position the tag appears, at the HEAD or at the BOTTOM of the HTML body tag

For example, this snippet will place the resulting optimized Javascript reference at the bottom of the page.

<cfset jsFiles = "jquery.js,jtree.js">
<cfjavascript src="#jsfiles#" minimize="true" munge="true" output="body">

Alternatively you can use it to optimize inline code:

<cfjavascript minimize="true" munge="true" output="body">
myClass = {
  someStuff : function(){
    // do something
  }
};
</cfjavascript>

Ready to use from the nightly build. Enjoy.

Comments (6) . Tuesday, 24 November 2009