private SqlQuery InitBuilderCountWithDistinct(SqlPropertyMetadata sqlProperty)
        {
            var query             = new SqlQuery();
            var partSqlCountQuery = !string.IsNullOrEmpty(sqlProperty.Alias)
                ? TableName + "." + sqlProperty.ColumnName + ") AS " + sqlProperty.PropertyName
                : TableName + "." + sqlProperty.ColumnName + ")";

            query.SqlBuilder.Append("SELECT COUNT(DISTINCT " + partSqlCountQuery);
            return(query);
        }
        private void InitProperties()
        {
            var entityType     = typeof(TEntity);
            var entityTypeInfo = entityType.GetTypeInfo();
            var tableAttribute = entityTypeInfo.GetCustomAttribute <TableAttribute>();

            TableName   = tableAttribute != null ? tableAttribute.Name : entityTypeInfo.Name;
            TableSchema = tableAttribute != null ? tableAttribute.Schema : string.Empty;

            AllProperties = entityType.FindClassProperties().Where(q => q.CanWrite).ToArray();

            var props = AllProperties.Where(ExpressionHelper.GetPrimitivePropertiesPredicate()).ToArray();

            var joinProperties = AllProperties.Where(p => p.GetCustomAttributes <JoinAttributeBase>().Any()).ToArray();

            SqlJoinProperties = GetJoinPropertyMetadata(joinProperties);

            // Filter the non stored properties
            SqlProperties = props.Where(p => !p.GetCustomAttributes <NotMappedAttribute>().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray();

            // Filter key properties
            KeySqlProperties = props.Where(p => p.GetCustomAttributes <KeyAttribute>().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray();

            // Use identity as key pattern
            var identityProperty = props.FirstOrDefault(p => p.GetCustomAttributes <IdentityAttribute>().Any());

            IdentitySqlProperty = identityProperty != null ? new SqlPropertyMetadata(identityProperty) : null;


            var dateChangedProperty = props.FirstOrDefault(p => p.GetCustomAttributes <UpdatedAtAttribute>().Count() == 1);

            if (dateChangedProperty != null && (dateChangedProperty.PropertyType == typeof(DateTime) || dateChangedProperty.PropertyType == typeof(DateTime?)))
            {
                UpdatedAtProperty         = props.FirstOrDefault(p => p.GetCustomAttributes <UpdatedAtAttribute>().Any());
                UpdatedAtPropertyMetadata = new SqlPropertyMetadata(UpdatedAtProperty);
            }


            var dateCreatedProperty = props.FirstOrDefault(p => p.GetCustomAttributes <CreatedAtAttribute>().Count() == 1);

            if (dateCreatedProperty != null && (dateCreatedProperty.PropertyType == typeof(DateTime) || dateCreatedProperty.PropertyType == typeof(DateTime?)))
            {
                CreatedAtProperty         = props.FirstOrDefault(p => p.GetCustomAttributes <CreatedAtAttribute>().Any());
                CreatedAtPropertyMetadata = new SqlPropertyMetadata(CreatedAtProperty);
            }
        }
        private SqlQuery InitBuilderCountWithDistinct(SqlPropertyMetadata sqlProperty)
        {
            var query = new SqlQuery(TableName, QueryType.Select);

            query.SqlBuilder.Append("SELECT COUNT(DISTINCT ");

            query.SqlBuilder
            .Append(TableName)
            .Append(".")
            .Append(sqlProperty.ColumnName)
            .Append(")");

            if (sqlProperty.Alias != null)
            {
                query.SqlBuilder
                .Append(" AS ")
                .Append(sqlProperty.PropertyName);
            }

            return(query);
        }
예제 #4
0
        private void InitProperties()
        {
            var entityType     = typeof(TEntity);
            var entityTypeInfo = entityType.GetTypeInfo();
            var tableAttribute = entityTypeInfo.GetCustomAttribute <TableAttribute>();

            TableName   = tableAttribute != null ? tableAttribute.Name : entityTypeInfo.Name;
            TableSchema = tableAttribute != null ? tableAttribute.Schema : string.Empty;

            AllProperties = entityType.FindClassProperties().Where(q => q.CanWrite).ToArray();

            var props = AllProperties.Where(ExpressionHelper.GetPrimitivePropertiesPredicate()).ToArray();

            var joinProperties = AllProperties.Where(p => p.GetCustomAttributes <JoinAttributeBase>().Any()).ToArray();

            SqlJoinProperties = GetJoinPropertyMetadata(joinProperties);

            // Filter the non stored properties
            SqlProperties = props.Where(p => !p.GetCustomAttributes <NotMappedAttribute>().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray();

            KeyUpsertSqlProperties =
                props.Where(e => e.GetCustomAttributes <UpsertKeyAttribute>().Any())
                .Select(p => new SqlPropertyMetadata(p))
                .ToArray();

            // Filter key properties
            KeySqlProperties = props.Where(p => p.GetCustomAttributes <KeyAttribute>().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray();

            if (!KeySqlProperties.Any())
            {
                var prop = props.SingleOrDefault(p => p.Name.Equals("Id", StringComparison.OrdinalIgnoreCase));
                //skip adding key if prop is null, which means the model doesn't have Id property....
                if (prop != null)
                {
                    KeySqlProperties    = new SqlPropertyMetadata[1];
                    KeySqlProperties[0] = new SqlPropertyMetadata(prop);
                }
            }

            // Use identity as key pattern
            var identityProperty = props.FirstOrDefault(p => p.GetCustomAttributes <IdentityAttribute>().Any());

            IdentitySqlProperty = identityProperty != null ? new SqlPropertyMetadata(identityProperty) : null;

            var dateChangedProperty = props.FirstOrDefault(p => p.GetCustomAttributes <UpdatedAtAttribute>().Count() == 1);

            if (dateChangedProperty != null && (dateChangedProperty.PropertyType == typeof(DateTime) || dateChangedProperty.PropertyType == typeof(DateTime?)))
            {
                UpdatedAtProperty         = props.FirstOrDefault(p => p.GetCustomAttributes <UpdatedAtAttribute>().Any());
                UpdatedAtPropertyMetadata = new SqlPropertyMetadata(UpdatedAtProperty);
            }

            var modifiedDateProperty = props.FirstOrDefault(p => p.GetCustomAttributes <ModifiedAtAttribute>().Count() == 1);

            if (modifiedDateProperty != null &&
                (modifiedDateProperty.PropertyType == typeof(DateTime) ||
                 modifiedDateProperty.PropertyType == typeof(DateTime?)))
            {
                ModifiedAtProperty         = props.FirstOrDefault(p => p.GetCustomAttributes <ModifiedAtAttribute>().Any());
                ModifiedAtPropertyMetadata = new SqlPropertyMetadata(ModifiedAtProperty);
            }
        }