public void AddColumn(Column column) { ExceptionUtils.ThrowIfNull(column, "column"); ExceptionUtils.ThrowIfNull(column.Table, "column.Table"); string tableName = string.IsNullOrEmpty(column.Table.TableAliasName) ? column.Table.TableName : column.Table.TableAliasName; ExceptionUtils.ThrowIfEmptyString(tableName, "tableName"); if (m_TableInfo == null) { m_TableInfo = new ObjectTableInfo(tableName); } else if (m_TableInfo.TableName != tableName) { throw new MappingException("The class \"{0}\" has more than one table mapped to it (\"{1}\" and \"{2}\"). This is not allowed in this version.", m_ObjectPath, m_TableInfo.TableName, tableName); } m_TableInfo.AddColumn(column); }
protected virtual void BuildObjectSql(Dictionary <string, ObjectPath> objectPaths, SpringBaseDao baseDao) { List <string> columnNames = new List <string>(); List <string> columnParamNames = new List <string>(); foreach (ObjectPath objectPath in objectPaths.Values) { ObjectTableInfo tableInfo = objectPath.TableInfo; if (tableInfo != null) { columnNames.Clear(); columnNames.Capacity = tableInfo.Columns.Count; columnParamNames.Clear(); columnParamNames.Capacity = tableInfo.Columns.Count; foreach (Column column in tableInfo.Columns) { if (!column.NoLoad) { columnNames.Add(column.ColumnName); columnParamNames.Add(baseDao.DbProvider.CreateParameterName(column.ColumnName)); } } if (columnNames.Count > 0) { string columnNameStr = StringUtils.Join(",", columnNames); string columnParamNamesStr = StringUtils.Join(",", columnParamNames); objectPath.InsertSql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", tableInfo.TableName, columnNameStr, columnParamNamesStr); objectPath.SelectSql = string.Format("SELECT {0} FROM {1}", columnNameStr, tableInfo.TableName); } else { objectPath.InsertSql = null; objectPath.SelectSql = null; } } } }
protected virtual IList LoadObjectInstancesToList(Type objectType, string objectPath, ObjectPath objectPathInstance, IDictionary <string, DbAppendSelectWhereClause> appendSelectWhereClauseMap, MappingContext mappingContext, IDbCommand command) { IList list = null; using (IDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ObjectTableInfo tableInfo = objectPathInstance.TableInfo; if (tableInfo != null) { if (tableInfo.Columns.Count > reader.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(), tableInfo.Columns.Count.ToString(), objectPath, objectPathInstance.SelectSql)); } object objectToSet = Activator.CreateInstance(objectType); int i = 0; foreach (Column column in tableInfo.Columns) { column.SetMemberValue(objectToSet, reader.GetValue(i++)); } if (list == null) { list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(objectType)); } list.Add(objectToSet); } } } if (list != null) { int elementCount = 0; foreach (object parent in list) { // Load children foreach (Relation relation in objectPathInstance.Relations) { if (relation is OneToManyRelation) { string elementTypePath = Utils.CombineTypePath(objectPath, relation.MemberInfo.Name); IList elements = LoadFromDatabase(relation.MemberValueType, elementTypePath, parent, relation, appendSelectWhereClauseMap, mappingContext, command); if (elements != null) { relation.SetMemberValue(parent, elements); } } else if (relation is OneToOneRelation) { string elementTypePath = Utils.CombineTypePath(objectPath, relation.MemberInfo.Name); IList elements = LoadFromDatabase(relation.MemberValueType, elementTypePath, parent, relation, appendSelectWhereClauseMap, mappingContext, command); if (elements != null) { if (elements.Count != 1) { throw new InvalidOperationException(string.Format("Relation is One-To-One but got more than one element: {0}", relation.ToString())); } relation.SetMemberValue(parent, elements[0]); } } else { throw new NotImplementedException(string.Format("Relationship not implemented: {0}", relation.ToString())); } } ++elementCount; } } return(list); }
protected virtual Dictionary <string, IList> LoadObjectInstancesToList(Type objectType, string objectPath, ObjectPath objectPathInstance, Dictionary <string, object> pkParentMap, IDictionary <string, DbAppendSelectWhereClause> appendSelectWhereClauseMap, MappingContext mappingContext, IDbCommand command) { Dictionary <string, IList> list = null; Dictionary <string, object> pkMap = null; Dictionary <object, bool> anyInstanceFieldsWereSetMap = null; using (IDataReader reader = command.ExecuteReader()) { object objectToSet = null; while (reader.Read()) { ObjectTableInfo tableInfo = objectPathInstance.TableInfo; if (tableInfo != null) { if (objectToSet == null) { objectToSet = Activator.CreateInstance(objectType); } int i = 0; string pk = null, fk = null; bool skip = false; bool anyInstanceFieldsWereSet = false; foreach (Column column in tableInfo.Columns) { if (!column.NoLoad) { object value = reader.GetValue(i++); column.SetMemberValue(objectToSet, value); if (column.IsPrimaryKey) { pk = value.ToString(); } else if (column.IsForeignKey) { fk = value.ToString(); if ((pkParentMap != null) && !pkParentMap.ContainsKey(fk)) { skip = true; break; } } else { if ((value != null) && !(value is DBNull)) { anyInstanceFieldsWereSet = true; } } } } if (skip) { continue; } if (i != reader.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(), i.ToString(), objectPath, objectPathInstance.SelectSql)); } if (pk == null) { // Object does not have a primary key, auto generate one here. This use case is only valid when // loading a single table of objects pk = StringUtils.CreateSequentialGuid(); } if (fk == null) { // Object does not have fk, so set fk to pk fk = pk; } if (list == null) { list = new Dictionary <string, IList>(); } IList listInstance; if (!list.TryGetValue(fk, out listInstance)) { listInstance = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(objectType)); list.Add(fk, listInstance); } if (pkMap == null) { pkMap = new Dictionary <string, object>(); } if (anyInstanceFieldsWereSetMap == null) { anyInstanceFieldsWereSetMap = new Dictionary <object, bool>(); } listInstance.Add(objectToSet); pkMap.Add(pk, objectToSet); anyInstanceFieldsWereSetMap.Add(objectToSet, anyInstanceFieldsWereSet); objectToSet = null; } } } if (list != null) { // Load children foreach (Relation relation in objectPathInstance.Relations) { if (relation is OneToManyRelation) { Dictionary <string, IList> elementsMap = LoadFromDatabase(relation.MemberValueType, relation.MemberInfoPath, pkMap, relation, appendSelectWhereClauseMap, mappingContext, command); if (elementsMap != null) { foreach (KeyValuePair <string, IList> pair in elementsMap) { object objectToSet = pkMap[pair.Key]; relation.SetMemberValue(objectToSet, pair.Value); if (!CollectionUtils.IsNullOrEmpty(pair.Value)) { anyInstanceFieldsWereSetMap[objectToSet] = true; } } } } else if (relation is OneToOneRelation) { Dictionary <string, IList> elementsMap = LoadFromDatabase(relation.MemberValueType, relation.MemberInfoPath, pkMap, relation, appendSelectWhereClauseMap, mappingContext, command); if (elementsMap != null) { foreach (KeyValuePair <string, IList> pair in elementsMap) { if (pair.Value.Count != 1) { throw new InvalidOperationException(string.Format("Relation is One-To-One but got more than one element: {0} to {1}", relation.ChildTable.TableName, relation.ParentTable.TableName)); } object itemToSet = CollectionUtils.FirstItem(pair.Value); object objectToSet = pkMap[pair.Key]; relation.SetMemberValue(objectToSet, itemToSet); if (itemToSet != null) { anyInstanceFieldsWereSetMap[objectToSet] = true; } } } } else { throw new NotImplementedException(string.Format("Relationship not implemented: {0}", relation.ToString())); } } } #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); }