예제 #1
0
        /// <summary>
        /// Saves the changes that were made to the
        /// specified <paramref name="dataModel"/>, within the specified
        /// database <paramref name="transactionContext"/>.
        /// <remarks>
        /// The specified object must have the Deleted property set to true,
        /// IsDirty evaluated as true, or IsNew evaluated as true, or else
        /// the entity will not be saved.
        /// </remarks>
        /// </summary>
        /// <param name="dataModel"></param>
        /// <param name="transactionContext"></param>
        public override void SaveModel <TModel>(TModel dataModel, DbTransaction transactionContext)
        {
            if (!dataModel.IsNew &&
                !dataModel.IsDirty &&
                !dataModel.MarkDeleted)
            {
                return;
            }

            DataModelMap demap = DataModelMap.GetEntityMapping(dataModel.GetType());

            DbConnection tmpConn = null;

            if (transactionContext == null)
            {
                tmpConn            = CreateAndOpenConnection();
                transactionContext = tmpConn.BeginTransaction();
            }
            try
            {
                DbDataProviderCommandBuilder <TModel> cmdBuilder = CreateCommandBuilder(dataModel);
                using (DbCommand cmd = cmdBuilder.CreateDbCommand(transactionContext))
                {
                    cmd.Transaction = transactionContext;
                    bool wasNew = dataModel.IsNew;

                    cmd.ExecuteNonQuery();

                    if (wasNew)
                    {
                        foreach (var field in demap.FieldMappings)
                        {
                            DataModelColumnAttribute f = field.Value;
                            if (f.ReturnAsOutputOnInsert)
                            {
                                dataModel[f.TargetMember.Name] = cmd.Parameters[f.InsertParam].Value;
                            }
                        }
                    }
                }
            }
            finally
            {
                if (tmpConn != null)
                {
                    transactionContext.Commit();
                    tmpConn.Close();
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Loads the first <see cref="DataModel"/> that
        /// the specified <paramref name="query"/> finds,
        /// within the specified database <paramref name="transactionContext"/>.
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="query"></param>
        /// <param name="transactionContext"></param>
        /// <returns></returns>
        public override TModel LoadModel <TModel>(DataModelQuery <TModel> query, DbTransaction transactionContext)
        {
            DbConnection tmpConn = null;

            if (transactionContext == null)
            {
                tmpConn            = CreateAndOpenConnection();
                transactionContext = tmpConn.BeginTransaction(ProviderDefaults.IsolationLevel);
            }
            try
            {
                DbConnection conn = transactionContext.Connection;
                DbDataProviderCommandBuilder <TModel> cmdBuilder = CreateCommandBuilder(query);
                using (DbCommand cmd = cmdBuilder.CreateDbCommand(transactionContext)) // conn.CreateCommand())
                {
                    cmd.Transaction = transactionContext;

                    using (DbDataReader dr = cmd.ExecuteReader(cmdBuilder.ExecuteBehavior ?? CommandBehavior.SingleRow))
                    {
                        if (dr.Read())
                        {
                            var ret = (TModel)Activator.CreateInstance(typeof(TModel));
                            ret.DataProvider = this;
                            ret.Load(dr);
                            return(ret);
                        }
                        return(null);
                    }
                }
            }
            finally
            {
                if (tmpConn != null)
                {
                    transactionContext.Commit();
                    tmpConn.Close();
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Loads a set of <see cref="DataModel"/> objects
        /// using the specified <paramref name="query"/> within the
        /// specified database <paramref name="transactionContext"/>.
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="query"></param>
        /// <param name="transactionContext"></param>
        /// <returns></returns>
        public override DataModelCollection <TModel> LoadModels <TModel>(
            DataModelQuery <TModel> query, DbTransaction transactionContext)
        {
            DataModelMap demap = DataModel.GetMapping <TModel>();

            DbConnection tmpConn = null;

            if (transactionContext == null)
            {
                tmpConn            = CreateAndOpenConnection();
                transactionContext = tmpConn.BeginTransaction(ProviderDefaults.IsolationLevel);
            }
            DbConnection conn = transactionContext.Connection;

            try
            {
                DbDataProviderCommandBuilder <TModel> cmdBuilder = CreateCommandBuilder(query);
                using (DbCommand cmd = cmdBuilder.CreateDbCommand(transactionContext))
                {
                    cmd.Transaction = transactionContext;

                    using (DbDataReader dr = cmd.ExecuteReader(cmdBuilder.ExecuteBehavior ?? CommandBehavior.Default))
                    {
                        var ret = new DataModelCollection <TModel> {
                            DataProvider = this
                        };

                        if (query.Pagination == null ||
                            query.Pagination.ItemsPerPage == int.MaxValue ||
                            query.Pagination.ItemsPerPage == 0 ||
                            cmdBuilder.PaginationIsHandled)
                        {
                            while (dr.Read())
                            {
                                var obj = (TModel)Activator.CreateInstance(typeof(TModel));
                                obj.DataProvider = this;
                                obj.Load(dr);
                                ret.Add(obj);
                            }
                        }
                        else
                        {
                            int start = query.Pagination.Page * query.Pagination.ItemsPerPage -
                                        query.Pagination.ItemsPerPage;
                            bool eof = false;
                            for (int i = 0; i < start; i++)
                            {
                                if (!dr.Read())
                                {
                                    eof = true;
                                    break;
                                }
                            }
                            for (int i = 0; i < query.Pagination.ItemsPerPage && !eof; i++)
                            {
                                if (!dr.Read())
                                {
                                    eof = true;
                                    break;
                                }
                                var obj = (TModel)Activator.CreateInstance(typeof(TModel));
                                obj.DataProvider = this;
                                obj.Load(dr);
                                ret.Add(obj);
                            }
                        }
                        if (!string.IsNullOrEmpty(demap.TableMapping.SelectManyProcedure) &&
                            query.OrderBy.Count > 0)
                        {
                            // exec sort from last sort field to first to get correct end result
                            for (int i = query.OrderBy.Count - 1; i >= 0; i--)
                            {
                                string fld = query.OrderBy[i].GetFieldMapping(typeof(TModel)).TargetMember.Name;
                                switch (query.OrderBy[i].SortDirection)
                                {
                                case Sort.Ascending:
                                    TModel[] asc = (from item in ret
                                                    orderby item[fld] ascending
                                                    select item).ToArray();
                                    ret = new DataModelCollection <TModel>(asc);
                                    break;

                                case Sort.Descending:
                                    TModel[] desc = (from item in ret
                                                     orderby item[fld] descending
                                                     select item).ToArray();
                                    ret = new DataModelCollection <TModel>(desc);
                                    break;
                                }
                            }
                        }
                        return(ret);
                    }
                }
            }
            finally
            {
                if (tmpConn != null)
                {
                    transactionContext.Commit();
                    tmpConn.Close();
                }
            }
        }