/// <summary> /// The mapping from a database record to data transfer object. /// </summary> /// <param name="reader"> /// An instance of the SQL reader. /// </param> /// <returns> /// A deserialized instance of <see cref="CDP4Common.DTO.NestedElement"/>. /// </returns> public virtual CDP4Common.DTO.NestedElement MapToDto(NpgsqlDataReader reader) { string tempModifiedOn; string tempIsVolatile; var valueDict = (Dictionary <string, string>)reader["ValueTypeSet"]; var iid = Guid.Parse(reader["Iid"].ToString()); var revisionNumber = int.Parse(valueDict["RevisionNumber"]); var dto = new CDP4Common.DTO.NestedElement(iid, revisionNumber); dto.ExcludedPerson.AddRange(Array.ConvertAll((string[])reader["ExcludedPerson"], Guid.Parse)); dto.ExcludedDomain.AddRange(Array.ConvertAll((string[])reader["ExcludedDomain"], Guid.Parse)); dto.RootElement = Guid.Parse(reader["RootElement"].ToString()); dto.ElementUsage.AddRange(Utils.ParseOrderedList <Guid>(reader["ElementUsage"] as string[, ])); dto.NestedParameter.AddRange(Array.ConvertAll((string[])reader["NestedParameter"], Guid.Parse)); if (valueDict.TryGetValue("ModifiedOn", out tempModifiedOn)) { dto.ModifiedOn = Utils.ParseUtcDate(tempModifiedOn); } if (valueDict.TryGetValue("IsVolatile", out tempIsVolatile)) { dto.IsVolatile = bool.Parse(tempIsVolatile); } return(dto); }
/// <summary> /// Insert a new database record from the supplied data transfer object. /// </summary> /// <param name="transaction"> /// The current <see cref="NpgsqlTransaction"/> to the database. /// </param> /// <param name="partition"> /// The database partition (schema) where the requested resource will be stored. /// </param> /// <param name="nestedElement"> /// The nestedElement DTO that is to be persisted. /// </param> /// <param name="container"> /// The container of the DTO to be persisted. /// </param> /// <returns> /// True if the concept was successfully persisted. /// </returns> public virtual bool Write(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.NestedElement nestedElement, CDP4Common.DTO.Thing container = null) { bool isHandled; var valueTypeDictionaryAdditions = new Dictionary <string, string>(); var beforeWrite = this.BeforeWrite(transaction, partition, nestedElement, container, out isHandled, valueTypeDictionaryAdditions); if (!isHandled) { beforeWrite = beforeWrite && base.Write(transaction, partition, nestedElement, container); var valueTypeDictionaryContents = new Dictionary <string, string> { { "IsVolatile", !this.IsDerived(nestedElement, "IsVolatile") ? nestedElement.IsVolatile.ToString() : string.Empty }, }.Concat(valueTypeDictionaryAdditions).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); using (var command = new NpgsqlCommand()) { var sqlBuilder = new System.Text.StringBuilder(); sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"NestedElement\"", partition); sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"RootElement\")"); sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :rootElement);"); command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = nestedElement.Iid; command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents; command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid; command.Parameters.Add("rootElement", NpgsqlDbType.Uuid).Value = !this.IsDerived(nestedElement, "RootElement") ? nestedElement.RootElement : Utils.NullableValue(null); command.CommandText = sqlBuilder.ToString(); command.Connection = transaction.Connection; command.Transaction = transaction; this.ExecuteAndLogCommand(command); } nestedElement.ElementUsage.ForEach(x => this.AddElementUsage(transaction, partition, nestedElement.Iid, x)); } return(this.AfterWrite(beforeWrite, transaction, partition, nestedElement, container)); }