Exemplo n.º 1
0
        /// <summary>
        /// Saves (inserts/updates) an object based on the following criteria:
        /// 1) If autoincrement is set to true and the primary key is the default value, it inserts
        /// 2) If autoincrement is set to true and the primary key is not the default value, it updates
        /// 3) If autoincrement is set to false and the primary key is the default value, it inserts
        /// 4) If autoincrement is set to false and the primary key is not the default value,
        /// it does an Any call to see if the item is already in the database. If it is, it does an
        /// update. Otherwise it does an insert.
        /// On an insert, the primary key property is updated with the resulting value of the insert.
        /// </summary>
        /// <param name="Object">Object to save</param>
        /// <param name="Parameters">Extra parameters to be added to the insert/update function</param>
        public virtual void Save <PrimaryKeyType>(ClassType Object, params IParameter[] Parameters)
        {
            PrimaryKeyType PrimaryKeyVal = (PrimaryKeyType)GetPrimaryKey(Object);
            GenericEqualityComparer <PrimaryKeyType> Comparer = new GenericEqualityComparer <PrimaryKeyType>();

            if (Comparer.Equals(PrimaryKeyVal, default(PrimaryKeyType)))
            {
                PrimaryKeyVal = Insert <PrimaryKeyType>(Object, Parameters);
                PrimaryKeyMapping.CopyRightToLeft(PrimaryKeyVal, Object);
                return;
            }
            if (AutoIncrement)
            {
                Update(Object, Parameters);
                return;
            }
            Parameter <PrimaryKeyType> Param1 = new Parameter <PrimaryKeyType>(PrimaryKeyVal, PrimaryKey, ParameterStarter);
            ClassType TempVal = Any(PrimaryKey, null, null, Param1);

            if (TempVal == null)
            {
                PrimaryKeyVal = Insert <PrimaryKeyType>(Object, Parameters);
                PrimaryKeyMapping.CopyRightToLeft(PrimaryKeyVal, Object);
                return;
            }
            Update(Object, Parameters);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves (inserts/updates) an object based on the following criteria:
        /// 1) If autoincrement is set to true and the primary key is the default value, it inserts
        /// 2) If autoincrement is set to true and the primary key is not the default value, it updates
        /// 3) If autoincrement is set to false and the primary key is the default value, it inserts
        /// 4) If autoincrement is set to false and the primary key is not the default value,
        /// it does an Any call to see if the item is already in the database. If it is, it does an
        /// update. Otherwise it does an insert.
        /// On an insert, the primary key property is updated with the resulting value of the insert.
        /// </summary>
        /// <param name="Object">Object to save</param>
        /// <param name="Parameters">Extra parameters to be added to the insert/update function</param>
        public virtual void Save <PrimaryKeyType>(ClassType Object, params IParameter[] Parameters)
        {
            PrimaryKeyType PrimaryKeyVal = GetPrimaryKey(Object).TryTo(default(PrimaryKeyType));
            GenericEqualityComparer <PrimaryKeyType> Comparer = new GenericEqualityComparer <PrimaryKeyType>();

            if (Comparer.Equals(PrimaryKeyVal, default(PrimaryKeyType)))
            {
                PrimaryKeyVal = Insert <PrimaryKeyType>(Object, Parameters);
                PrimaryKeyMapping.CopyRightToLeft(PrimaryKeyVal, Object);
                return;
            }
            if (AutoIncrement)
            {
                Update(Object, Parameters);
                return;
            }
            IParameter Param1 = null;

            if (typeof(PrimaryKeyType).IsOfType(typeof(string)))
            {
                Param1 = new StringEqualParameter(PrimaryKeyVal.ToString(), PrimaryKey, -1, ParameterStarter);
            }
            else
            {
                Param1 = new EqualParameter <PrimaryKeyType>(PrimaryKeyVal, PrimaryKey, ParameterStarter);
            }
            ClassType TempVal = Any(PrimaryKey, null, null, Param1);

            if (TempVal == null)
            {
                PrimaryKeyVal = Insert <PrimaryKeyType>(Object, Parameters);
                PrimaryKeyMapping.CopyRightToLeft(PrimaryKeyVal, Object);
                return;
            }
            Update(Object, Parameters);
        }