Exemplo n.º 1
0
        public EntityRecord ReadRecord(IDataRecord dataRecord, EntitySession session)
        {
            // Some outer join queries may produce entities that are null; so we first try to read Primary key values - if they're all null, we return null.
            if (_primaryKeyColumns.Count > 0 && PrimaryKeyIsNull(dataRecord))
            {
                return(null);
            }
            var              entRec  = new EntityRecord(_tableInfo.Entity, EntityStatus.Loading);
            object           dbValue = null;
            OutColumnMapping colMap  = null;

            //for-i loop is more efficient than foreach
            for (int i = 0; i < _columns.Count; i++)
            {
                try {
                    colMap  = _columns[i];
                    dbValue = dataRecord[colMap.ReaderColumnIndex];
                    //System.Diagnostics.Debug.WriteLine(colMap.DbColumn.ColumnName + " " + dbValue + "(" + dbValue.GetType() + ")");
                    var conv = colMap.DbColumn.TypeInfo.ColumnToPropertyConverter;
                    if (dbValue != null && conv != null)
                    {
                        dbValue = conv(dbValue);
                    }
                    entRec.SetValue(colMap.DbColumn.Member, dbValue);
                } catch (Exception ex) {
                    ex.AddValue("DataRecord", dataRecord);
                    ex.AddValue("ColumnName", colMap.DbColumn.ColumnName);
                    ex.AddValue("DbValue", dbValue);
                    throw;
                }
            }
            var sessionRec = session.Attach(entRec); //might return different, previously loaded record

            return(sessionRec);
        }
Exemplo n.º 2
0
 public CacheDisableToken(EntitySession session)
 {
     _session    = session;
     _wasEnabled = session.CacheEnabled;
     if (_wasEnabled)
     {
         session.EnableCache(false);
     }
 }
Exemplo n.º 3
0
        // Used for efficient reading entities in linq queries
        public object ReadEntity(IDataRecord dataRecord, EntitySession session)
        {
            var entRec = ReadRecord(dataRecord, session);

            if (entRec == null)
            {
                return(null);
            }
            return(entRec.EntityInstance);
        }
Exemplo n.º 4
0
        public static SearchResults <TEntity> ExecuteSearchImpl <TEntity>(
            EntitySession session,
            Expression <Func <TEntity, bool> > where,
            SearchParams searchParams,
            Expression <Func <TEntity, object> > include = null,
            QueryOptions options = QueryOptions.ForceIgnoreCase,
            Dictionary <string, string> nameMapping = null) where TEntity : class
        {
            try {
                var baseQuery = session.EntitySet <TEntity>().Where(where).WithOptions(options); //this will be reused by Count query
                //add include, order by, skip, take
                var incQuery     = include == null ? baseQuery : baseQuery.Include <TEntity>(include);
                var orderedQuery = string.IsNullOrEmpty(searchParams.OrderBy) ? incQuery : incQuery.OrderBy(searchParams.OrderBy, nameMapping);
                // Add Skip, Take; we return '+1' rows, to check if there are any more; if not, we do not need to query total
                var takePlusOne = searchParams.Take + 1;
                var pageQuery   = orderedQuery.Skip(searchParams.Skip).Take(takePlusOne);
                var rows        = pageQuery.ToList();

                if (rows.Count > 0 && rows.Count < takePlusOne)
                {
                    // important (bug #74) - we must check rows.Count > 0, that any rows returned
                    //We see the last row, we do not need to run total query
                    return new SearchResults <TEntity>()
                           {
                               Results = rows, TotalCount = searchParams.Skip + rows.Count
                           }
                }
                ;
                // we received more than Take number of rows (or zero); it means we need to run TotalCount
                //save main query command, and restore it after total query; in debugging main query is more interesting than total query
                var queryCmd   = session.GetLastCommand();
                var totalCount = baseQuery.Count(); //use baseQuery here, without OrderBy, Skip, Take
                session.SetLastCommand(queryCmd);   //restore main query command

                //remove last extra row if it is there - we queried for 1 extra
                if (rows.Count > searchParams.Take && rows.Count > 0)
                {
                    rows.RemoveAt(rows.Count - 1);
                }
                var results = new SearchResults <TEntity>()
                {
                    Results = rows, TotalCount = totalCount
                };
                return(results);
            } catch (Exception ex) {
                session.Context.App.AppEvents.OnError(session.Context, ex);
                throw;
            }
        }
Exemplo n.º 5
0
        public static SearchResults <TResult> ExecuteSearchImpl <TEntity, TResult>(
            EntitySession session,
            Expression <Func <TEntity, bool> > where,
            SearchParams searchParams,
            Func <TEntity, TResult> converter,
            Expression <Func <TEntity, object> > include = null,
            QueryOptions options = QueryOptions.ForceIgnoreCase,
            Dictionary <string, string> nameMapping = null)
            where TEntity : class
        {
            var tempResults = ExecuteSearchImpl <TEntity>(session, where, searchParams, include, options, nameMapping);

            if (converter == null)
            {
                Util.Check(typeof(TEntity) == typeof(TResult), "ExecuteSearch - if result type is different from entity type, converter method must be provided.");
                return((SearchResults <TResult>)(object) tempResults);
            }
            var results = tempResults.Convert(converter);

            return(results);
        }
Exemplo n.º 6
0
 public CacheDisableToken(EntitySession session)
 {
     _session              = session;
     _wasDisabled          = session.CacheDisabled;
     session.CacheDisabled = true;
 }
Exemplo n.º 7
0
 private IncludeProcessor(EntitySession session, IList <LambdaExpression> includes)
 {
     _session  = session;
     _includes = includes;
 }