ap快看1111111
Posted 123早点睡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ap快看1111111相关的知识,希望对你有一定的参考价值。
package com.aia.datasyc.perftest.service;
import com.aia.datasyc.perftest.util.DBUtils;
import com.aia.datasyc.perftest.util.FileUtils;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
@Slf4j
public class GenSqlStatement
//private static org.apache.logging.log4j.Logger log;
//private Logger log = LoggerFactory.getLogger(this.getClass());
static final String updateSrcFileSybase = "Sybase_UpdateSQL.sql";
static final String sybaseImportantTables = "Sybase_Important_Tables.sql";
static final String updateSrcFileSql = "SQLServer_UpdateSQL_DB__TYPE.sql";
static final String fileName = "data" + File.separator + LocalDate.now() + File.separator
+ "SQLServer_UpdateSQL_DB__TYPE.sql";
static final String getTablesSyb = "select distinct rr_source_table table_name from db_work.dbo.trr_provider_table_conf \\n" +
"where rr_status ='Active' and rr_update_time > dateadd(day, -10, getdate())";
static final String getTablesSql = "select distinct rr_table_name table_name from TABLE__NAME " +
"where date_time > dateadd(day, -10, getdate())";
static final String getColNameStr = "select 'TABLE__NAME' as table_name, name as col_name from DB__NAME.dbo.syscolumns where colid=2 and id = OBJECT_ID('TABLE__NAME')";
static final String getUpdateStatementSyb = "update top 8 TABLE__NAME set COL__NAME = COL__NAME\\n";
static final String getUpdateStatementSql = "update top (50) TABLE__NAME set COL__NAME = COL__NAME\\n";
static final String existTableSql = "select name from DB_NAME.dbo.sysobjects where name ='TABLE_NAME'";
public static void main(String[] args) throws Exception
genUpdateStatement();
public static void genUpdateStatement() throws SQLException
System.out.println("********* Start to generate update sql statement ...");
DBUtils dbUtils = new DBUtils();
genUpdateStatementSybase(dbUtils);
genUpdateStatementSqlServer(dbUtils, "Internal");
genUpdateStatementSqlServer(dbUtils, "Batch");
genUpdateStatementSqlServer(dbUtils, "External");
processSybaseImportantTables(dbUtils);
JudgeTableExistSybase(dbUtils, "Sybase");
JudgeTableExistSqlServer(dbUtils, "Internal");
JudgeTableExistSqlServer(dbUtils, "Batch");
JudgeTableExistSqlServer(dbUtils, "External");
public static void genUpdateStatementSybase(DBUtils dbUtils)
try (FileWriter fw = new FileWriter(fileName.replaceFirst("SQLServer", "Sybase").replaceFirst("_DB__TYPE", ""));)
//Get existing tables
List<String> sqlList = FileUtils.getFile(updateSrcFileSybase);
List<String> tableList = new ArrayList<>();
StringBuilder updateList = new StringBuilder();
String tableName = null;
//filter duplication
for (String str : sqlList)
int inx = str.indexOf(" set ");
if ( inx > -1 )
tableName = str.substring(0, inx).replaceFirst("update top ", "").split(" ")[1];
if ( !tableList.contains(tableName) )
tableList.add(tableName);
// updateList.append("--" + str).append(System.lineSeparator());
updateList.append(str).append(System.lineSeparator());
else
updateList.append(str).append(System.lineSeparator());
// for(String str: tableList)
// System.out.println(str);
//
//Get tables from DB
JSONArray tableListJson = dbUtils.getResultSetFromSybase(getTablesSyb);
List<String> newTableList = new ArrayList<>();
if ( tableListJson != null && tableListJson.size() > 0 )
for (int i = 0; i < tableListJson.size(); i++)
String tableNameDB = tableListJson.getJSONObject(i).getString("table_name");
boolean isExisting = false;
for (String str : tableList)
if ( str.equals(tableNameDB) )
isExisting = true;
break;
if ( !isExisting )
newTableList.add(tableNameDB);
if ( newTableList != null && newTableList.size() > 0 )
for (int j = 0; j < newTableList.size(); j++)
System.out.println(newTableList.get(j));
System.out.println("********* Sybase newTableList.size::" + newTableList.size());
else
System.out.println("********* Sybase newTableList.size:: 0");
//get column name of the new tables
StringBuilder sbExecStatement = new StringBuilder();
if ( newTableList != null && newTableList.size() > 0 )
String tableNameNew = null;
for (int i = 0; i < newTableList.size(); i++)
tableNameNew = newTableList.get(i);
sbExecStatement.append("union ")
.append(getColNameStr.replaceAll("DB__NAME", tableNameNew.split("\\\\.")[0]).replaceAll("TABLE__NAME", tableNameNew))
.append(System.lineSeparator());
// System.out.println("tableSB:::" + sbExecStatement.toString().replaceFirst("union ",""));
JSONArray jsonArray = dbUtils.getResultSetFromSybase(sbExecStatement.toString().replaceFirst("union ", ""));
//clean the content of sbExecStatement
sbExecStatement.delete(0, sbExecStatement.length());
//get the tables which has only one column --- begin
if ( jsonArray != null && jsonArray.size() > 0 )
for (int i = 0; i < newTableList.size(); i++)
boolean existing = false;
tableNameNew = newTableList.get(i);
for (int j = 0; j < jsonArray.size(); j++)
if ( tableNameNew.equals(jsonArray.getJSONObject(j).getString("table_name")) )
existing = true;
break;
if ( !existing )
sbExecStatement.append("union ")
.append(getColNameStr.replaceAll("DB__NAME", tableNameNew.split("\\\\.")[0])
.replaceAll("TABLE__NAME", tableNameNew).replaceAll("colid=2", "colid=1"))
.append(System.lineSeparator());
//System.out.println(sbExecStatement.toString().replaceFirst("union ",""));
//clean the content of sbExecStatement
sbExecStatement.delete(0, sbExecStatement.length());
if ( sbExecStatement != null && sbExecStatement.length() > 0 )
jsonArray.addAll(dbUtils.getResultSetFromSybase(sbExecStatement.toString().replaceFirst("union ", "")));
//get the tables which has only one column --- end
//update sql statements file
if ( jsonArray != null && jsonArray.size() > 0 )
for (int i = 0; i < jsonArray.size(); i++)
tableName = jsonArray.getJSONObject(i).getString("table_name");
String colName = jsonArray.getJSONObject(i).getString("col_name");
sbExecStatement.append(getUpdateStatementSyb.replaceAll("TABLE__NAME", tableName).replaceAll("COL__NAME", colName));
if ( sbExecStatement != null && sbExecStatement.length() > 0 )
//appending new tables
updateList.append(("--" + LocalDate.now() + System.lineSeparator())).append(sbExecStatement.toString());
//System.out.println(updateStatement);
fw.write(updateList.toString());
fw.flush();
fw.close();
catch (Exception e)
e.printStackTrace();
private static void genUpdateStatementSqlServer(DBUtils dbUtils, String dbType)
try (FileWriter fw = new FileWriter(fileName.replaceAll("DB__TYPE", dbType));)
//Get existing tables
List<String> sqlList = FileUtils.getFile(updateSrcFileSql.replaceFirst("DB__TYPE", dbType));
List<String> tableList = new ArrayList<>();
StringBuilder updateList = new StringBuilder();
String tableName = null;
//filter duplication
for (String str : sqlList)
int inx = str.indexOf(" set ");
if ( inx > -1 )
tableName = str.substring(0, inx).replaceFirst("update top ", "").split(" ")[1];
String tableName1 = tableName;
if ( !tableList.contains(tableName) )
tableList.add(tableName);
updateList.append(str).append(System.lineSeparator());
else
updateList.append(str).append(System.lineSeparator());
//Get tables from DB
JSONArray tableListJson = dbUtils.getResultSetFromSQLServer(getTablesSql
.replaceFirst("TABLE__NAME", "db_work_" + dbType.substring(0, 1).toLowerCase() + ".dbo.trr_provider_"
+ dbType.toLowerCase() + "_conf_sr"), dbType);
List<String> newTableList = new ArrayList<>();
if ( tableListJson != null && tableListJson.size() > 0 )
for (int i = 0; i < tableListJson.size(); i++)
String tableNameDB = tableListJson.getJSONObject(i).getString("table_name");
boolean isExisting = false;
for (String str : tableList)
if ( str.equals(tableNameDB) )
isExisting = true;
break;
if ( !isExisting )
newTableList.add(tableNameDB);
if ( newTableList != null && newTableList.size() > 0 )
for (int j = 0; j < newTableList.size(); j++)
System.out.println(newTableList.get(j));
System.out.println("********* SQLServer(" + dbType + ") newTableList.size::" + newTableList.size());
else
System.out.println("********* SQLServer(" + dbType + ") newTableList.size:: 0");
//get column name of the new tables
StringBuilder sbExecStatement = new StringBuilder();
if ( newTableList != null && newTableList.size() > 0 )
String tableNameNew = null;
for (int i = 0; i < newTableList.size(); i++)
tableNameNew = newTableList.get(i);
sbExecStatement.append("union ")
.append(getColNameStr.replaceAll("DB__NAME", tableNameNew.split("\\\\.")[0]).replaceAll("TABLE__NAME", tableNameNew))
.append(System.lineSeparator());
// System.out.println("sbExecStatement:::" + sbExecStatement.toString().replaceFirst("union ",""));
JSONArray jsonArray = dbUtils.getResultSetFromSQLServer(sbExecStatement.toString().replaceFirst("union ", ""), dbType);
//clean the content of sbExecStatement
sbExecStatement.delete(0, sbExecStatement.length());
//get the tables which has only one column --- begin
if ( jsonArray != null && jsonArray.size() > 0 )
for (int i = 0; i < newTableList.size(); i++)
boolean existing = false;
tableNameNew = newTableList.get(i);
for (int j = 0; j < jsonArray.size(); j++)
if ( tableNameNew.equals(jsonArray.getJSONObject(j).getString("table_name")) )
existing = true;
break;
if ( !existing )
sbExecStatement.append("union ")
.append(getColNameStr.replaceAll("DB__NAME", tableNameNew.split("\\\\.")[0])
.replaceAll("TABLE__NAME", tableNameNew).replaceAll("colid=2", "colid=1"))
.append(System.lineSeparator());
//System.out.println(sbExecStatement.toString().replaceFirst("union ",""));
//clean the content of sbExecStatement
sbExecStatement.delete(0, sbExecStatement.length());
if ( sbExecStatement != null && sbExecStatement.length() > 0 )
jsonArray.addAll(dbUtils.getResultSetFromSybase(sbExecStatement.toString().replaceFirst("union ", "")));
//get the tables which has only one column --- end
//update sql statements file
if ( jsonArray != null && jsonArray.size() > 0 )
for (int i = 0; i < jsonArray.size(); i++)
tableName = jsonArray.getJSONObject(i).getString("table_name");
String colName = jsonArray.getJSONObject(i).getString("col_name");
sbExecStatement.append(getUpdateStatementSql.replaceAll("TABLE__NAME", tableName).replaceAll("COL__NAME", colName));
if ( sbExecStatement != null && sbExecStatement.length() > 0 )
//appending new tables
updateList.append(("--" + LocalDate.now() + System.lineSeparator())).append(sbExecStatement.toString());
//System.out.println(updateStatement);
fw.write(updateList.toString());
fw.flush();
fw.close();
catch (Exception e)
e.printStackTrace();
public static void processSybaseImportantTables(DBUtils dbUtils)
String newFileName = fileName.replaceFirst("SQLServer", "Sybase").replaceFirst("_DB__TYPE", "");
//System.out.println("newFileName:: " + newFileName);
//Get tables update statements
List<String> sqlList = FileUtils.getFile(newFileName);
//get sql
List<String> importantTableList = FileUtils.getFile(sybaseImportantTables);
try (FileWriter fw = new FileWriter(newFileName);)
StringBuilder updateList = new StringBuilder();
String tableName = null;
//process important tables
//for (String table : importantTableList)
for (String str : sqlList)
int inx = str.indexOf(" set ");
if ( inx > -1 )
tableName = str.substring(0, inx).replaceFirst("update top ", "").split(" ")[1];
if ( importantTableList.contains(tableName) )
updateList.append("update top 20 " + tableName + str.substring(inx)).append(System.lineSeparator());
//System.out.println("update top 40 " + tableName + str.substring(inx));
else
updateList.append(str).append(System.lineSeparator());
else
updateList.append(str).append(System.lineSeparator());
//System.out.println(updateStatement);
fw.write(updateList.toString());
fw.flush();
fw.close();
catch (Exception e)
e.printStackTrace();
public static void JudgeTableExistSybase(DBUtils dbUtils, String dbType)
String newFileName = fileName.replaceFirst("SQLServer", "Sybase").replaceFirst("_DB__TYPE", "");
JudgeTableExist(dbUtils, dbType, newFileName);
public static void JudgeTableExistSqlServer(DBUtils dbUtils, String dbType)
String newFileName = fileName.replaceAll("DB__TYPE", dbType);
JudgeTableExist(dbUtils, dbType, newFileName);
// //Judge table exists
public static List<JSONArray> checkTableExist(DBUtils dbUtils, String dbType, List<String> dbNameList, List<String> tableNameList) throws SQLException
StringBuilder sbSql = new StringBuilder();
List<Object> list = new ArrayList<>();
int i = 0;
//每个db检查一遍
for (String db : dbNameList)
for (String tb : tableNameList)
sbSql.append("select name from " + db + ".dbo.sysobjects where name =" + "'" + tb + "' ")
.append(" UNION ");
i++;
if ( i%250 == 0 )
list.add(new String(sbSql.substring(0,sbSql.length()-8)));
sbSql = null;
list.add(new String(sbSql.substring(0,sbSql.length()-8)));//去掉最后的"UNION"
List<JSONArray> oneDB = new ArrayList<>();
for (Object sql:list)
JSONArray rs = dbUtils.getResultSetFromSQLServer((String) sql, dbType);
oneDB.add(rs);
// JSONArray rs = dbUtils.getResultSetFromSQLServer(sbSql, dbType);
// //保存每一个db中存在的table
for (Object s : )
oneDB.add((String) s);
List<JSONArray> allTable = new ArrayList<>();//保存所有db中存在的table
for (JSONArray at: oneDB )
allTable.add(at);
return allTable;
return null ;
public static void JudgeTableExist(DBUtils dbUtils, String dbType, String newFileName)
//System.out.println("newFileName:: " + newFileName);
//Get tables update statements
List<String> sqlList = FileUtils.getFile(newFileName);
try (FileWriter fw = new FileWriter(newFileName);)
StringBuilder updateList = new StringBuilder();
//新建集合保存获取到的元素
List<String> tableNameList = new ArrayList<>();
List<String> dbNameList = new ArrayList<>();
List<String> notExistTable = new ArrayList<>();
for (String str : sqlList)
int inx = str.indexOf(" set ");
if ( inx > -1 )
String tableFullName = str.substring(0, inx).replaceFirst("update top ", "").split(" ")[1];
String tableName = tableFullName.substring(tableFullName.lastIndexOf(".") + 1);
String dbName = tableFullName.substring(0, tableFullName.indexOf("."));
dbNameList.add(dbName);
tableNameList.add(tableName);
System.out.println("startTime:"+LocalDateTime.now());
List<JSONArray> existTableList = checkTableExist(dbUtils, dbType, dbNameList, tableNameList);
System.out.println("endTime:"+LocalDateTime.now());
for (String str : sqlList)
int inx = str.indexOf(" set ");
if ( inx > -1 )
for (int i = 0; i < tableNameList.size(); i++)
if ( !tableNameList.contains(existTableList) )
updateList.append("--table not exist ").append(str).append(System.lineSeparator());
else
updateList.append(str).append(System.lineSeparator());
// for (int i = 0; i < arrayList.size(); i += 500)
// if ( i + 500 > arrayList.size() ) //作用为toIndex最后没有500条数据则剩余为一组
// toIndex = arrayList.size() - i;
//
// List newList = arrayList.subList(i, i + toIndex);
// map.put("keyName" + keyToken, newList);//newList.toString().substring(1, newList.size() )
// keyToken++;
System.out.println(map);
System.out.println(map);
//
// String handleMap = map.values().toString().replaceAll("\\\\[|\\\\]", "");
// for (String str : sqlList)
// int inx = str.indexOf(" set ");
// if ( inx > -1 )
// String tableFullName = str.substring(0, inx).replaceFirst("update top ", "").split(" ")[1];
// String tableName = tableFullName.substring(tableFullName.lastIndexOf(".") + 1);
// String dbName = tableFullName.substring(0, tableFullName.indexOf("."));
//
//
//
// LocalDateTime startTime = LocalDateTime.now();
// System.out.println(" start time: " + startTime);
// //有问题
// for (int i = 0;i<notExistTable.size();i++)
//
//
// if ( notExistTable.contains(tableName) )
updateList.append(" update top 20 " + tableName + str.substring(inx)).append(System.lineSeparator());
// updateList.append("--table not exist ").append(str).append(System.lineSeparator());
// //System.out.println("update top 40 " + tableName + str.substring(inx));
// else
// updateList.append(str).append(System.lineSeparator());
//
//
// else
// updateList.append(str).append(System.lineSeparator());
//
//
// LocalDateTime endTime = LocalDateTime.now();
// System.out.println(" End time: " + endTime);
//
//System.out.println(updateStatement);
fw.write(updateList.toString());
fw.flush();
fw.close();
catch (Exception e)
e.printStackTrace();
// Judge table exists
// public static HashMap<String, Integer> getTableList(DBUtils dbUtils, String dbType, String dbName) throws SQLException
// //DBUtils dbUtils = new DBUtils();
// String dbStatement = "select name from " + dbName + "..sysobjects where type='U' order by name";
// JSONArray rs = dbUtils.getResultSetFromSQLServer(dbStatement, dbType);
ArrayList<String> arrayList = new ArrayList<>();
// HashMap<String, Integer> addTable = new HashMap<>();
// for (int i = 0;i<rs.size();i++)
// addTable.put(rs.getString(i),i);
//
// return addTable;
//
// public boolean HasTable(String dbType,String tableName)
// //判断某一个表是否存在
// boolean result = false;
// try
// DBUtils dbUtils = new DBUtils();
// dbUtils.openConn(dbType);
//
// DatabaseMetaData meta = sqlConn.getMetaData();
// ResultSet set = meta.getTables (null, null, tableName, null);
// if (set.next())
// result = true;
// else
// result =false;
//
// catch (Exception eHasTable)
// System.err.println(eHasTable);
// eHasTable.printStackTrace ();
//
// return result;
//
// public static boolean checkTableNull(DBUtils dbUtils, String dbType, String tableName) throws SQLException
// //DBUtils dbUtils = new DBUtils();
// boolean result = false;
// String dbStatement = "select * from " + tableName;
// ResultSet rs = dbUtils.useConnection(dbType, dbStatement);
// try
//
// if (rs.next())
// result = true;
// else
// result = false;
log.info("false, cause: table is empty");
//
// return result;
//
// catch (Exception e)
// e.printStackTrace();
// finally
// dbUtils.closeConn();
//
// return Boolean.parseBoolean(null);
//
//
// //check table trigger
// public static boolean checkTrigger(DBUtils dbUtils, String dbType, String tableName) throws SQLException
// //DBUtils dbUtils = new DBUtils();
// boolean result = false;
// String dbStatement = "select so2.name from sysobjects so1, sysobjects so2 where " +
// "(so2.id = so1.deltrig or so2.id = so1.instrig or so2.id=so1.updtrig or so2.id=so1.seltrig) and so1.name in('" + tableName + "')";
//
// ResultSet rs = dbUtils.useConnection(dbType,dbStatement);
// try
//
//
// if ( rs.next())
// result = true;
log.info("trigger:" + sql);
// else
// result = false;
log.info("false, cause: no trigger");
//
// return result;
//
// catch (Exception e)
// e.printStackTrace();
// finally
// dbUtils.closeConn();
//
// return Boolean.parseBoolean(null);
//
网上有个参考代码,但是我有点看不懂,你看看
StringBuffer addSql = new StringBuffer(10000);
int batchSize = 300;
int executeTime = 0;
for (int i=0; i<trackIds.size(); i++)
if(i/batchSize > executeTime) //300轮执行一次SQL)
String sql = addSql.substring(0, addSql.length() - 1);
baseJdbcDao.exceute(sql, null);
executeTime++;//执行次数++
if(i%batchSize == 0) //50轮重新拼接SQL(防止SQL过长)
addSql = addSql.delete(0, addSql.length());
addSql.append("insert into " + DatabaseConstants.BASE + ".yk_behavior_monitor (TRACK_ID, PROGRAM_ID, " +
"BEHAVIOR_ID, ACTION_TOTAL, ACTION_COMPLETED, CREATED_BY, CREATED_DATE, LAST_UPDATED_BY, LAST_UPDATED_DATE, REMOVE_FLAG) values");
for (Integer behaviorId : behaviorIds)
addSql.append("(");
addSql.append(trackIds.get(i) + ",");
addSql.append(programId + ",");
addSql.append(behaviorId + ",");
addSql.append("0,0,");
addSql.append(user.getUserId() + ",");
addSql.append("'" + sdf.format(new Date()) + "',");
addSql.append(user.getUserId() + ",");
addSql.append("'" + sdf.format(new Date()) + "',");
addSql.append("0),");
以上是关于ap快看1111111的主要内容,如果未能解决你的问题,请参考以下文章