public static void Update(FieldInfo fi, SoodaTransaction transaction) { if (!fi.IsDynamic) { throw new InvalidOperationException(fi.Name + " is not a dynamic field"); } ClassInfo ci = fi.ParentClass; SoodaDataSource ds = transaction.OpenDataSource(ci.GetDataSource()); ds.ExecuteNonQuery("update SoodaDynamicField set nullable={0} where class={1} and field={2}", fi.IsNullable ? 1 : 0, ci.Name, fi.Name); }
protected void LoadDataFromReader() { SoodaDataSource ds = transaction.OpenDataSource(relationInfo.GetDataSource()); TableInfo[] loadedTables; using (IDataReader reader = ds.LoadRefObjectList(transaction.Schema, relationInfo, masterColumn, masterValue, out loadedTables)) { while (reader.Read()) { SoodaObject obj = SoodaObject.GetRefFromRecordHelper(transaction, _factory, reader, 0, loadedTables, 0); InternalAdd(obj); } } }
static void Load(SoodaTransaction transaction) { SchemaInfo schema = transaction.Schema; HashSet <ClassInfo> affectedClasses = new HashSet <ClassInfo>(); foreach (DataSourceInfo dsi in schema.DataSources) { if (!dsi.EnableDynamicFields) { continue; } SoodaDataSource ds = transaction.OpenDataSource(dsi); using (IDataReader r = ds.ExecuteRawQuery("select class, field, type, nullable, fieldsize, precision from SoodaDynamicField")) { while (r.Read()) { string className = r.GetString(0); ClassInfo ci = schema.FindClassByName(className); if (ci == null) { logger.Warn("Ignoring a dynamic field of non-existent class {0} -- see the SoodaDynamicField table", className); continue; } FieldInfo fi = new FieldInfo(); fi.ParentClass = ci; fi.Name = r.GetString(1); fi.TypeName = r.GetString(2); fi.IsNullable = r.GetInt32(3) != 0; if (!r.IsDBNull(4)) { fi.Size = r.GetInt32(4); } if (!r.IsDBNull(5)) { fi.Precision = r.GetInt32(5); } ci.LocalTables.Add(Prepare(fi)); affectedClasses.Add(ci); } } } Resolve(schema, affectedClasses); }
public void RawExpression() { using (SoodaTransaction tran = new SoodaTransaction()) { SoqlQueryExpression query = new SoqlQueryExpression(); query.From.Add("Contact"); query.FromAliases.Add(string.Empty); query.SelectExpressions.Add(new SoqlRawExpression("COUNT(*)")); query.SelectAliases.Add(string.Empty); SoodaDataSource ds = tran.OpenDataSource("default"); using (IDataReader r = ds.ExecuteQuery(query, tran.Schema)) { bool b = r.Read(); Assert.IsTrue(b); int c = r.GetInt32(0); Assert.AreEqual(7, c); } } }
public void PassDbConnection() { using (SoodaDataSource sds = _DatabaseSchema.GetSchema().GetDataSourceInfo("default").CreateDataSource()) { sds.Open(); // sds.ExecuteNonQuery(sql, params); using (IDataReader r = sds.ExecuteRawQuery("select count(*) from contact")) { bool b = r.Read(); Assert.IsTrue(b); int c = r.GetInt32(0); Assert.AreEqual(7, c); } using (SoodaTransaction tran = new SoodaTransaction()) { tran.RegisterDataSource(sds); int c = Contact.GetList(true).Count; Assert.AreEqual(7, c); } } }
public static void Remove(FieldInfo fi, SoodaTransaction transaction) { if (!fi.IsDynamic) { throw new InvalidOperationException(fi.Name + " is not a dynamic field"); } ClassInfo ci = fi.ParentClass; SoodaDataSource ds = transaction.OpenDataSource(ci.GetDataSource()); LockCookie lockCookie = LockWrite(transaction); try { ds.ExecuteNonQuery("delete from SoodaDynamicField where class={0} and field={1}", ci.Name, fi.Name); ds.ExecuteNonQuery("drop table " + fi.Table.DBTableName); ci.LocalTables.Remove(fi.Table); Resolve(ci); } finally { transaction.Schema._rwLock.DowngradeFromWriterLock(ref lockCookie); } }
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; } } }
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); } } }
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); } } } } }