Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004 - 2011 Brian McCallister
   *
   *    Licensed under the Apache License, Version 2.0 (the "License");
   *    you may not use this file except in compliance with the License.
   *    You may obtain a copy of the License at
   *
   *        http://www.apache.org/licenses/LICENSE-2.0
   *
  *    Unless required by applicable law or agreed to in writing, software
  *    distributed under the License is distributed on an "AS IS" BASIS,
  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *    See the License for the specific language governing permissions and
  *    limitations under the License.
  */
 
 package org.skife.jdbi.v2;
 
 import  org.antlr.runtime.ANTLRStringStream;
 import  org.antlr.runtime.Token;
 
 import java.util.List;
 
 import static org.skife.jdbi.rewriter.hash.HashStatementLexer.*;

Statement rewriter which replaces named parameter tokens of the form #tokenName
 
 public class HashPrefixStatementRewriter implements StatementRewriter
 {
    
Munge up the SQL as desired. Responsible for figuring out ow to bind any arguments in to the resultant prepared statement.

Parameters:
sql The SQL to rewrite
params contains the arguments which have been bound to this statement.
ctx The statement context for the statement being executed
Returns:
somethign which can provde the actual SQL to prepare a statement from and which can bind the correct arguments to that prepared statement
 
     public RewrittenStatement rewrite(String sqlBinding paramsStatementContext ctx)
     {
         final ParsedStatement stmt = new ParsedStatement();
         try {
             final String parsedSql = parseString(sqlstmt);
             return new MyRewrittenStatement(parsedSqlstmtctx);
         }
         catch (IllegalArgumentException e) {
             throw new UnableToCreateStatementException("Exception parsing for named parameter replacement"ectx);
         }
 
     }
 
     String parseString(final String sqlfinal ParsedStatement stmtthrows IllegalArgumentException
     {
         StringBuilder b = new StringBuilder();
         HashStatementLexer lexer = new HashStatementLexer(new ANTLRStringStream(sql));
         Token t = lexer.nextToken();
         while (t.getType() != .) {
             switch (t.getType()) {
                 case :
                     b.append(t.getText());
                     break;
                 case :
                     stmt.addNamedParamAt(t.getText().substring(1, t.getText().length()));
                     b.append("?");
                     break;
                 case :
                     b.append(t.getText());
                     break;
                 case :
                     b.append(t.getText());
                     break;
                 case :
                     b.append("?");
                     stmt.addPositionalParamAt();
                     break;
                 case :
                     b.append(t.getText().substring(1));
                     break;
             }
             t = lexer.nextToken();
         }
         return b.toString();
     }
 
     private static class MyRewrittenStatement implements RewrittenStatement
     {
         private final String sql;
        private final ParsedStatement stmt;
        private final StatementContext context;
        public MyRewrittenStatement(String sqlParsedStatement stmtStatementContext ctx)
        {
            this. = ctx;
            this. = sql;
            this. = stmt;
        }
        public void bind(Binding paramsPreparedStatement statementthrows SQLException
        {
            if (.) {
                // no named params, is easy
                boolean finished = false;
                for (int i = 0; !finished; ++i) {
                    final Argument a = params.forPosition(i);
                    if (a != null) {
                        try {
                            a.apply(i + 1, statementthis.);
                        }
                        catch (SQLException e) {
                            throw new UnableToExecuteStatementException(
                                String.format("Exception while binding positional param at (0 based) position %d",
                                              i), e);
                        }
                    }
                    else {
                        finished = true;
                    }
                }
            }
            else {
                //List<String> named_params = stmt.params;
                int i = 0;
                for (String named_param : .) {
                    if ("*".equals(named_param)) continue;
                    Argument a = params.forName(named_param);
                    if (a == null) {
                        a = params.forPosition(i);
                    }
                    if (a == null) {
                        String msg = String.format("Unable to execute, no named parameter matches " +
                                                   "\"%s\" and no positional param for place %d (which is %d in " +
                                                   "the JDBC 'start at 1' scheme) has been set.",
                                                   named_paramii + 1);
                        throw new UnableToExecuteStatementException(msg);
                    }
                    try {
                        a.apply(i + 1, statementthis.);
                    }
                    catch (SQLException e) {
                        throw new UnableToCreateStatementException(String.format("Exception while binding '%s'",
                                                                                 named_param), e);
                    }
                    i++;
                }
            }
        }
        public String getSql()
        {
            return ;
        }
    }
    static class ParsedStatement
    {
        private boolean positionalOnly = true;
        private List<Stringparams = new ArrayList<String>();
        public void addNamedParamAt(String name)
        {
             = false;
            .add(name);
        }
        public void addPositionalParamAt()
        {
            .add("*");
        }
    }
New to GrepCode? Check out our FAQ X