看到同事痛苦的拼写SQL语句并转化成java String语句,自己不仅感到汗言,说实话过于复杂的SQL语句要换成我也未必会拼写的好.而且如果每个表都采用手工去拼写且转化的话,熟练的开发人员可能要5到10分钟,不熟悉的可能要更长的时间这样是很浪费时间的.很久以前我就写过类似的工具可以见我系列文章<自己动手编写DB2小工具>.
既然自己喜欢groovy,于是花了10来分钟写了个简单的根据表名自动生成 SQL语句的小脚本.这样只要输入表名就可以得到这个表的select\insert语句以及对应的java代码.
groovy脚本 run.groovy
- importgroovy.sql.Sql;
-
- defgetJavaStringBuilder={tableName,strSQL->
- tableName=tableName.replaceAll("\\.","_");
- tableName=tableName.replaceAll("","");
- strRetVal="StringBuilderstrBuilder_${tableName}=newStringBuilder(1007);\r\n"
- strSQL.eachLine{
- strRetVal+="strBuilder_${tableName}.append(\""+it+"\");\r\n";
- }
- returnstrRetVal;
- }
-
-
- sql=Sql.newInstance("jdbc:oracle:thin:@10.220.51.40:1521:ORA10G","用户名","密码","oracle.jdbc.driver.OracleDriver");
-
-
- strSqlTableName=this.args[0];
-
- strSQL="select*from${strSqlTableName}where1<>1"
- deffile_tableName=strSqlTableName;
- defresult_columnCount=0;
- defresult_columnName=[];
- defresult_columnType=[];
- defresult_rowsData=[];
- defrows_count=0;
-
- file_csv=newFile("${file_tableName}.sql");
-
- if(file_csv.exists()){
- println"在当前目录下发现已经存在${file_tableName},程序已经删除文件"
- file_csv.delete()
- }
- println"准备生成${file_tableName}.csv"
- sql.eachRow(strSQL,
- {
- result_columnCount=it.getColumnCount();
- println"*********${file_tableName}表结构**********"
- println":本次导出共生成${result_columnCount}个字段";
- for(iin1..result_columnCount){
- printlnit.getColumnName(i)+"|"+it.getColumnTypeName(i);
- result_columnName<<it.getColumnName(i);
- result_columnType<<it.getColumnTypeName(i);
- }
- println"*********${file_tableName}表结构**********"
-
- println"开始生成数据,请耐心等待......"
- },
- {
- }
- );
-
- file_csv<<"--SELECT语句\r\n"
- strTemp="SELECT\r\n${result_columnName.join(',\r\n')}\r\nFROM\r\n${strSqlTableName}"
- file_csv<<strTemp+"\r\n\r\n\r\n"
- file_csv<<"--SELECTjava语句\r\n"
- file_csv<<getJavaStringBuilder(file_tableName,strTemp);
- file_csv<<"\r\n\r\n\r\n"
-
- file_csv<<"--INSERT语句\r\n"
- strTemp="INSERTINTO${strSqlTableName}\r\n(\r\n${result_columnName.join(',\r\n')}\r\nVALUES(\r\n${result_columnType.join(',\r\n')}\r\n)";
- file_csv<<strTemp+"\r\n\r\n\r\n"
- file_csv<<"--INSERTjava语句\r\n"
- file_csv<<getJavaStringBuilder(file_tableName,strTemp);
- file_csv<<"\r\n\r\n\r\n"
- println"结束"
接着就是如何使用这个脚本了,只要调用的时候加上你要查询的表明即可 如: groovy run.groovy [表名]
那么在当前目录下就会自动生成一个[表名].sql文件里面就有select \ insert语句
使用的时候如图:
最后在当前目录下就会生成一个[表名].sql文件
内容如下:
- --SELECT语句
- SELECT
- C_SERIAL,
- C_MONTH_ID,
- C_AREA_ID,
- WARNING_ID,
- WARNING_ID_DESC,
- WARNING_LEVEL,
- WARNING_LEVEL_DESC,
- WARNING_OBJECT,
- WARNING_OBJECT_DESC,
- REFERENCE_VALUE_TYP,
- REFERENCE_VALUE_TYP_DESC,
- WARNING_VALUE_TYP,
- WARNING_VALUE_TYP_DESC,
- ACTUAL_VALUE,
- REFERENCE_VALUE,
- WARNING_VALUE,
- VALUE_UP,
- VALUE_DOWN,
- IS_WARNING_DESC,
- IS_GIS_SHOW,
- CREATE_TIME,
- ID,
- VERSION
- FROM
- WI.WARNING_RESULT
- --SELECTjava语句
- StringBuilderstrBuilder_WI_WARNING_RESULT=newStringBuilder(1007);
- strBuilder_WI_WARNING_RESULT.append("SELECT");
- strBuilder_WI_WARNING_RESULT.append("C_SERIAL,");
- strBuilder_WI_WARNING_RESULT.append("C_MONTH_ID,");
- strBuilder_WI_WARNING_RESULT.append("C_AREA_ID,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_ID,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_ID_DESC,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_LEVEL,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_LEVEL_DESC,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_OBJECT,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_OBJECT_DESC,");
- strBuilder_WI_WARNING_RESULT.append("REFERENCE_VALUE_TYP,");
- strBuilder_WI_WARNING_RESULT.append("REFERENCE_VALUE_TYP_DESC,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_VALUE_TYP,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_VALUE_TYP_DESC,");
- strBuilder_WI_WARNING_RESULT.append("ACTUAL_VALUE,");
- strBuilder_WI_WARNING_RESULT.append("REFERENCE_VALUE,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_VALUE,");
- strBuilder_WI_WARNING_RESULT.append("VALUE_UP,");
- strBuilder_WI_WARNING_RESULT.append("VALUE_DOWN,");
- strBuilder_WI_WARNING_RESULT.append("IS_WARNING_DESC,");
- strBuilder_WI_WARNING_RESULT.append("IS_GIS_SHOW,");
- strBuilder_WI_WARNING_RESULT.append("CREATE_TIME,");
- strBuilder_WI_WARNING_RESULT.append("ID,");
- strBuilder_WI_WARNING_RESULT.append("VERSION");
- strBuilder_WI_WARNING_RESULT.append("FROM");
- strBuilder_WI_WARNING_RESULT.append("WI.WARNING_RESULT");
- --INSERT语句
- INSERTINTOWI.WARNING_RESULT
- (
- C_SERIAL,
- C_MONTH_ID,
- C_AREA_ID,
- WARNING_ID,
- WARNING_ID_DESC,
- WARNING_LEVEL,
- WARNING_LEVEL_DESC,
- WARNING_OBJECT,
- WARNING_OBJECT_DESC,
- REFERENCE_VALUE_TYP,
- REFERENCE_VALUE_TYP_DESC,
- WARNING_VALUE_TYP,
- WARNING_VALUE_TYP_DESC,
- ACTUAL_VALUE,
- REFERENCE_VALUE,
- WARNING_VALUE,
- VALUE_UP,
- VALUE_DOWN,
- IS_WARNING_DESC,
- IS_GIS_SHOW,
- CREATE_TIME,
- ID,
- VERSION
- VALUES(
- NUMBER,
- NUMBER,
- NUMBER,
- NUMBER,
- VARCHAR2,
- NUMBER,
- VARCHAR2,
- VARCHAR2,
- VARCHAR2,
- NUMBER,
- VARCHAR2,
- NUMBER,
- VARCHAR2,
- NUMBER,
- NUMBER,
- NUMBER,
- NUMBER,
- NUMBER,
- VARCHAR2,
- NUMBER,
- DATE,
- NUMBER,
- NUMBER
- )
- --INSERTjava语句
- StringBuilderstrBuilder_WI_WARNING_RESULT=newStringBuilder(1007);
- strBuilder_WI_WARNING_RESULT.append("INSERTINTOWI.WARNING_RESULT");
- strBuilder_WI_WARNING_RESULT.append("(");
- strBuilder_WI_WARNING_RESULT.append("C_SERIAL,");
- strBuilder_WI_WARNING_RESULT.append("C_MONTH_ID,");
- strBuilder_WI_WARNING_RESULT.append("C_AREA_ID,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_ID,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_ID_DESC,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_LEVEL,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_LEVEL_DESC,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_OBJECT,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_OBJECT_DESC,");
- strBuilder_WI_WARNING_RESULT.append("REFERENCE_VALUE_TYP,");
- strBuilder_WI_WARNING_RESULT.append("REFERENCE_VALUE_TYP_DESC,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_VALUE_TYP,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_VALUE_TYP_DESC,");
- strBuilder_WI_WARNING_RESULT.append("ACTUAL_VALUE,");
- strBuilder_WI_WARNING_RESULT.append("REFERENCE_VALUE,");
- strBuilder_WI_WARNING_RESULT.append("WARNING_VALUE,");
- strBuilder_WI_WARNING_RESULT.append("VALUE_UP,");
- strBuilder_WI_WARNING_RESULT.append("VALUE_DOWN,");
- strBuilder_WI_WARNING_RESULT.append("IS_WARNING_DESC,");
- strBuilder_WI_WARNING_RESULT.append("IS_GIS_SHOW,");
- strBuilder_WI_WARNING_RESULT.append("CREATE_TIME,");
- strBuilder_WI_WARNING_RESULT.append("ID,");
- strBuilder_WI_WARNING_RESULT.append("VERSION");
- strBuilder_WI_WARNING_RESULT.append("VALUES(");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("VARCHAR2,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("VARCHAR2,");
- strBuilder_WI_WARNING_RESULT.append("VARCHAR2,");
- strBuilder_WI_WARNING_RESULT.append("VARCHAR2,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("VARCHAR2,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("VARCHAR2,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("VARCHAR2,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("DATE,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER,");
- strBuilder_WI_WARNING_RESULT.append("NUMBER");
- strBuilder_WI_WARNING_RESULT.append(")");
这样基本上改改就可以拿来用了,当然关键是这一切都只是瞬间就自动完成的事情.
或许你还是会觉得这样的脚本不够灵活,另外我喜欢用JEDIT这个编辑器的缘故,所以我也为这个编辑器写过一个宏脚本.
也是完成同样的事情,不同的是我可以写编写好我要的SQL语句,然后同过这个宏自动生成java代码.
JEDIT 宏脚本
- voidsql2Java()
- {
- StringBuildersb=newStringBuilder(1007);
- Stringline=textArea.getSelectedText();
- String[]strArray=line.split("\n");
- sb.append("StringBuildersb=newStringBuilder(1007);\n");
- for(inti=0;i<strArray.length;i++)
- {
- sb.append("sb.append(\""+strArray[i]+"\");\n");
- }
- textArea.setSelectedText(sb.toString());
- }
- if(buffer.isReadOnly())
- Macros.error(view,"Bufferisread-only.");
- else
- sql2Java();
然后我调节了一下JEDIT,吧这个脚本放到了编辑区域的右击鼠标菜单上,只要我编辑好sql语句,在点右击鼠标菜单既可使用这个装换的功能
如:原始的sql如
当我使用宏脚本后
这样原本就不复杂的事情就变得更加简单和高效了.