插入ts以及判断列是否存在(支持多数据库)
1:增加ts.dr字段,先判断ts.dr字段是否存在,其中ts字段插入的是日期,默认值为当前插入的时间,dr字段是数值型,默认值为0 * 增加ts/dr字段
*
* @param tableList
*/
private void addTSAndDRColumn(List<String> tableList) {
// 先判断是否有ts,dr列 String dsname = getDsName();
boolean addTS = false;
boolean addDR = false;
List<String> sqlList = new ArrayList<String>();
for (int i = ; i < tableList.size(); i++) {
String addSql = null;
String tableName = tableList.get(i);
addTS = !isColumnExist(dsname, tableName, "ts");
addDR = !isColumnExist(dsname, tableName, "dr");
if (addTS) {
if (addDR) {
addSql = addTSAndDR(tableName);
} else {
addSql = addTS(tableName);
}
} else {
if (addDR) {
addSql = addDR(tableName);
}
}
if (!StringUtils.isEmpty(addSql)) {
sqlList.add(addSql);
} }
String[] sqls = sqlList.toArray(new String[]);
if (!ArrayUtils.isEmpty(sqls)) {
SqlExecAction_Client.execBatch(dsname, sqls);// 批量处理
} } /**
* 判断列是否存在。思路:针对sqlserver/oracle/postgrel做了特殊处理,其他类型的数据库,则按照字段执行查询,如果出错,则说明字段不存在
*
* @param dsname
* 数据源
* @param tableName
* 表名
* @param column
* 列名
* @return
*/
private boolean isColumnExist(String dsname, String tableName, String column) {
boolean isExist = true;
DbType dbType = SmartUtilities.getDbType(dsname);
if (dbType == DbType.SQLSERVER || dbType == DbType.ORACLE
|| dbType == DbType.POSTGRESQL) {
return isColumnExist(dsname, tableName, column, dbType);
} else {
try {
String sql = "select count(" + column + ") from " + tableName;
SqlExecAction_Client.execQuery(dsname, sql,
new ResultSetProcessor() {
private static final long serialVersionUID = -3528176693847689283L; @Override
public Object handleResultSet(ResultSet rs)
throws SQLException {
while (rs.next()) {
return rs.getInt();
}
return ;
} });
} catch (Exception e) {// 如果有异常,则说明字段不存在
AppDebug.error(e);
isExist = false;
}
} return isExist;
} private boolean isColumnExist(String dsname, String tableName,
String column, DbType dbType) {
String sql = null;
if (dbType == DbType.SQLSERVER) {
sql = "select count(*) from syscolumns where id=object_id('"
+ tableName.toLowerCase() + "') and name='" + column + "'";
} else if (dbType == DbType.ORACLE) {
sql = "select count(*) from cols where table_name=upper('" + tableName
+ "') and column_name=upper('" + column + "')";
} else if (dbType == DbType.POSTGRESQL) {
sql = "select count(*) from pg_attribute where attname='"
+ column
+ "' and attrelid=(select oid from pg_class where relname='"
+ tableName.toLowerCase() + "')";
}
Integer count = (Integer) SqlExecAction_Client.execQuery(dsname, sql,
new ResultSetProcessor() {
private static final long serialVersionUID = -3528176693847689283L; @Override
public Object handleResultSet(ResultSet rs)
throws SQLException {
while (rs.next()) {
return rs.getInt();
}
return ;
} });
int icount = count.intValue();
return (icount != ); } /**
* @param tableName
* @return
*/
private String addTSAndDR(String tableName) {
String sql = "alter table " + tableName
+ " add (ts CHAR(19),dr numeric(10,0) DEFAULT 0)";
String dsName = FileStorageConfigFactory.getBaseConfig().getDsName();
DbType dbType = SmartUtilities.getDbType(dsName);
if (dbType == DbType.SQLSERVER) {
sql = "alter table "
+ tableName
+ " add ts NCHAR(19) COLLATE Chinese_PRC_CI_AS DEFAULT CONVERT(nchar(19),GETDATE(),20),dr SMALLINT DEFAULT 0";
} else if (dbType == DbType.ORACLE) {
sql = "alter table "
+ tableName
+ " add (ts CHAR(19) DEFAULT to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),dr numeric(10,0) DEFAULT 0)";
} else if (dbType == DbType.DB2) {
sql = "alter table "
+ tableName
+ " add COLUMN ts CHAR(19) DEFAULT char(current timestamp,'yyyy-mm-dd hh24:mi:ss')"
+ " add COLUMN dr SMALLINT DEFAULT 0)";
} else if (dbType == DbType.POSTGRESQL) {
sql = "alter table "
+ tableName
+ " add ts CHAR(19) DEFAULT to_char(current_timestamp,'yyyy-mm-dd hh24:mi:ss'),add dr numeric(10,0) DEFAULT 0"; }
return sql;
} private String addTS(String tableName) {
String sql = "alter table " + tableName + " add ts CHAR(19)";
String dsName = FileStorageConfigFactory.getBaseConfig().getDsName();
DbType dbType = SmartUtilities.getDbType(dsName);
if (dbType == DbType.SQLSERVER) {
sql = "alter table "
+ tableName
+ " add ts NCHAR(19) COLLATE Chinese_PRC_CI_AS DEFAULT CONVERT(nchar(19),GETDATE(),20)";
} else if (dbType == DbType.ORACLE) {
sql = "alter table "
+ tableName
+ " add ts CHAR(19) DEFAULT to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')";
} else if (dbType == DbType.DB2) {
sql = "alter table "
+ tableName
+ " add COLUMN ts CHAR(19) DEFAULT char(current timestamp,'yyyy-mm-dd hh24:mi:ss')";
} else if (dbType == DbType.POSTGRESQL) {
sql = "alter table "
+ tableName
+ " add ts CHAR(19) DEFAULT to_char(current_timestamp,'yyyy-mm-dd hh24:mi:ss')"; }
return sql;
} private String addDR(String tableName) {
String sql = "alter table " + tableName
+ " add dr numeric(10,0) DEFAULT 0";
String dsName = FileStorageConfigFactory.getBaseConfig().getDsName();
DbType dbType = SmartUtilities.getDbType(dsName);
if (dbType == DbType.SQLSERVER) {
sql = "alter table " + tableName + " add dr SMALLINT DEFAULT 0";
} else if (dbType == DbType.DB2) {
sql = "alter table " + tableName
+ " add COLUMN dr SMALLINT DEFAULT 0)";
}
return sql;
}
插入ts以及判断列是否存在(支持多数据库)的更多相关文章
- .NET 百万级 大数据插入、更新 ,支持多种数据库
功能介绍 (需要版本5.0.44) 大数据操作ORM性能瓶颈在实体转换上面,并且不能使用常规的Sql去实现 当列越多转换越慢,SqlSugar将转换性能做到极致,并且采用数据库最佳API 操作数据库 ...
- ASP.NET 为GridView添加序号列,且支持分页连续累计显示
为GridView添加序号列,且支持分页连续累计显示,废话不多说,直接上代码: <%@ Page Language="C#" AutoEventWireup="tr ...
- DataGridView绑定数据库,取得的数据插入到DataGridView指定列(一)
实现: 点击button1,从数据库中获得数据,指定数据库的某列数据插入到DataGridView指定列 一.双击button1进入事件代码 private void button1_Click(ob ...
- sql 判断表是否存在,判断列是否存在
判断表是否存在: 语法: SELECT * FROM dbo.SysObjects where id = object_id(N'表名') 例子: SELECT * FROM dbo.SysObjec ...
- VBA小功能集合-判断列内是否有重复值
1.判断列内是否有重复值: Dim arrT As Range Dim rng As Range Set arrT = Range("A:A")'判读A列单元格 For Each ...
- FreeSql (七)插入数据时忽略列
var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initia ...
- FreeSql (八)插入数据时指定列
插入数据时指定列,和忽略列对应,未被指定的列将被忽略. var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Passwor ...
- python numpy.array插入一行或一列
numpy.array插入一行或一列 import numpy as np a = np.array([[1,2,3],[4,5,6],[7,8,9]]) b = np.array([[0,0,0]] ...
- CYQ.Data 支持 PostgreSQL 数据库
前言: 很久之前,就有同学问我CYQ.Data能不能支持下PostgreSQL,之后小做了下调查,发现这个数据库用的人少,加上各种因素,就一直没动手. 前两天,不小心看了一下Github上的消息: 看 ...
随机推荐
- Hibernate 注解时 hibernate.hbm.xml的配置方法 以及与SSH整合里的配置方式
①纯Hibernate开发: 当你在Bean中写入注解后,需要告诉hibernate哪些类使用了注解. 方法是在hibernate.hbm.xml文件中配置 <!DOCTYPE hibernat ...
- Delphi xe10下载(包含破解补丁和破解视频)
软件名称:RAD Studio 10 Seattle软件大小:7.18 GB RAD Studio 10 Seattle官方下载地址:http://altd.embarcadero.com/downl ...
- Win7资源管理器已停止工作——StackHash_6c37,R6205错误
2013-9-20 此问题由来已久,截图及"问题签名"如下: 问题签名: 问题事件名称: BEX64 应用程序名: Explorer.EXE 应用程序版本: 6.1.7601. ...
- 3D旋转特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- android webview js交互 第一节 (java和js交互)
转载请注明出处 挺帅的移动开发专栏 http://blog.csdn.net/wangtingshuai/article/details/8631835 在androi ...
- jQuery开始之旅
(1)首先到http://jquery.com/download/网站进行现在jQuery库. (2)这里演示一下,使用CDN进行jQuery编程的例子: 选择的是jQuery的CDN,<scr ...
- 动态规划(DP计数):HDU 5117 Fluorescent
Matt, a famous adventurer who once defeated a pack of dire wolves alone, found a lost court. Matt fi ...
- poj 2586 Y2K Accounting Bug
http://poj.org/problem?id=2586 大意是一个公司在12个月中,或固定盈余s,或固定亏损d. 但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(<0为 ...
- selenium-webdriver的等待方法
Wait commands in WebDriver Listing out the different WebDriver Wait statements that can be useful fo ...
- ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA
Centos5.5 安装Oracle11g客户端,配置了本地的net服务后,用sqlplus连接报错: tnsnames.ora配置如下 orcl = (DESCRIPTION = (ADDRESS ...