Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added a Schema property that returns, dynamically, all the informatio…

…n_schema stuff for use with whatever. Added an overload for Single that allows you to specify where clause
  • Loading branch information...
commit bb4497e82ee4900f1f5290fcd03d0fea4436429f 1 parent a0016ca
SubSonic Project authored June 16, 2011

Showing 1 changed file with 19 additions and 2 deletions. Show diff stats Hide diff stats

  1. 21  Massive.cs
21  Massive.cs
@@ -101,6 +101,15 @@ public class DynamicModel : DynamicObject {
101 101
             _connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
102 102
         }
103 103
         /// <summary>
  104
+        /// List out all the schema bits for use with ... whatever
  105
+        /// </summary>
  106
+        public IEnumerable<dynamic> Schema {
  107
+            get {
  108
+                return Query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @0", TableName);
  109
+            }
  110
+        }
  111
+
  112
+        /// <summary>
104 113
         /// Enumerates the reader yielding the result - thanks to Jeroen Haegebaert
105 114
         /// </summary>
106 115
         public virtual IEnumerable<dynamic> Query(string sql, params object[] args) {
@@ -123,6 +132,7 @@ public class DynamicModel : DynamicObject {
123 132
                 task.ContinueWith(x => callback.Invoke(x.Result.ToExpandoList()));
124 133
             }
125 134
         }
  135
+
126 136
         public virtual IEnumerable<dynamic> Query(string sql, DbConnection connection, params object[] args) {
127 137
             using (var rdr = CreateCommand(sql, connection, args).ExecuteReader()) {
128 138
                 while (rdr.Read()) {
@@ -185,6 +195,7 @@ public class DynamicModel : DynamicObject {
185 195
             var commands = BuildCommands(things);
186 196
             return Execute(commands);
187 197
         }
  198
+
188 199
         public virtual int Execute(DbCommand command) {
189 200
             return Execute(new DbCommand[] { command });
190 201
         }
@@ -370,10 +381,16 @@ public class DynamicModel : DynamicObject {
370 381
         /// <summary>
371 382
         /// Returns a single row from the database
372 383
         /// </summary>
  384
+        public virtual dynamic Single(string where, params object[] args) {
  385
+            var sql = string.Format("SELECT * FROM {0} WHERE {1}", TableName, where);
  386
+            return Query(sql, args).First();
  387
+        }
  388
+        /// <summary>
  389
+        /// Returns a single row from the database
  390
+        /// </summary>
373 391
         public virtual dynamic Single(object key, string columns = "*") {
374 392
             var sql = string.Format("SELECT {0} FROM {1} WHERE {2} = @0", columns, TableName, PrimaryKeyField);
375  
-            var items = Query(sql, key).ToList();
376  
-            return items.FirstOrDefault();
  393
+            return Query(sql, key).FirstOrDefault();
377 394
         }
378 395
         /// <summary>
379 396
         /// A little Rails-y love for ya

0 notes on commit bb4497e

Please sign in to comment.
Something went wrong with that request. Please try again.