Exemple #1
0
        public bool Delete(object obj, IQueryOptions options = null)
        {
            IQueryBuilder <object> query     = FromPrimaryKey(obj, options);
            IClassInfo             classInfo = query.ClassInfo;

            if (ClassInfo.IsNew(obj))
            {
                throw new InvalidOperationException("The object in question has a default-valued primary key, you can't delete it.");
            }
            if (!classInfo.DoEvent(obj, IQEventType.BeforeDelete, this))
            {
                return(false);
            }

            bool success = query.Delete() > 0;

            if (success)
            {
                classInfo.PrimaryKeyField.SetValue(obj, classInfo.PrimaryKeyDefaultValue);
            }

            if (!classInfo.DoEvent(obj, IQEventType.OnDelete, this))
            {
                throw new InvalidOperationException("The operation was cancelled after the database query was executed.");
            }

            return(success);
        }
Exemple #2
0
        /// <summary>
        /// options may include: an IDbConnection, an IDbTransaction, CommandBehavior. Save queries should not
        /// include any other parameters
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public bool Save(object obj, IQueryOptions options = null)
        {
            // TODO: This method is way too long. Break it down

            IClassInfo classInfo = GetClassInfo(obj);

            if (!classInfo.DoEvent(obj, IQEventType.BeforeSave, this))
            {
                return(false);
            }

            // Determine if the object is tracked for changes. If not, we will just save everything.

            IObjectData dbData;
            bool        tracked = IQ.MapperCache.TryGetObjectData(obj, out dbData);

            // Determine if the object is a new record based on the primary key value

            bool isNew = ClassInfo.IsNew(obj);

            QueryType queryType = isNew ?
                                  QueryType.Insert :
                                  QueryType.Update;

            ISqlQueryMaker query = classInfo.GetQuery(queryType, options);

            bool isDirty = false;

            string pk = classInfo.Query.PrimaryKey;

            foreach (var item in classInfo.Fields)
            {
                string name = item.Name;
                if (!item.IsPrimaryKey && !item.IsSqlReadOnly &&
                    (isNew || !tracked || dbData.IsDirty(name)))
                {
                    query.AddUpdateData(classInfo[name].SqlName, classInfo[name].GetValue(obj));
                    isDirty = true;
                }
            }

            bool success = false;

            if (isDirty)
            {
                if (queryType == QueryType.Insert)
                {
                    if (!classInfo.DoEvent(obj, IQEventType.BeforeInsert, this))
                    {
                        return(false);
                    }

                    int newPK = DataStorageController.RunQueryInsert(Connection, query,
                                                                     Transaction, CommandBehavior);

                    if (newPK <= 0)
                    {
                        throw new InvalidOperationException("The record could not be inserted.");
                    }

                    classInfo[pk].SetValue(obj, newPK);
                    if (!classInfo.DoEvent(obj, IQEventType.OnInsert, this))
                    {
                        throw new InvalidOperationException("The operation was cancelled after the database query was executed.");
                    }

                    success = true;
                }
                else
                {
                    if (!classInfo.DoEvent(obj, IQEventType.BeforeUpdate, this))
                    {
                        return(false);
                    }

                    query.Where.Add(classInfo.PrimaryKeyField.Name,
                                    classInfo.PrimaryKeyField.GetValue(obj));

                    success = DataStorageController.RunQueryScalar(Connection, query,
                                                                   Transaction, CommandBehavior) > 0;

                    if (!classInfo.DoEvent(obj, IQEventType.OnUpdate, this))
                    {
                        throw new InvalidOperationException("The operation was cancelled after the database query was executed.");
                    }
                }
            }
            else
            {
                success = false;
            }

            if (!classInfo.DoEvent(obj, IQEventType.OnSave, this))
            {
                throw new InvalidOperationException("The operation was cancelled after the database query was executed.");
            }

            if (success && tracked)
            {
                dbData.Clean();
            }

            return(success);
        }