public object[] SelectWhere(Type type, string where, dynamic paramenter)
        {
            //Concret declaration is nessesary because we are working with dynmaics, so the compiler has ne space to guess the type wrong
            IEnumerable <IQueryParameter> enumarateFromDynamics = DbAccessLayerHelper.EnumarateFromDynamics(paramenter);

            return(SelectWhere(type, where, enumarateFromDynamics));
        }
        public T[] RunPrimetivSelect <T>(string query, dynamic parameters)
        {
            IEnumerable <IQueryParameter> enumarateFromDynamics = DbAccessLayerHelper.EnumarateFromDynamics(parameters);
            var runPrimetivSelect = RunPrimetivSelect(typeof(T), query, enumarateFromDynamics);

            return(runPrimetivSelect.Cast <T>().ToArray());
        }
        /// <summary>
        ///     Adds Parameter to the QueryCommand object without adding a Statement
        /// </summary>
        /// <returns></returns>
        public static TQuery WithParamerters <TQuery>(this TQuery query, dynamic paramerters)
            where TQuery : class, IQueryBuilder
        {
            IEnumerable <IQueryParameter> parameters = DbAccessLayerHelper.EnumarateFromDynamics(paramerters);

            return(query.QueryQ(string.Empty, parameters.ToArray()));
        }
        /// <summary>
        ///     Runs
        ///     <paramref name="command" />
        ///     and parses output into
        ///     <paramref name="type" />
        /// </summary>
        /// <returns></returns>
        public async Task <object[]> SelectNativeAsync(Type type, IDbCommand command, dynamic paramenter)
        {
            IEnumerable <IQueryParameter> enumarateFromDynamics = DbAccessLayerHelper.EnumarateFromDynamics(paramenter);

            foreach (var enumarateFromDynamic in enumarateFromDynamics)
            {
                command.Parameters.AddWithValue(enumarateFromDynamic.Name, enumarateFromDynamic.Value, Database);
            }

            return(await RunSelectAsync(type, command));
        }
        /// <summary>
        ///     Adds a QueryCommand part to <paramref name="builder" />
        /// </summary>
        /// <returns></returns>
        public static TQuery QueryD <TQuery>(this TQuery builder, string query, dynamic paramerters)
            where TQuery : class, IQueryBuilder
        {
            if (paramerters != null)
            {
                IEnumerable <IQueryParameter> parameters = DbAccessLayerHelper.EnumarateFromDynamics(paramerters);
                return(builder.Add(new GenericQueryPart(query, parameters, builder)));
            }

            return(builder.Add(new GenericQueryPart(query)));
        }
示例#6
0
        /// <summary>
        ///     Wraps a QueryCommand and its Paramters from Dynamic and then executes it
        /// </summary>
        /// <returns></returns>
        public int ExecuteGenericCommand(string query, dynamic paramenter)
        {
            var parameters = paramenter as IEnumerable <IQueryParameter>;

            if (parameters != null)
            {
                var parm = parameters;
                return(ExecuteGenericCommand(query, parm));
            }

            return(ExecuteGenericCommand(query,
                                         (IEnumerable <IQueryParameter>)DbAccessLayerHelper.EnumarateFromDynamics(paramenter)));
        }
示例#7
0
        /// <summary>
        ///     Wraps a QueryCommand and its Paramters and then executes it
        /// </summary>
        /// <returns></returns>
        public int ExecuteGenericCommand(string query, IEnumerable <IQueryParameter> values)
        {
            var command = DbAccessLayerHelper.CreateCommand(Database, query);

            if (values != null)
            {
                foreach (var item in values)
                {
                    command.Parameters.AddWithValue(item.Name, item.Value, Database);
                }
            }

            Database.LastExecutedQuery?.Refresh();
            return(ExecuteGenericCommand(command));
        }
示例#8
0
        public IQueryFactoryResult Process(IQueryContainer container)
        {
            var subSelect =
                DbAccessLayerHelper.MergeQueryFactoryResult(true, 1, true, null,
                                                            SubSelectionQueryParts.Select(e => e.Process(container)).Where(e => e != null).ToArray());
            var modifer = Distinct ? "DISTINCT" : "";

            modifer += Limit.HasValue ? " TOP" + Limit.Value : "";

            var select = new QueryFactoryResult(
                $"SELECT {modifer} {Columns.Select(e => e.ColumnAliasStatement()).Aggregate((e, f) => e + "," + f)} " +
                $"FROM ({subSelect.Query}) AS [{Alias.GetAlias()}]",
                subSelect.Parameters.ToArray());

            return(select);
        }
        /// <inheritdoc />
        public IQueryFactoryResult Compile(out IEnumerable <ColumnInfo> columns)
        {
            var commands = new List <IQueryFactoryResult>();

            columns = new ColumnInfo[0];
            foreach (var queryPart in Parts)
            {
                commands.Add(queryPart.Process(this));
                var isSelectQuery = (queryPart is ISelectableQueryPart && !(queryPart is JoinTableQueryPart));
                if (isSelectQuery)
                {
                    columns = (queryPart as ISelectableQueryPart).Columns;
                }
            }

            return(DbAccessLayerHelper.MergeQueryFactoryResult(true, 1, true, null,
                                                               commands.Where(e => e != null).ToArray()));
        }
        public object[] RunSelect(Type type, string queryString,
                                  IEnumerable <IQueryParameter> paramenter)
        {
            return
                (Database.Run(
                     s =>
            {
                var query = DbAccessLayerHelper.CreateCommand(s, queryString);

                foreach (var item in paramenter)
                {
                    query.Parameters.AddWithValue(item.Name, item.Value, s);
                }
                if (Database.LastExecutedQuery != null)
                {
                    Database.LastExecutedQuery.Refresh();
                }
                Database.PrepaireRemoteExecution(query);
                return RunSelect(type, query);
            }
                     ));
        }
示例#11
0
        public IQueryFactoryResult Process(IQueryContainer container)
        {
            var commandBuilder = new StringBuilder();
            var commands       = new List <IQueryFactoryResult>();
            var first          = true;

            foreach (var cteInfo in CteInfos)
            {
                if (!first)
                {
                    commandBuilder.Append(", ");
                }

                first = false;
                commandBuilder.Append($"WITH {cteInfo.Name.Value} AS (");
                var cteCommand = DbAccessLayerHelper.MergeQueryFactoryResult(true, 1, true, null,
                                                                             cteInfo.CteContentParts.Select(e => e.Process(container)).Where(e => e != null).ToArray());
                commandBuilder.Append(cteCommand.Query);
                commandBuilder.Append(")");
                commands.Add(new QueryFactoryResult(commandBuilder.ToString(), cteCommand.Parameters.ToArray()));
            }

            return(DbAccessLayerHelper.MergeQueryFactoryResult(true, 1, true, null, commands.ToArray()));
        }
示例#12
0
 /// <summary>
 ///     Execute a QueryCommand without Paramters
 /// </summary>
 /// <returns></returns>
 public int ExecuteGenericCommand(string query)
 {
     return(ExecuteGenericCommand(DbAccessLayerHelper.CreateCommand(Database, query)));
 }
        internal IDbCommand GenericQueryCreation <TE>(
            DbClassInfoCache type,
            Func <object, IDatabase, IDbCommand> fallback,
            object entity = null,
            params object[] parameter)
            where TE : DbAccessTypeAttribute
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (fallback == null)
            {
                throw new ArgumentNullException("fallback");
            }

            var factoryAttribute = typeof(TE);

            try
            {
                if (_isIndented.Value)
                {
                    if (Multipath)
                    {
                        return(fallback(entity, Database));
                    }
                    else
                    {
                        throw new InvalidOperationException(
                                  "This method is not allowed in the context of any FactoryMethod. Enable Multipath to allow the Intiligent Query creation");
                    }
                }
                _isIndented.Value = true;

                var arguments = parameter.ToList();

                //try to get the attribute for static selection
                if (!arguments.Any())
                {
                    if (factoryAttribute == typeof(SelectFactoryMethodAttribute) && type.SelectFactory != null &&
                        (!IsMultiProviderEnvironment || type.SelectFactory.Attribute.TargetDatabase == Database.TargetDatabase))
                    {
                        return(DbAccessLayerHelper.CreateCommand(Database, type.SelectFactory.Attribute.Query));
                    }
                }

                var methods =
                    type.Mehtods
                    .Where(s => s.Attributes.Any(e => e.Attribute is TE && (!IsMultiProviderEnvironment
                                                                            ||
                                                                            ((TE)e.Attribute).TargetDatabase ==
                                                                            Database.TargetDatabase)))
                    .ToArray();

                if (methods.Any())
                {
                    var searchMethodWithFittingParams = methods.Where(s =>
                    {
                        var parameterInfos = s.Arguments.Where(f => typeof(RootQuery) != f.Type).ToArray();

                        if (parameterInfos.Length != arguments.Count)
                        {
                            return(false);
                        }

                        for (var i = 0; i < parameterInfos.Length; i++)
                        {
                            var para = parameterInfos[i];
                            if (para.ParameterInfo.IsOptional)
                            {
                                continue;
                            }
                            var tryParam = arguments[i];
                            if (tryParam == null)
                            {
                                return(false);
                            }
                            if (!(para.Type == tryParam.GetType()))
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }).ToArray();

                    if (searchMethodWithFittingParams.Length != 1)
                    {
                        if (CheckFactoryArguments && arguments.Any())
                        {
                            ThrowNoFactoryFoundException <TE>(arguments);
                        }
                        return(fallback(entity, Database));
                    }

                    var method = searchMethodWithFittingParams.First();
                    //must be public static if its an Select otherwise it has to be an instance member
                    if (factoryAttribute == typeof(SelectFactoryMethodAttribute) &&
                        !method.MethodInfo.IsStatic ||
                        factoryAttribute != typeof(SelectFactoryMethodAttribute) &&
                        method.MethodInfo.IsStatic)
                    {
                        if (CheckFactoryArguments && arguments.Any())
                        {
                            ThrowNoFactoryFoundException <TE>(arguments);
                        }
                        return(fallback(entity, Database));
                    }

                    var           cleanParams      = arguments.Any() ? arguments : null;
                    var           dbMethodArgument = method.Arguments.FirstOrDefault();
                    IQueryBuilder queryBuilder     = null;
                    if (dbMethodArgument != null && dbMethodArgument.Type == typeof(RootQuery))
                    {
                        if (method.ReturnType != typeof(IQueryBuilder))
                        {
                            ThrowNoFactoryFoundException <TE>(arguments);
                        }

                        queryBuilder = Query();
                        if (cleanParams == null)
                        {
                            cleanParams = new List <object>();
                        }

                        cleanParams.Insert(0, queryBuilder);
                    }

                    object invoke;
                    if (cleanParams != null)
                    {
                        invoke = method.Invoke(entity, cleanParams.ToArray());
                    }
                    else
                    {
                        invoke = method.Invoke(entity);
                    }
                    if (invoke != null)
                    {
                        if (invoke is IQueryBuilder)
                        {
                            return((invoke as IQueryBuilder).ContainerObject.Compile());
                        }

                        if (!string.IsNullOrEmpty(invoke as string))
                        {
                            return(DbAccessLayerHelper.CreateCommand(Database, invoke as string));
                        }
                        if (invoke is IQueryFactoryResult)
                        {
                            var result = invoke as IQueryFactoryResult;
                            return(Database.CreateCommandWithParameterValues(result.Query, result.Parameters));
                        }
                    }
                }
                if (CheckFactoryArguments && arguments.Any())
                {
                    ThrowNoFactoryFoundException <TE>(arguments);
                }
                return(fallback(entity, Database));
            }
            finally
            {
                _isIndented.Value = false;
            }
        }
 /// <summary>
 ///     Creates a Select with appended query.
 ///		Should be only executed inside an open <code>Database.Run</code>
 /// </summary>
 /// <returns></returns>
 public IDbCommand CreateSelect(Type type, string query)
 {
     return(DbAccessLayerHelper.CreateCommand(Database,
                                              CreateSelectQueryFactory(GetClassInfo(type)).CommandText + " " + query));
 }
        /// <summary>
        ///     Runs
        ///     <paramref name="query" />
        ///     and parses output into
        ///     <paramref name="type" />
        /// </summary>
        /// <returns></returns>
        public async Task <object[]> SelectNativeAsync(Type type, string query, dynamic paramenter)
        {
            IEnumerable <IQueryParameter> enumarateFromDynamics = DbAccessLayerHelper.EnumarateFromDynamics(paramenter);

            return(await SelectNativeAsync(type, query, enumarateFromDynamics));
        }
 /// <summary>
 ///     Runs
 ///     <paramref name="query" />
 ///     and parses output into
 ///     <paramref name="type" />
 /// </summary>
 /// <returns></returns>
 public async Task <object[]> SelectNativeAsync(Type type, string query)
 {
     return(await SelectNativeAsync(type, DbAccessLayerHelper.CreateCommand(Database, query)));
 }