|
| 1 | +/* |
| 2 | + * Datart |
| 3 | + * <p> |
| 4 | + * Copyright 2021 |
| 5 | + * <p> |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package datart.core.common; |
| 20 | + |
| 21 | +import org.apache.ibatis.javassist.ClassPool; |
| 22 | +import org.apache.ibatis.javassist.CtClass; |
| 23 | +import org.apache.ibatis.javassist.CtMethod; |
| 24 | + |
| 25 | +public class ClassTransformer { |
| 26 | + |
| 27 | + public static void transform() { |
| 28 | + transformSqlWriter(); |
| 29 | + transformFlyway(); |
| 30 | + } |
| 31 | + |
| 32 | + private static void transformSqlWriter() { |
| 33 | + try { |
| 34 | + ClassPool classPool = ClassPool.getDefault(); |
| 35 | + CtClass ctClass = classPool.get("org.apache.calcite.sql.pretty.SqlPrettyWriter"); |
| 36 | + CtMethod keyword = ctClass.getDeclaredMethod("keyword"); |
| 37 | + keyword.setBody("{ maybeWhitespace($1);" + |
| 38 | + " buf.append($1);" + |
| 39 | + " if (!$1.equals(\"\")) {" + |
| 40 | + " setNeedWhitespace(needWhitespaceAfter($1));" + |
| 41 | + " } " + |
| 42 | + "return;} "); |
| 43 | + ctClass.toClass(); |
| 44 | + } catch (Exception e) { |
| 45 | + e.printStackTrace(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void transformFlyway() { |
| 50 | + try { |
| 51 | + ClassPool classPool = ClassPool.getDefault(); |
| 52 | + CtClass ctClass = classPool.get("org.flywaydb.core.internal.database.mysql.MySQLConnection"); |
| 53 | + CtMethod getIntVariableValue = ctClass.getDeclaredMethod("getIntVariableValue"); |
| 54 | + getIntVariableValue.setBody("return 0;"); |
| 55 | + |
| 56 | + CtMethod doRestoreOriginalState = ctClass.getDeclaredMethod("doRestoreOriginalState"); |
| 57 | + doRestoreOriginalState.setBody("return;"); |
| 58 | + |
| 59 | + CtMethod hasUserVariableResetCapability = ctClass.getDeclaredMethod("hasUserVariableResetCapability"); |
| 60 | + hasUserVariableResetCapability.setBody("{return false;}"); |
| 61 | + ctClass.toClass(); |
| 62 | + } catch (Exception e) { |
| 63 | + e.printStackTrace(); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments