public override void Insert(IDictionary values, DataSourceViewOperationCallback callback) { try { ISoodaObjectFactory fact = SoodaTransaction.ActiveTransaction.GetFactory(_owner.ClassName); SoodaObject obj = fact.CreateNew(SoodaTransaction.ActiveTransaction); Type type = obj.GetType(); foreach (DictionaryEntry v in values) { PropertyInfo pi = type.GetProperty((string)v.Key, BindingFlags.Public | BindingFlags.Instance); if (pi == null) { throw new SoodaException(v.Key + " not found in " + type.FullName); } pi.SetValue(obj, v.Value, null); } callback(1, null); } catch (Exception ex) { callback(0, ex); } }
private SoodaObject BeginObjectDeserialization(ISoodaObjectFactory factory, object pkValue, string mode) { SoodaObject retVal = factory.TryGet(this, pkValue); if (retVal == null) { if (mode == "update") { transactionLogger.Debug("Object not found. GetRef() ing"); retVal = factory.GetRef(this, pkValue); } else { transactionLogger.Debug("Object not found. Getting new raw object."); retVal = factory.GetRawObject(this); Statistics.RegisterObjectUpdate(); SoodaStatistics.Global.RegisterObjectUpdate(); retVal.SetPrimaryKeyValue(pkValue); retVal.SetInsertMode(); } } else if (mode == "insert") { retVal.SetInsertMode(); } return(retVal); }
private void AddObjectWithKey(string className, object keyValue, ISoodaObjectFactory factory) { lock (this) { GetObjectFactoryDictionaryForClass(className)[keyValue] = factory; } }
public override void Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) { try { if (keys.Count == 0) { throw new SoodaException("No keys passed to SoodaObjectDataSourceView.Update"); } if (keys.Count > 1) { throw new SoodaException("More than one key passed to SoodaObjectDataSourceView.Update"); } ISoodaObjectFactory fact = SoodaTransaction.ActiveTransaction.GetFactory(_owner.ClassName); // we just want to get the first key from "keys" // therefore we break at the end of the loop foreach (DictionaryEntry de in keys) { SoodaObject obj = SoodaTransaction.ActiveTransaction.GetObject(_owner.ClassName, Convert.ToString(de.Value)); obj.MarkForDelete(); break; } callback(1, null); } catch (Exception ex) { callback(0, ex); } }
protected override IEnumerable ExecuteSelect(System.Web.UI.DataSourceSelectArguments arguments) { object value; if (_owner.List != null) { value = _owner.List; } else if (_owner.Key != null) { value = SoodaTransaction.ActiveTransaction.GetObject(_owner.ClassName, _owner.Key); } else { ISoodaObjectFactory factory = SoodaTransaction.ActiveTransaction.GetFactory(_owner.ClassName, true); SoodaOrderBy orderBy = SoodaOrderBy.Unsorted; if (arguments.SortExpression != "") { orderBy = SoodaOrderBy.Parse(arguments.SortExpression); } value = factory.GetList( SoodaTransaction.ActiveTransaction, new SoodaWhereClause(_owner.WhereClause), orderBy, SoodaSnapshotOptions.Default); } return(value as IEnumerable ?? new object[] { value }); }
private SoodaObject LoadObject(ISoodaObjectFactory factory, string keyString) { object keyValue = factory.GetPrimaryKeyFieldHandler().RawDeserialize(keyString) ?? string.Empty; SoodaObject obj = factory.GetRef(this, keyValue); obj.LoadAllData(); return(obj); }
public void SetObjectFactory(string className, object primaryKeyValue, ISoodaObjectFactory factory) { if (logger.IsDebugEnabled) { logger.Debug("Adding {0}[{1}]={2} to the factory cache", className, primaryKeyValue, factory.GetClassInfo().Name); } AddObjectWithKey(className, primaryKeyValue, factory); }
public SoodaObjectManyToManyCollection(SoodaTransaction transaction, int masterColumn, object masterValue, Type relationType, Sooda.Schema.RelationInfo relationInfo) : base(transaction, masterColumn == 0 ? relationInfo.GetRef1ClassInfo() : relationInfo.GetRef2ClassInfo()) { this.relationInfo = relationInfo; this.masterValue = masterValue; this.masterColumn = masterColumn; this.relationType = relationType; _factory = transaction.GetFactory(classInfo); }
/// <summary> /// Determines whether this ObjectToSoodaObjectFactoryAssociation contains a specific value. /// </summary> /// <param name="value"> /// The ISoodaObjectFactory value to locate in this ObjectToSoodaObjectFactoryAssociation. /// </param> /// <returns> /// true if this ObjectToSoodaObjectFactoryAssociation contains an element with the specified value; /// otherwise, false. /// </returns> public virtual bool ContainsValue(ISoodaObjectFactory value) { foreach (ISoodaObjectFactory item in this.Dictionary.Values) { if (item == value) { return(true); } } return(false); }
public static SoodaObject TryGetRefFieldValue(ref SoodaObject refCache, object fieldValue, SoodaTransaction tran, ISoodaObjectFactory factory) { if (refCache != null) return refCache; if (fieldValue == null) return null; refCache = factory.TryGet(tran, fieldValue); return refCache; }
public static SoodaObject GetRefFieldValue(ref SoodaObject refCache, SoodaObject theObject, int tableNumber, int fieldOrdinal, SoodaTransaction tran, ISoodaObjectFactory factory) { if (refCache != null) return refCache; theObject.EnsureDataLoaded(tableNumber); if (theObject._fieldValues.IsNull(fieldOrdinal)) return null; refCache = factory.GetRef(tran, theObject._fieldValues.GetBoxedFieldValue(fieldOrdinal)); return refCache; }
public override void Update(System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues, System.Web.UI.DataSourceViewOperationCallback callback) { try { if (keys.Count == 0) { throw new SoodaException("No keys passed to SoodaObjectDataSourceView.Update"); } if (keys.Count > 1) { throw new SoodaException("More than one key passed to SoodaObjectDataSourceView.Update"); } ISoodaObjectFactory fact = SoodaTransaction.ActiveTransaction.GetFactory(_owner.ClassName); // we just want to get the first key from "keys" // therefore we break at the end of the loop foreach (DictionaryEntry de in keys) { SoodaObject obj = SoodaTransaction.ActiveTransaction.GetObject(_owner.ClassName, Convert.ToString(de.Value)); Type type = obj.GetType(); foreach (DictionaryEntry v in values) { if (oldValues[v.Key] != v.Value) { PropertyInfo pi = type.GetProperty((string)v.Key, BindingFlags.Public | BindingFlags.Instance); if (pi == null) { throw new SoodaException(v.Key + " not found in " + type.FullName); } pi.SetValue(obj, v.Value, null); } } break; } callback(1, null); } catch (Exception ex) { callback(0, ex); } }
public ISoodaObjectFactory FindObjectFactory(string className, object primaryKeyValue) { ISoodaObjectFactory fact = FindObjectWithKey(className, primaryKeyValue); if (logger.IsDebugEnabled) { if (fact == null) { logger.Debug("{0}[{1}] not found in the factory cache", className, primaryKeyValue); } else { logger.Debug("{0}[{1}] found in factory cache as {2}", className, primaryKeyValue, fact.GetClassInfo().Name); } } return(fact); }
public void SaveTuples(SoodaTransaction tran, bool isPrecommit) { if (count == 0) { return; } SoodaDataSource ds = tran.OpenDataSource(relationInfo.GetDataSource()); ISoodaObjectFactory leftFactory = tran.GetFactory(relationInfo.GetRef1ClassInfo()); ISoodaObjectFactory rightFactory = tran.GetFactory(relationInfo.GetRef2ClassInfo()); bool first = true; for (int i = 0; i < count; ++i) { if (!tuples[i].saved) { if (isPrecommit) { SoodaObject leftObject = leftFactory.GetRef(tran, tuples[i].ref1); SoodaObject rightObject = rightFactory.GetRef(tran, tuples[i].ref2); tran.PrecommitObject(leftObject); tran.PrecommitObject(rightObject); if (first) { first = false; tran.PrecommitRelation(this.relationInfo); } } ds.MakeTuple(tableName, leftColumnName, rightColumnName, tuples[i].ref1, tuples[i].ref2, tuples[i].tupleMode); tuples[i].saved = true; } } }
static SoodaObject GetRefFromRecordHelper(SoodaTransaction tran, ISoodaObjectFactory factory, IDataRecord record, int firstColumnIndex, TableInfo[] loadedTables, int tableIndex, bool loadData) { object keyValue; Sooda.Schema.FieldInfo[] pkFields = factory.GetClassInfo().GetPrimaryKeyFields(); if (pkFields.Length == 1) { int pkFieldOrdinal = loadData ? firstColumnIndex + pkFields[0].OrdinalInTable : 0; try { keyValue = factory.GetPrimaryKeyFieldHandler().RawRead(record, pkFieldOrdinal); } catch (Exception ex) { logger.Error("Error while reading field {0}.{1}: {2}", factory.GetClassInfo().Name, pkFieldOrdinal, ex); throw ex; } } else { object[] pkParts = new object[pkFields.Length]; for (int currentPkPart = 0; currentPkPart < pkFields.Length; currentPkPart++) { int pkFieldOrdinal = loadData ? firstColumnIndex + pkFields[currentPkPart].OrdinalInTable : currentPkPart; SoodaFieldHandler handler = factory.GetFieldHandler(pkFieldOrdinal); pkParts[currentPkPart] = handler.RawRead(record, pkFieldOrdinal); } keyValue = new SoodaTuple(pkParts); //logger.Debug("Tuple: {0}", keyValue); } SoodaObject retVal = factory.TryGet(tran, keyValue); if (retVal != null) { if (loadData && !retVal.IsDataLoaded(0)) retVal.LoadDataFromRecord(record, firstColumnIndex, loadedTables, tableIndex); return retVal; } factory = GetFactoryFromRecord(tran, factory, record, firstColumnIndex, keyValue, loadData); retVal = factory.GetRawObject(tran); tran.Statistics.RegisterObjectUpdate(); SoodaStatistics.Global.RegisterObjectUpdate(); retVal.InsertMode = false; retVal.SetPrimaryKeyValue(keyValue); if (loadData) retVal.LoadDataFromRecord(record, firstColumnIndex, loadedTables, tableIndex); return retVal; }
static ISoodaObjectFactory GetFactoryFromRecord(SoodaTransaction tran, ISoodaObjectFactory factory, IDataRecord record, int firstColumnIndex, object keyValue, bool loadData) { ClassInfo classInfo = factory.GetClassInfo(); List<ClassInfo> subclasses = tran.Schema.GetSubclasses(classInfo); if (subclasses.Count == 0) return factory; // more complex case - we have to determine the actual factory to be used for object creation int selectorFieldOrdinal = loadData ? classInfo.SubclassSelectorField.OrdinalInTable : record.FieldCount - 1; object selectorActualValue = factory.GetFieldHandler(selectorFieldOrdinal).RawRead(record, firstColumnIndex + selectorFieldOrdinal); IComparer comparer = selectorActualValue is string ? (IComparer) CaseInsensitiveComparer.DefaultInvariant : Comparer.DefaultInvariant; if (0 == comparer.Compare(selectorActualValue, classInfo.SubclassSelectorValue)) return factory; ISoodaObjectFactory newFactory; if (!factory.GetClassInfo().DisableTypeCache) { newFactory = SoodaTransaction.SoodaObjectFactoryCache.FindObjectFactory(classInfo.Name, keyValue); if (newFactory != null) return newFactory; } foreach (ClassInfo ci in subclasses) { if (0 == comparer.Compare(selectorActualValue, ci.SubclassSelectorValue)) { newFactory = tran.GetFactory(ci); SoodaTransaction.SoodaObjectFactoryCache.SetObjectFactory(classInfo.Name, keyValue, newFactory); return newFactory; } } throw new Exception("Cannot determine subclass. Selector actual value: " + selectorActualValue + " base class: " + classInfo.Name); }
internal void SetRefFieldValue(int tableNumber, string fieldName, int fieldOrdinal, SoodaObject newValue, SoodaObject[] refcache, int refCacheOrdinal, ISoodaObjectFactory factory) { if (newValue != null) { // transaction check if (newValue.GetTransaction() != this.GetTransaction()) throw new SoodaException("Attempted to assign object " + newValue.GetObjectKeyString() + " from another transaction to " + this.GetObjectKeyString() + "." + fieldName); } EnsureFieldsInited(); EnsureDataLoaded(tableNumber); SoodaObject oldValue = null; SoodaObjectImpl.GetRefFieldValue(ref oldValue, this, tableNumber, fieldOrdinal, GetTransaction(), factory); if (Object.Equals(oldValue, newValue)) return; object[] triggerArgs = new object[] { oldValue, newValue }; if (AreFieldUpdateTriggersEnabled()) { MethodInfo mi = this.GetType().GetMethod("BeforeFieldUpdate_" + fieldName, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public); if (mi != null) mi.Invoke(this, triggerArgs); } Sooda.Schema.FieldInfo fieldInfo = GetClassInfo().UnifiedFields[fieldOrdinal]; StringCollection backRefCollections = GetTransaction().Schema.GetBackRefCollections(fieldInfo); if (oldValue != null && backRefCollections != null) { foreach (string collectionName in backRefCollections) { PropertyInfo coll = oldValue.GetType().GetProperty(collectionName, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public); if (coll == null) throw new Exception(collectionName + " not found in " + oldValue.GetType().Name + " while setting " + this.GetType().Name + "." + fieldName); ISoodaObjectListInternal listInternal = (ISoodaObjectListInternal)coll.GetValue(oldValue, null); listInternal.InternalRemove(this); } } SetFieldValue(fieldOrdinal, newValue != null ? newValue.GetPrimaryKeyValue() : null); refcache[refCacheOrdinal] = null; if (newValue != null && backRefCollections != null) { foreach (string collectionName in backRefCollections) { PropertyInfo coll = newValue.GetType().GetProperty(collectionName, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public); if (coll == null) throw new Exception(collectionName + " not found in " + newValue.GetType().Name + " while setting " + this.GetType().Name + "." + fieldName); ISoodaObjectListInternal listInternal = (ISoodaObjectListInternal)coll.GetValue(newValue, null); listInternal.InternalAdd(this); } } if (AreFieldUpdateTriggersEnabled()) { MethodInfo mi = this.GetType().GetMethod("AfterFieldUpdate_" + fieldName, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public); if (mi != null) mi.Invoke(this, triggerArgs); } }
public static void SetRefFieldValue(SoodaObject theObject, int tableNumber, string fieldName, int fieldOrdinal, SoodaObject newValue, SoodaObject[] refcache, int refcacheOrdinal, ISoodaObjectFactory factory) { theObject.SetRefFieldValue(tableNumber, fieldName, fieldOrdinal, newValue, refcache, refcacheOrdinal, factory); }
/// <summary> /// Adds an element with the specified key and value to this ObjectToSoodaObjectFactoryAssociation. /// </summary> /// <param name="key"> /// The object key of the element to add. /// </param> /// <param name="value"> /// The ISoodaObjectFactory value of the element to add. /// </param> public virtual void Add(object key, ISoodaObjectFactory value) { this.Dictionary.Add(key, value); }
public void SetObjectFactory(string className, object primaryKeyValue, ISoodaObjectFactory factory) { }
public static SoodaObject GetRefHelper(SoodaTransaction tran, ISoodaObjectFactory factory, Guid keyValue) { return GetRefHelper(tran, factory, (object)keyValue); }
public void Deserialize(XmlReader reader) { Reset(); SoodaObject currentObject = null; SoodaRelationTable currentRelation = null; bool inDebug = false; // state data for just-being-read object bool objectForcePostCommit = false; bool objectDisableObjectTriggers = false; bool objectDelete = false; string objectClassName; string objectMode = null; object[] objectPrimaryKey = null; ClassInfo objectClassInfo; ISoodaObjectFactory objectFactory = null; int objectKeyCounter = 0; int objectTotalKeyCounter = 0; try { _savingObjects = true; // in case we get any "deleteobject" which require us to delete the objects // within transaction foreach (SoodaDataSource source in _dataSources) { source.BeginSaveChanges(); } while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && !inDebug) { switch (reader.Name) { case "field": if (currentObject == null) { throw new Exception("Field without an object during deserialization!"); } currentObject.DeserializeField(reader); break; case "persistent": if (currentObject == null) { throw new Exception("Field without an object during deserialization!"); } currentObject.DeserializePersistentField(reader); break; case "object": if (currentObject != null) { // end deserialization currentObject.EnableFieldUpdateTriggers(); currentObject = null; } objectKeyCounter = 0; objectForcePostCommit = false; objectDisableObjectTriggers = false; objectClassName = reader.GetAttribute("class"); objectMode = reader.GetAttribute("mode"); objectDelete = false; objectFactory = GetFactory(objectClassName); objectClassInfo = objectFactory.GetClassInfo(); objectTotalKeyCounter = objectClassInfo.GetPrimaryKeyFields().Length; if (objectTotalKeyCounter > 1) { objectPrimaryKey = new object[objectTotalKeyCounter]; } if (reader.GetAttribute("forcepostcommit") != null) { objectForcePostCommit = true; } if (reader.GetAttribute("disableobjecttriggers") != null) { objectDisableObjectTriggers = true; } if (reader.GetAttribute("delete") != null) { objectDelete = true; } break; case "key": int ordinal = Convert.ToInt32(reader.GetAttribute("ordinal")); object val = objectFactory.GetFieldHandler(ordinal).RawDeserialize(reader.GetAttribute("value")); if (objectTotalKeyCounter > 1) { objectPrimaryKey[objectKeyCounter] = val; } objectKeyCounter++; if (objectKeyCounter == objectTotalKeyCounter) { object primaryKey = objectTotalKeyCounter == 1 ? val : new SoodaTuple(objectPrimaryKey); currentObject = BeginObjectDeserialization(objectFactory, primaryKey, objectMode); if (objectForcePostCommit) { currentObject.ForcePostCommit(); } if (objectDisableObjectTriggers) { currentObject.DisableObjectTriggers(); } currentObject.DisableFieldUpdateTriggers(); if (objectDelete) { DeletedObjects.Add(currentObject); currentObject.DeleteMarker = true; currentObject.CommitObjectChanges(); currentObject.SetObjectDirty(); } } break; case "transaction": break; case "relation": currentRelation = GetRelationFromXml(reader); break; case "tuple": currentRelation.DeserializeTuple(reader); break; case "debug": if (!reader.IsEmptyElement) { inDebug = true; } break; default: throw new NotImplementedException("Element not implemented in deserialization: " + reader.Name); } } else if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "debug") { inDebug = false; } else if (reader.Name == "object") { currentObject.EnableFieldUpdateTriggers(); } } } foreach (WeakSoodaObject wr in _objectList) { SoodaObject ob = wr.TargetSoodaObject; if (ob != null) { ob.AfterDeserialize(); } } } finally { _savingObjects = false; foreach (SoodaDataSource source in _dataSources) { source.FinishSaveChanges(); } } }
protected override void LoadData() { SoodaDataSource ds = transaction.OpenDataSource(classInfo.GetDataSource()); TableInfo[] loadedTables; items = new Dictionary <SoodaObject, int>(); itemsArray = new List <SoodaObject>(); ISoodaObjectFactory factory = transaction.GetFactory(classInfo); SoodaWhereClause whereClause = new SoodaWhereClause(Soql.FieldEqualsParam(childRefField, 0), parentObject.GetPrimaryKeyValue()); if (additionalWhereClause != null) { whereClause = whereClause.Append(additionalWhereClause); } string cacheKey = null; if (cached) { // cache makes sense only on clean database if (!transaction.HasBeenPrecommitted(classInfo.GetRootClass())) { cacheKey = SoodaCache.GetCollectionKey(classInfo, whereClause); } } IEnumerable keysCollection = transaction.LoadCollectionFromCache(cacheKey, logger); if (keysCollection != null) { foreach (object o in keysCollection) { SoodaObject obj = factory.GetRef(transaction, o); // this binds to cache obj.EnsureFieldsInited(); if (tempItems != null) { CollectionChange change; if (tempItems.TryGetValue(obj, out change) && change == CollectionChange.Removed) { continue; } } items.Add(obj, itemsArray.Count); itemsArray.Add(obj); } } else { using (IDataReader reader = ds.LoadObjectList(transaction.Schema, classInfo, whereClause, null, 0, -1, SoodaSnapshotOptions.Default, out loadedTables)) { List <SoodaObject> readObjects = null; if (cached) { readObjects = new List <SoodaObject>(); } while (reader.Read()) { SoodaObject obj = SoodaObject.GetRefFromRecordHelper(transaction, factory, reader, 0, loadedTables, 0); if (readObjects != null) { readObjects.Add(obj); } if (tempItems != null) { CollectionChange change; if (tempItems.TryGetValue(obj, out change) && change == CollectionChange.Removed) { continue; } } items.Add(obj, itemsArray.Count); itemsArray.Add(obj); } if (cached) { TimeSpan expirationTimeout; bool slidingExpiration; if (transaction.CachingPolicy.GetExpirationTimeout( classInfo, whereClause, null, 0, -1, readObjects.Count, out expirationTimeout, out slidingExpiration)) { transaction.StoreCollectionInCache(cacheKey, classInfo, readObjects, null, true, expirationTimeout, slidingExpiration); } } } } if (tempItems != null) { foreach (KeyValuePair <SoodaObject, CollectionChange> entry in tempItems) { if (entry.Value == CollectionChange.Added) { SoodaObject obj = (SoodaObject)entry.Key; if (!items.ContainsKey(obj)) { items.Add(obj, itemsArray.Count); itemsArray.Add(obj); } } } } }
public static SoodaObject TryGetRefFieldValue(ref SoodaObject refCache, object fieldValue, SoodaTransaction tran, ISoodaObjectFactory factory) { if (refCache != null) { return(refCache); } if (fieldValue == null) { return(null); } refCache = factory.TryGet(tran, fieldValue); return(refCache); }
public static SoodaObject GetRefFromRecordHelper(SoodaTransaction tran, ISoodaObjectFactory factory, IDataRecord record, int firstColumnIndex, TableInfo[] loadedTables, int tableIndex) { return GetRefFromRecordHelper(tran, factory, record, firstColumnIndex, loadedTables, tableIndex, true); }
private SoodaObject BeginObjectDeserialization(ISoodaObjectFactory factory, object pkValue, string mode) { SoodaObject retVal = factory.TryGet(this, pkValue); if (retVal == null) { if (mode == "update") { transactionLogger.Debug("Object not found. GetRef() ing"); retVal = factory.GetRef(this, pkValue); } else { transactionLogger.Debug("Object not found. Getting new raw object."); retVal = factory.GetRawObject(this); Statistics.RegisterObjectUpdate(); SoodaStatistics.Global.RegisterObjectUpdate(); retVal.SetPrimaryKeyValue(pkValue); retVal.SetInsertMode(); } } else if (mode == "insert") { retVal.SetInsertMode(); } return retVal; }
internal static SoodaObject GetRefFromKeyRecordHelper(SoodaTransaction tran, ISoodaObjectFactory factory, IDataRecord record) { return GetRefFromRecordHelper(tran, factory, record, 0, null, -1, false); }
private SoodaObject LoadObject(ISoodaObjectFactory factory, string keyString) { object keyValue = factory.GetPrimaryKeyFieldHandler().RawDeserialize(keyString) ?? string.Empty; SoodaObject obj = factory.GetRef(this, keyValue); obj.LoadAllData(); return obj; }
public static SoodaObject GetRefHelper(SoodaTransaction tran, ISoodaObjectFactory factory, object keyValue) { SoodaObject retVal = factory.TryGet(tran, keyValue); if (retVal != null) return retVal; ClassInfo classInfo = factory.GetClassInfo(); if (classInfo.InheritsFromClass != null && tran.ExistsObjectWithKey(classInfo.GetRootClass().Name, keyValue)) throw new SoodaObjectNotFoundException(); if (classInfo.GetSubclassesForSchema(tran.Schema).Count > 0) { ISoodaObjectFactory newFactory = null; if (!classInfo.DisableTypeCache) { newFactory = SoodaTransaction.SoodaObjectFactoryCache.FindObjectFactory(classInfo.Name, keyValue); } if (newFactory != null) { factory = newFactory; } else { // if the class is actually inherited, we delegate the responsibility // to the appropriate GetRefFromRecord which will be called by the snapshot SoqlBooleanExpression where = null; Sooda.Schema.FieldInfo[] pkFields = classInfo.GetPrimaryKeyFields(); object[] par = new object[pkFields.Length]; for (int i = 0; i < pkFields.Length; ++i) { par[i] = SoodaTuple.GetValue(keyValue, i); SoqlBooleanExpression cmp = Soql.FieldEqualsParam(pkFields[i].Name, i); where = where == null ? cmp : where.And(cmp); } SoodaWhereClause whereClause = new SoodaWhereClause(where, par); IList list = factory.GetList(tran, whereClause, SoodaOrderBy.Unsorted, SoodaSnapshotOptions.NoTransaction | SoodaSnapshotOptions.NoWriteObjects | SoodaSnapshotOptions.NoCache); if (list.Count == 1) return (SoodaObject)list[0]; else if (list.Count == 0) throw new SoodaObjectNotFoundException("No matching object."); else throw new SoodaObjectNotFoundException("More than one object found. Fatal error."); } } retVal = factory.GetRawObject(tran); tran.Statistics.RegisterObjectUpdate(); SoodaStatistics.Global.RegisterObjectUpdate(); if (factory.GetClassInfo().ReadOnly) { retVal.LoadReadOnlyObject(keyValue); } else { retVal.SetUpdateMode(keyValue); } return retVal; }
private SoodaObject GetObject(ISoodaObjectFactory factory, string keyString) { object keyValue = factory.GetPrimaryKeyFieldHandler().RawDeserialize(keyString) ?? string.Empty; return factory.GetRef(this, keyValue); }
public static SoodaObject GetRefFieldValue(ref SoodaObject refCache, SoodaObject theObject, int tableNumber, int fieldOrdinal, SoodaTransaction tran, ISoodaObjectFactory factory) { if (refCache != null) { return(refCache); } theObject.EnsureDataLoaded(tableNumber); if (theObject._fieldValues.IsNull(fieldOrdinal)) { return(null); } refCache = factory.GetRef(tran, theObject._fieldValues.GetBoxedFieldValue(fieldOrdinal)); return(refCache); }
private SoodaObject GetNewObject(ISoodaObjectFactory factory) { return factory.CreateNew(this); }
private SoodaObject GetNewObject(ISoodaObjectFactory factory) { return(factory.CreateNew(this)); }
private SoodaObject GetObject(ISoodaObjectFactory factory, string keyString) { object keyValue = factory.GetPrimaryKeyFieldHandler().RawDeserialize(keyString) ?? string.Empty; return(factory.GetRef(this, keyValue)); }
private void LoadList(SoodaTransaction transaction, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, SoodaSnapshotOptions options, string[] involvedClassNames, bool useCache) { ISoodaObjectFactory factory = transaction.GetFactory(classInfo); string cacheKey = null; if (useCache) { // cache makes sense only on clean database if (!transaction.HasBeenPrecommitted(classInfo)) { cacheKey = SoodaCache.GetCollectionKey(classInfo, whereClause); } IEnumerable keysCollection = transaction.LoadCollectionFromCache(cacheKey, logger); if (keysCollection != null) { foreach (object o in keysCollection) { SoodaObject obj = factory.GetRef(transaction, o); // this binds to cache obj.EnsureFieldsInited(); items.Add(obj); } if (orderBy != null) { items.Sort(orderBy.GetComparer()); } count = items.Count; if (startIdx > 0) { if (startIdx < count) { items.RemoveRange(0, startIdx); } else { items.Clear(); } } if (pageCount != -1 && pageCount < items.Count) { items.RemoveRange(pageCount, items.Count - pageCount); } return; } } SoodaDataSource ds = transaction.OpenDataSource(classInfo.GetDataSource()); if ((options & SoodaSnapshotOptions.KeysOnly) != 0) { if (pageCount != -1) { using (IDataReader reader = ds.LoadMatchingPrimaryKeys(transaction.Schema, classInfo, whereClause, orderBy, 0, -1)) { count = 0; while (reader.Read()) { count++; } } } using (IDataReader reader = ds.LoadMatchingPrimaryKeys(transaction.Schema, classInfo, whereClause, orderBy, startIdx, pageCount)) { while (reader.Read()) { SoodaObject obj = SoodaObject.GetRefFromKeyRecordHelper(transaction, factory, reader); items.Add(obj); } if (pageCount == -1) { count = items.Count; } } } else { if (pageCount != -1) { using (IDataReader reader = ds.LoadMatchingPrimaryKeys(transaction.Schema, classInfo, whereClause, orderBy, 0, -1)) { count = 0; while (reader.Read()) { count++; } } } TableInfo[] loadedTables; using (IDataReader reader = ds.LoadObjectList(transaction.Schema, classInfo, whereClause, orderBy, startIdx, pageCount, options, out loadedTables)) { while (reader.Read()) { SoodaObject obj = SoodaObject.GetRefFromRecordHelper(transaction, factory, reader, 0, loadedTables, 0); if ((options & SoodaSnapshotOptions.VerifyAfterLoad) != 0 && whereClause != null && !whereClause.Matches(obj, false)) { continue; // don't add the object } items.Add(obj); } if (pageCount == -1) { count = items.Count; } } } if (cacheKey != null && useCache && startIdx == 0 && pageCount == -1 && involvedClassNames != null) { TimeSpan expirationTimeout; bool slidingExpiration; if (transaction.CachingPolicy.GetExpirationTimeout( classInfo, whereClause, orderBy, startIdx, pageCount, items.Count, out expirationTimeout, out slidingExpiration)) { transaction.StoreCollectionInCache(cacheKey, classInfo, items, involvedClassNames, (options & SoodaSnapshotOptions.KeysOnly) == 0, expirationTimeout, slidingExpiration); } } }
/// <summary> /// Determines whether this ObjectToSoodaObjectFactoryAssociation contains a specific value. /// </summary> /// <param name="value"> /// The ISoodaObjectFactory value to locate in this ObjectToSoodaObjectFactoryAssociation. /// </param> /// <returns> /// true if this ObjectToSoodaObjectFactoryAssociation contains an element with the specified value; /// otherwise, false. /// </returns> public virtual bool ContainsValue(ISoodaObjectFactory value) { foreach (ISoodaObjectFactory item in this.Dictionary.Values) { if (item == value) return true; } return false; }