Browse files

Nother update. Moved everything over to streaming-results using yield…

…/return, except for Fetch routine.
  • Loading branch information...
1 parent 23f2b03 commit 5e5d710bc9de85e416988ea437cd5a754ef6e97c @subsonic subsonic committed Feb 20, 2011
Showing with 19 additions and 13 deletions.
  1. +5 −11 Massive.cs
  2. +14 −2 README.markdown
View
16 Massive.cs
@@ -107,7 +107,7 @@ public class DynamicModel {
/// <summary>
/// Enumerates the reader yielding the result - thanks to Jeroen Haegebaert
/// </summary>
- public IEnumerable<dynamic> Enumerate(string sql, params object[] args) {
+ public IEnumerable<dynamic> Query(string sql, params object[] args) {
using (var conn = OpenConnection()) {
var rdr = CreateCommand(sql, conn, args).ExecuteReader(CommandBehavior.CloseConnection);
while (rdr.Read()) {
@@ -122,14 +122,8 @@ public class DynamicModel {
/// <summary>
/// Runs a query against the database
/// </summary>
- public IList<dynamic> Query(string sql, params object[] args) {
- var result = new List<dynamic>();
- using (var conn = OpenConnection()) {
- using (var rdr = CreateCommand(sql, conn, args).ExecuteReader(CommandBehavior.CloseConnection)) {
- result = rdr.ToExpandoList();
- }
- }
- return result;
+ public IList<dynamic> Fetch(string sql, params object[] args) {
+ return Query(sql, args).ToList<dynamic>();
}
/// <summary>
/// Returns a single result
@@ -343,7 +337,7 @@ public class DynamicModel {
if (String.IsNullOrEmpty(orderBy))
orderBy = PrimaryKeyField;
var sql = string.Format("SELECT {0} FROM (SELECT ROW_NUMBER() OVER (ORDER BY {2}) AS Row, {0} FROM {3}) AS Paged ",columns,pageSize,orderBy,TableName);
- var pageStart = currentPage * pageSize;
+ var pageStart = (currentPage -1) * pageSize;
sql+= string.Format(" WHERE Row >={0} AND Row <={1}",pageStart, (pageStart + pageSize));
if (!string.IsNullOrEmpty(where)) {
if (where.Trim().StartsWith("where", StringComparison.CurrentCultureIgnoreCase)) {
@@ -365,7 +359,7 @@ public class DynamicModel {
/// <returns>ExpandoObject</returns>
public dynamic Single(object key, string columns = "*") {
var sql = string.Format("SELECT {0} FROM {1} WHERE {2} = @0", columns,TableName, PrimaryKeyField);
- return Query(sql, key).FirstOrDefault();
+ return Fetch(sql, key).FirstOrDefault();
}
}
}
View
16 README.markdown
@@ -30,7 +30,9 @@ Let's say we have a table named "Products". You create a class like this:
}
-This class inherits from DynamicModel, which does all the querying (that's the class you see above). We've told it how to connect (using the "northwind" connectionStringName), we've explicitly told it what the PK name is, and we can also tell it to use a TableName if we like. In addition there are a number of Callbacks you can use - like "AfterInsert()", "BeforeInsert()" and so on that fire ... guess when!
+You could also just instantiate it inline, as needed:
+ var tbl = new DynamicModel("northwind", tableName:"Products", primaryKeyField:"ProductID");
+
Now you can query thus:
var table = new Products();
@@ -39,6 +41,16 @@ Now you can query thus:
//just grab from category 4. This uses named parameters
var productsFour = table.All(columns: "ProductName as Name", where: "WHERE categoryID=@0",args: 4);
+You can also run ad-hoc queries as needed:
+ var result = tbl.Query("SELECT * FROM Categories");
+
+This will pull categories and enumerate the results - streaming them as opposed to bulk-fetching them (thanks to Jeroen Haegebaert for the code). If you need to run a Fetch - you can:
+ var result = tbl.Fetch("SELECT * FROM Categories");
+
+If you want to have a Paged result set - you can:
+ var result = tbl.Paged(where: "UnitPrice > 20", currentPage:2, pageSize: 20);
+In this example, ALL of the arguments are optional and default to reasonable values. CurrentPage defaults to 1, pageSize defaults to 20, where defaults to nothing.
+
What you get back is IEnumerable < ExpandoObject > - meaning that it's malleable and exciting. It will take the shape of whatever you return in your query, and it will have properties and so on. You can assign events to it, you can create delegates on the fly. You can give it chocolate, and it will kiss you.
That's pretty much it. One thing I really like is the groovy DSL that Massive uses - it looks just like SQL. If you want, you can use this DSL to query the database:
@@ -80,4 +92,4 @@ Yippee Skippy! Now we get to the fun part - and one of the reasons I had to spen
item.CategoryID = 12;
}
//Let's update these in bulk, in a transaction shall we?
- table.Transact(drinks);
+ table.Save(drinks);

0 comments on commit 5e5d710

Please sign in to comment.