protected virtual TOut OnGetByQueryAs <TOut>(Type structureType, Expression <Func <TOut, bool> > predicate)
            where TOut : class
        {
            Ensure.That(structureType, "structureType").IsNotNull();
            Ensure.That(predicate, "predicate").IsNotNull();

            var structureSchema = OnUpsertStructureSchema(structureType);

            return(Db.CacheProvider.Consume(
                       structureSchema,
                       predicate,
                       e =>
            {
                var queryBuilder = Db.ProviderFactory.GetQueryBuilder <TOut>(Db.StructureSchemas);
                queryBuilder.Where(predicate);
                var query = queryBuilder.Build();

                var sqlQuery = QueryGenerator.GenerateQuery(query);
                var sourceData = DbClient.ReadJson(structureSchema, sqlQuery.Sql, sqlQuery.Parameters).SingleOrDefault();

                return Db.Serializer.Deserialize <TOut>(sourceData);
            },
                       CacheConsumeMode));
        }
Exemple #2
0
        protected virtual IEnumerable <object> OnQuery(IQuery query, Type structureType)
        {
            Ensure.That(structureType, "structureType").IsNotNull();
            Ensure.That(query, "query").IsNotNull();

            var structureSchema = OnUpsertStructureSchema(structureType);

            if (query.IsEmpty)
            {
                return(Db.Serializer.DeserializeMany(DbClient.GetJsonOrderedByStructureId(structureSchema), structureType));
            }

            var sqlQuery = QueryGenerator.GenerateQuery(query);

            return(Db.CacheProvider.Consume(
                       structureSchema,
                       sqlQuery,
                       q =>
            {
                var sourceData = DbClient.ReadJson(structureSchema, sqlQuery.Sql, sqlQuery.Parameters);
                return Db.Serializer.DeserializeMany(sourceData, structureType);
            },
                       ExecutionContext.Session.CacheConsumeMode));
        }
Exemple #3
0
        public string Generate(string name, Action <IQueryBuilder <T> > spec)
        {
            Ensure.That(name, "name").IsNotNullOrWhiteSpace();
            Ensure.That(spec, "spec").IsNotNull();

            QueryBuilder.Clear();
            spec.Invoke(QueryBuilder);

            //Turns lamba expressions to a custom (serializable) node tree
            var query = QueryBuilder.Build();

            //Turns the parsed lambdas above to an internal SQL Expression tree and then to a SQL Query
            var dbQuery = DbQueryGenerator.GenerateQuery(query);

            const string format     = "create procedure {0}\r\n{1}\r\nas\r\nbegin\r\n{2}\r\nend;";
            var          parameters = GenerateInputParametersString(dbQuery);
            var          body       = GenerateBody(dbQuery);

            return(string.Format(format, name, parameters, body));
        }