Пример #1
0
		protected virtual string GetRemoveCollectionPropertyStatement(object obj, IPropertyMap propertyMap, IList parameters)
		{
			IClassMap classMap;
			IColumnMap idColumnMap;
			IPropertyMap idPropertyMap;
			ITableMap tableMap;
			string paramName = "";
			IObjectManager om = m_SqlEngineManager.Context.ObjectManager;
			classMap = propertyMap.ClassMap;
			tableMap = propertyMap.MustGetTableMap();

			SqlDeleteStatement delete = new SqlDeleteStatement(tableMap.SourceMap) ; 
			SqlTableAlias table = delete.GetSqlTableAlias(tableMap);

			delete.SqlFromClause.AddSqlAliasTableSource(table);

			idColumnMap = propertyMap.GetIdColumnMap();
			SqlColumnAlias idColumn = table.GetSqlColumnAlias(idColumnMap);
			idPropertyMap = classMap.MustGetPropertyMapForColumnMap(idColumnMap.MustGetPrimaryKeyColumnMap());
			
			paramName = GetParameterName(idPropertyMap, "Id_");
			SqlParameter param =  AddSqlParameter(delete, parameters, paramName, obj, idPropertyMap, om.GetPropertyValue(obj, idPropertyMap.Name), idColumnMap);

			SqlSearchCondition search = delete.SqlWhereClause.GetNextSqlSearchCondition();
			search.GetSqlComparePredicate(idColumn, SqlCompareOperatorType.Equals,  param);

			foreach (IColumnMap iIdColumnMap in propertyMap.GetAdditionalIdColumnMaps())
			{
				idColumnMap = iIdColumnMap;
				idColumn = table.GetSqlColumnAlias(idColumnMap);

				paramName = GetParameterName(propertyMap, idColumnMap, "Id_");
				if (!(classMap.GetTypeColumnMap() == null && classMap.GetTypeColumnMap() == idColumnMap.MustGetPrimaryKeyColumnMap()))
				{
					param = AddSqlParameter(delete, parameters, paramName, obj, null, classMap.TypeValue, idColumnMap, true);
				}
				else
				{
					idPropertyMap = classMap.MustGetPropertyMapForColumnMap(idColumnMap.MustGetPrimaryKeyColumnMap());
					param = AddSqlParameter(delete, parameters, paramName, obj, idPropertyMap, om.GetPropertyValue(obj, idPropertyMap.Name), idColumnMap);
				}
				search = delete.SqlWhereClause.GetNextSqlSearchCondition();
				search.GetSqlComparePredicate(idColumn, SqlCompareOperatorType.Equals,  param);
								
			}
			return GenerateSql(delete);
		}
Пример #2
0
		protected virtual string GetRemoveNonPrimaryStatement(object obj, ArrayList propertyMaps, IList parameters)
		{
			IClassMap classMap = null;
			IPropertyMap firstPropertyMap = null;
			IPropertyMap propertyMap;
			ITableMap tableMap = null;
			string compareOp = "";
			string wrappedValue;
			IObjectManager om;
			IPersistenceManager pm;
			IColumnMap idColumnMap = null;
			IPropertyMap idPropertyMap = null;
			string paramName = "";
			OptimisticConcurrencyBehaviorType propOptBehavior;
			om = m_SqlEngineManager.Context.ObjectManager;
			pm = m_SqlEngineManager.Context.PersistenceManager;
			foreach (IPropertyMap iPropertyMap in propertyMaps)
			{
				propertyMap = iPropertyMap;
				firstPropertyMap = propertyMap;
				classMap = propertyMap.ClassMap;
				tableMap = propertyMap.MustGetTableMap();
				idColumnMap = propertyMap.GetIdColumnMap();
				idPropertyMap = classMap.MustGetPropertyMapForColumnMap(idColumnMap.MustGetPrimaryKeyColumnMap());
				break;
			}

			SqlDeleteStatement delete = new SqlDeleteStatement(tableMap.SourceMap) ; 
			SqlTableAlias table = delete.GetSqlTableAlias(tableMap);
			SqlColumnAlias idColumn = table.GetSqlColumnAlias(idColumnMap);

			delete.SqlFromClause.AddSqlAliasTableSource(table);
			
			paramName = GetParameterName(idPropertyMap, "Id_");
			SqlParameter param = AddSqlParameter(delete, parameters, paramName, obj, idPropertyMap, om.GetPropertyValue(obj, idPropertyMap.Name), idColumnMap);

			SqlSearchCondition search = delete.SqlWhereClause.GetNextSqlSearchCondition();
			search.GetSqlComparePredicate(idColumn, SqlCompareOperatorType.Equals,  param);
			
			foreach (IColumnMap iIdColumnMap in firstPropertyMap.GetAdditionalIdColumnMaps())
			{
				idColumnMap = iIdColumnMap;
				idColumn = table.GetSqlColumnAlias(idColumnMap);

				paramName = GetParameterName(firstPropertyMap, idColumnMap, "Id_");
				if (!(classMap.GetTypeColumnMap() == null && classMap.GetTypeColumnMap() == idColumnMap.MustGetPrimaryKeyColumnMap()))
				{
					param = AddSqlParameter(delete, parameters, paramName, obj, null, classMap.TypeValue, idColumnMap, true);
				}
				else
				{
					idPropertyMap = classMap.MustGetPropertyMapForColumnMap(idColumnMap.MustGetPrimaryKeyColumnMap());
					param = AddSqlParameter(delete, parameters, paramName, obj, idPropertyMap, om.GetPropertyValue(obj, idPropertyMap.Name), idColumnMap);
				}
				search = delete.SqlWhereClause.GetNextSqlSearchCondition();
				search.GetSqlComparePredicate(idColumn, SqlCompareOperatorType.Equals,  param);
				
			}
			if (!(pm.GetDeleteOptimisticConcurrencyBehavior(OptimisticConcurrencyBehaviorType.DefaultBehavior, classMap) == OptimisticConcurrencyBehaviorType.Disabled))
			{
				foreach (IPropertyMap iPropertyMap in propertyMaps)
				{
					propertyMap = iPropertyMap;
					propOptBehavior = pm.GetDeleteOptimisticConcurrencyBehavior(OptimisticConcurrencyBehaviorType.DefaultBehavior, propertyMap);
					if (propOptBehavior == OptimisticConcurrencyBehaviorType.IncludeWhenLoaded || (propOptBehavior == OptimisticConcurrencyBehaviorType.IncludeWhenDirty && om.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.Dirty))
					{
						if (om.HasOriginalValues(obj, propertyMap.Name))
						{
							bool first = true;
							foreach (IColumnMap columnMap in propertyMap.GetAllColumnMaps())
							{
								SqlColumnAlias column = table.GetSqlColumnAlias(columnMap);

								search = delete.SqlWhereClause.GetNextSqlSearchCondition();

								//Hack: For some reason it doesn't work to match NULL in parameterized queries...?
								wrappedValue = WrapValue(obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, ref compareOp, true);
								if (wrappedValue == "NULL" && compareOp == "Is")
									search.GetSqlIsNullPredicate(column); 
								else if (compareOp == "LIKE")
								{
									if (first)
										paramName = GetParameterName(propertyMap, "Org_");
									else
										paramName = GetParameterName(propertyMap, columnMap, "Org_");
									param = AddSqlParameter(delete, parameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, true);
									search.GetSqlLikePredicate(column, param);
								}
								else
								{
									paramName = GetParameterName(propertyMap, "Org_");
									param = AddSqlParameter(delete, parameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, true);
									search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals,  param);
								}
								first = false;
							}
						}
					}
				}
			}
			return GenerateSql(delete);
		}
Пример #3
0
		protected virtual string GetDeleteStatement(object obj, IList parameters)
		{
			IClassMap classMap;
			IPropertyMap propertyMap;
			ITableMap tableMap;
			IObjectManager om;
			IPersistenceManager pm;
			string compareOp = "";
			string paramName = "";
			string wrappedValue = "";
			OptimisticConcurrencyBehaviorType propOptBehavior;
			classMap = m_SqlEngineManager.Context.DomainMap.MustGetClassMap(obj.GetType());

			om = m_SqlEngineManager.Context.ObjectManager;
			pm = m_SqlEngineManager.Context.PersistenceManager;
			tableMap = classMap.MustGetTableMap();

			SqlDeleteStatement delete = new SqlDeleteStatement(tableMap.SourceMap) ; 
			SqlTableAlias table = delete.GetSqlTableAlias(tableMap);

			delete.SqlFromClause.AddSqlAliasTableSource(table);

			if (!(pm.GetDeleteOptimisticConcurrencyBehavior(OptimisticConcurrencyBehaviorType.DefaultBehavior, classMap) == OptimisticConcurrencyBehaviorType.Disabled))
			{
				foreach (IPropertyMap iPropertyMap in classMap.GetAllPropertyMaps())
				{
					propertyMap = iPropertyMap;
					if (!(propertyMap.IsIdentity))
					{
						if (!propertyMap.IsReadOnly && !propertyMap.IsSlave)
						{
							if (!(propertyMap.IsCollection))
							{
								if (propertyMap.MustGetTableMap() == tableMap)
								{
									propOptBehavior = pm.GetDeleteOptimisticConcurrencyBehavior(OptimisticConcurrencyBehaviorType.DefaultBehavior, propertyMap);
									if (propOptBehavior == OptimisticConcurrencyBehaviorType.IncludeWhenLoaded || (propOptBehavior == OptimisticConcurrencyBehaviorType.IncludeWhenDirty && om.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.Dirty))
									{
										if (om.HasOriginalValues(obj, propertyMap.Name))
										{
											bool first = true;
											foreach (IColumnMap columnMap in propertyMap.GetAllColumnMaps() )
											{
												SqlColumnAlias column = table.GetSqlColumnAlias(columnMap);
												SqlSearchCondition search = delete.SqlWhereClause.GetNextSqlSearchCondition();

												//Hack: For some reason it doesn't work to match NULL in parameterized queries...?
												wrappedValue = WrapValue(obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, ref compareOp, true);
												if (wrappedValue == "NULL" && compareOp == "Is")
												{					
													search.GetSqlIsNullPredicate(column);
												}
												else if (compareOp == "LIKE")
												{
													if (first)
														paramName = GetParameterName(propertyMap, "Org_");
													else
														paramName = GetParameterName(propertyMap, columnMap, "Org_");
													SqlParameter param = AddSqlParameter(delete, parameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, true);
													search.GetSqlLikePredicate(column, param);
												}
												else
												{
													if (first)
														paramName = GetParameterName(propertyMap, "Org_");
													else
														paramName = GetParameterName(propertyMap, columnMap, "Org_");
													SqlParameter param = AddSqlParameter(delete, parameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, false);
													search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
												}
												first = false;
											}
										}
									}
								}
							}
						}
					}
				}
			}
			foreach (IPropertyMap iPropertyMap in classMap.GetIdentityPropertyMaps())
			{
				propertyMap = iPropertyMap;
				SqlSearchCondition search = delete.SqlWhereClause.GetNextSqlSearchCondition();
				IColumnMap columnMap = propertyMap.GetColumnMap();
				SqlColumnAlias column = table.GetSqlColumnAlias(columnMap);
				SqlParameter param;

				paramName = GetParameterName(propertyMap, "Id_");
				if (om.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.Dirty)
				{
					param = AddSqlParameter(delete, parameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), propertyMap.GetColumnMap(), true);
				}
				else
				{
					param = AddSqlParameter(delete, parameters, paramName, obj, propertyMap, om.GetPropertyValue(obj, propertyMap.Name), propertyMap.GetColumnMap());
				}
				search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
			}
			return GenerateSql(delete);
		}