Commits

Steve Klee committed 096fede

* bug for RemoveByID where always expected a document to already exist
* bug in performance when binding scalar values....not setting nvarchar properly

Comments (0)

Files changed (2)

BsonSql.Driver/BsonSqlCollection.cs

 			{
                 if (_logging)
                 {
-                    if (current.Contains("_id"))
+                    if (current != null && current.Contains("_id"))
                         MDC.Set("auditKey.BsonID", current["_id"].AsGuid.ToString());
+
                     MDC.Set("auditKey.Server", conn.DataSource);
                     MDC.Set("auditKey.Db", conn.Database);
 
-                    foreach (BsonElement e in current)
+                    if (current != null)
                     {
-                        MDC.Set("audit." + e.Name, e.ToJson());
+                        foreach (BsonElement e in current)
+                        {
+                            MDC.Set("audit." + e.Name, e.ToJson());
+                        }
                     }
                     _logger.Info(_logMask + ((forDelete) ? "Delete" : "Update"));
 
                     //have to wipe them out or else another thread gets them and may taint the results
                     MDC.Set("auditKey.BsonID", "");
-                    foreach (BsonElement e in current)
+
+                    if (current != null)
                     {
-                        MDC.Set("audit." + e.Name, "");
+                        foreach (BsonElement e in current)
+                        {
+                            MDC.Set("audit." + e.Name, "");
+                        }
                     }
                 }
 

BsonSql.Driver/Builders/Query.cs

             //and modify the parameter type to the correct type...by default SQL will use nvarchar and if you indexed
             //a field as varchar the resulting sql will cause an index scan because of the conversion, not allowing you
             //to fully leverage indexing...
-            
-            
+
+            Dictionary<string, bool> processedParameters = new Dictionary<string, bool>();
+
             foreach (KeyValuePair<string, BsonSqlFieldWrapper> kvp in _wrappedValuesForWhereClause)
             {
                 SqlParameter parameter = command.Parameters[kvp.Key];
-                
+                processedParameters[parameter.ParameterName] = true;    
                 BsonSqlFieldWrapper wrapper = kvp.Value;
                 IndexKey indexKey = null;
                 if (indexedKeys.TryGetValue(wrapper.FieldName, out indexKey))
                 }
             }
 
+            //for any not processed, which would be the static values, need to match those against the indexed keys as well
+            foreach (SqlParameter parameter in command.Parameters)
+            {
+                bool processed = false;
+                if (!processedParameters.TryGetValue(parameter.ParameterName, out processed))
+                {
+                    string fieldName = parameter.ParameterName.Substring(parameter.ParameterName.IndexOf('_') + 1).Replace("_", ".");
+                    IndexKey indexKey = null;
+                    if (indexedKeys.TryGetValue(fieldName, out indexKey))
+                    {
+                        switch (indexKey.DataType)
+                        {
+                            case BsonType.String:
+                            case BsonType.Array:
+                                parameter.SqlDbType = ((indexKey.UseVarChar) ? SqlDbType.VarChar : SqlDbType.NVarChar);
+                                break;
+                            case BsonType.Int32:
+                                parameter.SqlDbType = SqlDbType.Int;
+                                break;
+                            case BsonType.Int64:
+                                parameter.SqlDbType = SqlDbType.BigInt;
+                                break;
+                        }
+                    }
+                }
+            }
+
             return command;
         }
 	}