Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Initial support for queries. #3

Merged
merged 1 commit into from

2 participants

Raymond Camden Mark Mandel
Raymond Camden

No description provided.

Mark Mandel markmandel merged commit 27a401f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 71 additions and 0 deletions.
  1. +24 −0 sesame/collections.cfm
  2. +47 −0 tests/cases/CollectionsTest.cfc
24 sesame/collections.cfm
View
@@ -42,6 +42,10 @@
{
ArrayAppend(collection, transform(k, v));
});
+ } else if(isQuery(arguments.data)) {
+ for(var r in arguments.data) {
+ arrayAppend(collection, transform(r));
+ }
}
return collection;
@@ -81,6 +85,13 @@
{
StructEach(arguments.data, function(k, v) { addToCollection( _transform(k, v) ); });
}
+ else if(isQuery(arguments.data))
+ {
+ for(var r in arguments.data) {
+ addToCollection(_transform(r));
+ }
+
+ }
return collection;
}
@@ -128,6 +139,19 @@
}
/**
+ * Iterates over a query and executes the closure on each row.
+ *
+ * @data the query
+ * @func the closure
+ */
+ public any function _queryEach(required query data, function func)
+ {
+ for(var row in data) {
+ func(row);
+ }
+ }
+
+ /**
* Returns an array with all the duplicates removed (i.e. unique). For structs, this iterates through all the
* values, and returns an array from that, with duplicates removed.
*
47 tests/cases/CollectionsTest.cfc
View
@@ -17,6 +17,20 @@ component extends="tests.AbstractTestCase"
}
/**
+ * test collecting an query
+ */
+ public void function testCollectQuery()
+ {
+ data = queryNew("id,name,age", "integer,varchar,varchar",
+ [{id:1,name:"Ray",age:30},
+ {id:2,name:"Jeanne",age:28}]);
+
+ var collected = _collect(data, function(r) { return r.id*2; });
+
+ assertEquals([2,4], collected);
+ }
+
+ /**
* test collecting an array
*/
public void function testCollectStruct()
@@ -81,6 +95,23 @@ component extends="tests.AbstractTestCase"
}
/**
+ * testCollectEntriesQuery
+ */
+ public void function testCollectEntriesQuery()
+ {
+ var data = queryNew("id,name,age", "integer,varchar,varchar",
+ [{id:1,name:"Ray",age:30},
+ {id:2,name:"Jeanne",age:28}]);
+
+ var collected = _collectEntries(data, function(r) {
+ return [r.id,r.name];
+ });
+
+
+ assertEquals({1="Ray", 2="Jeanne"}, collected);
+ }
+
+ /**
* test group by with an array
*/
public void function testGroupByWithArray()
@@ -104,6 +135,22 @@ component extends="tests.AbstractTestCase"
assertEquals({1={a=1, c=3, e=5}, 0={b=2, d=4, f=6}}, grouped);
}
+ /**
+ * Test queryEach
+ */
+ public void function testQueryEach()
+ {
+ var data = queryNew("id,name,age", "integer,varchar,varchar",
+ [{id:1,name:"Ray",age:30},
+ {id:2,name:"Jeanne",age:28}]);
+ var total = 0;
+ var iterator = function(r) {
+ total += r.id;
+ };
+ _queryEach(data, iterator);
+ assertEquals(3, total);
+ }
+
/**
* test unique collections, no comparatpr
*/
Something went wrong with that request. Please try again.