public virtual void Visiting(SqlUpdateStatement updateStatement)
        {
            updateStatement.SqlUpdateClause.Accept(this);
            sqlBuilder.Append(" Set ");

            int i = 0;
            foreach (SqlColumn sqlColumn in updateStatement.SqlColumnList)
            {
                SqlExpression sqlExpression = (SqlExpression) updateStatement.ValueList[i];
                sqlColumn.Accept(this);
                sqlBuilder.Append(" = ");
                sqlExpression.Accept(this);
                sqlBuilder.Append(", ");
                i++;
            }
            sqlBuilder.Length -= 2;
            updateStatement.SqlFromClause.Accept(this);
            updateStatement.SqlWhereClause.Accept(this);
        }
 public virtual void Visited(SqlUpdateStatement updateStatement)
 {
 }
		protected virtual string GetUpdateNonPrimaryStatement(object obj, ArrayList propertyMaps, IList stillDirty, 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;
			OptimisticConcurrencyBehaviorType propOptBehavior;
			PropertyStatus propStatus;
			bool hadDirty = false;
			bool ignore;
			object refObj;
			string paramName = "";
			IList orgParameters = new ArrayList() ;
			Hashtable addedPropertyMaps = new Hashtable() ;
			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;
			}

			SqlUpdateStatement update = new SqlUpdateStatement(tableMap.SourceMap) ;
			SqlTableAlias table = update.GetSqlTableAlias(tableMap);
			SqlColumnAlias idColumn = table.GetSqlColumnAlias(idColumnMap);
			SqlSearchCondition search;
			SqlParameter param;

			update.SqlUpdateClause.SqlTable = table.SqlTable;

			bool hadStillDirty = false;
			if (stillDirty.Count > 0)
				hadStillDirty = true;

			foreach (IPropertyMap iPropertyMap in propertyMaps)
			{
				propertyMap = iPropertyMap;
				propOptBehavior = pm.GetUpdateOptimisticConcurrencyBehavior(OptimisticConcurrencyBehaviorType.DefaultBehavior, propertyMap);
				propStatus = om.GetPropertyStatus(obj, propertyMap.Name);
				if (hadStillDirty)
				{
					if (stillDirty.Contains(propertyMap))
					{
						propStatus = PropertyStatus.Dirty ;													
					}
					else
					{
						if (propStatus == PropertyStatus.Dirty)
							propStatus = PropertyStatus.Clean ;
					}
				}
				bool first = true;
				foreach (IColumnMap columnMap in propertyMap.GetAllColumnMaps())
				{
					SqlColumnAlias column = table.GetSqlColumnAlias(columnMap);
					if (propStatus == PropertyStatus.Dirty)
					{
						ignore = false;
						if (!(propertyMap.ReferenceType == ReferenceType.None))
						{
							refObj = om.GetPropertyValue(obj, propertyMap.Name);
							if (refObj != null)
							{
								ObjectStatus refObjectStatus = om.GetObjectStatus(refObj);
								if (refObjectStatus == ObjectStatus.UpForCreation)
								{
									ignore = true;
									if (!(stillDirty.Contains(propertyMap)))
										stillDirty.Add(propertyMap) ;
								}
							}
						}
						if (!(ignore))
						{
							paramName = GetParameterName(propertyMap);
							param = AddSqlParameter(update, parameters, paramName, obj, propertyMap, om.GetPropertyValue(obj, propertyMap.Name), columnMap);

							update.AddSqlColumnAndValue(column, param);
							hadDirty = true;

							addedPropertyMaps[propertyMap] = propertyMap;

							if (hadStillDirty)
								stillDirty.Remove(propertyMap);
						}
					}
					if (!(propOptBehavior == OptimisticConcurrencyBehaviorType.Disabled))
					{
						if (om.HasOriginalValues(obj, propertyMap.Name))
						{
							search = update.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(update, parameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, true);
								search.GetSqlLikePredicate(column, param);
							}
							else
							{
								paramName = GetParameterName(propertyMap, "Org_");
								param = AddSqlParameter(update, orgParameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, true);
								search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
							}
						}
					}
					first = false;					
				}
			}
			if (!(hadDirty))
				return "";

			IList remove = new ArrayList(); 
			foreach (IPropertyMap removePropertyMap in propertyMaps)
			{
				if (addedPropertyMaps.ContainsKey(removePropertyMap))
				{
					remove.Add(removePropertyMap);
				}
			}
			foreach (IPropertyMap removePropertyMap in remove)
			{
				propertyMaps.Remove(removePropertyMap);
			}

			paramName = GetParameterName(idPropertyMap, "Id_");
			param = AddSqlParameter(update, parameters, paramName, obj, idPropertyMap, om.GetPropertyValue(obj, idPropertyMap.Name), idColumnMap, true);

			search = update.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(update, orgParameters, paramName, obj, null, classMap.TypeValue, idColumnMap, true);
				else
				{
					idPropertyMap = classMap.MustGetPropertyMapForColumnMap(idColumnMap.MustGetPrimaryKeyColumnMap());					
					param = AddSqlParameter(update, orgParameters, paramName, obj, idPropertyMap, om.GetPropertyValue(obj, idPropertyMap.Name), idColumnMap, true);
				}

				search = update.SqlWhereClause.GetNextSqlSearchCondition();
				search.GetSqlComparePredicate(idColumn, SqlCompareOperatorType.Equals, param);
			}

			
			foreach (IQueryParameter orgParameter in orgParameters)
				parameters.Add(orgParameter);

			return GenerateSql(update);
		}
		protected virtual string GetUpdateStatement(object obj, ArrayList propertyMaps, IList stillDirty, ArrayList nonPrimaryPropertyMaps, ArrayList collectionPropertyMaps, IList parameters)
		{
			IClassMap classMap;
			IPropertyMap propertyMap;
			ITableMap tableMap;
			IObjectManager om;
			IPersistenceManager pm;
			string compareOp = "";
			string wrappedValue;
			bool hadDirty = false;
			OptimisticConcurrencyBehaviorType propOptBehavior;
			PropertyStatus propStatus;
			bool ignore = false;
			object refObj;
			string paramName = "";
			IList orgParameters = new ArrayList(); 
			classMap = m_SqlEngineManager.Context.DomainMap.MustGetClassMap(obj.GetType());
			om = m_SqlEngineManager.Context.ObjectManager;
			pm = m_SqlEngineManager.Context.PersistenceManager;
			tableMap = classMap.MustGetTableMap();

			
			SqlUpdateStatement update = new SqlUpdateStatement(tableMap.SourceMap) ;
			SqlTableAlias table = update.GetSqlTableAlias(tableMap);
			SqlSearchCondition search;
			SqlParameter param;

			update.SqlUpdateClause.SqlTable = table.SqlTable;

			bool hadStillDirty = false;
			if (stillDirty.Count > 0)
				 hadStillDirty = true;
 
			foreach (IPropertyMap iPropertyMap in classMap.GetAllPropertyMaps())
			{
				propertyMap = iPropertyMap;
				if (!propertyMap.IsReadOnly && !propertyMap.IsSlave)
				{
					propOptBehavior = pm.GetUpdateOptimisticConcurrencyBehavior(OptimisticConcurrencyBehaviorType.DefaultBehavior, propertyMap);
					propStatus = om.GetPropertyStatus(obj, propertyMap.Name);						
					if (hadStillDirty)
					{
						if (stillDirty.Contains(propertyMap))
						{
							propStatus = PropertyStatus.Dirty ;													
						}
						else
						{
							if (propStatus == PropertyStatus.Dirty)
								propStatus = PropertyStatus.Clean ;
						}
					}
					if (propStatus != PropertyStatus.NotLoaded)
					{
						if (propStatus == PropertyStatus.Dirty || propOptBehavior == OptimisticConcurrencyBehaviorType.IncludeWhenLoaded)
						{
							if (propertyMap.IsCollection)
							{
								if (propStatus == PropertyStatus.Dirty)
								{
									collectionPropertyMaps.Add(propertyMap);
								}
							}
							else
							{
								ignore = false;
								if (!(propertyMap.ReferenceType == ReferenceType.None))
								{
									refObj = om.GetPropertyValue(obj, propertyMap.Name);
									if (refObj != null)
									{
										ObjectStatus refObjectStatus = om.GetObjectStatus(refObj);
										if (refObjectStatus == ObjectStatus.UpForCreation)
										{
											ignore = true;
											if (!(stillDirty.Contains(propertyMap)))
												stillDirty.Add(propertyMap);
										}
									}
								}
								if (!(ignore))
								{
									if (!(propertyMap.MustGetTableMap() == tableMap))
									{
										if (propStatus == PropertyStatus.Dirty)
										{
											nonPrimaryPropertyMaps.Add(propertyMap);
										}
									}
									else
									{
										bool first = true;
										foreach (IColumnMap columnMap in propertyMap.GetAllColumnMaps() )
										{
											SqlColumnAlias column = table.GetSqlColumnAlias(columnMap);
											if (propStatus == PropertyStatus.Dirty)
											{
												if (first)
													paramName = GetParameterName(propertyMap);
												else
													paramName = GetParameterName(propertyMap, columnMap);

												param = AddSqlParameter(update, parameters, paramName, obj, propertyMap, om.GetPropertyValue(obj, propertyMap.Name), columnMap);
												update.AddSqlColumnAndValue(column, param);
										
												propertyMaps.Add(propertyMap);
												hadDirty = true;

												if (hadStillDirty)
													stillDirty.Remove(propertyMap);
											}
											if (!(propOptBehavior == OptimisticConcurrencyBehaviorType.Disabled))
											{
												if (om.HasOriginalValues(obj, propertyMap.Name))
												{
													//Hack: For some reason it doesn't work to match NULL in parameterized queries...?
													search = update.SqlWhereClause.GetNextSqlSearchCondition();
												
													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(update, orgParameters, 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_");
														param = AddSqlParameter(update, orgParameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, true);
														search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
													}

													//TODO: The rest of the columns??
												}
											}	
											first = false;
										}
									}
								}
							}
						}
						
					}
				}
			}
			if (!(hadDirty))
				return "";

			foreach (IPropertyMap iPropertyMap in classMap.GetIdentityPropertyMaps())
			{
				propertyMap = iPropertyMap;
				IColumnMap columnMap = propertyMap.GetColumnMap();
				SqlColumnAlias column = table.GetSqlColumnAlias(columnMap);

				paramName = GetParameterName(propertyMap, "Id_");
				if (om.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.Dirty)
					param = AddSqlParameter(update, orgParameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), propertyMap.GetColumnMap(), true);
				else
					param = AddSqlParameter(update, orgParameters, paramName, obj, propertyMap, om.GetPropertyValue(obj, propertyMap.Name), propertyMap.GetColumnMap());

				search = update.SqlWhereClause.GetNextSqlSearchCondition();
				search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
			}

			foreach (IQueryParameter orgParameter in orgParameters)
				parameters.Add(orgParameter);

			return GenerateSql(update);
		}
 public SqlUpdateClause(SqlUpdateStatement sqlUpdateStatement)
     : base(sqlUpdateStatement)
 {
 }
Exemple #6
0
 public SqlUpdateClause(SqlUpdateStatement sqlUpdateStatement) : base(sqlUpdateStatement)
 {
 }