Beispiel #1
0
        public int QueryScalar(string query, params object[] parameters)
        {
            var raw = new SqlQueryDef(query, parameters);

            return(DataStorageController.RunQueryScalar(Connection,
                                                        raw,
                                                        Transaction,
                                                        CommandBehavior));
        }
Beispiel #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);
        }