/// <summary> /// Converts a JObject to a series of assignment objects, set to this document. /// </summary> /// <param name="data">The data to convert</param> /// <returns>this</returns> public IDatabaseObject FromJObject(JObject data) { foreach (var property in data) { var value = property.Value as JValue; var obj = property.Value as JObject; var array = property.Value as JArray; if (value != null) { this.Set(property.Key, value.Value); } else if (obj != null) { DocumentValue document = new DocumentValue(); document.FromJObject(obj); this.Set(property.Key, document.ToObject()); } else if (array != null) { CollectionValue collection = new CollectionValue(); collection.FromJObject(array); this.Set(property.Key, collection.ToObject()); } } return this; }
/// <summary> /// Query a SELECT statement which returns results /// </summary> /// <param name="query"></param> /// <param name="result"></param> /// <returns></returns> protected CollectionValue Read(ICompiledQuery query, CollectionValue result) { if (this.Connection != null && this.Connection.State == ConnectionState.Open) { using (IDbCommand command = this.Connection.CreateCommand()) { command.CommandText = query.Compiled.FirstOrDefault(); using (IDataReader reader = command.ExecuteReader()) { if (reader != null) { while (reader.Read() == true) { DocumentValue row = new DocumentValue(); for (int field = 0; field < reader.FieldCount; field++) { row.Set(reader.GetName(field), reader.GetValue(field)); } result.Add(row); } } } } } return result; }
/// <summary> /// Converts a BsonDocument to a DocumentValue (known to Potato) /// </summary> /// <param name="document"></param> /// <returns></returns> protected DocumentValue ToDocument(BsonDocument document) { DocumentValue row = new DocumentValue(); foreach (BsonElement value in document.Elements) { var dotNetValue = BsonTypeMapper.MapToDotNetValue(value.Value); if (dotNetValue is ObjectId) { dotNetValue = dotNetValue.ToString(); } row.Set(value.Name, dotNetValue); } return row; }
/// <summary> /// Parse field assignments, similar to conditions, but without the conditionals. /// </summary> /// <param name="query"></param> /// <returns></returns> protected virtual List<String> ParseAssignments(IDatabaseObject query) { DocumentValue document = new DocumentValue(); document.AddRange(query.Where(statement => statement is Assignment)); return new List<String>() { new JArray() { new JObject() { new JProperty("$set", document.ToJObject()) } }.ToString(Formatting.None) }; }