Exemplo n.º 1
0
        public static QueryFieldAttribute Init(this QueryFieldAttribute query)
        {
            query.ValueStyle = ValueStyle.Single;
            if (query.Template == null || query.ValueFormat != null)
            {
                return(query);
            }

            var i = 0;

            while (query.Template.Contains("{Value" + (i + 1) + "}"))
            {
                i++;
            }
            if (i > 0)
            {
                query.ValueStyle = ValueStyle.Multiple;
                query.ValueArity = i;
            }
            else
            {
                query.ValueStyle = !query.Template.Contains("{Values}")
                    ? ValueStyle.Single
                    : ValueStyle.List;
            }
            return(query);
        }
Exemplo n.º 2
0
        public void Register(IAppHost appHost)
        {
            foreach (var entry in ImplicitConventions)
            {
                var key = entry.Key.Trim('%');
                var fmt = entry.Value;
                var query = new QueryFieldAttribute {
                    Template = fmt
                }.Init();
                if (entry.Key.EndsWith("%"))
                {
                    StartsWithConventions[key] = query;
                }
                if (entry.Key.StartsWith("%"))
                {
                    EndsWithConventions[key] = query;
                }
            }

            appHost.GetContainer().Register <IAutoQuery>(c =>
                                                         new AutoQuery
            {
                IgnoreProperties         = IgnoreProperties,
                IllegalSqlFragmentTokens = IllegalSqlFragmentTokens,
                MaxLimit                      = MaxLimit,
                EnableUntypedQueries          = EnableUntypedQueries,
                EnableSqlFilters              = EnableRawSqlFilters,
                OrderByPrimaryKeyOnLimitQuery = OrderByPrimaryKeyOnPagedQuery,
                QueryFilters                  = QueryFilters,
                ResponseFilters               = ResponseFilters,
                StartsWithConventions         = StartsWithConventions,
                EndsWithConventions           = EndsWithConventions,
                Db = UseNamedConnection != null
                        ? c.Resolve <IDbConnectionFactory>().OpenDbConnection(UseNamedConnection)
                        : c.Resolve <IDbConnectionFactory>().OpenDbConnection(),
            })
            .ReusedWithin(ReuseScope.None);

            appHost.Metadata.GetOperationAssemblies()
            .Each(x => LoadFromAssemblies.Add(x));

            if (EnableAutoQueryViewer)
            {
                appHost.RegisterService <AutoQueryMetadataService>();
            }
        }
Exemplo n.º 3
0
 public MatchQuery(Tuple<ModelDefinition,FieldDefinition> match, QueryFieldAttribute implicitQuery)
 {
     ModelDef = match.Item1;
     FieldDef = match.Item2;
     ImplicitQuery = implicitQuery;
 }
Exemplo n.º 4
0
        private static void AddCondition(SqlExpression<From> q, string defaultTerm, string quotedColumn, object value, QueryFieldAttribute implicitQuery)
        {
            var seq = value as IEnumerable;
            if (value is string)
                seq = null;
            var format = seq == null 
                ? quotedColumn + " = {0}"
                : quotedColumn + " IN ({0})";
            if (implicitQuery != null)
            {
                var operand = implicitQuery.Operand ?? "=";
                if (implicitQuery.Term == QueryTerm.Or)
                    defaultTerm = "OR";
                else if (implicitQuery.Term == QueryTerm.And)
                    defaultTerm = "AND";

                format = "(" + quotedColumn + " " + operand + " {0}" + ")";
                if (implicitQuery.Template != null)
                {
                    format = implicitQuery.Template.Replace("{Field}", quotedColumn);

                    if (implicitQuery.ValueStyle == ValueStyle.Multiple)
                    {
                        if (seq == null)
                            throw new ArgumentException("{0} requires {1} values"
                                .Fmt(implicitQuery.Field, implicitQuery.ValueArity));

                        var args = new object[implicitQuery.ValueArity];
                        int i = 0;
                        foreach (var x in seq)
                        {
                            if (i < args.Length)
                            {
                                format = format.Replace("{Value" + (i + 1) + "}", "{" + i + "}");
                                args[i++] = x;
                            }
                        }

                        q.AddCondition(defaultTerm, format, args);
                        return;
                    }
                    if (implicitQuery.ValueStyle == ValueStyle.List)
                    {
                        if (seq == null)
                            throw new ArgumentException("{0} expects a list of values".Fmt(implicitQuery.Field));

                        format = format.Replace("{Values}", "{0}");
                        value = new SqlInValues(seq);
                    }
                    else
                    {
                        format = format.Replace("{Value}", "{0}");
                    }

                    if (implicitQuery.ValueFormat != null)
                        value = string.Format(implicitQuery.ValueFormat, value);
                }
            }
            else
            {
                if (seq != null)
                    value = new SqlInValues(seq);
            }

            q.AddCondition(defaultTerm, format, value);
        }