public int Compare(SoodaObject dbo1, SoodaObject dbo2) { _context1.SetRootObject(dbo1); _context2.SetRootObject(dbo2); foreach (ExpressionCompareInfo eci in expressions) { object v1 = eci.Expression.Evaluate(_context1); object v2 = eci.Expression.Evaluate(_context2); int result = DoCompare(v1, v2); if (result != 0) { if (eci.SortOrder == SortOrder.Ascending) { return(result); } else { return(-result); } } } return(PrimaryKeyCompare(dbo1, dbo2)); }
public SoodaObjectListSnapshot(SoodaTransaction tran, SoodaObjectFilter filter, ClassInfo ci) { this.classInfo = ci; List <WeakSoodaObject> al = tran.GetObjectsByClassName(ci.Name); if (al != null) { // al.Clone() is needed because // the filter expression may materialize new objects // during checking. This way we avoid "collection modified" exception List <SoodaObject> clonedArray = new List <SoodaObject>(); foreach (WeakSoodaObject wr in al) { SoodaObject obj = wr.TargetSoodaObject; if (obj != null) { clonedArray.Add(obj); } } foreach (SoodaObject obj in clonedArray) { if (filter(obj)) { items.Add(obj); } } count = items.Count; } }
void DoUpdates(SoodaObject obj, bool isPrecommit) { foreach (TableInfo table in obj.GetClassInfo().DatabaseTables) { DoUpdatesForTable(obj, table, isPrecommit); } }
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); }
int IComparer.Compare(object o1, object o2) { SoodaObject dbo1 = o1 as SoodaObject; SoodaObject dbo2 = o2 as SoodaObject; return(Compare(dbo1, dbo2)); }
public void InternalAdd(SoodaObject c) { if (!childType.IsInstanceOfType(c)) { return; } if (items == null) { if (tempItems == null) { tempItems = new Dictionary <SoodaObject, CollectionChange>(); } tempItems[c] = CollectionChange.Added; return; } if (items.ContainsKey(c)) { return; } items.Add(c, itemsArray.Count); itemsArray.Add(c); }
object GetFieldValue(SoodaObject obj, FieldInfo fi, bool isPrecommit) { object val = obj.GetFieldValue(fi.ClassUnifiedOrdinal); if (!fi.IsNullable && SqlBuilder.IsNullValue(val, fi)) { if (!isPrecommit) { throw new SoodaDatabaseException(obj.GetObjectKeyString() + "." + fi.Name + " cannot be null on commit."); } val = fi.PrecommitTypedValue; if (val == null) { throw new SoodaDatabaseException(obj.GetObjectKeyString() + "." + fi.Name + " is null on precommit and no 'precommitValue' has been defined for it."); } if (val == SchemaInfo.NullPrecommitValue) { val = null; } if (logger.IsDebugEnabled) { logger.Debug("Using precommit value of {0} for {1}.{2}", val, fi.Table.NameToken, fi.Name); } } return(val); }
public override IDataReader LoadObjectTable(SoodaObject obj, object keyVal, int tableNumber, out TableInfo[] loadedTables) { ClassInfo classInfo = obj.GetClassInfo(); IDbCommand cmd = Connection.CreateCommand(); try { cmd.CommandTimeout = CommandTimeout; } catch (NotSupportedException e) { logger.Debug("CommandTimeout not supported. {0}", e.Message); } if (Transaction != null) { cmd.Transaction = this.Transaction; } SqlBuilder.BuildCommandWithParameters(cmd, false, GetLoadingSelectStatement(classInfo, classInfo.UnifiedTables[tableNumber], out loadedTables), SoodaTuple.GetValuesArray(keyVal), false); IDataReader reader = TimedExecuteReader(cmd); if (reader.Read()) { return(reader); } else { reader.Dispose(); return(null); } }
internal NameValueCollection GetPersistentValues(SoodaObject obj) { NameValueCollection dict; _persistentValues.TryGetValue(obj, out dict); return(dict); }
protected internal void RegisterObject(SoodaObject o) { // Console.WriteLine("Registering object {0}...", o.GetObjectKey()); object pkValue = o.GetPrimaryKeyValue(); // Console.WriteLine("Adding key: " + o.GetObjectKey() + " of type " + o.GetType()); for (ClassInfo ci = o.GetClassInfo(); ci != null; ci = ci.InheritsFromClass) { AddObjectWithKey(ci.Name, pkValue, o); List <WeakSoodaObject> al; if (!_objectsByClass.TryGetValue(ci.Name, out al)) { al = new List <WeakSoodaObject>(); _objectsByClass[ci.Name] = al; } al.Add(new WeakSoodaObject(o)); } if (!UseWeakReferences) { _strongReferences.Add(o); } _objectList.Add(new WeakSoodaObject(o)); if (_precommitQueue != null) { _precommitQueue.Enqueue(o); } }
public void InternalRemove(SoodaObject c) { if (!childType.IsInstanceOfType(c)) { return; } if (items == null) { if (tempItems == null) { tempItems = new Dictionary <SoodaObject, CollectionChange>(); } tempItems[c] = CollectionChange.Removed; return; } int pos; if (!items.TryGetValue(c, out pos)) { throw new InvalidOperationException("Attempt to remove object not in collection"); } SoodaObject lastObj = itemsArray[itemsArray.Count - 1]; if (lastObj != c) { itemsArray[pos] = lastObj; items[lastObj] = pos; } itemsArray.RemoveAt(itemsArray.Count - 1); items.Remove(c); }
internal void PrecommitClasses(IEnumerable <string> classes) { if (classes == null) { // don't know what to precommit - precommit everything SaveObjectChanges(true, null); return; } List <SoodaObject> objectsToPrecommit = new List <SoodaObject>(); foreach (string className in classes) { List <WeakSoodaObject> dirtyObjects = GetDirtyObjectsByClassName(className); if (dirtyObjects != null) { foreach (WeakSoodaObject wr in dirtyObjects) { SoodaObject o = wr.TargetSoodaObject; if (o != null) { objectsToPrecommit.Add(o); } } } } SaveObjectChanges(true, objectsToPrecommit); }
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); } }
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); } }
public override bool ShouldCacheObject(SoodaObject theObject) { if (theObject.GetClassInfo().Cardinality == ClassCardinality.Small) return true; else return false; }
public static SoqlBooleanExpression FieldEquals(string field, SoodaObject obj) { return(new SoqlBooleanRelationalExpression( new SoqlPathExpression(field), new SoqlLiteralExpression(obj.GetPrimaryKeyValue()), SoqlRelationalOperator.Equal)); }
private static int DoCompare(object v1, object v2, SoodaObject dbo1, SoodaObject dbo2) { if (v1 == null) { if (v2 == null) { return(PrimaryKeyCompare(dbo1, dbo2)); } else { return(-1); // null is less than anything } } ; if (v2 == null) { return(1); // not null is greater than anything } int v = ((IComparable)v1).CompareTo(v2); if (v == 0) { return(PrimaryKeyCompare(dbo1, dbo2)); } else { return(v); } }
internal void AddToPostCommitQueue(SoodaObject o) { if (transactionLogger.IsTraceEnabled) { transactionLogger.Trace("Adding {0} to post-commit queue", o.GetObjectKeyString()); } _postCommitQueue.Add(o); }
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); }
internal void PrecommitObject(SoodaObject o) { if (!o.VisitedOnCommit && !o.IsMarkedForDelete()) { MarkPrecommitted(o); o.SaveObjectChanges(); } }
void DoDeletes(SoodaObject obj) { List <TableInfo> tables = obj.GetClassInfo().UnifiedTables; for (int i = tables.Count - 1; i >= 0; --i) { DoDeletesForTable(obj, tables[i]); } }
public SoodaObjectOneToManyCollection(SoodaTransaction tran, Type childType, SoodaObject parentObject, string childRefField, Sooda.Schema.ClassInfo classInfo, SoodaWhereClause additionalWhereClause, bool cached) : base(tran, classInfo) { this.childType = childType; this.parentObject = parentObject; this.childRefField = childRefField; this.additionalWhereClause = additionalWhereClause; this.cached = cached; }
void DoDeletesForTable(SoodaObject obj, TableInfo table) { StringBuilder builder = new StringBuilder(); ArrayList queryParams = new ArrayList(); builder.Append("delete from "); builder.Append(table.DBTableName); DoWithWhere(obj, builder, queryParams, true); }
private void AddObjectWithKey(string className, object keyValue, SoodaObject obj) { // Console.WriteLine("AddObjectWithKey('{0}',{1})", className, keyValue); if (keyValue == null) { keyValue = ""; } GetObjectDictionaryForClass(className)[keyValue] = new WeakSoodaObject(obj); }
void DoInsertsForTable(SoodaObject obj, TableInfo table, bool isPrecommit) { if (table.IsDynamic && obj.GetFieldValue(table.Fields[table.Fields.Count - 1].ClassUnifiedOrdinal) == null) { // optimization: don't insert null dynamic fields return; } StringBuilder builder = new StringBuilder(500); builder.Append("insert into "); builder.Append(table.DBTableName); builder.Append('('); ArrayList par = new ArrayList(); bool comma = false; foreach (FieldInfo fi in table.Fields) { if (fi.ReadOnly) { continue; } if (comma) { builder.Append(','); } comma = true; builder.Append(fi.DBColumnName); } builder.Append(") values ("); comma = false; foreach (FieldInfo fi in table.Fields) { if (fi.ReadOnly) { continue; } if (comma) { builder.Append(','); } comma = true; object val = GetFieldValue(obj, fi, isPrecommit); builder.Append('{'); builder.Append(par.Add(val)); builder.Append(':'); builder.Append(fi.DataType); builder.Append('}'); } builder.Append(')'); SqlBuilder.BuildCommandWithParameters(_updateCommand, true, builder.ToString(), par.ToArray(), false); FlushUpdateCommand(false); }
internal string GetPersistentValue(SoodaObject obj, string name) { NameValueCollection dict; if (!_persistentValues.TryGetValue(obj, out dict)) { return(null); } return(dict[name]); }
static int Compare(SoodaObject o1, SoodaObject o2) { int retval = string.CompareOrdinal(o1.GetClassInfo().Name, o2.GetClassInfo().Name); if (retval != 0) { return(retval); } return(((IComparable)o1.GetPrimaryKeyValue()).CompareTo(o2.GetPrimaryKeyValue())); }
internal void SetPersistentValue(SoodaObject obj, string name, string value) { NameValueCollection dict; if (!_persistentValues.TryGetValue(obj, out dict)) { dict = new NameValueCollection(); _persistentValues.Add(obj, dict); } dict[name] = value; }
public override bool ShouldCacheObject(SoodaObject theObject) { if (theObject.GetClassInfo().Cardinality == ClassCardinality.Small) { return(true); } else { 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 bool InternalContains(SoodaObject obj) { if (obj == null) { return(false); } if (itemsArray == null) { LoadData(); } return(items.ContainsKey(obj)); }
protected int InternalAdd(SoodaObject obj) { if (itemsArray == null) { return(-1); } if (!items.ContainsKey(obj)) { items.Add(obj, itemsArray.Count); itemsArray.Add(obj); } return(-1); }
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 bool ShouldCacheObject(SoodaObject theObject) { return false; }
protected Sooda.QL.TypedWrappers.SoqlBooleanWrapperExpression ContainsImpl(SoodaObject obj) { return new SoqlBooleanWrapperExpression(new SoqlContainsExpression(_left, _collectionName, new SoqlLiteralExpression(obj == null ? null : obj.GetPrimaryKeyValue()))); }
object GetFieldValue(SoodaObject obj, FieldInfo fi, bool isPrecommit) { object val = obj.GetFieldValue(fi.ClassUnifiedOrdinal); if (!fi.IsNullable && SqlBuilder.IsNullValue(val, fi)) { if (!isPrecommit) throw new SoodaDatabaseException(obj.GetObjectKeyString() + "." + fi.Name + " cannot be null on commit."); val = fi.PrecommitTypedValue; if (val == null) throw new SoodaDatabaseException(obj.GetObjectKeyString() + "." + fi.Name + " is null on precommit and no 'precommitValue' has been defined for it."); if (val == SchemaInfo.NullPrecommitValue) val = null; if (logger.IsDebugEnabled) { logger.Debug("Using precommit value of {0} for {1}.{2}", val, fi.Table.NameToken, fi.Name); } } return val; }
protected void InternalRemove(SoodaObject obj) { if (itemsArray == null) return; int pos; if (!items.TryGetValue(obj, out pos)) return; SoodaObject lastObj = itemsArray[itemsArray.Count - 1]; if (lastObj != obj) { itemsArray[pos] = lastObj; items[lastObj] = pos; } itemsArray.RemoveAt(itemsArray.Count - 1); items.Remove(obj); }
protected int InternalAdd(SoodaObject obj) { if (itemsArray == null) return -1; if (!items.ContainsKey(obj)) { items.Add(obj, itemsArray.Count); itemsArray.Add(obj); } return -1; }
public static void LoadAllData(SoodaObject theObject) { theObject.LoadAllData(); }
public static object GetBoxedFieldValue(SoodaObject theObject, int tableNumber, int fieldOrdinal) { theObject.EnsureDataLoaded(tableNumber); return theObject._fieldValues.GetBoxedFieldValue(fieldOrdinal); }
public static bool IsFieldNull(SoodaObject theObject, int tableNumber, int fieldOrdinal) { theObject.EnsureDataLoaded(tableNumber); return theObject._fieldValues.IsNull(fieldOrdinal); }
public static bool IsFieldDirty(SoodaObject theObject, int tableNumber, int fieldOrdinal) { return theObject.IsFieldDirty(fieldOrdinal); }
public static SoodaObjectFieldValues GetFieldValuesForRead(SoodaObject t, int tableNumber) { t.EnsureDataLoaded(tableNumber); return t._fieldValues; }
public WeakSoodaObject(SoodaObject target) : base(target) { }
public static void SetPlainFieldValue(SoodaObject theObject, int tableNumber, string fieldName, int fieldOrdinal, object newValue, SoodaFieldUpdateDelegate before, SoodaFieldUpdateDelegate after) { theObject.SetPlainFieldValue(tableNumber, fieldName, fieldOrdinal, newValue, before, after); }
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); }
protected void AddObjectToSnapshot(SoodaObject o) { items.Add(o); count = items.Count; }
public static SoqlBooleanExpression FieldEquals(string field, SoodaObject obj) { return new SoqlBooleanRelationalExpression( new SoqlPathExpression(field), new SoqlLiteralExpression(obj.GetPrimaryKeyValue()), SoqlRelationalOperator.Equal); }
public bool InternalContains(SoodaObject obj) { if (obj == null) return false; if (itemsArray == null) LoadData(); return items.ContainsKey(obj); }
public static SoqlBooleanExpression CollectionFor(CollectionManyToManyInfo coll, SoodaObject parent) { SoqlPathExpression needle = new SoqlPathExpression(coll.GetItemClass().GetFirstPrimaryKeyField().Name); RelationInfo relation = coll.GetRelationInfo(); SoqlQueryExpression query = new SoqlQueryExpression(); query.SelectExpressions.Add(new SoqlPathExpression(relation.Table.Fields[coll.MasterField].Name)); query.SelectAliases.Add(string.Empty); query.From.Add(relation.Name); query.FromAliases.Add(string.Empty); query.WhereClause = FieldEquals(relation.Table.Fields[1 - coll.MasterField].Name, parent); SoqlExpressionCollection haystack = new SoqlExpressionCollection(); haystack.Add(query); return new SoqlBooleanInExpression(needle, haystack); }
void DoUpdatesForTable(SoodaObject obj, TableInfo table, bool isPrecommit) { if (table.IsDynamic) { // For dynamic fields do DELETE+INSERT instead of UPDATE. // This is because if a dynamic field is added to an existing object, there is no dynamic field row to update. // Another reason is that we never store null dynamic fields in the database - an INSERT will be ommitted in this case. DoDeletesForTable(obj, table); DoInsertsForTable(obj, table, true); return; } StringBuilder builder = new StringBuilder(500); builder.Append("update "); builder.Append(table.DBTableName); builder.Append(" set "); ArrayList par = new ArrayList(); bool anyChange = false; foreach (FieldInfo fi in table.Fields) { if (obj.IsFieldDirty(fi.ClassUnifiedOrdinal)) { if (anyChange) builder.Append(", "); FieldEquals(fi, GetFieldValue(obj, fi, isPrecommit), builder, par); anyChange = true; } } if (!anyChange) return; DoWithWhere(obj, builder, par, false); }
public void InternalRemove(SoodaObject c) { if (!childType.IsInstanceOfType(c)) return; if (items == null) { if (tempItems == null) tempItems = new Dictionary<SoodaObject, CollectionChange>(); tempItems[c] = CollectionChange.Removed; return; } int pos; if (!items.TryGetValue(c, out pos)) throw new InvalidOperationException("Attempt to remove object not in collection"); SoodaObject lastObj = itemsArray[itemsArray.Count - 1]; if (lastObj != c) { itemsArray[pos] = lastObj; items[lastObj] = pos; } itemsArray.RemoveAt(itemsArray.Count - 1); items.Remove(c); }
void DoInsertsForTable(SoodaObject obj, TableInfo table, bool isPrecommit) { if (table.IsDynamic && obj.GetFieldValue(table.Fields[table.Fields.Count - 1].ClassUnifiedOrdinal) == null) { // optimization: don't insert null dynamic fields return; } StringBuilder builder = new StringBuilder(500); builder.Append("insert into "); builder.Append(table.DBTableName); builder.Append('('); ArrayList par = new ArrayList(); bool comma = false; foreach (FieldInfo fi in table.Fields) { if (fi.ReadOnly) continue; if (comma) builder.Append(','); comma = true; builder.Append(fi.DBColumnName); } builder.Append(") values ("); comma = false; foreach (FieldInfo fi in table.Fields) { if (fi.ReadOnly) continue; if (comma) builder.Append(','); comma = true; object val = GetFieldValue(obj, fi, isPrecommit); builder.Append('{'); builder.Append(par.Add(val)); builder.Append(':'); builder.Append(fi.DataType); builder.Append('}'); } builder.Append(')'); SqlBuilder.BuildCommandWithParameters(_updateCommand, true, builder.ToString(), par.ToArray(), false); FlushUpdateCommand(false); }
public bool GetExpirationTimeout(SoodaObject theObject, out TimeSpan expirationTimeout, out bool slidingExpiration) { expirationTimeout = _expirationTimeout; slidingExpiration = _slidingExpiration; return true; }
public override IDataReader LoadObject(SoodaObject obj, object keyVal, out TableInfo[] loadedTables) { return LoadObjectTable(obj, keyVal, 0, out loadedTables); }
public void InternalAdd(SoodaObject c) { if (!childType.IsInstanceOfType(c)) return; if (items == null) { if (tempItems == null) tempItems = new Dictionary<SoodaObject, CollectionChange>(); tempItems[c] = CollectionChange.Added; return; } if (items.ContainsKey(c)) return; items.Add(c, itemsArray.Count); itemsArray.Add(c); }
bool predicate1(SoodaObject c0) { Contact c = (Contact)c0; return (string)c.Name != "Mary Manager"; }
public abstract bool ShouldCacheObject(SoodaObject theObject);