private string GetJoinedExistedCountSql(IConnectiveSqlClause ownerKeyBuilder, IConnectiveSqlClause existedKeys, ORMappingItemCollection mapping)
        {
            HashSet <string> fields = MergeFields(ownerKeyBuilder.GetFields(), existedKeys.GetFields());

            WhereSqlClauseBuilder builder = new WhereSqlClauseBuilder();

            foreach (string field in fields)
            {
                builder.AppendItem("A." + field, "B." + field, SqlClauseBuilderBase.EqualTo, true);
            }

            string innerSql = this.GetInnerExistedObjsSql(ownerKeyBuilder, existedKeys, mapping);
            string sql      = string.Format("SELECT COUNT(1) FROM ({0}) B WHERE {1}", innerSql, builder.ToSqlString(TSqlBuilder.Instance));

            return(sql);
        }
        private static void InnerGetFields(IConnectiveSqlClause connective, HashSet <string> fields)
        {
            HashSet <string> innerFields = connective.GetFields();

            foreach (string field in innerFields)
            {
                if (fields.Contains(field) == false)
                {
                    fields.Add(field);
                }
            }

            foreach (IConnectiveSqlClause subConnective in connective.GetSubConnectiveSqlClause())
            {
                InnerGetFields(subConnective, fields);
            }
        }
        private static WhereSqlClauseBuilder GetExistedKeysBuilder(IConnectiveSqlClause ownerKeyBuilder, T obj, ORMappingItemCollection mapping)
        {
            WhereSqlClauseBuilder keyBuilder = ORMapping.GetWhereSqlClauseBuilderByPrimaryKey(obj, mapping);

            if (obj.VersionStartTime == DateTime.MinValue)
            {
                keyBuilder.IfExists(GetPropertyFieldName("VersionStartTime", mapping),
                                    item =>
                {
                    item.IsExpression = true;
                    item.Data         = DBTimePointActionContext.CurrentTimeTSqlVarName;
                });
            }

            foreach (string ownerKey in ownerKeyBuilder.GetFields())
            {
                keyBuilder.Remove(item => ((SqlClauseBuilderItemUW)item).DataField == ownerKey);
            }

            return(keyBuilder);
        }