Exemplo n.º 1
0
        public BulkQuery GetExistsQuery <T>(DataContext dataContext)
        {
            BulkQuery result = null;

            if (ExistsQueryCache.TryGetValue(typeof(T), out result))
            {
                if (result.DataContext == dataContext)
                {
                    return(result);
                }
            }

            var upsertParameters = GetUpsertParameters <T>(true, Columns <T>(), dataContext);
            var sqlProvider      = dataContext.Vendor.SqlProvider;
            var sqlBuilder       = new SqlStatementBuilder("SELECT COUNT (*) FROM ");

            sqlBuilder.Append(sqlProvider.GetTable(upsertParameters.Table.TableName));
            sqlBuilder.Append(" WHERE ");
            var valueSet = false;

            for (IEnumerator <SqlStatement> column = upsertParameters.InputPKColumns.GetEnumerator(), value = upsertParameters.InputPKValues.GetEnumerator(); column.MoveNext() && value.MoveNext();)
            {
                if (valueSet)
                {
                    sqlBuilder.Append(" AND ");
                }
                sqlBuilder.AppendFormat("{0} = {1}", column.Current, value.Current);
                valueSet = true;
            }

            result = new BulkQuery {
                DataContext          = dataContext,
                Sql                  = sqlBuilder.ToSqlStatement(),
                InputParameters      = upsertParameters.PKParameters,
                PrimaryKeyParameters = upsertParameters.PKParameters
            };
            ExistsQueryCache[typeof(T)] = result;
            return(result);
        }