Example #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);
            bool             isView  = _tableInfo.Entity.Kind == EntityKind.View;
            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];
                    var dbCol  = colMap.DbColumn;
                    var isNull = dataRecord.IsDBNull(colMap.ReaderColumnIndex);
                    if (isNull)
                    {
                        // Views might have NULLs unexpectedly in columns like Count() or Sum() - LINQ expr has non-nullable type, but in fact
                        // the view column can return NULL
                        if (isView && !dbCol.Flags.IsSet(DbColumnFlags.Nullable))
                        {
                            dbValue = dbCol.Member.DefaultValue;
                        }
                        else
                        {
                            dbValue = DBNull.Value;
                        }
                    }
                    else
                    {
                        // not NULL
                        dbValue = dbCol.TypeInfo.ColumnReader(dataRecord, colMap.ReaderColumnIndex);
                        var conv = dbCol.Converter.ColumnToProperty;
                        if (dbValue != null && conv != null)
                        {
                            dbValue = conv(dbValue);
                        }
                        // quick fix: views sometimes have out columns changed from expected
                        if (isView && !ConvertHelper.UnderlyingTypesMatch(dbCol.Member.DataType, dbValue.GetType()))
                        {
                            dbValue = ConvertHelper.ChangeType(dbValue, dbCol.Member.DataType);
                        }
                    }
                    var member = dbCol.Member;
                    entRec.ValuesOriginal[member.ValueIndex] = 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);
        }
Example #2
0
 public EntityRecord Lookup(EntityKey primaryKey, EntitySession session)
 {
     var strKey = primaryKey.AsString();
       var data = _cacheTable.Lookup(strKey);
       if(data == null)
     return null;
       var needVersion = session.Context.EntityCacheVersion;
       if(data.Version < needVersion) {
     _cacheTable.Remove(primaryKey.AsString());
     return null;
       }
       var rec = new EntityRecord(primaryKey);
       Array.Copy(data.Values, rec.ValuesOriginal, data.Values.Length);
       rec.SourceCacheType = CacheType.Sparse;
       session.Attach(rec);
       return rec;
 }
Example #3
0
        public static EntityRecord CloneAndAttach(EntitySession toSession, EntityRecord record, bool forceFullCopy = false)
        {
            if (record.Session == toSession)
            {
                return(record); //do not clone if it is already in this session
            }
            //If record with the same PK is already loaded in session, use it in result; otherwise, clone record from cache and attach
            var sessionRec = toSession.GetRecord(record.PrimaryKey, LoadFlags.None);

            if (sessionRec == null)
            {
                sessionRec = new EntityRecord(record); //make clone
                toSession.Attach(sessionRec);
            }
            else if (sessionRec.Status == EntityStatus.Stub || forceFullCopy)
            {
                sessionRec.CopyOriginalValues(record);
            }
            sessionRec.SourceCacheType = CacheType.FullSet;
            return(sessionRec);
        }
Example #4
0
        public EntityRecord Lookup(EntityKey primaryKey, EntitySession session)
        {
            var strKey = primaryKey.AsString();
            var data   = _cacheTable.Lookup(strKey);

            if (data == null)
            {
                return(null);
            }
            var needVersion = session.Context.EntityCacheVersion;

            if (data.Version < needVersion)
            {
                _cacheTable.Remove(primaryKey.AsString());
                return(null);
            }
            var rec = new EntityRecord(primaryKey);

            Array.Copy(data.Values, rec.ValuesOriginal, data.Values.Length);
            rec.SourceCacheType = CacheType.Sparse;
            session.Attach(rec);
            return(rec);
        }
Example #5
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.ValuesOriginal[colMap.DbColumn.Member.ValueIndex] = 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;
        }