Пример #1
0
 public virtual void Visiting(SqlCountFunction countFunction)
 {
     if (countFunction.SqlExpression != null)
     {
         sqlBuilder.Append("Count(");
         if (countFunction.Distinct)
             sqlBuilder.Append("Distinct ");
         countFunction.SqlExpression.Accept(this);
         sqlBuilder.Append(")");
     }
     else
         sqlBuilder.Append("Count(*)");
 }
Пример #2
0
 public virtual void Visited(SqlCountFunction countFunction)
 {
 }
        public virtual void GetListPropertySubselectAndAlias(IPropertyMap propertyMap, object hash, Hashtable columns, ArrayList order, string propPath, string suggestion)
        {
            if (hash == null) { hash = propertyMap; }
            ITableMap listTableMap = propertyMap.MustGetTableMap();
            ITableMap parentTableMap = propertyMap.ClassMap.MustGetTableMap();
            SqlTableAlias parentTable = sqlEmitter.GetTableAlias(parentTableMap, hash);

            SqlSelectStatement subSelect = new SqlSelectStatement(parentTableMap.SourceMap);

            //Hmmm....can an alias be redefined in a subselect?
            //SqlTableAlias listTable = subSelect.GetSqlTableAlias(listTableMap, "cnt" + subSelect.GetNextTableAliasIndex());
            SqlTableAlias listTable = subSelect.GetSqlTableAlias(listTableMap, "cnt" + sqlEmitter.Select.GetNextTableAliasIndex());

            SqlCountFunction count = new SqlCountFunction();

            subSelect.SqlSelectClause.AddSqlAliasSelectListItem(count);

            subSelect.SqlFromClause.AddSqlAliasTableSource(listTable);

            foreach (IColumnMap fkIdColumnMap in propertyMap.GetAllIdColumnMaps())
            {
                IColumnMap idColumnMap = fkIdColumnMap.MustGetPrimaryKeyColumnMap();

                SqlColumnAlias fkIdColumn = listTable.GetSqlColumnAlias(fkIdColumnMap);
                SqlColumnAlias idColumn = parentTable.GetSqlColumnAlias(idColumnMap);
                SqlSearchCondition search = subSelect.SqlWhereClause.GetNextSqlSearchCondition();

                search.GetSqlComparePredicate(fkIdColumn, SqlCompareOperatorType.Equals, idColumn);
            }

            if (suggestion == "")
                suggestion = propPath;

            SqlAliasSelectListItem countAlias = this.sqlEmitter.Select.SqlSelectClause.AddSqlAliasSelectListItem(subSelect, suggestion);
            this.sqlEmitter.PropertyColumnMap[propPath] = countAlias.SqlExpressionAlias.Alias;
        }
Пример #4
0
        //NOTE: shouldnt this also load related tables in inheritance scenarios? , eg classtable inheritance?
        protected virtual string GetSelectStatement(object obj, ArrayList propertyNames, string keyPropertyName, object keyValue, IList parameters)
        {
            IPropertyMap propertyMap;
            IClassMap classMap;
            ITableMap tableMap;
            IObjectManager om;
            IColumnMap typeColumnMap;
            string paramName = "";

            classMap = m_SqlEngineManager.Context.DomainMap.MustGetClassMap(obj.GetType());

            tableMap = classMap.MustGetTableMap();
            ISourceMap sourceMap = tableMap.SourceMap;

            SqlSelectStatement select = new SqlSelectStatement(sourceMap) ;

            SqlTableAlias table = select.GetSqlTableAlias(tableMap, "t" + select.GetNextTableAliasIndex());

            foreach (IPropertyMap iPropertyMap in classMap.GetAllPropertyMaps())
            {
                propertyMap = iPropertyMap;
                if (propertyMap.IsCollection)
                {
                    if (this.Context.PersistenceManager.GetListCountLoadBehavior(LoadBehavior.Default, propertyMap) == LoadBehavior.Eager)
                    {
                        if (propertyMap.ReferenceType != ReferenceType.None)
                        {
                            ITableMap listTableMap = propertyMap.GetTableMap();

                            ISourceMap listSourceMap = listTableMap.SourceMap;

                            SqlSelectStatement subSelect = new SqlSelectStatement(listSourceMap);
                            SqlTableAlias listTable = subSelect.GetSqlTableAlias(listTableMap, "t" + select.GetNextTableAliasIndex());

                            SqlCountFunction count = new SqlCountFunction();

                            subSelect.SqlSelectClause.AddSqlAliasSelectListItem(count);

                            subSelect.SqlFromClause.AddSqlAliasTableSource(listTable);

                            foreach (IColumnMap fkIdColumnMap in propertyMap.GetAllIdColumnMaps())
                            {
                                IColumnMap idColumnMap = fkIdColumnMap.MustGetPrimaryKeyColumnMap();

                                SqlColumnAlias fkIdColumn = listTable.GetSqlColumnAlias(fkIdColumnMap);
                                SqlColumnAlias idColumn = table.GetSqlColumnAlias(idColumnMap);
                                SqlSearchCondition search = subSelect.SqlWhereClause.GetNextSqlSearchCondition();

                                search.GetSqlComparePredicate(fkIdColumn, SqlCompareOperatorType.Equals, idColumn);
                            }

                            select.SqlSelectClause.AddSqlAliasSelectListItem(subSelect, propertyMap.Name);
                            propertyNames.Add(propertyMap.Name);
                        }
                    }
                }
                else
                {
                    if (propertyMap.MustGetTableMap() == tableMap)
                    {
                        if (!(propertyMap.GetAllColumnMaps().Count > 1))
                        {
                            if (!(propertyMap.LazyLoad))
                            {
                                IColumnMap columnMap = propertyMap.GetColumnMap();
                                SqlColumnAlias column = table.GetSqlColumnAlias(columnMap);
                                select.SqlSelectClause.AddSqlAliasSelectListItem(column);
                                propertyNames.Add(propertyMap.Name);
                            }
                        }
                    }
                }
            }
            select.SqlFromClause.AddSqlAliasTableSource(table);
            om = m_SqlEngineManager.Context.ObjectManager;
            if (keyPropertyName.Length > 0)
            {
                propertyMap = classMap.MustGetPropertyMap(keyPropertyName);
                IColumnMap columnMap = propertyMap.GetColumnMap();

                SqlColumnAlias column = select.GetSqlColumnAlias(columnMap);
                SqlSearchCondition search = select.SqlWhereClause.GetSqlSearchCondition();

                paramName = GetParameterName(propertyMap, "Id_");

                //SqlParameter param = AddSqlParameter(select, parameters, paramName, obj, propertyMap, om.GetPropertyValue(obj, propertyMap.Name), columnMap);
                SqlParameter param = AddSqlParameter(select, parameters, paramName, obj, propertyMap, keyValue, columnMap, true);

                search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
            }
            else
            {
                foreach (IPropertyMap iPropertyMap in classMap.GetIdentityPropertyMaps())
                {
                    propertyMap = iPropertyMap;
                    IColumnMap columnMap = propertyMap.GetColumnMap();

                    SqlColumnAlias column = select.GetSqlColumnAlias(columnMap);
                    SqlSearchCondition search = select.SqlWhereClause.GetNextSqlSearchCondition();

                    paramName = GetParameterName(propertyMap, "Id_");
                    SqlParameter param;

                    if (om.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.Dirty)
                        param = AddSqlParameter(select, parameters, paramName, obj, propertyMap, om.GetOriginalPropertyValue(obj, propertyMap.Name), columnMap, true);
                    else
                        param = AddSqlParameter(select, parameters, paramName, obj, propertyMap, om.GetPropertyValue(obj, propertyMap.Name), columnMap);

                    search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
                }
                typeColumnMap = classMap.GetTypeColumnMap();
                if (typeColumnMap != null)
                {
                    SqlColumnAlias column = select.GetSqlColumnAlias(typeColumnMap);
                    paramName = GetParameterName(classMap, "Type_");
                    SqlParameter param = AddSqlParameter(select, parameters, paramName, obj, null, classMap.TypeValue, typeColumnMap, true);
                    SqlSearchCondition search = select.SqlWhereClause.GetNextSqlSearchCondition();

                    search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
                }
            }
            return GenerateSql(select);;
        }
Пример #5
0
        //Mats : Fixed non-primary ref prop bug by improving One-One backref identification
        protected virtual string GetSelectSingleReferencePropertyStatement(object obj, IPropertyMap propertyMap, IList idColumns, IList typeColumns, Hashtable hashPropertyColumnMap, IList parameters)
        {
            IColumnMap columnMap;
            IClassMap classMap;
            IPropertyMap refPropertyMap;
            ITableMap tableMap;
            IPropertyMap myPropertyMap;
            string colName;
            IColumnMap theColumnMap;
            IColumnMap forColumnMap;
            ITableMap forTableMap;
            IColumnMap addColumnMap;
            IColumnMap addForColumnMap;
            ITableMap addForTableMap;
            IColumnMap typeColumnMap;
            IDomainMap domainMap;
            bool isBackRef = false;
            string paramName = "";
            classMap = propertyMap.MustGetReferencedClassMap();
            if (propertyMap.ReferenceType == ReferenceType.OneToOne)
            {
                columnMap = propertyMap.GetColumnMap();
                if (columnMap != null)
                    if (columnMap.TableMap == propertyMap.MustGetReferencedClassMap().MustGetTableMap())
                        if (columnMap.IsPrimaryKey)
                            isBackRef = true;
                columnMap = null;
            }

            SqlSelectStatement select;
            SqlTableAlias table;
            SqlColumnAlias theColumn;
            SqlTableAlias forTable;
            SqlColumnAlias forColumn;

            if (isBackRef)
            {
                tableMap = propertyMap.ClassMap.MustGetTableMap();

                select = new SqlSelectStatement(tableMap.SourceMap) ;
                table = select.GetSqlTableAlias(tableMap);

                //select.SqlSelectClause.AddSqlAliasSelectListItem(column);
                //select.SqlFromClause.AddSqlAliasTableSource(table);

                theColumnMap = propertyMap.GetIdColumnMap();
                //theColumn = table.GetSqlColumnAlias(theColumnMap);

                forTableMap = theColumnMap.TableMap;

                forColumnMap = theColumnMap;
                theColumnMap = forColumnMap.MustGetPrimaryKeyColumnMap();
                if (forTableMap == null)
                    throw new MappingException("TableMap '" + theColumnMap.PrimaryKeyTable + "' Not Found!"); // do not localize
                if (forColumnMap == null)
                    throw new MappingException("ColumnMap '" + theColumnMap.PrimaryKeyColumn + "' Not Found!"); // do not localize

                forTable = select.GetSqlTableAlias(forTableMap);
                forColumn = forTable.GetSqlColumnAlias(forColumnMap);
            }
            else
            {
                tableMap = propertyMap.MustGetTableMap();

                select = new SqlSelectStatement(tableMap.SourceMap) ;
                table = select.GetSqlTableAlias(tableMap);

                theColumnMap = propertyMap.GetColumnMap();
                //theColumn = table.GetSqlColumnAlias(theColumnMap);

                forTableMap = theColumnMap.MustGetPrimaryKeyTableMap();
                forColumnMap = theColumnMap.MustGetPrimaryKeyColumnMap();
                if (forTableMap == null)
                    throw new MappingException("TableMap '" + theColumnMap.PrimaryKeyTable + "' Not Found!"); // do not localize
                if (forColumnMap == null)
                    throw new MappingException("ColumnMap '" + theColumnMap.PrimaryKeyColumn + "' Not Found!"); // do not localize

                forTable = select.GetSqlTableAlias(forTableMap);
                forColumn = forTable.GetSqlColumnAlias(forColumnMap);
            }

            IObjectManager om;
            foreach (IPropertyMap iRefPropertyMap in classMap.GetAllPropertyMaps())
            {
                refPropertyMap = iRefPropertyMap;
                if (refPropertyMap.IsCollection)
                {
                    if (this.Context.PersistenceManager.GetListCountLoadBehavior(LoadBehavior.Default, refPropertyMap) == LoadBehavior.Eager)
                    {
                        if (refPropertyMap.ReferenceType != ReferenceType.None)
                        {
                            ITableMap listTableMap = refPropertyMap.GetTableMap();

                            ISourceMap listSourceMap = listTableMap.SourceMap;

                            SqlSelectStatement subSelect = new SqlSelectStatement(listSourceMap);
                            SqlTableAlias listTable = subSelect.GetSqlTableAlias(listTableMap, "t" + select.GetNextTableAliasIndex());

                            SqlCountFunction count = new SqlCountFunction();

                            subSelect.SqlSelectClause.AddSqlAliasSelectListItem(count);

                            subSelect.SqlFromClause.AddSqlAliasTableSource(listTable);

                            foreach (IColumnMap fkIdColumnMap in refPropertyMap.GetAllIdColumnMaps())
                            {
                                IColumnMap pkIdColumnMap = fkIdColumnMap.MustGetPrimaryKeyColumnMap();

                                SqlColumnAlias fkIdColumn = listTable.GetSqlColumnAlias(fkIdColumnMap);
                                SqlColumnAlias pkIdColumn = forTable.GetSqlColumnAlias(pkIdColumnMap);
                                SqlSearchCondition searchCount = subSelect.SqlWhereClause.GetNextSqlSearchCondition();

                                searchCount.GetSqlComparePredicate(fkIdColumn, SqlCompareOperatorType.Equals, pkIdColumn);
                            }

                            select.SqlSelectClause.AddSqlAliasSelectListItem(subSelect, refPropertyMap.Name);
                            hashPropertyColumnMap[refPropertyMap.Name] = refPropertyMap.Name;
                        }
                    }
                }
                else
                {
                    if (refPropertyMap.MustGetTableMap() == forTableMap)
                    {
                        if (!(refPropertyMap.LazyLoad))
                        {
                            if (!((refPropertyMap.IsCollection || ((refPropertyMap.ReferenceType != ReferenceType.None && refPropertyMap.GetAdditionalColumnMaps().Count > 0) && !(refPropertyMap.IsIdentity)))))
                            {
                                IColumnMap refColumnMap = refPropertyMap.GetColumnMap();
                                SqlColumnAlias refColumn = forTable.GetSqlColumnAlias(refColumnMap);
                                colName = refColumnMap.Name;
                                if (refPropertyMap.IsIdentity)
                                    idColumns.Add(colName);

                                if (!(refPropertyMap.LazyLoad))
                                {
                                    select.SqlSelectClause.AddSqlAliasSelectListItem(refColumn);
                                    hashPropertyColumnMap[refPropertyMap.Name] = colName;
                                }
                            }
                        }
                    }
                }
            }
            typeColumnMap = classMap.GetTypeColumnMap();
            if (typeColumnMap != null)
            {
                SqlColumnAlias typeColumn = forTable.GetSqlColumnAlias(typeColumnMap);
                typeColumns.Add(typeColumnMap.Name);
                select.SqlSelectClause.AddSqlAliasSelectListItem(typeColumn);
            }
            select.SqlFromClause.AddSqlAliasTableSource(table);
            SqlTableAlias selfTable ;

            //if (tableMap.SourceMap.Schema.ToLower(CultureInfo.InvariantCulture) == forTableMap.SourceMap.Schema.ToLower(CultureInfo.InvariantCulture) && tableMap.Name.ToLower(CultureInfo.InvariantCulture) == forTableMap.Name.ToLower(CultureInfo.InvariantCulture))
            if (tableMap == forTableMap)
            {
                selfTable = select.GetSqlTableAlias(tableMap, "NPersistSelfRefTable");
                select.SqlFromClause.AddSqlAliasTableSource(selfTable);
            }
            else
            {
                selfTable = select.GetSqlTableAlias(tableMap);
                select.SqlFromClause.AddSqlAliasTableSource(forTable);
            }

            theColumn = selfTable.GetSqlColumnAlias(theColumnMap);

            SqlSearchCondition search = select.SqlWhereClause.GetNextSqlSearchCondition()  ;
            search.GetSqlComparePredicate(theColumn, SqlCompareOperatorType.Equals, forColumn);

            foreach (IColumnMap iAddColumnMap in propertyMap.GetAdditionalColumnMaps())
            {
                addColumnMap = iAddColumnMap;
                addForTableMap = addColumnMap.GetPrimaryKeyTableMap();
                addForColumnMap = addColumnMap.MustGetPrimaryKeyColumnMap();
                if (addForTableMap == null)
                    throw new MappingException("TableMap '" + addColumnMap.PrimaryKeyTable + "' Not Found!"); // do not localize
                if (addForColumnMap == null)
                    throw new MappingException("ColumnMap '" + addColumnMap.PrimaryKeyColumn + "' Not Found!"); // do not localize

                SqlColumnAlias addColumn = selfTable.GetSqlColumnAlias(addColumnMap);
                SqlColumnAlias addForColumn = forTable.GetSqlColumnAlias(addForColumnMap);

                search = select.SqlWhereClause.GetNextSqlSearchCondition()  ;
                search.GetSqlComparePredicate(addColumn, SqlCompareOperatorType.Equals, addForColumn);
            }

            om = m_SqlEngineManager.Context.ObjectManager;
            foreach (IPropertyMap iMyPropertyMap in propertyMap.ClassMap.GetIdentityPropertyMaps())
            {
                myPropertyMap = iMyPropertyMap;
                columnMap = myPropertyMap.GetColumnMap();

                SqlColumnAlias column = selfTable.GetSqlColumnAlias(columnMap);
                SqlParameter param;

                paramName = GetParameterName(myPropertyMap, "Id_");
                if (om.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.Dirty)
                    param = AddSqlParameter(select, parameters, paramName, obj, myPropertyMap, om.GetOriginalPropertyValue(obj, myPropertyMap.Name), columnMap, true);
                else
                    param = AddSqlParameter(select, parameters, paramName, obj, myPropertyMap, om.GetPropertyValue(obj, myPropertyMap.Name), columnMap);

                search = select.SqlWhereClause.GetNextSqlSearchCondition()  ;
                search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, param);
            }
            typeColumnMap = propertyMap.ClassMap.GetTypeColumnMap();
            if (typeColumnMap != null)
            {
                domainMap = propertyMap.ClassMap.DomainMap;
                SqlColumnAlias typeColumn = selfTable.GetSqlColumnAlias(typeColumnMap);

                paramName = GetParameterName(propertyMap.ClassMap, "Type_");
                SqlParameter param = AddSqlParameter(select, parameters, paramName, obj, null, domainMap.MustGetClassMap(obj.GetType()).TypeValue, typeColumnMap, true);

                search = select.SqlWhereClause.GetNextSqlSearchCondition()  ;
                search.GetSqlComparePredicate(typeColumn, SqlCompareOperatorType.Equals, param);
            }
            return GenerateSql(select);
        }
Пример #6
0
        protected virtual string GetSelectManyOnePropertyStatement(object obj, IPropertyMap propertyMap, IList idColumns, IList typeColumns, Hashtable hashPropertyColumnMap, IList parameters)
        {
            string colName = "";
            IColumnMap columnMap;
            IClassMap classMap;
            IPropertyMap refPropertyMap;
            ITableMap tableMap;
            ITableMap rootTableMap;
            IColumnMap idColumnMap;
            IPropertyMap myPropertyMap;
            IColumnMap myColumnMap;
            ITableMap myTableMap;
            IColumnMap addIdColumnMap;
            IColumnMap addMyColumnMap;
            ITableMap addMyTableMap;
            IColumnMap typeColumnMap;
            IPropertyMap orderByMap;
            string paramName = "";
            classMap = propertyMap.MustGetReferencedClassMap();

            IClassMap rootClassMap = classMap;
            rootTableMap = classMap.MustGetTableMap();
            tableMap = propertyMap.MustGetTableMap();

            if (tableMap != rootTableMap)
            {
                bool done = false;
                while (done == false && rootClassMap.InheritanceType != InheritanceType.ConcreteTableInheritance)
                {
                    done = true;
                    IClassMap super = rootClassMap.GetInheritedClassMap();
                    if (super != null)
                    {
                        if (super.MustGetTableMap() == rootTableMap)
                        {
                            rootClassMap = super ;
                            done = false;
                        }
                    }
                }
                if (rootClassMap == null)
                {
                    rootClassMap = classMap;
                }
            }

            SqlSelectStatement select = new SqlSelectStatement(tableMap.SourceMap) ;
            SqlTableAlias table = select.GetSqlTableAlias(tableMap);

            idColumnMap = propertyMap.GetIdColumnMap();
            SqlColumnAlias idColumn = table.GetSqlColumnAlias(idColumnMap);

            myTableMap = idColumnMap.MustGetPrimaryKeyTableMap();
            myColumnMap = idColumnMap.MustGetPrimaryKeyColumnMap();

            if (myTableMap == null)
                throw new MappingException("TableMap '" + idColumnMap.PrimaryKeyTable + "' Not Found!"); // do not localize
            if (myColumnMap == null)
                throw new MappingException("ColumnMap '" + idColumnMap.PrimaryKeyColumn + "' Not Found!"); // do not localize

            SqlTableAlias rootTable = table;
            if (tableMap != rootTableMap)
            {
                rootTable = select.GetSqlTableAlias(rootTableMap, "NPersistRootTable");
            }

            SqlTableAlias myTable;
            SqlColumnAlias myColumn;

            IObjectManager om;
            orderByMap = propertyMap.GetOrderByPropertyMap();

            //foreach (IPropertyMap iRefPropertyMap in rootClassMap.GetPrimaryPropertyMaps())
            foreach (IPropertyMap iRefPropertyMap in rootClassMap.GetAllPropertyMaps())
            {
                refPropertyMap = iRefPropertyMap;
                if (refPropertyMap.IsCollection)
                {
                    if (this.Context.PersistenceManager.GetListCountLoadBehavior(LoadBehavior.Default, refPropertyMap) == LoadBehavior.Eager)
                    {
                        if (refPropertyMap.ReferenceType != ReferenceType.None)
                        {
                            ITableMap listTableMap = refPropertyMap.GetTableMap();

                            ISourceMap listSourceMap = listTableMap.SourceMap;

                            SqlSelectStatement subSelect = new SqlSelectStatement(listSourceMap);
                            SqlTableAlias listTable = subSelect.GetSqlTableAlias(listTableMap, "t" + select.GetNextTableAliasIndex());

                            SqlCountFunction count = new SqlCountFunction();

                            subSelect.SqlSelectClause.AddSqlAliasSelectListItem(count);

                            subSelect.SqlFromClause.AddSqlAliasTableSource(listTable);

                            foreach (IColumnMap fkIdColumnMap in refPropertyMap.GetAllIdColumnMaps())
                            {
                                IColumnMap pkIdColumnMap = fkIdColumnMap.MustGetPrimaryKeyColumnMap();

                                SqlColumnAlias fkIdColumn = listTable.GetSqlColumnAlias(fkIdColumnMap);
                                SqlColumnAlias pkIdColumn = table.GetSqlColumnAlias(pkIdColumnMap);
                                SqlSearchCondition searchCount = subSelect.SqlWhereClause.GetNextSqlSearchCondition();

                                searchCount.GetSqlComparePredicate(fkIdColumn, SqlCompareOperatorType.Equals, pkIdColumn);
                            }

                            select.SqlSelectClause.AddSqlAliasSelectListItem(subSelect, refPropertyMap.Name);
                            hashPropertyColumnMap[refPropertyMap.Name] = refPropertyMap.Name;
                        }
                    }
                }
                else
                {
                    if (refPropertyMap.MustGetTableMap() == rootTableMap)
                    {
                        if (!((refPropertyMap.IsCollection || ((refPropertyMap.ReferenceType != ReferenceType.None && refPropertyMap.GetAdditionalColumnMaps().Count > 0) && !(refPropertyMap.IsIdentity)))))
                        {
                            IColumnMap refColumnMap = refPropertyMap.GetColumnMap();
                            //SqlColumnAlias refColumn = table.GetSqlColumnAlias(refColumnMap);
                            SqlColumnAlias refColumn = rootTable.GetSqlColumnAlias(refColumnMap);

                            colName = refColumnMap.Name;
                            if (refPropertyMap.IsIdentity)
                                idColumns.Add(colName);

                            if (!(refPropertyMap.LazyLoad))
                            {
                                select.SqlSelectClause.AddSqlAliasSelectListItem(refColumn);
                                hashPropertyColumnMap[refPropertyMap.Name] = colName;
                                if (refPropertyMap == orderByMap)
                                    select.SqlOrderByClause.AddSqlOrderByItem(refColumn);
                            }
                        }
                    }
                }
            }

            select.SqlFromClause.AddSqlAliasTableSource(table);

            typeColumnMap = classMap.GetTypeColumnMap();
            if (typeColumnMap != null)
            {
                SqlColumnAlias typeColumn = rootTable.GetSqlColumnAlias(typeColumnMap);
                select.SqlSelectClause.AddSqlAliasSelectListItem(typeColumn);
                typeColumns.Add(typeColumnMap.Name);
            }

            if (table != rootTable)
            {
                select.SqlFromClause.AddSqlAliasTableSource(rootTable);
            }

            //if (tableMap.SourceMap.Schema.ToLower(CultureInfo.InvariantCulture) == myTableMap.SourceMap.Schema.ToLower(CultureInfo.InvariantCulture) && tableMap.Name.ToLower(CultureInfo.InvariantCulture) == myTableMap.Name.ToLower(CultureInfo.InvariantCulture))
            if (myTableMap == tableMap)
                myTable = select.GetSqlTableAlias(myTableMap, "NPersistSelfRefTable");
            else
                myTable = select.GetSqlTableAlias(myTableMap, "NPersistOwnerTable");

            myColumn = myTable.GetSqlColumnAlias(myColumnMap);
            select.SqlFromClause.AddSqlAliasTableSource(myTable);

            SqlSearchCondition search = select.SqlWhereClause.GetNextSqlSearchCondition();
            search.GetSqlComparePredicate(idColumn, SqlCompareOperatorType.Equals,  myColumn);

            foreach (IColumnMap iAddIdColumnMap in propertyMap.GetAdditionalIdColumnMaps())
            {
                addIdColumnMap = iAddIdColumnMap;
                addMyTableMap = addIdColumnMap.GetPrimaryKeyTableMap();
                addMyColumnMap = addIdColumnMap.MustGetPrimaryKeyColumnMap();
                if (addMyTableMap == null)
                    throw new MappingException("TableMap '" + addIdColumnMap.PrimaryKeyTable + "' Not Found!"); // do not localize
                if (addMyColumnMap == null)
                    throw new MappingException("ColumnMap '" + addIdColumnMap.PrimaryKeyColumn + "' Not Found!"); // do not localize

                SqlColumnAlias addIdColumn = table.GetSqlColumnAlias(addIdColumnMap);
                SqlColumnAlias addMyColumn = myTable.GetSqlColumnAlias(addMyColumnMap);

                search = select.SqlWhereClause.GetNextSqlSearchCondition();
                search.GetSqlComparePredicate(addIdColumn, SqlCompareOperatorType.Equals,  addMyColumn);
            }
            if (table != rootTable)
            {
                foreach (IPropertyMap testPropertyMap in classMap.GetAllPropertyMaps())
                {
                    if (testPropertyMap.MustGetTableMap() == propertyMap.MustGetTableMap())
                    {
                        foreach (IColumnMap iAddIdColumnMap in testPropertyMap.GetAllIdColumnMaps())
                        {
                            addIdColumnMap = iAddIdColumnMap;

                            SqlColumnAlias addIdColumn = table.GetSqlColumnAlias(addIdColumnMap);
                            SqlColumnAlias rootColumn = rootTable.GetSqlColumnAlias(addIdColumnMap.MustGetPrimaryKeyColumnMap());

                            search = select.SqlWhereClause.GetNextSqlSearchCondition();
                            search.GetSqlComparePredicate(addIdColumn, SqlCompareOperatorType.Equals,  rootColumn);
                        }
                        break;
                    }
                }
            }
            om = m_SqlEngineManager.Context.ObjectManager;
            foreach (IPropertyMap iMyPropertyMap in propertyMap.ClassMap.GetIdentityPropertyMaps())
            {
                myPropertyMap = iMyPropertyMap;
                columnMap = myPropertyMap.GetColumnMap();
                SqlColumnAlias column = myTable.GetSqlColumnAlias(columnMap);
                SqlParameter param;
                paramName = GetParameterName(myPropertyMap, "Id_");
                if (om.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.Dirty)
                    param = AddSqlParameter(select, parameters, paramName, obj, myPropertyMap, om.GetOriginalPropertyValue(obj, myPropertyMap.Name), columnMap, true);
                else
                    param = AddSqlParameter(select, parameters, paramName, obj, myPropertyMap, om.GetPropertyValue(obj, myPropertyMap.Name), columnMap);

                search = select.SqlWhereClause.GetNextSqlSearchCondition();
                search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals,  param);
            }
            typeColumnMap = propertyMap.ClassMap.GetTypeColumnMap();
            if (typeColumnMap != null)
            {
                IClassMap actual = this.Context.DomainMap.MustGetClassMap(obj.GetType());

                SqlColumnAlias typeColumn = myTable.GetSqlColumnAlias(typeColumnMap);
                paramName = GetParameterName(propertyMap.ClassMap, "Type_");
                //SqlParameter param = AddSqlParameter(select, parameters, paramName, obj, null, propertyMap.ClassMap.TypeValue, typeColumnMap, true);
                SqlParameter param = AddSqlParameter(select, parameters, paramName, obj, null, actual.TypeValue, typeColumnMap, true);

                search = select.SqlWhereClause.GetNextSqlSearchCondition();
                search.GetSqlComparePredicate(typeColumn, SqlCompareOperatorType.Equals,  param);
            }

            return GenerateSql(select);
        }