コード例 #1
0
ファイル: RecordBase.cs プロジェクト: vogong/SubSonic-2.0
 /// <summary>
 /// Called after any property is set. Sets IsDirty to <c>true</c>.
 /// </summary>
 public void MarkClean()
 {
     foreach (TableSchema.TableColumnSetting setting in columnSettings)
     {
         setting.IsDirty = false;
     }
     DirtyColumns.Clear();
 }
コード例 #2
0
        /// <summary>
        /// Sets a value for a particular column in the record
        /// </summary>
        /// <param name="columnName">Name of the column, as defined in the database</param>
        /// <param name="oValue">The value to set the type to</param>
        public void SetColumnValue(string columnName, object oValue)
        {
            columnSettings = columnSettings ?? new TableSchema.TableColumnSettingCollection();

            // add the column to the DirtyColumns
            // if this instance has already been loaded
            // and this is a change to existing values
            if (IsLoaded && !IsNew)
            {
                TableSchema.Table schema      = GetSchema();
                object            oldValue    = null;
                string            oldValueMsg = "NULL";
                string            newValueMsg = "NULL";
                bool areEqualOrBothNull       = false;

                try
                {
                    oldValue = columnSettings.GetValue(columnName);
                }
                catch {}

                if (oldValue == null && oValue == null)
                {
                    areEqualOrBothNull = true;
                }
                else
                {
                    if (oldValue != null)
                    {
                        oldValueMsg        = oldValue.ToString();
                        areEqualOrBothNull = oldValue.Equals(oValue);
                    }

                    if (oValue != null)
                    {
                        newValueMsg = oValue.ToString();
                    }
                }

                TableSchema.TableColumn dirtyCol = schema.GetColumn(columnName);

                if (dirtyCol != null && !areEqualOrBothNull)
                {
                    string auditMessage = String.Format("Value changed from {0} to {1}{2}", oldValueMsg, newValueMsg, Environment.NewLine);
                    TableSchema.TableColumn dirtyEntry = DirtyColumns.GetColumn(columnName);
                    if (dirtyEntry != null)
                    {
                        DirtyColumns.Remove(dirtyEntry);
                        auditMessage = String.Concat(dirtyCol.AuditMessage, auditMessage);
                    }

                    dirtyCol.AuditMessage = auditMessage;
                    DirtyColumns.Add(dirtyCol);
                }
            }

            columnSettings.SetValue(columnName, oValue);
        }
コード例 #3
0
        /// <summary>
        /// Saves this object's state to the selected Database.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        public void Save(string userName)
        {
            bool isValid = true;

            if (ValidateWhenSaving)
            {
                BeforeValidate();

                isValid = Validate();
            }

            if (isValid)
            {
                if (IsNew)
                {
                    BeforeInsert();
                }
                else if (IsDirty)
                {
                    BeforeUpdate();
                }

                QueryCommand cmd = GetSaveCommand(userName);
                if (cmd == null)
                {
                    return;
                }

                // reset the Primary Key with the id passed back by the operation
                object pkVal = DataService.ExecuteScalar(cmd);

                // clear out the DirtyColumns
                DirtyColumns.Clear();

                if (pkVal != null)
                {
                    if (pkVal.GetType() == typeof(decimal))
                    {
                        pkVal = Convert.ToInt32(pkVal);
                    }

                    // set the primaryKey, only if an auto-increment
                    if (BaseSchema.PrimaryKey.AutoIncrement || BaseSchema.PrimaryKey.DataType == DbType.Guid)
                    {
                        try
                        {
                            SetPrimaryKey(pkVal);
                        }
                        catch
                        {
                            // this will happen if there is no PK defined on a table. Catch this and notify
                            throw new Exception("No Primary Key is defined for this table. A primary key is required to use SubSonic");
                        }
                    }
                }

                // set this object as old
                bool isNew = IsNew;
                MarkOld();
                MarkClean();
                AfterCommit(isNew);
            }
            else
            {
                // throw an Exception
                string notification = String.Empty;
                foreach (string message in Errors)
                {
                    notification += message + Environment.NewLine;
                }

                throw new Exception("Can't save: " + notification);
            }
        }