Esempio n. 1
0
        /// <summary>
        /// This method transforms the input SQL query into a set of statements,
        /// prepares and executes them against the provided context.
        /// </summary>
        /// <param name="context">The context used to prepare and execute the statements
        /// resolved from the compilation of the input query.</param>
        /// <param name="query">The input SQL query with optional parameters, that is
        /// compiled into a set of statements to be executed.</param>
        /// <remarks>
        /// The method first tries to resolve the compiled statements from the specialized
        /// cache (<see cref="StatementCache"/>) from the system, to reduce the compilation time.
        /// </remarks>
        /// <returns>
        /// Returns an array of <see cref="ITable"/> objects that represent the results
        /// of the execution of the input query.
        /// </returns>
        public static ITable[] Execute(IQueryContext context, SqlQuery query)
        {
            if (query == null)
                throw new ArgumentNullException("query");

            var sqlSouce = query.Text;

            // TODO: find it from the cache...

            var statements = SqlStatement.Parse(sqlSouce);

            // TODO: set it in cache ...

            var preparer = new QueryPreparer(query);

            bool statementSeen = false;

            var results = new List<ITable>();
            foreach (var statement in statements) {
                context.RegisterQuery(statement);

                // TODO: Invoke diagnostics for the preparation...

                var prepared = statement.Prepare(preparer, context);

                ITable result;

                try {
                    result = prepared.Execute(context);
                } catch(StatementException ex) {
                    context.RegisterError(ex);
                    throw;
                } catch (Exception ex) {
                    var sex = new StatementException("An unhanded error occurred while executing the statement.", ex);
                    context.RegisterError(sex);
                    throw sex;
                } finally {
                    statementSeen = true;
                }

                results.Add(result);
            }

            if (!statementSeen)
                throw new SqlParseException("The input query was not parsed in any statements that could be executed.");

            return results.ToArray();
        }