/// <summary>
        /// Update a 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 updated.
        /// </param>
        /// <param name="parameterSubscriptionValueSet">
        /// The ParameterSubscriptionValueSet DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.ParameterSubscriptionValueSet parameterSubscriptionValueSet, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, parameterSubscriptionValueSet, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, parameterSubscriptionValueSet, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Manual", !this.IsDerived(parameterSubscriptionValueSet, "Manual") ? parameterSubscriptionValueSet.Manual.ToHstoreString() : string.Empty },
                    { "ValueSwitch", !this.IsDerived(parameterSubscriptionValueSet, "ValueSwitch") ? parameterSubscriptionValueSet.ValueSwitch.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("UPDATE \"{0}\".\"ParameterSubscriptionValueSet\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"SubscribedValueSet\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :subscribedValueSet)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = parameterSubscriptionValueSet.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("subscribedValueSet", NpgsqlDbType.Uuid).Value    = !this.IsDerived(parameterSubscriptionValueSet, "SubscribedValueSet") ? parameterSubscriptionValueSet.SubscribedValueSet : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, parameterSubscriptionValueSet, container));
        }
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="modellingAnnotationItem">
        /// The modellingAnnotationItem 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.ModellingAnnotationItem modellingAnnotationItem, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, modellingAnnotationItem, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, modellingAnnotationItem, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Status", !this.IsDerived(modellingAnnotationItem, "Status") ? modellingAnnotationItem.Status.ToString() : string.Empty },
                    { "Title", !this.IsDerived(modellingAnnotationItem, "Title") ? modellingAnnotationItem.Title.Escape() : string.Empty },
                    { "Classification", !this.IsDerived(modellingAnnotationItem, "Classification") ? modellingAnnotationItem.Classification.ToString() : string.Empty },
                    { "ShortName", !this.IsDerived(modellingAnnotationItem, "ShortName") ? modellingAnnotationItem.ShortName.Escape() : 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}\".\"ModellingAnnotationItem\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"Owner\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :owner);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = modellingAnnotationItem.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value = !this.IsDerived(modellingAnnotationItem, "Owner") ? modellingAnnotationItem.Owner : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }

                modellingAnnotationItem.SourceAnnotation.ForEach(x => this.AddSourceAnnotation(transaction, partition, modellingAnnotationItem.Iid, x));
                modellingAnnotationItem.Category.ForEach(x => this.AddCategory(transaction, partition, modellingAnnotationItem.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, modellingAnnotationItem, container));
        }
Esempio n. 3
0
        /// <summary>
        /// Update a 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 updated.
        /// </param>
        /// <param name="referenceSource">
        /// The ReferenceSource DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.ReferenceSource referenceSource, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, referenceSource, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, referenceSource, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Author", !this.IsDerived(referenceSource, "Author") ? referenceSource.Author.Escape() : null },
                    { "IsDeprecated", !this.IsDerived(referenceSource, "IsDeprecated") ? referenceSource.IsDeprecated.ToString() : string.Empty },
                    { "Language", !this.IsDerived(referenceSource, "Language") ? referenceSource.Language.Escape() : null },
                    { "PublicationYear", !this.IsDerived(referenceSource, "PublicationYear") && referenceSource.PublicationYear.HasValue ? referenceSource.PublicationYear.Value.ToString() : null },
                    { "VersionDate", !this.IsDerived(referenceSource, "VersionDate") && referenceSource.VersionDate.HasValue ? referenceSource.VersionDate.Value.ToString(Utils.DateTimeUtcSerializationFormat) : null },
                    { "VersionIdentifier", !this.IsDerived(referenceSource, "VersionIdentifier") ? referenceSource.VersionIdentifier.Escape() : null },
                }.Concat(valueTypeDictionaryAdditions).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();
                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"ReferenceSource\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"PublishedIn\", \"Publisher\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :publishedIn, :publisher)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = referenceSource.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("publishedIn", NpgsqlDbType.Uuid).Value           = !this.IsDerived(referenceSource, "PublishedIn") ? Utils.NullableValue(referenceSource.PublishedIn) : Utils.NullableValue(null);
                    command.Parameters.Add("publisher", NpgsqlDbType.Uuid).Value             = !this.IsDerived(referenceSource, "Publisher") ? Utils.NullableValue(referenceSource.Publisher) : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, referenceSource, container));
        }
        /// <summary>
        /// Update a database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be updated.
        /// </param>
        /// <param name="nestedParameter">
        /// The nestedParameter DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.NestedParameter nestedParameter, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, nestedParameter, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, nestedParameter, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Formula", !this.IsDerived(nestedParameter, "Formula") ? nestedParameter.Formula.Escape() : string.Empty },
                    { "ActualValue", !this.IsDerived(nestedParameter, "ActualValue") ? nestedParameter.ActualValue.Escape() : string.Empty },
                    { "IsVolatile", !this.IsDerived(nestedParameter, "IsVolatile") ? nestedParameter.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("UPDATE \"{0}\".\"NestedParameter\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"AssociatedParameter\", \"ActualState\", \"Owner\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" = (:container, :associatedParameter, :actualState, :owner, \"ValueTypeDictionary\" || :valueTypeDictionary)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = nestedParameter.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("associatedParameter", NpgsqlDbType.Uuid).Value   = !this.IsDerived(nestedParameter, "AssociatedParameter") ? nestedParameter.AssociatedParameter : Utils.NullableValue(null);
                    command.Parameters.Add("actualState", NpgsqlDbType.Uuid).Value           = !this.IsDerived(nestedParameter, "ActualState") ? Utils.NullableValue(nestedParameter.ActualState) : Utils.NullableValue(null);
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value                 = !this.IsDerived(nestedParameter, "Owner") ? nestedParameter.Owner : Utils.NullableValue(null);
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, nestedParameter, container));
        }
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="fileRevision">
        /// The fileRevision 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.FileRevision fileRevision, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, fileRevision, container);

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "ContentHash", fileRevision.ContentHash.Escape() },
                { "CreatedOn", !this.IsDerived(fileRevision, "CreatedOn") ? fileRevision.CreatedOn.ToString(Utils.DateTimeUtcSerializationFormat) : string.Empty },
                { "Name", !this.IsDerived(fileRevision, "Name") ? fileRevision.Name.Escape() : 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}\".\"FileRevision\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"ContainingFolder\", \"Creator\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :containingFolder, :creator)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = fileRevision.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                command.Parameters.Add("containingFolder", NpgsqlDbType.Uuid).Value      = !this.IsDerived(fileRevision, "ContainingFolder") ? Utils.NullableValue(fileRevision.ContainingFolder) : Utils.NullableValue(null);
                command.Parameters.Add("creator", NpgsqlDbType.Uuid).Value = !this.IsDerived(fileRevision, "Creator") ? fileRevision.Creator : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"Container\", \"ContainingFolder\", \"Creator\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :container, :containingFolder, :creator);");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }
            fileRevision.FileType.ForEach(x => this.UpsertFileType(transaction, partition, fileRevision.Iid, x));

            return(true);
        }
        /// <summary>
        /// Update a database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be updated.
        /// </param>
        /// <param name="changeProposal">
        /// The changeProposal DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.ChangeProposal changeProposal, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, changeProposal, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, changeProposal, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"ChangeProposal\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ChangeRequest\")");
                    sqlBuilder.AppendFormat(" = (:changeRequest)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value           = changeProposal.Iid;
                    command.Parameters.Add("changeRequest", NpgsqlDbType.Uuid).Value = !this.IsDerived(changeProposal, "ChangeRequest") ? changeProposal.ChangeRequest : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, changeProposal, container));
        }
        /// <summary>
        /// Update a 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 updated.
        /// </param>
        /// <param name="userRuleVerification">
        /// The UserRuleVerification DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.UserRuleVerification userRuleVerification, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, userRuleVerification, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, userRuleVerification, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();
                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"UserRuleVerification\"", partition);
                    sqlBuilder.AppendFormat(" SET \"Rule\"");
                    sqlBuilder.AppendFormat(" = :rule");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value  = userRuleVerification.Iid;
                    command.Parameters.Add("rule", NpgsqlDbType.Uuid).Value = !this.IsDerived(userRuleVerification, "Rule") ? userRuleVerification.Rule : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, userRuleVerification, container));
        }
Esempio n. 8
0
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="parameterGroup">
        /// The parameterGroup 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.ParameterGroup parameterGroup, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, parameterGroup, container);

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "Name", !this.IsDerived(parameterGroup, "Name") ? parameterGroup.Name.Escape() : 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}\".\"ParameterGroup\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"ContainingGroup\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :containingGroup)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = parameterGroup.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                command.Parameters.Add("containingGroup", NpgsqlDbType.Uuid).Value       = !this.IsDerived(parameterGroup, "ContainingGroup") ? Utils.NullableValue(parameterGroup.ContainingGroup) : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"Container\", \"ContainingGroup\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :container, :containingGroup);");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
Esempio n. 9
0
        /// <summary>
        /// Update a 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 updated.
        /// </param>
        /// <param name="organizationalParticipant">
        /// The OrganizationalParticipant DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.OrganizationalParticipant organizationalParticipant, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, organizationalParticipant, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, organizationalParticipant, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();
                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"OrganizationalParticipant\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"Organization\")");
                    sqlBuilder.AppendFormat(" = (:container, :organization)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value          = organizationalParticipant.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value    = container.Iid;
                    command.Parameters.Add("organization", NpgsqlDbType.Uuid).Value = !this.IsDerived(organizationalParticipant, "Organization") ? organizationalParticipant.Organization : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, organizationalParticipant, container));
        }
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="requirement">
        /// The requirement 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.Requirement requirement, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, requirement, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, requirement, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "IsDeprecated", !this.IsDerived(requirement, "IsDeprecated") ? requirement.IsDeprecated.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}\".\"Requirement\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"Group\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :group);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = requirement.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("group", NpgsqlDbType.Uuid).Value = !this.IsDerived(requirement, "Group") ? Utils.NullableValue(requirement.Group) : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }

                requirement.Category.ForEach(x => this.AddCategory(transaction, partition, requirement.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, requirement, container));
        }
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="quantityKind">
        /// The quantityKind 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.QuantityKind quantityKind, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, quantityKind, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, quantityKind, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "QuantityDimensionSymbol", !this.IsDerived(quantityKind, "QuantityDimensionSymbol") ? quantityKind.QuantityDimensionSymbol.Escape() : null },
                }.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}\".\"QuantityKind\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"DefaultScale\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :defaultScale);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = quantityKind.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("defaultScale", NpgsqlDbType.Uuid).Value          = !this.IsDerived(quantityKind, "DefaultScale") ? quantityKind.DefaultScale : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }

                quantityKind.PossibleScale.ForEach(x => this.AddPossibleScale(transaction, partition, quantityKind.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, quantityKind, container));
        }
Esempio n. 12
0
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="unitFactor">
        /// The unitFactor DTO that is to be persisted.
        /// </param>
        /// <param name="sequence">
        /// The order sequence used to persist this instance.
        /// </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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.UnitFactor unitFactor, long sequence, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, unitFactor, container);

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "Exponent", !this.IsDerived(unitFactor, "Exponent") ? unitFactor.Exponent.Escape() : 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}\".\"UnitFactor\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Sequence\", \"Container\", \"Unit\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :sequence, :container, :unit)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = unitFactor.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                command.Parameters.Add("sequence", NpgsqlDbType.Bigint).Value            = sequence;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                command.Parameters.Add("unit", NpgsqlDbType.Uuid).Value = !this.IsDerived(unitFactor, "Unit") ? unitFactor.Unit : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"Container\", \"Unit\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :container, :unit);");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
        /// <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="thingReference">
        /// The thingReference 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.ThingReference thingReference, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, thingReference, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, thingReference, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "ReferencedRevisionNumber", !this.IsDerived(thingReference, "ReferencedRevisionNumber") ? thingReference.ReferencedRevisionNumber.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}\".\"ThingReference\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"ReferencedThing\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :referencedThing);");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = thingReference.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("referencedThing", NpgsqlDbType.Uuid).Value       = !this.IsDerived(thingReference, "ReferencedThing") ? thingReference.ReferencedThing : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, thingReference, container));
        }
Esempio n. 14
0
        /// <summary>
        /// Update a database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be updated.
        /// </param>
        /// <param name="stakeHolderValueMapSettings">
        /// The stakeHolderValueMapSettings DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.StakeHolderValueMapSettings stakeHolderValueMapSettings, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, stakeHolderValueMapSettings, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, stakeHolderValueMapSettings, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"StakeHolderValueMapSettings\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"GoalToValueGroupRelationship\", \"ValueGroupToStakeholderValueRelationship\", \"StakeholderValueToRequirementRelationship\")");
                    sqlBuilder.AppendFormat(" = (:container, :goalToValueGroupRelationship, :valueGroupToStakeholderValueRelationship, :stakeholderValueToRequirementRelationship)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = stakeHolderValueMapSettings.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid;
                    command.Parameters.Add("goalToValueGroupRelationship", NpgsqlDbType.Uuid).Value              = !this.IsDerived(stakeHolderValueMapSettings, "GoalToValueGroupRelationship") ? Utils.NullableValue(stakeHolderValueMapSettings.GoalToValueGroupRelationship) : Utils.NullableValue(null);
                    command.Parameters.Add("valueGroupToStakeholderValueRelationship", NpgsqlDbType.Uuid).Value  = !this.IsDerived(stakeHolderValueMapSettings, "ValueGroupToStakeholderValueRelationship") ? Utils.NullableValue(stakeHolderValueMapSettings.ValueGroupToStakeholderValueRelationship) : Utils.NullableValue(null);
                    command.Parameters.Add("stakeholderValueToRequirementRelationship", NpgsqlDbType.Uuid).Value = !this.IsDerived(stakeHolderValueMapSettings, "StakeholderValueToRequirementRelationship") ? Utils.NullableValue(stakeHolderValueMapSettings.StakeholderValueToRequirementRelationship) : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, stakeHolderValueMapSettings, container));
        }
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="elementBase">
        /// The elementBase 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.ElementBase elementBase, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, elementBase, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, elementBase, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"ElementBase\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"Owner\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :owner);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value   = elementBase.Iid;
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value = !this.IsDerived(elementBase, "Owner") ? elementBase.Owner : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }

                elementBase.Category.ForEach(x => this.AddCategory(transaction, partition, elementBase.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, elementBase, container));
        }
Esempio n. 16
0
        /// <summary>
        /// Update a database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be updated.
        /// </param>
        /// <param name="unitFactor">
        /// The unitFactor DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.UnitFactor unitFactor, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, unitFactor, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, unitFactor, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "Exponent", !this.IsDerived(unitFactor, "Exponent") ? unitFactor.Exponent.Escape() : string.Empty },
                }.Concat(valueTypeDictionaryAdditions).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"UnitFactor\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"Unit\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" = (:container, :unit, \"ValueTypeDictionary\" || :valueTypeDictionary)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = unitFactor.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("unit", NpgsqlDbType.Uuid).Value                  = !this.IsDerived(unitFactor, "Unit") ? unitFactor.Unit : Utils.NullableValue(null);
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, unitFactor, container));
        }
Esempio n. 17
0
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="actualFiniteStateList">
        /// The actualFiniteStateList 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.ActualFiniteStateList actualFiniteStateList, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, actualFiniteStateList, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, actualFiniteStateList, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"ActualFiniteStateList\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"Container\", \"Owner\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :container, :owner);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = actualFiniteStateList.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid;
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value     = !this.IsDerived(actualFiniteStateList, "Owner") ? actualFiniteStateList.Owner : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }

                actualFiniteStateList.PossibleFiniteStateList.ForEach(x => this.AddPossibleFiniteStateList(transaction, partition, actualFiniteStateList.Iid, x));
                actualFiniteStateList.ExcludeOption.ForEach(x => this.AddExcludeOption(transaction, partition, actualFiniteStateList.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, actualFiniteStateList, container));
        }
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="ruleVerificationList">
        /// The ruleVerificationList 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.RuleVerificationList ruleVerificationList, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, ruleVerificationList, container);

            using (var command = new NpgsqlCommand())
            {
                var sqlBuilder = new System.Text.StringBuilder();

                sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"RuleVerificationList\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"Container\", \"Owner\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :container, :owner)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = ruleVerificationList.Iid;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid;
                command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value     = !this.IsDerived(ruleVerificationList, "Owner") ? ruleVerificationList.Owner : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"Container\", \"Owner\")");
                sqlBuilder.Append(" = (:container, :owner);");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="userRuleVerification">
        /// The userRuleVerification 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.UserRuleVerification userRuleVerification, long sequence, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, userRuleVerification, sequence, container);

            using (var command = new NpgsqlCommand())
            {
                var sqlBuilder = new System.Text.StringBuilder();

                sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"UserRuleVerification\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"Rule\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :rule)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value  = userRuleVerification.Iid;
                command.Parameters.Add("rule", NpgsqlDbType.Uuid).Value = !this.IsDerived(userRuleVerification, "Rule") ? userRuleVerification.Rule : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET \"Rule\"");
                sqlBuilder.Append(" = :rule;");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
Esempio n. 20
0
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="section">
        /// The section DTO that is to be persisted.
        /// </param>
        /// <param name="sequence">
        /// The order sequence used to persist this instance.
        /// </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.Section section, long sequence, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, section, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, section, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "ShortName", !this.IsDerived(section, "ShortName") ? section.ShortName.Escape() : string.Empty },
                    { "Name", !this.IsDerived(section, "Name") ? section.Name.Escape() : string.Empty },
                    { "CreatedOn", !this.IsDerived(section, "CreatedOn") ? section.CreatedOn.ToString(Utils.DateTimeUtcSerializationFormat) : 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}\".\"Section\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Sequence\", \"Container\", \"Owner\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :sequence, :container, :owner);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = section.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("sequence", NpgsqlDbType.Bigint).Value            = sequence;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("owner", NpgsqlDbType.Uuid).Value = !this.IsDerived(section, "Owner") ? section.Owner : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }

                section.Category.ForEach(x => this.AddCategory(transaction, partition, section.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, section, container));
        }
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="multiRelationshipRule">
        /// The multiRelationshipRule 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.MultiRelationshipRule multiRelationshipRule, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, multiRelationshipRule, container);

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "MaxRelated", !this.IsDerived(multiRelationshipRule, "MaxRelated") ? multiRelationshipRule.MaxRelated.ToString() : string.Empty },
                { "MinRelated", !this.IsDerived(multiRelationshipRule, "MinRelated") ? multiRelationshipRule.MinRelated.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}\".\"MultiRelationshipRule\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"RelationshipCategory\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :relationshipCategory)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = multiRelationshipRule.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                command.Parameters.Add("relationshipCategory", NpgsqlDbType.Uuid).Value  = !this.IsDerived(multiRelationshipRule, "RelationshipCategory") ? multiRelationshipRule.RelationshipCategory : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"RelationshipCategory\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :relationshipCategory);");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }
            multiRelationshipRule.RelatedCategory.ForEach(x => this.UpsertRelatedCategory(transaction, partition, multiRelationshipRule.Iid, x));

            return(true);
        }
        /// <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="siteDirectoryDataAnnotation">
        /// The siteDirectoryDataAnnotation 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.SiteDirectoryDataAnnotation siteDirectoryDataAnnotation, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, siteDirectoryDataAnnotation, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, siteDirectoryDataAnnotation, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"SiteDirectoryDataAnnotation\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"Container\", \"Author\", \"PrimaryAnnotatedThing\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :container, :author, :primaryAnnotatedThing);");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = siteDirectoryDataAnnotation.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("author", NpgsqlDbType.Uuid).Value                = !this.IsDerived(siteDirectoryDataAnnotation, "Author") ? siteDirectoryDataAnnotation.Author : Utils.NullableValue(null);
                    command.Parameters.Add("primaryAnnotatedThing", NpgsqlDbType.Uuid).Value = !this.IsDerived(siteDirectoryDataAnnotation, "PrimaryAnnotatedThing") ? siteDirectoryDataAnnotation.PrimaryAnnotatedThing : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, siteDirectoryDataAnnotation, container));
        }
Esempio n. 23
0
        /// <summary>
        /// Insert a new database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="modelLogEntry">
        /// The modelLogEntry 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.ModelLogEntry modelLogEntry, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeWrite = this.BeforeWrite(transaction, partition, modelLogEntry, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeWrite = beforeWrite && base.Write(transaction, partition, modelLogEntry, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "LanguageCode", !this.IsDerived(modelLogEntry, "LanguageCode") ? modelLogEntry.LanguageCode.Escape() : string.Empty },
                    { "Content", !this.IsDerived(modelLogEntry, "Content") ? modelLogEntry.Content.Escape() : string.Empty },
                    { "CreatedOn", !this.IsDerived(modelLogEntry, "CreatedOn") ? modelLogEntry.CreatedOn.ToString(Utils.DateTimeUtcSerializationFormat) : string.Empty },
                    { "Level", !this.IsDerived(modelLogEntry, "Level") ? modelLogEntry.Level.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}\".\"ModelLogEntry\"", partition);
                    sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"Author\")");
                    sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :author);");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = modelLogEntry.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("author", NpgsqlDbType.Uuid).Value = !this.IsDerived(modelLogEntry, "Author") ? Utils.NullableValue(modelLogEntry.Author) : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }

                modelLogEntry.AffectedItemIid.ForEach(x => this.AddAffectedItemIid(transaction, partition, modelLogEntry.Iid, x));
                modelLogEntry.Category.ForEach(x => this.AddCategory(transaction, partition, modelLogEntry.Iid, x));
            }

            return(this.AfterWrite(beforeWrite, transaction, partition, modelLogEntry, container));
        }
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="siteDirectoryDataAnnotation">
        /// The siteDirectoryDataAnnotation 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.SiteDirectoryDataAnnotation siteDirectoryDataAnnotation, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, siteDirectoryDataAnnotation, container);

            using (var command = new NpgsqlCommand())
            {
                var sqlBuilder = new System.Text.StringBuilder();

                sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"SiteDirectoryDataAnnotation\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"Container\", \"Author\", \"PrimaryAnnotatedThing\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :container, :author, :primaryAnnotatedThing)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value                   = siteDirectoryDataAnnotation.Iid;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                command.Parameters.Add("author", NpgsqlDbType.Uuid).Value                = !this.IsDerived(siteDirectoryDataAnnotation, "Author") ? siteDirectoryDataAnnotation.Author : Utils.NullableValue(null);
                command.Parameters.Add("primaryAnnotatedThing", NpgsqlDbType.Uuid).Value = !this.IsDerived(siteDirectoryDataAnnotation, "PrimaryAnnotatedThing") ? siteDirectoryDataAnnotation.PrimaryAnnotatedThing : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"Container\", \"Author\", \"PrimaryAnnotatedThing\")");
                sqlBuilder.Append(" = (:container, :author, :primaryAnnotatedThing);");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
        /// <summary>
        /// Update a 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 updated.
        /// </param>
        /// <param name="fileRevision">
        /// The FileRevision DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.FileRevision fileRevision, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, fileRevision, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, fileRevision, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "ContentHash", fileRevision.ContentHash.Escape() },
                    { "CreatedOn", !this.IsDerived(fileRevision, "CreatedOn") ? fileRevision.CreatedOn.ToString(Utils.DateTimeUtcSerializationFormat) : string.Empty },
                    { "Name", !this.IsDerived(fileRevision, "Name") ? fileRevision.Name.Escape() : string.Empty },
                }.Concat(valueTypeDictionaryAdditions).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();
                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"FileRevision\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"ContainingFolder\", \"Creator\")");
                    sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :containingFolder, :creator)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = fileRevision.Iid;
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                    command.Parameters.Add("containingFolder", NpgsqlDbType.Uuid).Value      = !this.IsDerived(fileRevision, "ContainingFolder") ? Utils.NullableValue(fileRevision.ContainingFolder) : Utils.NullableValue(null);
                    command.Parameters.Add("creator", NpgsqlDbType.Uuid).Value = !this.IsDerived(fileRevision, "Creator") ? fileRevision.Creator : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, fileRevision, container));
        }
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="engineeringModelDataDiscussionItem">
        /// The engineeringModelDataDiscussionItem 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.EngineeringModelDataDiscussionItem engineeringModelDataDiscussionItem, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, engineeringModelDataDiscussionItem, container);

            using (var command = new NpgsqlCommand())
            {
                var sqlBuilder = new System.Text.StringBuilder();

                sqlBuilder.AppendFormat("INSERT INTO \"{0}\".\"EngineeringModelDataDiscussionItem\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"Container\", \"Author\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :container, :author)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = engineeringModelDataDiscussionItem.Iid;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid;
                command.Parameters.Add("author", NpgsqlDbType.Uuid).Value    = !this.IsDerived(engineeringModelDataDiscussionItem, "Author") ? engineeringModelDataDiscussionItem.Author : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"Container\", \"Author\")");
                sqlBuilder.Append(" = (:container, :author);");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }

            return(true);
        }
Esempio n. 27
0
        /// <summary>
        /// Insert a new database record, or updates one if it already exists from the supplied data transfer object.
        /// This is typically used during the import of existing data to the Database.
        /// </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="referenceSource">
        /// The referenceSource 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 Upsert(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.ReferenceSource referenceSource, CDP4Common.DTO.Thing container = null)
        {
            var valueTypeDictionaryAdditions = new Dictionary <string, string>();

            base.Upsert(transaction, partition, referenceSource, container);

            var valueTypeDictionaryContents = new Dictionary <string, string>
            {
                { "Author", !this.IsDerived(referenceSource, "Author") ? referenceSource.Author.Escape() : null },
                { "IsDeprecated", !this.IsDerived(referenceSource, "IsDeprecated") ? referenceSource.IsDeprecated.ToString() : string.Empty },
                { "Language", !this.IsDerived(referenceSource, "Language") ? referenceSource.Language.Escape() : null },
                { "PublicationYear", !this.IsDerived(referenceSource, "PublicationYear") && referenceSource.PublicationYear.HasValue ? referenceSource.PublicationYear.Value.ToString() : null },
                { "VersionDate", !this.IsDerived(referenceSource, "VersionDate") && referenceSource.VersionDate.HasValue ? referenceSource.VersionDate.Value.ToString(Utils.DateTimeUtcSerializationFormat) : null },
                { "VersionIdentifier", !this.IsDerived(referenceSource, "VersionIdentifier") ? referenceSource.VersionIdentifier.Escape() : null },
            }.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}\".\"ReferenceSource\"", partition);
                sqlBuilder.AppendFormat(" (\"Iid\", \"ValueTypeDictionary\", \"Container\", \"PublishedIn\", \"Publisher\")");
                sqlBuilder.AppendFormat(" VALUES (:iid, :valueTypeDictionary, :container, :publishedIn, :publisher)");

                command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = referenceSource.Iid;
                command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;
                command.Parameters.Add("container", NpgsqlDbType.Uuid).Value             = container.Iid;
                command.Parameters.Add("publishedIn", NpgsqlDbType.Uuid).Value           = !this.IsDerived(referenceSource, "PublishedIn") ? Utils.NullableValue(referenceSource.PublishedIn) : Utils.NullableValue(null);
                command.Parameters.Add("publisher", NpgsqlDbType.Uuid).Value             = !this.IsDerived(referenceSource, "Publisher") ? Utils.NullableValue(referenceSource.Publisher) : Utils.NullableValue(null);
                sqlBuilder.Append(" ON CONFLICT (\"Iid\")");
                sqlBuilder.Append(" DO UPDATE ");
                sqlBuilder.Append(" SET (\"ValueTypeDictionary\", \"Container\", \"PublishedIn\", \"Publisher\")");
                sqlBuilder.Append(" = (:valueTypeDictionary, :container, :publishedIn, :publisher);");

                command.CommandText = sqlBuilder.ToString();
                command.Connection  = transaction.Connection;
                command.Transaction = transaction;

                this.ExecuteAndLogCommand(command);
            }
            referenceSource.Category.ForEach(x => this.UpsertCategory(transaction, partition, referenceSource.Iid, x));

            return(true);
        }
        /// <summary>
        /// Update a 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 updated.
        /// </param>
        /// <param name="engineeringModelDataDiscussionItem">
        /// The EngineeringModelDataDiscussionItem DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.EngineeringModelDataDiscussionItem engineeringModelDataDiscussionItem, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, engineeringModelDataDiscussionItem, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, engineeringModelDataDiscussionItem, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();
                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"EngineeringModelDataDiscussionItem\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"Author\")");
                    sqlBuilder.AppendFormat(" = (:container, :author)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");

                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value       = engineeringModelDataDiscussionItem.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid;
                    command.Parameters.Add("author", NpgsqlDbType.Uuid).Value    = !this.IsDerived(engineeringModelDataDiscussionItem, "Author") ? engineeringModelDataDiscussionItem.Author : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;

                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, engineeringModelDataDiscussionItem, container));
        }
Esempio n. 29
0
        /// <summary>
        /// Update a database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be updated.
        /// </param>
        /// <param name="binaryRelationshipRule">
        /// The binaryRelationshipRule DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.BinaryRelationshipRule binaryRelationshipRule, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, binaryRelationshipRule, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, binaryRelationshipRule, container);

                var valueTypeDictionaryContents = new Dictionary <string, string>
                {
                    { "ForwardRelationshipName", !this.IsDerived(binaryRelationshipRule, "ForwardRelationshipName") ? binaryRelationshipRule.ForwardRelationshipName.Escape() : string.Empty },
                    { "InverseRelationshipName", !this.IsDerived(binaryRelationshipRule, "InverseRelationshipName") ? binaryRelationshipRule.InverseRelationshipName.Escape() : string.Empty },
                }.Concat(valueTypeDictionaryAdditions).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"BinaryRelationshipRule\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"RelationshipCategory\", \"SourceCategory\", \"TargetCategory\", \"ValueTypeDictionary\")");
                    sqlBuilder.AppendFormat(" = (:relationshipCategory, :sourceCategory, :targetCategory, \"ValueTypeDictionary\" || :valueTypeDictionary)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = binaryRelationshipRule.Iid;
                    command.Parameters.Add("relationshipCategory", NpgsqlDbType.Uuid).Value  = !this.IsDerived(binaryRelationshipRule, "RelationshipCategory") ? binaryRelationshipRule.RelationshipCategory : Utils.NullableValue(null);
                    command.Parameters.Add("sourceCategory", NpgsqlDbType.Uuid).Value        = !this.IsDerived(binaryRelationshipRule, "SourceCategory") ? binaryRelationshipRule.SourceCategory : Utils.NullableValue(null);
                    command.Parameters.Add("targetCategory", NpgsqlDbType.Uuid).Value        = !this.IsDerived(binaryRelationshipRule, "TargetCategory") ? binaryRelationshipRule.TargetCategory : Utils.NullableValue(null);
                    command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents;

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, binaryRelationshipRule, container));
        }
        /// <summary>
        /// Update a database record from the supplied data transfer object.
        /// </summary>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be updated.
        /// </param>
        /// <param name="diagramElementThing">
        /// The diagramElementThing DTO that is to be updated.
        /// </param>
        /// <param name="container">
        /// The container of the DTO to be updated.
        /// </param>
        /// <returns>
        /// True if the concept was successfully updated.
        /// </returns>
        public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.DiagramElementThing diagramElementThing, CDP4Common.DTO.Thing container = null)
        {
            bool isHandled;
            var  valueTypeDictionaryAdditions = new Dictionary <string, string>();
            var  beforeUpdate = this.BeforeUpdate(transaction, partition, diagramElementThing, container, out isHandled, valueTypeDictionaryAdditions);

            if (!isHandled)
            {
                beforeUpdate = beforeUpdate && base.Update(transaction, partition, diagramElementThing, container);

                using (var command = new NpgsqlCommand())
                {
                    var sqlBuilder = new System.Text.StringBuilder();

                    sqlBuilder.AppendFormat("UPDATE \"{0}\".\"DiagramElementThing\"", partition);
                    sqlBuilder.AppendFormat(" SET (\"Container\", \"DepictedThing\", \"SharedStyle\")");
                    sqlBuilder.AppendFormat(" = (:container, :depictedThing, :sharedStyle)");
                    sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;");
                    command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value           = diagramElementThing.Iid;
                    command.Parameters.Add("container", NpgsqlDbType.Uuid).Value     = container.Iid;
                    command.Parameters.Add("depictedThing", NpgsqlDbType.Uuid).Value = !this.IsDerived(diagramElementThing, "DepictedThing") ? Utils.NullableValue(diagramElementThing.DepictedThing) : Utils.NullableValue(null);
                    command.Parameters.Add("sharedStyle", NpgsqlDbType.Uuid).Value   = !this.IsDerived(diagramElementThing, "SharedStyle") ? Utils.NullableValue(diagramElementThing.SharedStyle) : Utils.NullableValue(null);

                    command.CommandText = sqlBuilder.ToString();
                    command.Connection  = transaction.Connection;
                    command.Transaction = transaction;
                    this.ExecuteAndLogCommand(command);
                }
            }

            return(this.AfterUpdate(beforeUpdate, transaction, partition, diagramElementThing, container));
        }