Weitere ähnliche Inhalte Ähnlich wie Language enhancements in cold fusion 11 (20) Mehr von ColdFusionConference (20) Kürzlich hochgeladen (20) Language enhancements in cold fusion 111. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Language Enhancements in ColdFusion 11 (Splendor)
Ram Kulkarni
1
@ram_kulkarni
ramkulkarni.com
2. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Language Enhancements in ColdFusion 11
§ Full CFScript support
§ Member functions for data types
§ JSON enhancements
§ Query functions
§ Elvis operator
§ Built-in functions as data type
§ Collection functions
§ Application specific dynamic datasource
2
3. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Tags in CFScript
3
4. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Tags in CFScript
§ Can call most tags in cfscript
§ Call tags as functions
§ Name of the function is same as tag name
§ Attributes are passed as name-value pair separated by comma
§ Example
<cfscript>
cfdocument (format="PDF" ,src="report.cfm");
</cfscript>
4
5. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Tags in CFScript– child tags
<cfscript>
cfchart ( format="html")
{
cfchartseries (type="bar", seriescolor="##4dc", label="Sales")
{
cfchartdata (item="Jan" ,value="100" );
cfchartdata (item="Feb" ,value="400" );
cfchartdata (item="Mar" ,value="200" );
cfchartdata (item="Aprl" ,value="500" );
cfchartdata (item="May" ,value="700" );
cfchartdata (item="Jun" ,value="300" );
}
}
</cfscript>
5
6. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Tags in CFScript– using writeoutput
<cfscript>
name = "Ram";
cfsavecontent (variable="var1" )
{
writeOutput("<b>");
writeOutput("Hello #name# !<b>");
}
writeOutput(var1);
</cfscript>
6
7. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Custom Tags in CFScript
Call custom tags in cfscript with “cf_” prefix
<cfscript>
cf_sayHello(name="Ram");
</cfscript>
Using imported custom tags
<cfimport prefix="hello" taglib="customtags">
<cfscript>
hello:sayHello(name="Ram");
</cfscript>
7
8. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Tags not available in CFScript
§ cfscript
§ cfoutput
§ cfdump
§ cfinvoke
§ cfinvokeargument
§ cfobject
§ cfset
8
9. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Data Type Member Functions
9
10. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Member Functions for Data Types
§ Access functions on data types as member functions
Example
<cfscript>
st = {
firstName: "Ram",
lastName: "Kulkarni"
};
writeDump(st.find("firstName"));
//structFind(st,"firstName");
st.append({company:"Adobe"});
//structAppend(st,{company:"Adobe"});
writeDump(st);
</cfscript>
10
11. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Supported Data Types
§ Array
§ Struct
§ List
§ Date
§ String
§ Query
§ Xml
§ Image
§ Spreadsheet
11
12. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
JSON Enhancements
12
13. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
JSON Enhancements
§ Case of Struct keys now are preserved
§ Select option “Preserve case for Struct keys for Serialization”
in the Administrator->Settings
§ Data types inferred for Query columns and CFC properties
§ Option to serialize Query as Struct
§ Custom Serializers
13
14. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
JSON Case Sensitivity
§ Setting case preservation for Struct keys
§ Application Level
§ Server Level
§ Application.cfc setting
component {
this.name = "JSONEnhancements";
this.serialization.preserveCaseForStructKey = true;
}
§ Server setting in ColdFusion Administrator
14
15. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Type of data preserved in CFC and Query
§ ColdFusion is loosely typed language
§ It infers data type based on value assigned
§ But sometime it can get data type wrong
e.g. “0123” could be converted to number 123
§ JSON Serialization for Query
§ Infers data type from column type of Database
§ JSON Serialization for CFC
§ Infers data type from ‘type’ attribute of CFC property
15
16. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Serialize Query as Struct
§ Before ColdFusion 11
§ serializeJSON (object [,boolean queryFormat])
§ In ColdFusion 11
§ serializeJSON (object [, object queryFormat])
§ Second argument could be
§ True, False, “row”, “column”, “struct”
§ Application.cfc setting
component {
this.serialization.serializeQueryAs = "struct";
}
16
17. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Custom Serializer
§ New serialize functions
§ serialzeXML and deserializeXML
§ serializeJSON and deserializeJSON already existed
§ Set custom serializer in Application.cfc
component {
this.customSerializer = "MyJSONSerializer";
}
§ Create a CFC for custom serialization and implement following functions –
function canSerialize (type) access="remote" returntype="boolean”
function serialize (objToSerialize, acceptHeader) access="remote" returntype="String”
function canDeSerialize (type) access="remote" returntype="boolean"
function deSerialize (objToSerialize, acceptHeader) access="remote" returntype="String"
17
18. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Query Functions
18
19. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
queryExecute
§ Syntax
queryExecute (sql [,queryParams] [,queryOptions])
§ Examples:
<cfscript>
rs = queryExecute("select * from employee",[], {datasource:”EmployeeDS”});
rs = queryExecute("select * from employee where id = ?", [1]);
rs = queryExecute("select * from employee where id = :empId", {empId:1});
rs = queryExecute("select * from employee where id = :empId", {empId:
{type="integer", value=1}});
rs = queryExecute("select * from employee where id = :empId and first_name
like :firstName", {empId:1, firstName:"Ram"});
</cfscript>
19
20. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
queryGetRow Function
§ Retrieves specific query row
§ Result is returned as Struct
§ Throws RowNumberOutOfBoundException if index is greater than num rows
§ Syntax:
row = queryGetRow (queryObj, rowIndex) OR
row = queryObj.getRow(rowIndex)
<cfscript>
queryObj = queryExecute("select * from employee order by first_name");
writeDump(queryObj);
writeDump(queryObj.getRow(2));
writeDump(queryGetRow(queryObj, 1));
</cfscript>
20
21. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Other Language Enhancements
21
22. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Elvis Operator
§ Special case of Ternary operator - ?:
<cfscript>
var1 = isDefined("var2") ? var2 : "defaultValue";
writeOutput(var1 & "<br>");
var1 = var2 ?: "defaultValue";
writeOutput(var1);
</cfscript>
Output is the same – “defaultValue”
22
23. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Build-in functions as data types
§ Pass built-in function as function argument
§ Return built-in function from a function
§ Assign built-in function to a variable
You could do all of above with UDFs earlier, now supported for built-in functions
<cfscript>
a = [10, 44, 55, 60];
function arrayOperation (opFunc)
{
return opFunc(a);
}
writeOutput("Array Max = " & arrayOperation(arrayMax) & "<br>");
writeOutput("Array Min = " & arrayOperation(arrayMin) & "<br>");
writeOutput("Array Average = " & arrayOperation(arrayAvg) & "<br>");
§ </cfscript>
23
24. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Enhancements to collection functions
§ Index and array object arguments added to closure function of arrayEach
§ arrayEach(array, function (arrayElement, index, arrayObj) { … });
§ New function listEach
§ listEach
(
list/String,
function (listObj, index){}
[, delimiters_string]
[, includeEmpltyFields_boolean]
)
24
25. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Enhancements to collection functions - Continued
§ arrayReduce
§ arrayReduce (arrayObj, function(prevResult,item, index, arrayObj){});
§ Returns a single value
§ arrayMap
§ arrayMap (arrayObj, function (item, index, arrayObj){});
§ Replaces value of array element at given index and returns new array
§ Similarly structReduce, structMap, listReduce and listMap are added
25
26. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Application Specific Datasource
§ Ways to create datasource
§ In the Administrator
§ Using Admin API
§ CF11 – New way to create dynamic datasources
§ In application.cfc
this.datasources.myDataSource = {
database = “path_to_folder",
driver = "Apache Derby Embedded"
};
this.datasource = "myDataSource";
26
27. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Application specific datasource – continued …
§ Creating datasource using JDBC URL
this.datasources.dsn2={"driver"="MSSQLServer",
url="jdbc:macromedia:sqlserver://localhost
MSSQL2008;databaseName=regression;;sendStringParametersAsUnicode=false;querytimeout=0;M
axPooledStatements=1000","username"="sa","password"="pass"};
§ In case of clash, Application datasource will get preference
27
28. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
CFZip Enhancements
§ Support for password protected zip files
§ CFZip – added two attributes
§ password
§ encryptionAlgorithm – Standard, AES-128, AES-256 (default)
<cfscript>
cfzip (action="zip", file="C:ZipTesttestzipfile.zip",
source="C:ZipTestsample.txt", password="pass");
cfzip (action="list", name="zipFileList",
file="C:ZipTesttestzipfile.zip");
cfzip (action="unzip",
file="C:ZipTesttestzipfile.zip",
destination="C:ZipTest", password="pass", overwrite="true");
</cfscript>
28
29. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
CFZip Enhancements
§ Can specify password for individual files
<cfscript>
cfzip(action="zip",
file="C:ZipTesttestzipfile.zip", overwrite="true")
{
cfzipparam(source="C:ZipTestsample.txt", password="pass");
};
cfzip(action="unzip", file="C:ZipTesttestzipfile.zip",
destination="C:ZipTest", overwrite="true")
{
cfzipparam(source="sample.txt", password="pass");
};
</cfscript>
29
30. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Thank you
30
31. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.