Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

added basic support for PostgreSQL (npgsql driver) #72

Merged
merged 1 commit into from over 2 years ago

2 participants

Tomasz Kubacki Rob Conery
Tomasz Kubacki

tested using Npgsql driver some basic select, insert, update and delete queries on Mono 2.10.2

npgsql driver download page: http://pgfoundry.org/frs/?group_id=1000140

Provider declaration example (needed to run with npgsql - add in app.config or machine.config): see 3.4 Using Npgsql with ProviderFactory at http://npgsql.projects.postgresql.org/docs/manual/UserManual.html

Rob Conery robconery merged commit 63c546c into from August 08, 2011
Rob Conery robconery closed this August 08, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 01, 2011
Tomasz Kubacki added basic support for PostgreSQL (npgsql driver) c7cb924
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 529 additions and 0 deletions. Show diff stats Hide diff stats

  1. 529  Massive.PostgreSQL.cs
529  Massive.PostgreSQL.cs
... ...
@@ -0,0 +1,529 @@
  1
+using System;
  2
+using System.Collections.Generic;
  3
+using System.Collections.Specialized;
  4
+using System.Configuration;
  5
+using System.Data;
  6
+using System.Data.Common;
  7
+using System.Dynamic;
  8
+using System.Linq;
  9
+using System.Text;
  10
+using System.Threading.Tasks;
  11
+using System.Data.SqlClient;
  12
+
  13
+namespace Massive.PostgreSQL {
  14
+    public static class ObjectExtensions {
  15
+        /// <summary>
  16
+        /// Extension method for adding in a bunch of parameters
  17
+        /// </summary>
  18
+        public static void AddParams(this DbCommand cmd, params object[] args) {
  19
+            foreach (var item in args) {
  20
+                AddParam(cmd, item);
  21
+            }
  22
+        }
  23
+        /// <summary>
  24
+        /// Extension for adding single parameter
  25
+        /// </summary>
  26
+        public static void AddParam(this DbCommand cmd, object item) {
  27
+            var p = cmd.CreateParameter();
  28
+            p.ParameterName = string.Format("@{0}", cmd.Parameters.Count);
  29
+            if (item == null) {
  30
+                p.Value = DBNull.Value;
  31
+            } else {
  32
+                if (item.GetType() == typeof(Guid)) {
  33
+                    p.Value = item.ToString();
  34
+                    p.DbType = DbType.String;
  35
+                    p.Size = 4000;
  36
+                } else if (item.GetType() == typeof(ExpandoObject)) {
  37
+                    var d = (IDictionary<string, object>)item;
  38
+                    p.Value = d.Values.FirstOrDefault();
  39
+                } else {
  40
+                    p.Value = item;
  41
+                }
  42
+                if (item.GetType() == typeof(string))
  43
+                    p.Size = ((string)item).Length > 4000 ? -1 : 4000;
  44
+            }
  45
+            cmd.Parameters.Add(p);
  46
+        }
  47
+        /// <summary>
  48
+        /// Turns an IDataReader to a Dynamic list of things
  49
+        /// </summary>
  50
+        public static List<dynamic> ToExpandoList(this IDataReader rdr) {
  51
+            var result = new List<dynamic>();
  52
+            while (rdr.Read()) {
  53
+                result.Add(rdr.RecordToExpando());
  54
+            }
  55
+            return result;
  56
+        }
  57
+        public static dynamic RecordToExpando(this IDataReader rdr) {
  58
+            dynamic e = new ExpandoObject();
  59
+            var d = e as IDictionary<string, object>;
  60
+            for (int i = 0; i < rdr.FieldCount; i++)
  61
+                d.Add(rdr.GetName(i), DBNull.Value.Equals(rdr[i]) ? null : rdr[i]);
  62
+            return e;
  63
+        }
  64
+        /// <summary>
  65
+        /// Turns the object into an ExpandoObject
  66
+        /// </summary>
  67
+        public static dynamic ToExpando(this object o) {
  68
+            var result = new ExpandoObject();
  69
+            var d = result as IDictionary<string, object>; //work with the Expando as a Dictionary
  70
+            if (o.GetType() == typeof(ExpandoObject)) return o; //shouldn't have to... but just in case
  71
+            if (o.GetType() == typeof(NameValueCollection) || o.GetType().IsSubclassOf(typeof(NameValueCollection))) {
  72
+                var nv = (NameValueCollection)o;
  73
+                nv.Cast<string>().Select(key => new KeyValuePair<string, object>(key, nv[key])).ToList().ForEach(i => d.Add(i));
  74
+            } else {
  75
+                var props = o.GetType().GetProperties();
  76
+                foreach (var item in props) {
  77
+                    d.Add(item.Name, item.GetValue(o, null));
  78
+                }
  79
+            }
  80
+            return result;
  81
+        }
  82
+        /// <summary>
  83
+        /// Turns the object into a Dictionary
  84
+        /// </summary>
  85
+        public static IDictionary<string, object> ToDictionary(this object thingy) {
  86
+            return (IDictionary<string, object>)thingy.ToExpando();
  87
+        }
  88
+    }
  89
+    /// <summary>
  90
+    /// A class that wraps your database table in Dynamic Funtime
  91
+    /// </summary>
  92
+    public class DynamicModel : DynamicObject {
  93
+        DbProviderFactory _factory;
  94
+        string ConnectionString;
  95
+        public static DynamicModel Open(string connectionStringName) {
  96
+            dynamic dm = new DynamicModel(connectionStringName);
  97
+            return dm;
  98
+        }
  99
+        public DynamicModel(string connectionStringName, string tableName = "", string primaryKeyField = "") {
  100
+            TableName = tableName == "" ? this.GetType().Name : tableName;
  101
+            PrimaryKeyField = string.IsNullOrEmpty(primaryKeyField) ? "id" : primaryKeyField;
  102
+            var _providerName = "Npgsql";
  103
+            _factory = DbProviderFactories.GetFactory(_providerName);
  104
+            ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
  105
+        }
  106
+
  107
+        /// <summary>
  108
+        /// Creates a new Expando from a Form POST - white listed against the columns in the DB
  109
+        /// </summary>
  110
+        public dynamic CreateFrom(NameValueCollection coll) {
  111
+            dynamic result = new ExpandoObject();
  112
+            var dc = (IDictionary<string, object>)result;
  113
+            var schema = Schema;
  114
+            //loop the collection, setting only what's in the Schema
  115
+            foreach (var item in coll.Keys) {
  116
+                var exists = schema.Any(x => x.COLUMN_NAME.ToLower() == item.ToString().ToLower());
  117
+                if (exists) {
  118
+                    var key = item.ToString();
  119
+                    var val = coll[key];
  120
+                    if (!String.IsNullOrEmpty(val)) {
  121
+                        //what to do here? If it's empty... set it to NULL?
  122
+                        //if it's a string value - let it go through if it's NULLABLE?
  123
+                        //Empty? WTF?
  124
+                        dc.Add(key, val);
  125
+                    }
  126
+                }
  127
+            }
  128
+            return result;
  129
+        }
  130
+        /// <summary>
  131
+        /// Gets a default value for the column
  132
+        /// </summary>
  133
+        public dynamic DefaultValue(dynamic column) {
  134
+            dynamic result = null;
  135
+            string def = column.COLUMN_DEFAULT;
  136
+            if (String.IsNullOrEmpty(def)) {
  137
+                result = null;
  138
+            } else if (def == "getdate()" || def == "(getdate())") {
  139
+                result = DateTime.Now.ToShortDateString();
  140
+            } else if (def == "newid()") {
  141
+                result = Guid.NewGuid().ToString();
  142
+            } else {
  143
+                result = def.Replace("(", "").Replace(")", "");
  144
+            }
  145
+            return result;
  146
+        }
  147
+        /// <summary>
  148
+        /// Creates an empty Expando set with defaults from the DB
  149
+        /// </summary>
  150
+        public dynamic Prototype {
  151
+            get {
  152
+                dynamic result = new ExpandoObject();
  153
+                var schema = Schema;
  154
+                foreach (dynamic column in schema) {
  155
+                    var dc = (IDictionary<string, object>)result;
  156
+                    dc.Add(column.COLUMN_NAME, DefaultValue(column));
  157
+                }
  158
+                result._Table = this;
  159
+                return result;
  160
+            }
  161
+        }
  162
+        /// <summary>
  163
+        /// List out all the schema bits for use with ... whatever
  164
+        /// </summary>
  165
+        IEnumerable<dynamic> _schema;
  166
+        public IEnumerable<dynamic> Schema {
  167
+            get {
  168
+                if(_schema == null)
  169
+                    _schema= Query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @0", TableName);
  170
+                return _schema;
  171
+            }
  172
+        }
  173
+
  174
+        /// <summary>
  175
+        /// Enumerates the reader yielding the result - thanks to Jeroen Haegebaert
  176
+        /// </summary>
  177
+        public virtual IEnumerable<dynamic> Query(string sql, params object[] args) {
  178
+            using (var conn = OpenConnection()) {
  179
+                var rdr = CreateCommand(sql, conn, args).ExecuteReader();
  180
+                while (rdr.Read()) {
  181
+                    yield return rdr.RecordToExpando(); ;
  182
+                }
  183
+            }
  184
+        }
  185
+        /// <summary>
  186
+        /// Executes the reader using SQL async API - thanks to Damian Edwards
  187
+        /// </summary>
  188
+        public void QueryAsync(string sql, Action<List<dynamic>> callback, params object[] args) {
  189
+            using (var conn = new SqlConnection(ConnectionString)) {
  190
+                var cmd = new SqlCommand(sql, conn);
  191
+                cmd.AddParams(args);
  192
+                conn.Open();
  193
+                var task = Task.Factory.FromAsync<IDataReader>(cmd.BeginExecuteReader, cmd.EndExecuteReader, null);
  194
+                task.ContinueWith(x => callback.Invoke(x.Result.ToExpandoList()));
  195
+                //make sure this is closed off.
  196
+                conn.Close();
  197
+            }
  198
+        }
  199
+
  200
+        public virtual IEnumerable<dynamic> Query(string sql, DbConnection connection, params object[] args) {
  201
+            using (var rdr = CreateCommand(sql, connection, args).ExecuteReader()) {
  202
+                while (rdr.Read()) {
  203
+                    yield return rdr.RecordToExpando(); ;
  204
+                }
  205
+            }
  206
+        }
  207
+        /// <summary>
  208
+        /// Returns a single result
  209
+        /// </summary>
  210
+        public virtual object Scalar(string sql, params object[] args) {
  211
+            object result = null;
  212
+            using (var conn = OpenConnection()) {
  213
+                result = CreateCommand(sql, conn, args).ExecuteScalar();
  214
+            }
  215
+            return result;
  216
+        }
  217
+        /// <summary>
  218
+        /// Creates a DBCommand that you can use for loving your database.
  219
+        /// </summary>
  220
+        DbCommand CreateCommand(string sql, DbConnection conn, params object[] args) {
  221
+            var result = _factory.CreateCommand();
  222
+            result.Connection = conn;
  223
+            result.CommandText = sql;
  224
+            if (args.Length > 0)
  225
+                result.AddParams(args);
  226
+            return result;
  227
+        }
  228
+        /// <summary>
  229
+        /// Returns and OpenConnection
  230
+        /// </summary>
  231
+        public virtual DbConnection OpenConnection() {
  232
+            var result = _factory.CreateConnection();
  233
+            result.ConnectionString = ConnectionString;
  234
+            result.Open();
  235
+            return result;
  236
+        }
  237
+        /// <summary>
  238
+        /// Builds a set of Insert and Update commands based on the passed-on objects.
  239
+        /// These objects can be POCOs, Anonymous, NameValueCollections, or Expandos. Objects
  240
+        /// With a PK property (whatever PrimaryKeyField is set to) will be created at UPDATEs
  241
+        /// </summary>
  242
+        public virtual List<DbCommand> BuildCommands(params object[] things) {
  243
+            var commands = new List<DbCommand>();
  244
+            foreach (var item in things) {
  245
+                if (HasPrimaryKey(item)) {
  246
+                    commands.Add(CreateUpdateCommand(item, GetPrimaryKey(item)));
  247
+                } else {
  248
+                    commands.Add(CreateInsertCommand(item));
  249
+                }
  250
+            }
  251
+            return commands;
  252
+        }
  253
+        /// <summary>
  254
+        /// Executes a set of objects as Insert or Update commands based on their property settings, within a transaction.
  255
+        /// These objects can be POCOs, Anonymous, NameValueCollections, or Expandos. Objects
  256
+        /// With a PK property (whatever PrimaryKeyField is set to) will be created at UPDATEs
  257
+        /// </summary>
  258
+        public virtual int Save(params object[] things) {
  259
+            var commands = BuildCommands(things);
  260
+            return Execute(commands);
  261
+        }
  262
+
  263
+        public virtual int Execute(DbCommand command) {
  264
+            return Execute(new DbCommand[] { command });
  265
+        }
  266
+
  267
+        public virtual int Execute(string sql, params object[] args) {
  268
+            return Execute(CreateCommand(sql, null, args));
  269
+        }
  270
+        /// <summary>
  271
+        /// Executes a series of DBCommands in a transaction
  272
+        /// </summary>
  273
+        public virtual int Execute(IEnumerable<DbCommand> commands) {
  274
+            var result = 0;
  275
+            using (var conn = OpenConnection()) {
  276
+                using (var tx = conn.BeginTransaction()) {
  277
+                    foreach (var cmd in commands) {
  278
+                        cmd.Connection = conn;
  279
+                        cmd.Transaction = tx;
  280
+                        result += cmd.ExecuteNonQuery();
  281
+                    }
  282
+                    tx.Commit();
  283
+                }
  284
+            }
  285
+            return result;
  286
+        }
  287
+        public virtual string PrimaryKeyField { get; set; }
  288
+        /// <summary>
  289
+        /// Conventionally introspects the object passed in for a field that 
  290
+        /// looks like a PK. If you've named your PrimaryKeyField, this becomes easy
  291
+        /// </summary>
  292
+        public virtual bool HasPrimaryKey(object o) {
  293
+            return o.ToDictionary().ContainsKey(PrimaryKeyField);
  294
+        }
  295
+        /// <summary>
  296
+        /// If the object passed in has a property with the same name as your PrimaryKeyField
  297
+        /// it is returned here.
  298
+        /// </summary>
  299
+        public virtual object GetPrimaryKey(object o) {
  300
+            object result = null;
  301
+            o.ToDictionary().TryGetValue(PrimaryKeyField, out result);
  302
+            return result;
  303
+        }
  304
+        public virtual string TableName { get; set; }
  305
+        /// <summary>
  306
+        /// Creates a command for use with transactions - internal stuff mostly, but here for you to play with
  307
+        /// </summary>
  308
+        public virtual DbCommand CreateInsertCommand(object o) {
  309
+            DbCommand result = null;
  310
+            var expando = o.ToExpando();
  311
+            var settings = (IDictionary<string, object>)expando;
  312
+            var sbKeys = new StringBuilder();
  313
+            var sbVals = new StringBuilder();
  314
+            var stub = "INSERT INTO {0} ({1}) \r\n VALUES ({2}) RETURNING {3}";
  315
+            result = CreateCommand(stub, null);
  316
+            int counter = 0;
  317
+            foreach (var item in settings) {
  318
+                sbKeys.AppendFormat("{0},", item.Key);
  319
+                sbVals.AppendFormat("@{0},", counter.ToString());
  320
+                result.AddParam(item.Value);
  321
+                counter++;
  322
+            }
  323
+            if (counter > 0) {
  324
+                var keys = sbKeys.ToString().Substring(0, sbKeys.Length - 1);
  325
+                var vals = sbVals.ToString().Substring(0, sbVals.Length - 1);
  326
+                var sql = string.Format(stub, TableName, keys, vals,PrimaryKeyField);
  327
+                result.CommandText = sql;
  328
+            } else throw new InvalidOperationException("Can't parse this object to the database - there are no properties set");
  329
+            return result;
  330
+        }
  331
+        /// <summary>
  332
+        /// Creates a command for use with transactions - internal stuff mostly, but here for you to play with
  333
+        /// </summary>
  334
+        public virtual DbCommand CreateUpdateCommand(object o, object key) {
  335
+            var expando = o.ToExpando();
  336
+            var settings = (IDictionary<string, object>)expando;
  337
+            var sbKeys = new StringBuilder();
  338
+            var stub = "UPDATE {0} SET {1} WHERE {2} = @{3}";
  339
+            var args = new List<object>();
  340
+            var result = CreateCommand(stub, null);
  341
+            int counter = 0;
  342
+            foreach (var item in settings) {
  343
+                var val = item.Value;
  344
+                if (!item.Key.Equals(PrimaryKeyField, StringComparison.CurrentCultureIgnoreCase) && item.Value != null) {
  345
+                    result.AddParam(val);
  346
+                    sbKeys.AppendFormat("{0} = @{1}, \r\n", item.Key, counter.ToString());
  347
+                    counter++;
  348
+                }
  349
+            }
  350
+            if (counter > 0) {
  351
+                //add the key
  352
+                result.AddParam(key);
  353
+                //strip the last commas
  354
+                var keys = sbKeys.ToString().Substring(0, sbKeys.Length - 4);
  355
+                result.CommandText = string.Format(stub, TableName, keys, PrimaryKeyField, counter);
  356
+            } else throw new InvalidOperationException("No parsable object was sent in - could not divine any name/value pairs");
  357
+            return result;
  358
+        }
  359
+        /// <summary>
  360
+        /// Removes one or more records from the DB according to the passed-in WHERE
  361
+        /// </summary>
  362
+        public virtual DbCommand CreateDeleteCommand(string where = "", object key = null, params object[] args) {
  363
+            var sql = string.Format("DELETE FROM {0} ", TableName);
  364
+            if (key != null) {
  365
+                sql += string.Format("WHERE {0}=@0", PrimaryKeyField);
  366
+                args = new object[] { key };
  367
+            } else if (!string.IsNullOrEmpty(where)) {
  368
+                sql += where.Trim().StartsWith("where", StringComparison.CurrentCultureIgnoreCase) ? where : "WHERE " + where;
  369
+            }
  370
+            return CreateCommand(sql, null, args);
  371
+        }
  372
+        /// <summary>
  373
+        /// Adds a record to the database. You can pass in an Anonymous object, an ExpandoObject,
  374
+        /// A regular old POCO, or a NameValueColletion from a Request.Form or Request.QueryString
  375
+        /// </summary>
  376
+        public virtual object Insert(object o) {
  377
+            dynamic result = 0;
  378
+            using (var conn = OpenConnection()) {
  379
+                var cmd = CreateInsertCommand(o);				 
  380
+                cmd.Connection = conn;
  381
+                result = cmd.ExecuteScalar();
  382
+            }
  383
+            return result;
  384
+        }
  385
+        /// <summary>
  386
+        /// Updates a record in the database. You can pass in an Anonymous object, an ExpandoObject,
  387
+        /// A regular old POCO, or a NameValueCollection from a Request.Form or Request.QueryString
  388
+        /// </summary>
  389
+        public virtual int Update(object o, object key) {
  390
+            return Execute(CreateUpdateCommand(o, key));
  391
+        }
  392
+        /// <summary>
  393
+        /// Removes one or more records from the DB according to the passed-in WHERE
  394
+        /// </summary>
  395
+        public int Delete(object key = null, string where = "", params object[] args) {
  396
+            return Execute(CreateDeleteCommand(where: where, key: key, args: args));
  397
+        }
  398
+        /// <summary>
  399
+        /// Returns all records complying with the passed-in WHERE clause and arguments, 
  400
+        /// ordered as specified, limited (TOP) by limit.
  401
+        /// </summary>
  402
+        public virtual IEnumerable<dynamic> All(string where = "", string orderBy = "", int limit = 0, string columns = "*", params object[] args) {
  403
+            string sql = BuildSelect(where, orderBy, limit);
  404
+            return Query(string.Format(sql, columns, TableName), args);
  405
+        }
  406
+        private static string BuildSelect(string where, string orderBy, int limit) {
  407
+            string sql = limit > 0 ? "SELECT TOP " + limit + " {0} FROM {1} " : "SELECT {0} FROM {1} ";
  408
+            if (!string.IsNullOrEmpty(where))
  409
+                sql += where.Trim().StartsWith("where", StringComparison.CurrentCultureIgnoreCase) ? where : "WHERE " + where;
  410
+            if (!String.IsNullOrEmpty(orderBy))
  411
+                sql += orderBy.Trim().StartsWith("order by", StringComparison.CurrentCultureIgnoreCase) ? orderBy : " ORDER BY " + orderBy;
  412
+            return sql;
  413
+        }
  414
+        /// <summary>
  415
+        /// Returns all records complying with the passed-in WHERE clause and arguments, 
  416
+        /// ordered as specified, limited (TOP) by limit.
  417
+        /// </summary>
  418
+        public virtual void AllAsync(Action<List<dynamic>> callback, string where = "", string orderBy = "", int limit = 0, string columns = "*", params object[] args) {
  419
+            string sql = BuildSelect(where, orderBy, limit);
  420
+            QueryAsync(string.Format(sql, columns, TableName), callback, args);
  421
+        }
  422
+        /// <summary>
  423
+        /// Returns a dynamic PagedResult. Result properties are Items, TotalPages, and TotalRecords.
  424
+        /// </summary>
  425
+        public virtual dynamic Paged(string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args) {
  426
+            dynamic result = new ExpandoObject();
  427
+            var countSQL = string.Format("SELECT COUNT({0}) FROM {1}", PrimaryKeyField, TableName);
  428
+            if (String.IsNullOrEmpty(orderBy))
  429
+                orderBy = PrimaryKeyField;
  430
+
  431
+            if (!string.IsNullOrEmpty(where)) {
  432
+                if (!where.Trim().StartsWith("where", StringComparison.CurrentCultureIgnoreCase)) {
  433
+                    where = "WHERE " + where;
  434
+                }
  435
+            }
  436
+            var sql = string.Format("SELECT {0} FROM (SELECT ROW_NUMBER() OVER (ORDER BY {2}) AS Row, {0} FROM {3} {4}) AS Paged ", columns, pageSize, orderBy, TableName, where);
  437
+            var pageStart = (currentPage - 1) * pageSize;
  438
+            sql += string.Format(" WHERE Row > {0} AND Row <={1}", pageStart, (pageStart + pageSize));
  439
+            countSQL += where;
  440
+            result.TotalRecords = Scalar(countSQL, args);
  441
+            result.TotalPages = result.TotalRecords / pageSize;
  442
+            if (result.TotalRecords % pageSize > 0)
  443
+                result.TotalPages += 1;
  444
+            result.Items = Query(string.Format(sql, columns, TableName), args);
  445
+            return result;
  446
+        }
  447
+        /// <summary>
  448
+        /// Returns a single row from the database
  449
+        /// </summary>
  450
+        public virtual dynamic Single(string where, params object[] args) {
  451
+            var sql = string.Format("SELECT * FROM {0} WHERE {1}", TableName, where);
  452
+            return Query(sql, args).FirstOrDefault();
  453
+        }
  454
+        /// <summary>
  455
+        /// Returns a single row from the database
  456
+        /// </summary>
  457
+        public virtual dynamic Single(object key, string columns = "*") {
  458
+            var sql = string.Format("SELECT {0} FROM {1} WHERE {2} = @0", columns, TableName, PrimaryKeyField);
  459
+            return Query(sql, key).FirstOrDefault();
  460
+        }
  461
+        /// <summary>
  462
+        /// A helpful query tool
  463
+        /// </summary>
  464
+        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) {
  465
+            //parse the method
  466
+            var constraints = new List<string>();
  467
+            var counter = 0;
  468
+            var info = binder.CallInfo;
  469
+            // accepting named args only... SKEET!
  470
+            if (info.ArgumentNames.Count != args.Length) {
  471
+                throw new InvalidOperationException("Please use named arguments for this type of query - the column name, orderby, columns, etc");
  472
+            }
  473
+
  474
+
  475
+            //first should be "FindBy, Last, Single, First"
  476
+            var op = binder.Name;
  477
+            var columns = " * ";
  478
+            string orderBy = string.Format(" ORDER BY {0}", PrimaryKeyField);
  479
+            string where = "";
  480
+            var whereArgs = new List<object>();
  481
+
  482
+            //loop the named args - see if we have order, columns and constraints
  483
+            if (info.ArgumentNames.Count > 0) {
  484
+
  485
+                for (int i = 0; i < args.Length; i++) {
  486
+                    var name = info.ArgumentNames[i].ToLower();
  487
+                    switch (name) {
  488
+                        case "orderby":
  489
+                            orderBy = " ORDER BY " + args[i];
  490
+                            break;
  491
+                        case "columns":
  492
+                            columns = args[i].ToString();
  493
+                            break;
  494
+                        default:
  495
+                            constraints.Add(string.Format(" {0} = @{1}", name, counter));
  496
+                            whereArgs.Add(args[i]);
  497
+                            counter++;
  498
+                            break;
  499
+                    }
  500
+                }
  501
+            }
  502
+            //Build the WHERE bits
  503
+            if (constraints.Count > 0) {
  504
+                where = " WHERE " + string.Join(" AND ", constraints.ToArray());
  505
+            }
  506
+            //build the SQL
  507
+            string sql = "SELECT TOP 1 " + columns + " FROM " + TableName + where;
  508
+            var justOne = op.StartsWith("First") || op.StartsWith("Last") || op.StartsWith("Get");
  509
+
  510
+            //Be sure to sort by DESC on the PK (PK Sort is the default)
  511
+            if (op.StartsWith("Last")) {
  512
+                orderBy = orderBy + " DESC ";
  513
+            } else {
  514
+                //default to multiple
  515
+                sql = "SELECT " + columns + " FROM " + TableName + where;
  516
+            }
  517
+
  518
+            if (justOne) {
  519
+                //return a single record
  520
+                result = Query(sql + orderBy, whereArgs.ToArray()).FirstOrDefault();
  521
+            } else {
  522
+                //return lots
  523
+                result = Query(sql + orderBy, whereArgs.ToArray());
  524
+            }
  525
+
  526
+            return true;
  527
+        }
  528
+    }
  529
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.