Ejemplo n.º 1
0
 /**
  * Convert JDBC escape sequences in the SQL statement if required. This method throws an exception
  * if the SQL statement is null.
  *
  * @param sql the SQL statement with or without JDBC escape sequences
  * @param escapeProcessing whether escape sequences should be replaced
  * @return the SQL statement without JDBC escape sequences
  */
 String translateSQL(String sql, boolean escapeProcessing) {
   if (sql == null) {
     throw DbException.getInvalidValueException(sql, "SQL");
   }
   if (!escapeProcessing) {
     return sql;
   }
   if (sql.indexOf('{') < 0) {
     return sql;
   }
   int len = sql.length();
   char[] chars = null;
   int level = 0;
   for (int i = 0; i < len; i++) {
     char c = sql.charAt(i);
     switch (c) {
       case '\'':
       case '"':
       case '/':
       case '-':
         i = translateGetEnd(sql, i, c);
         break;
       case '{':
         level++;
         if (chars == null) {
           chars = sql.toCharArray();
         }
         chars[i] = ' ';
         while (Character.isSpaceChar(chars[i])) {
           i++;
           checkRunOver(i, len, sql);
         }
         int start = i;
         if (chars[i] >= '0' && chars[i] <= '9') {
           chars[i - 1] = '{';
           while (true) {
             checkRunOver(i, len, sql);
             c = chars[i];
             if (c == '}') {
               break;
             }
             switch (c) {
               case '\'':
               case '"':
               case '/':
               case '-':
                 i = translateGetEnd(sql, i, c);
                 break;
               default:
             }
             i++;
           }
           level--;
           break;
         } else if (chars[i] == '?') {
           chars[i++] = ' ';
           checkRunOver(i, len, sql);
           while (Character.isSpaceChar(chars[i])) {
             i++;
             checkRunOver(i, len, sql);
           }
           if (sql.charAt(i) != '=') {
             throw DbException.getSyntaxError(sql, i, "=");
           }
           chars[i++] = ' ';
           checkRunOver(i, len, sql);
           while (Character.isSpaceChar(chars[i])) {
             i++;
             checkRunOver(i, len, sql);
           }
         }
         while (!Character.isSpaceChar(chars[i])) {
           i++;
           checkRunOver(i, len, sql);
         }
         int remove = 0;
         if (found(sql, start, "fn")) {
           remove = 2;
         } else if (found(sql, start, "escape")) {
           break;
         } else if (found(sql, start, "call")) {
           break;
         } else if (found(sql, start, "oj")) {
           remove = 2;
         } else if (found(sql, start, "ts")) {
           remove = 2;
         } else if (found(sql, start, "t")) {
           remove = 1;
         } else if (found(sql, start, "d")) {
           remove = 1;
         } else if (found(sql, start, "params")) {
           remove = "params".length();
         }
         for (i = start; remove > 0; i++, remove--) {
           chars[i] = ' ';
         }
         break;
       case '}':
         if (--level < 0) {
           throw DbException.getSyntaxError(sql, i);
         }
         chars[i] = ' ';
         break;
       case '$':
         i = translateGetEnd(sql, i, c);
         break;
       default:
     }
   }
   if (level != 0) {
     throw DbException.getSyntaxError(sql, sql.length() - 1);
   }
   if (chars != null) {
     sql = new String(chars);
   }
   return sql;
 }
Ejemplo n.º 2
0
 private int translateGetEnd(String sql, int i, char c) {
   int len = sql.length();
   switch (c) {
     case '$':
       {
         if (i < len - 1 && sql.charAt(i + 1) == '$' && (i == 0 || sql.charAt(i - 1) <= ' ')) {
           int j = sql.indexOf("$$", i + 2);
           if (j < 0) {
             throw DbException.getSyntaxError(sql, i);
           }
           return j + 1;
         }
         return i;
       }
     case '\'':
       {
         int j = sql.indexOf('\'', i + 1);
         if (j < 0) {
           throw DbException.getSyntaxError(sql, i);
         }
         return j;
       }
     case '"':
       {
         int j = sql.indexOf('"', i + 1);
         if (j < 0) {
           throw DbException.getSyntaxError(sql, i);
         }
         return j;
       }
     case '/':
       {
         checkRunOver(i + 1, len, sql);
         if (sql.charAt(i + 1) == '*') {
           // block comment
           int j = sql.indexOf("*/", i + 2);
           if (j < 0) {
             throw DbException.getSyntaxError(sql, i);
           }
           i = j + 1;
         } else if (sql.charAt(i + 1) == '/') {
           // single line comment
           i += 2;
           while (i < len && (c = sql.charAt(i)) != '\r' && c != '\n') {
             i++;
           }
         }
         return i;
       }
     case '-':
       {
         checkRunOver(i + 1, len, sql);
         if (sql.charAt(i + 1) == '-') {
           // single line comment
           i += 2;
           while (i < len && (c = sql.charAt(i)) != '\r' && c != '\n') {
             i++;
           }
         }
         return i;
       }
     default:
       throw DbException.throwInternalError("c=" + c);
   }
 }