public static IEnumerable <SqlStatement> Parse(IContext context, SqlQuery query) { if (query == null) { throw new ArgumentNullException("query"); } ISqlCompiler compiler = null; if (context != null) { compiler = context.ResolveService <ISqlCompiler>(); } if (compiler == null) { compiler = new PlSqlCompiler(); } try { var compileContext = new SqlCompileContext(context, query.Text); var result = compiler.Compile(compileContext); if (result.HasErrors) { var messages = new StringBuilder(); messages.AppendFormat("SqlParseException for '{0}'" + Environment.NewLine, query.Text); foreach (var m in result.Messages) { messages.AppendFormat("Level: {0}", m.Level); if (null != m.Location) { messages.AppendFormat(", Line: {0}, Column: {1}", m.Location.Line, m.Location.Column); } messages.AppendFormat(", Message: {0}", m.Text); messages.AppendLine(); } throw new FormatException(messages.ToString()); } var statements = result.Statements; foreach (var statement in statements) { if (statement != null) { statement.SetSource(query); } } return(statements); } catch (Exception ex) { var messages = new StringBuilder(); messages.AppendFormat("The input string '{0}'" + Environment.NewLine, query.Text); messages.AppendFormat(" cannot be parsed into SQL Statements, because of {0}" + Environment.NewLine, ex.ToString()); throw new FormatException(messages.ToString(), ex); } }
public static StatementResult[] ExecuteQuery(this IQuery query, SqlQuery sqlQuery) { if (sqlQuery == null) { throw new ArgumentNullException("sqlQuery"); } SqlStatement[] statements = null; bool cacheGet = false; var cache = query.Context.ResolveService <IStatementCache>(); if (cache != null) { if (cache.TryGet(sqlQuery.Text, out statements)) { cacheGet = true; } } if (statements == null) { var sqlSouce = sqlQuery.Text; var compiler = query.Context.SqlCompiler(); if (compiler == null) { compiler = new PlSqlCompiler(); } var compileResult = compiler.Compile(new SqlCompileContext(query.Context, sqlSouce)); if (compileResult.HasErrors) { // TODO: throw a specialized exception... throw new InvalidOperationException("The compilation of the query thrown errors."); } statements = compileResult.Statements.ToArray(); } if (statements.Length == 0) { return(new StatementResult[0]); } if (cache != null && !cacheGet) { cache.Set(sqlQuery.Text, statements); } query.Context.RegisterEvent(new QueryEvent(sqlQuery, QueryEventType.BeforeExecute, null)); var paramStyle = sqlQuery.ParameterStyle; if (paramStyle == QueryParameterStyle.Default) { paramStyle = query.ParameterStyle(); } var preparer = new QueryPreparer(sqlQuery, paramStyle); var result = query.ExecuteStatements(preparer, statements); query.Context.RegisterEvent(new QueryEvent(sqlQuery, QueryEventType.AfterExecute, result)); return(result); }