Commits

George Ribeiro committed a3b0054

refatorado Utils.Params.ObjectToDictionary para aceitar Hashtable como entrada

  • Participants
  • Parent commits a9cb6c2

Comments (0)

Files changed (3)

File FluentSql.Test/QueryInsert.cs

 using System.Text;
 using NUnit.Framework;
 using FluentSql.Command;
+using System.Collections;
 
 namespace FluentSql.Test
 {
         {
             ITable users = new Table("users");
             users.Insert(new { nome = "george", data_nascimento = new DateTime(1989, 8, 22), sexo = "M" });
-            Assert.AreEqual("INSERT INTO users(nome, data_nascimento, sexo) VALUES(@users_nome_1, @users_data_nascimento_1, @users_sexo_1)", users.ToSql());
+            string sql_expected = "INSERT INTO users(nome, data_nascimento, sexo) VALUES(@users_nome_1, @users_data_nascimento_1, @users_sexo_1)";
+            Assert.AreEqual(sql_expected, users.ToSql());
+        }
+
+        [Test]
+        public void Insert_Usando_Hashtable()
+        {
+            Hashtable hs = new Hashtable();
+            hs.Add("nome", "george");
+            hs.Add("data_nascimento", new DateTime(1989, 8, 22));
+            hs.Add("sexo", "M");
+            var t_users = new Table("users");
+            t_users.Insert(hs);
+            string sql_expected = "INSERT INTO users(data_nascimento, nome, sexo) VALUES(@users_data_nascimento_1, @users_nome_1, @users_sexo_1)";
+            Assert.AreEqual(sql_expected, t_users.ToSql());
         }
     }
 }

File FluentSql.Test/QueryUpdate.cs

 using System.Text;
 using NUnit.Framework;
 using FluentSql.Command;
+using System.Collections;
 
 namespace FluentSql.Test
 {
             string sql_expected = "UPDATE users SET ativo=@users_ativo_1, salario=@users_salario_1 WHERE users.idade > @users_idade_1";
             Assert.AreEqual(sql_expected, users.ToSql());
         }
+
+        [Test]
+        public void Update_Com_Where_E_Hashtable()
+        {
+            var users = new Table("users");
+            Hashtable hs = new Hashtable();
+            hs.Add("superuser", true);
+            hs.Add("ativo", true);
+            users.Where(users["salario"] > 2000)
+                .Update(hs);
+            string sql_expected = "UPDATE users SET ativo=@users_ativo_1, superuser=@users_superuser_1 WHERE users.salario > @users_salario_1";
+            Assert.AreEqual(sql_expected, users.ToSql());
+        }
     }
 }

File FluentSql/Utils/Params.cs

 using System.Text;
 using System.Reflection;
 using System.ComponentModel;
+using System.Collections;
 
 namespace FluentSql.Utils
 {
         public static IDictionary<string, object> ObjectToDicionary(object o)
         {
             var result = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
-            if (o != null)
+            if (o == null)
+            {
+                return result;
+            }
+            if (o is Hashtable)
+            {
+                foreach (DictionaryEntry de in (Hashtable)o)
+                {
+                    result.Add(de.Key.ToString(), de.Value);
+                }
+            }
+            else
             {
                 foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(o))
                 {
                     result.Add(prop.Name, prop.GetValue(o));
-                }    
+                }
             }
             
             return result;