Ejemplo n.º 1
0
        public void SaveReferenceProperty(int versionId, PropertyType propertyType, IEnumerable <int> value)
        {
            if (propertyType == null)
            {
                throw new ArgumentNullException("propertyType");
            }

            // Remove/Add referredNodeIds to update the database state to the proper one
            SqlProcedure cmd = null;

            try
            {
                string referredListXml = SqlProvider.CreateIdXmlForReferencePropertyUpdate(value);

                cmd = new SqlProcedure {
                    CommandText = "proc_ReferenceProperty_Update"
                };
                cmd.Parameters.Add("@VersionId", SqlDbType.Int).Value             = versionId;
                cmd.Parameters.Add("@PropertyTypeId", SqlDbType.Int).Value        = propertyType.Id;
                cmd.Parameters.Add("@ReferredNodeIdListXml", SqlDbType.Xml).Value = referredListXml;
                cmd.ExecuteNonQuery();
            }
            finally
            {
                cmd.Dispose();
            }
        }
Ejemplo n.º 2
0
        public void SaveReferenceProperty(int versionId, PropertyType propertyType, IEnumerable <int> value)
        {
            if (propertyType == null)
            {
                throw new ArgumentNullException("propertyType");
            }

            for (short tryAgain = 3; tryAgain > 0; tryAgain--)
            {
                // Optimistic approach: try to save the value as is, without checking and compensate if it fails
                try
                {
                    // Create XML
                    var referredListXml = SqlProvider.CreateIdXmlForReferencePropertyUpdate(value);

                    // Execute SQL
                    using (var cmd = new SqlProcedure {
                        CommandText = "proc_ReferenceProperty_Update"
                    })
                    {
                        cmd.Parameters.Add("@VersionId", SqlDbType.Int).Value             = versionId;
                        cmd.Parameters.Add("@PropertyTypeId", SqlDbType.Int).Value        = propertyType.Id;
                        cmd.Parameters.Add("@ReferredNodeIdListXml", SqlDbType.Xml).Value = referredListXml;
                        cmd.ExecuteNonQuery();

                        // Success, don't try again
                        tryAgain = 0;
                    }
                }
                catch (SqlException exc)
                {
                    // This was the last try and it failed, throw
                    if (tryAgain == 1)
                    {
                        throw;
                    }

                    // The value contains a node ID which no longer exists in the database, let's compensate for that
                    if (exc.Message.Contains("The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_ReferenceProperties_Nodes\"."))
                    {
                        // Get node heads for the IDs
                        var heads = DataProvider.Current.LoadNodeHeads(value);
                        // Select the IDs of the existing node heads
                        value = heads.Where(h => h != null).Select(h => h.Id);
                    }
                    else
                    {
                        // If the error is something else, just throw it up
                        throw;
                    }
                }
            }
        }