protected virtual bool PopulateSaveValues(IDbCommand command, object parentOfObjectToSave, Table parentTableOfObjectToSave, object objectToSave, ColumnCachedValues cachedValues, ICollection <SameTableElementInfo> sameTableElements) { bool anyPopulated = false; if (sameTableElements != null) { foreach (SameTableElementInfo sameTableElementInfo in sameTableElements) { object objectInstance = sameTableElementInfo.GetMemberValue(objectToSave); if (PopulateSaveValues(command, objectToSave, parentTableOfObjectToSave, objectInstance, cachedValues, sameTableElementInfo.DirectColumns)) { anyPopulated = true; } if (PopulateSaveValues(command, objectToSave, parentTableOfObjectToSave, objectInstance, cachedValues, sameTableElementInfo.ChildSameTableElements)) { anyPopulated = true; } } } return(anyPopulated); }
public override void SetSelectColumnValue <T>(T objectToSet, object value, ColumnCachedValues cachedValues) { base.SetSelectColumnValue(objectToSet, value, cachedValues); if (cachedValues != null) { cachedValues.ObjectToPrimaryKeyMap[objectToSet] = value; } }
public virtual IList LoadFromDatabase(Type typeToLoad, SpringBaseDao baseDao, IDictionary <string, DbAppendSelectWhereClause> appendSelectWhereClauses, Type mappingAttributesType) { MappingContext mappingContext = MappingContext.GetMappingContext(typeToLoad, mappingAttributesType); BuildObjectSql(mappingContext, baseDao); Dictionary <object, IList> list = null; baseDao.AdoTemplate.ClassicAdoTemplate.Execute(delegate(IDbCommand command) { ColumnCachedValues cachedValues = new ColumnCachedValues(); list = LoadObjectInstancesToList(typeToLoad, null, null, appendSelectWhereClauses, cachedValues, mappingContext, command); return(null); }); if (list != null) { IList rtnList = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(typeToLoad)); foreach (KeyValuePair <object, IList> pair in list) { foreach (object listElement in pair.Value) { rtnList.Add(listElement); } } foreach (object obj in rtnList) { IAfterLoadFromDatabase afterLoadFromDatabase = obj as IAfterLoadFromDatabase; if (afterLoadFromDatabase != null) { afterLoadFromDatabase.AfterLoadFromDatabase(baseDao); } } return(rtnList); } else { return(null); } }
protected virtual void LoadSameTableInstances(object objectToSet, ICollection <SameTableElementInfo> sameTableElements, ColumnCachedValues cachedValues, IDataReader reader, ref int readerIndex, ref bool anyObjectToSetFieldsWereSet) { if (sameTableElements != null) { foreach (SameTableElementInfo sameTableElementInfo in sameTableElements) { object sameTableElementObjectToSet = null; foreach (Column column in sameTableElementInfo.DirectColumns) { if (!column.NoLoad) { object value = reader.GetValue(readerIndex++); if (value == DBNull.Value) { value = null; } if (value != null) { if (sameTableElementObjectToSet == null) { sameTableElementObjectToSet = Activator.CreateInstance(sameTableElementInfo.MemberType); } column.SetSelectColumnValue(sameTableElementObjectToSet, value, cachedValues); } } } if (sameTableElementObjectToSet != null) { sameTableElementInfo.SetMemberValue(objectToSet, sameTableElementObjectToSet); anyObjectToSetFieldsWereSet = true; } LoadSameTableInstances(objectToSet, sameTableElementInfo.ChildSameTableElements, cachedValues, reader, ref readerIndex, ref anyObjectToSetFieldsWereSet); } } }
protected virtual bool PopulateSaveValues(IDbCommand command, object parentOfObjectToSave, Table parentTableOfObjectToSave, object objectToSave, ColumnCachedValues cachedValues, ICollection <Column> columns) { bool anyPopulated = false; if (columns != null) { foreach (Column column in columns) { if (!column.NoLoad) { IDbDataParameter parameter = command.CreateParameter(); parameter.DbType = column.ColumnType.Value; parameter.Direction = ParameterDirection.Input; parameter.Size = column.ColumnSize; parameter.ParameterName = column.ColumnName; if (column.IsForeignKey) { Table foreignTable = ((ForeignKeyColumn)column).ForeignTable; ExceptionUtils.ThrowIfNull(parentTableOfObjectToSave, "parentTableOfObjectToSave"); if (foreignTable == parentTableOfObjectToSave) { parameter.Value = column.GetInsertColumnValue(parentOfObjectToSave, objectToSave, cachedValues); } else { parameter.Value = DBNull.Value; } } else { parameter.Value = column.GetInsertColumnValue(parentOfObjectToSave, objectToSave, cachedValues); } command.Parameters.Add(parameter); anyPopulated = true; } } } return(anyPopulated); }
protected virtual Dictionary <object, IList> LoadObjectInstancesToList(Type objectTypeToLoad, Table objectTypeToLoadTable, Dictionary <object, object> parentPKToObjectMap, IDictionary <string, DbAppendSelectWhereClause> appendSelectWhereClauseMap, ColumnCachedValues cachedValues, MappingContext mappingContext, IDbCommand command) { if (objectTypeToLoadTable == null) { objectTypeToLoadTable = mappingContext.GetTableForType(objectTypeToLoad); } Dictionary <object, IList> list = null; if (!string.IsNullOrEmpty(objectTypeToLoadTable.SelectSql)) { command.CommandText = objectTypeToLoadTable.SelectSql; command.CommandType = CommandType.Text; command.Parameters.Clear(); AppendSelectWhereClause(objectTypeToLoadTable.TableName, command, appendSelectWhereClauseMap); list = LoadObjectInstancesToList(objectTypeToLoadTable, parentPKToObjectMap, appendSelectWhereClauseMap, cachedValues, mappingContext, command); } return(list); }
public virtual bool PopulateSaveValues(IDbCommand command, bool isUpdate, object parentOfObjectToSave, Table parentTableOfObjectToSave, object objectToSave, ColumnCachedValues cachedValues) { bool anyPopulated = false; command.CommandText = isUpdate ? UpdateSql : InsertSql; if (!string.IsNullOrEmpty(command.CommandText)) { command.CommandType = CommandType.Text; command.Parameters.Clear(); if (PopulateSaveValues(command, parentOfObjectToSave, parentTableOfObjectToSave, objectToSave, cachedValues, DirectColumns)) { anyPopulated = true; } if (PopulateSaveValues(command, parentOfObjectToSave, parentTableOfObjectToSave, objectToSave, cachedValues, m_ChildSameTableElements)) { anyPopulated = true; } } return(anyPopulated); }
public override void SetSelectColumnValue <T>(T objectToSet, object value, ColumnCachedValues cachedValues) { }
public override object GetInsertColumnValue(object parentOfObjectToSet, object objectToSave, ColumnCachedValues cachedValues) { ExceptionUtils.ThrowIfNull(parentOfObjectToSet, "parentOfObjectToSave"); object pkGuid; ExceptionUtils.ThrowIfFalse(cachedValues.ObjectToPrimaryKeyMap.TryGetValue(parentOfObjectToSet, out pkGuid), "Missing FK value"); return(pkGuid); }
public override void SetSelectColumnValue <T>(T objectToSet, object value, ColumnCachedValues cachedValues) { DebugUtils.AssertDebuggerBreak(cachedValues != null); cachedValues.ObjectToPrimaryKeyMap[objectToSet] = value; }
public override object GetInsertColumnValue(object parentOfObjectToSave, object objectToSave, ColumnCachedValues cachedValues) { object pkGuid; if (Utils.IsValidColumnType(objectToSave.GetType())) { // This is a virtual table for string[], int[], enum[], etc. pkGuid = StringUtils.CreateSequentialGuid().ToUpper(); } else { ExceptionUtils.ThrowIfNull(cachedValues, "cachedValues"); if (!cachedValues.ObjectToPrimaryKeyMap.TryGetValue(objectToSave, out pkGuid)) { pkGuid = StringUtils.CreateSequentialGuid().ToUpper(); cachedValues.ObjectToPrimaryKeyMap[objectToSave] = pkGuid; } else { DebugUtils.CheckDebuggerBreak(); } } return(pkGuid); }
public override object GetInsertColumnValue(object parentOfObjectToSave, object objectToSave, ColumnCachedValues cachedValues) { object pkGuid = base.GetInsertColumnValue(parentOfObjectToSave, objectToSave, cachedValues); ExceptionUtils.ThrowIfNull(pkGuid, "pkGuid"); if (cachedValues != null) { cachedValues.ObjectToPrimaryKeyMap[objectToSave] = pkGuid; } return(pkGuid); }
public virtual void SetSelectColumnValue <T>(T objectToSet, object value, ColumnCachedValues cachedValues) { base.SetMemberValue <T>(objectToSet, value); }
public virtual object GetInsertColumnValue(object parentOfObjectToSave, object objectToSave, ColumnCachedValues cachedValues) { object value = null; if (MemberInfo == null) { if (objectToSave != null) { // This would be string, int, double, etc. virtual table DebugUtils.AssertDebuggerBreak(Utils.IsValidColumnType(objectToSave.GetType())); value = objectToSave; } } else { value = GetMemberValue(objectToSave); } if (value == null) { if (this.IsNullable) { value = DBNull.Value; } else { throw new MappingException("A column.MemberInfo has a null value, but the column is specified as non-null: {0}", this.ToString()); } } else if (m_IsDbBoolString) { value = ((bool)value) ? "Y" : "N"; } else if (m_IsTenDigitDateString) { value = ((DateTime)value).ToString("yyyy-MM-dd"); } else if (m_IsTimeType) { if (m_MemberType == typeof(string)) { TimeSpan timeSpan = TimeSpan.Parse(value.ToString()); value = new DateTime(1900, 1, 1, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); } else { DateTime existingValue = (DateTime)value; value = new DateTime(1900, 1, 1, existingValue.Hour, existingValue.Minute, existingValue.Second); } } else if ((m_ColumnType == DbType.DateTime) || (m_ColumnType == DbType.Date)) { if (m_MemberType == typeof(string)) { DateTime dateTime = DateTime.Parse(value.ToString()); return(MakeValidDbDateTime(dateTime)); } else { DateTime dateTime = (DateTime)value; return(MakeValidDbDateTime(dateTime)); } } return(value); }
protected virtual Dictionary <object, IList> LoadObjectInstancesToList(Table tableOfObjectsToLoad, Dictionary <object, object> parentPKToObjectMap, IDictionary <string, DbAppendSelectWhereClause> appendSelectWhereClauseMap, ColumnCachedValues cachedValues, MappingContext mappingContext, IDbCommand command) { Dictionary <object, IList> list = null; Dictionary <object, bool> anyInstanceFieldsWereSetMap = null; Dictionary <object, object> pkToObjectMap = null; bool isVirtualObjectTable = tableOfObjectsToLoad.IsVirtualTable; bool isCustomXmlStringFormatTypeTable = false; if (isVirtualObjectTable) { isCustomXmlStringFormatTypeTable = tableOfObjectsToLoad.TableRootType.IsSubclassOf(typeof(CustomXmlStringFormatTypeBase)); } using (IDataReader reader = command.ExecuteReader()) { while (reader.Read()) { int readerIndex = 0; object pk = null, fk = null; bool skip = false; bool anyObjectToSetFieldsWereSet = false; Column pkColumn = null, fkColumn = null; object objectToSet = null; // isVirtualObjectTable == true if this table represents a string[], int[], etc. member foreach (Column column in tableOfObjectsToLoad.DirectColumns) { if (!column.NoLoad) { object value = reader.GetValue(readerIndex++); if (value == DBNull.Value) { value = null; } if (column.IsPrimaryKey) { pkColumn = column; pk = value; } else if (column.IsForeignKey) { if (value != null) { if (fkColumn != null) { if (pk != null) { throw new ArgException("The table \"{0}\" has a row with a primary key of \"{1}\" that has more than one foreign key specified. Please specify only one foreign key per row for this table.", column.Table.TableName, pk.ToString()); } else { throw new ArgException("The table \"{0}\" has a row that has more than one foreign key specified. Please specify only one foreign key per row for this table.", column.Table.TableName); } } if (!parentPKToObjectMap.ContainsKey(value)) { // This object has no parent, assume we skip it skip = true; break; } fkColumn = column; fk = value; } } else { if (value != null) { if (isVirtualObjectTable) { if (isCustomXmlStringFormatTypeTable) { CustomXmlStringFormatTypeBase newObjectToSet = (CustomXmlStringFormatTypeBase)Activator.CreateInstance(tableOfObjectsToLoad.TableRootType); newObjectToSet.SetValue(value); objectToSet = newObjectToSet; } else { objectToSet = column.GetSetMemberValue(value); } } else { if (objectToSet == null) { objectToSet = Activator.CreateInstance(tableOfObjectsToLoad.TableRootType); } column.SetSelectColumnValue(objectToSet, value, cachedValues); anyObjectToSetFieldsWereSet = true; } } } } } if (skip) { continue; } if (objectToSet == null) { objectToSet = Activator.CreateInstance(tableOfObjectsToLoad.TableRootType); } ExceptionUtils.ThrowIfNull(pkColumn, "pkColumn"); ExceptionUtils.ThrowIfNull(pk, "pk"); if (!isVirtualObjectTable) { pkColumn.SetSelectColumnValue(objectToSet, pk, cachedValues); if (fkColumn != null) { fkColumn.SetSelectColumnValue(objectToSet, fk, cachedValues); } else { if (parentPKToObjectMap != null) { throw new ArgException("The table \"{0}\" has a row with a primary key of \"{1}\" that does not have a foreign key specified.", pkColumn.Table.TableName, pk.ToString()); } fk = pk; } LoadSameTableInstances(objectToSet, tableOfObjectsToLoad.ChildSameTableElements, cachedValues, reader, ref readerIndex, ref anyObjectToSetFieldsWereSet); } int fieldCount = reader.FieldCount; if (readerIndex != fieldCount) { throw new IndexOutOfRangeException(string.Format("The number of selected column values ({0}) is less than the expected number ({1}) for the object \"{2}\" and sql \"{3}\".", reader.FieldCount.ToString(), readerIndex.ToString(), tableOfObjectsToLoad.TableRootType.Name, tableOfObjectsToLoad.SelectSql)); } if (list == null) { list = new Dictionary <object, IList>(); pkToObjectMap = new Dictionary <object, object>(); anyInstanceFieldsWereSetMap = new Dictionary <object, bool>(); } IList listInstance; if (!list.TryGetValue(fk, out listInstance)) { listInstance = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(tableOfObjectsToLoad.TableRootType)); list.Add(fk, listInstance); } try { listInstance.Add(objectToSet); } catch (Exception) { throw; } if (!isVirtualObjectTable) { pkToObjectMap.Add(pk, objectToSet); anyInstanceFieldsWereSetMap.Add(objectToSet, anyObjectToSetFieldsWereSet); } } } if (!isVirtualObjectTable) { if (list != null) { // Load children if (tableOfObjectsToLoad.ChildRelationMembers != null) { foreach (ChildRelationInfo childRelation in tableOfObjectsToLoad.ChildRelationMembers) { Table elementTable = null; if (Utils.IsValidColumnType(childRelation.ValueType)) { elementTable = childRelation.ChildTable; } if (childRelation.IsOneToMany) { Dictionary <object, IList> fkMap = LoadObjectInstancesToList(childRelation.ValueType, elementTable, pkToObjectMap, appendSelectWhereClauseMap, cachedValues, mappingContext, command); if (fkMap != null) { foreach (KeyValuePair <object, IList> pair in fkMap) { object objectToSet = pkToObjectMap[pair.Key]; if (childRelation.ParentToMemberChain != null) { foreach (SameTableElementInfo sameTableElementInfo in childRelation.ParentToMemberChain) { object childObjectToSet = sameTableElementInfo.GetMemberValue(objectToSet); if (childObjectToSet == null) { childObjectToSet = Activator.CreateInstance(sameTableElementInfo.MemberType); } objectToSet = childObjectToSet; } } DebugUtils.AssertDebuggerBreak(pair.Value != null); childRelation.SetMemberValue(objectToSet, pair.Value); anyInstanceFieldsWereSetMap[objectToSet] = true; } } } else // One to one { Dictionary <object, IList> fkMap = LoadObjectInstancesToList(childRelation.ValueType, elementTable, pkToObjectMap, appendSelectWhereClauseMap, cachedValues, mappingContext, command); if (fkMap != null) { foreach (KeyValuePair <object, IList> pair in fkMap) { if (pair.Value.Count != 1) { throw new InvalidOperationException(string.Format("Relation is One-To-One but got more than one element: {0}", childRelation.ToString())); } object itemToSet = CollectionUtils.FirstItem(pair.Value); DebugUtils.AssertDebuggerBreak(itemToSet != null); object objectToSet = pkToObjectMap[pair.Key]; if (childRelation.ParentToMemberChain != null) { foreach (SameTableElementInfo sameTableElementInfo in childRelation.ParentToMemberChain) { object childObjectToSet = sameTableElementInfo.GetMemberValue(objectToSet); if (childObjectToSet == null) { childObjectToSet = Activator.CreateInstance(sameTableElementInfo.MemberType); } objectToSet = childObjectToSet; } } childRelation.SetMemberValue(objectToSet, itemToSet); anyInstanceFieldsWereSetMap[objectToSet] = true; } } } } } } } #if REMOVE_EMPTY_OBJECTS if (list != null) { List <string> removeKeys = new List <string>(); foreach (KeyValuePair <string, IList> pair in list) { if (pair.Value != null) { for (int i = pair.Value.Count - 1; i >= 0; --i) { object checkObject = pair.Value[i]; if (!anyInstanceFieldsWereSetMap[checkObject]) { pair.Value.RemoveAt(i); } } } if (CollectionUtils.IsNullOrEmpty(pair.Value)) { removeKeys.Add(pair.Key); } } foreach (string removeKey in removeKeys) { list.Remove(removeKey); } } #endif // REMOVE_EMPTY_OBJECTS return(CollectionUtils.IsNullOrEmpty(list) ? null : list); }