Skip to content

Instantly share code, notes, and snippets.

@roryl
Last active March 16, 2016 00:03
Show Gist options
  • Save roryl/0b45eb21342466f5243d to your computer and use it in GitHub Desktop.
Save roryl/0b45eb21342466f5243d to your computer and use it in GitHub Desktop.
Lucee Query Object Examples
<cfscript>
myQuery = queryNew("col1,col2,col3");
myQuery.addColumn("col4", ["foo","bar","baz"]);
dump(myQuery);
</cfscript>
<cfscript>
myQuery = queryNew("col1,col2,col3");
myQuery.addRow(["foo","bar","baz"]);
dump(myQuery);
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
dump(myQuery.columnData("col1"));
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
dump(myQuery);
</cfscript>
<cfscript>
myQuery = queryNew("col1,col2,col3");
dump(myQuery);
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
for(row in myQuery){
writeDump(row); //Dumps a structure representing the row
echo(row.col1); //output the col1 value of this row
echo(row.col2); //output the col2 value of this row
echo(row.col3) //output the col3 value of this row
}
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
for(i = 1; i <= myQuery.recordCount(); i++){
echo(myQuery.col1[i]); //output the col1 value of this row
echo(myQuery.col2[i]); //output the col2 value of this row
echo(myQuery.col3[i]) //output the col3 value of this row
}
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
loop query="myQuery" index="i"{
echo(col1); //output the col1 value of this row
echo(col2); //output the col2 value of this row
echo(col3) //output the col3 value of this row
}
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["ford","chevy","kia"],
["hyundai","vw","toyota"],
["banana","berry","apple"],
["coffee","water","soda"]
]);
loop query="myQuery" startrow="2" maxrows="2"{
echo(col1 & " | "); //output the col1 value of this row
echo(col2 & " | "); //output the col2 value of this row
echo(col3 & " | "); //output the col3 value of this row
echo("<br />");
}
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
query name="filterQuery" dbtype="query" {
echo("Select col2 from myQuery");
}
writeDump(filterQuery);
query name="filterQuery" dbtype="query" {
echo("Select * from myQuery where col2 = 'am'");
}
writeDump(filterQuery);
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
echo(myQuery.col2[3]); //outputs col2 row 3, "am"
echo(myQuery.col1[2]); //outputs col1 row 2, "foo"
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
dump(myQuery.rowData(2));
</cfscript>
<cfscript>
myQuery = queryNew(columns="col1,col2,col3",
data=[
["one","two","three"],
["foo","bar","baz"],
["i","am","query"]
]);
myQuery.setCell("col2", "hello"); //sets the last row of this column
myQuery.setCell("col2", "there", 2); //Sets the specific row of this column
dump(myQuery);
</cfscript>
/**
* My xUnit Test
*/
component extends="testbox.system.BaseSpec"{
/*********************************** LIFE CYCLE Methods ***********************************/
// executes before all test cases
function beforeTests(){
}
// executes after all test cases
function afterTests(){
}
// executes before every test case
function setup( currentMethod ){
}
// executes after every test case
function teardown( currentMethod ){
}
/*********************************** TEST CASES BELOW ***********************************/
// Remember that test cases MUST start or end with the keyword 'test'
function checkAllSyntaxTest(){
var files = directoryList("");
for(file IN files){
if(file CONTAINS ".cfm"){
include template="#getFileFromPath(file)#";
}
}
}
function queryLoopTest(){
include template="query_loop.cfm";
}
function queryLoopAttributesTest(){
include template="query_loop_attributes.cfm";
}
function queryOfQueriesTest(){
include template="query_of_queries.cfm";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment