写些小工具来帮助工作更有效率
Posted zhangjianying
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写些小工具来帮助工作更有效率相关的知识,希望对你有一定的参考价值。
看到同事痛苦的拼写SQL语句并转化成java String语句,自己不仅感到汗言,说实话过于复杂的SQL语句要换成我也未必会拼写的好.而且如果每个表都采用手工去拼写且转化的话,熟练的开发人员可能要5到10分钟,不熟悉的可能要更长的时间这样是很浪费时间的.很久以前我就写过类似的工具可以见我系列文章<自己动手编写DB2小工具>.
既然自己喜欢groovy,于是花了10来分钟写了个简单的根据表名自动生成 SQL语句的小脚本.这样只要输入表名就可以得到这个表的select/insert语句以及对应的java代码.
groovy脚本 run.groovy
既然自己喜欢groovy,于是花了10来分钟写了个简单的根据表名自动生成 SQL语句的小脚本.这样只要输入表名就可以得到这个表的select/insert语句以及对应的java代码.
groovy脚本 run.groovy
- import groovy.sql.Sql;
- /*生成java代码*/
- def getJavaStringBuilder={ tableName,strSQL ->
- tableName=tableName.replaceAll("//.","_");
- tableName=tableName.replaceAll(" ","");
- strRetVal="StringBuilder strBuilder_${tableName} = new StringBuilder(1007); /r/n"
- strSQL.eachLine{
- strRetVal+="strBuilder_${tableName}.append(/" "+it+" /"); /r/n";
- }
- return strRetVal;
- }
- /*
- 连接对象
- 可以根据你自己的需要调整
- */
- sql = Sql.newInstance("jdbc:oracle:thin:@10.220.51.40:1521:ORA10G","用户名","密码","oracle.jdbc.driver.OracleDriver");
- /* SQL语句 可以根据自己需要调整*/
- //strSqlTableName=this.args[0];
- strSqlTableName=this.args[0];
- //方法_获取表名
- strSQL="select * from ${strSqlTableName} where 1<>1"
- def file_tableName=strSqlTableName;//表名
- def result_columnCount=0;//列数
- def result_columnName=[];//列名
- def result_columnType=[];//列类型
- def result_rowsData=[];//行数据
- def rows_count=0;
- //在该脚本的同级目录下生成已表名命名的csv文件
- file_csv= new File("${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 (i in 1..result_columnCount){ //不同与java,groovy下标从1开始
- println it.getColumnName(i) +" | "+ it.getColumnTypeName(i) ;
- result_columnName<<it.getColumnName(i);
- result_columnType<<it.getColumnTypeName(i);
- }
- println "*********${file_tableName}表结构**********"
- // file_csv<<result_columnName.join(",")+System.getProperty("line.separator");
- println "开始生成数据,请耐心等待......"
- },
- {
- }
- );
- /*生成 select 语句*/
- 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 << " -- SELECT java 语句 /r/n"
- file_csv << getJavaStringBuilder(file_tableName,strTemp);
- file_csv << "/r/n/r/n/r/n"
- /*生成insert 语句*/
- file_csv << " -- INSERT 语句 /r/n"
- strTemp="INSERT INTO ${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 << " -- INSERT java 语句 /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
- -- SELECT java 语句
- StringBuilder strBuilder_WI_WARNING_RESULT = new StringBuilder(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 语句
- INSERT INTO WI.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
- )
- -- INSERT java 语句
- StringBuilder strBuilder_WI_WARNING_RESULT = new StringBuilder(1007);
- strBuilder_WI_WARNING_RESULT.append(" INSERT INTO WI.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 宏脚本
- void sql2Java()
- {
- StringBuilder sb=new StringBuilder(1007);
- String line=textArea.getSelectedText();
- String[] strArray= line.split("/n");
- sb.append(" StringBuilder sb=new StringBuilder(1007); /n");
- for (int i=0;i<strArray.length;i++)
- {
- sb.append(" sb.append(/" "+strArray[i]+" /"); /n");
- }
- textArea.setSelectedText(sb.toString());
- }
- if(buffer.isReadOnly())
- Macros.error(view, "Buffer is read-only.");
- else
- sql2Java();
然后我调节了一下JEDIT,吧这个脚本放到了编辑区域的右击鼠标菜单上,只要我编辑好sql语句,在点右击鼠标菜单既可使用这个装换的功能
如:原始的 sql如
当我使用宏脚本后
这样原本就不复杂的事情就变得更加简单和高效了.
以上是关于写些小工具来帮助工作更有效率的主要内容,如果未能解决你的问题,请参考以下文章