List of usage examples for com.mongodb BasicDBObject toBsonDocument
@Override public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry)
From source file:cn.com.infcn.superspider.service.impl.DbServiceImpl.java
@SuppressWarnings("static-access") @Override//from www . j a va 2 s . c om public List<FieldMapping> getAllColumns(DbSource dbSource) { List<FieldMapping> columns = new ArrayList<FieldMapping>(); if (Constant.MYSQL.equals(dbSource.getDsType()) || Constant.SQLSERVER.equals(dbSource.getDsType()) || Constant.ORACLE.equals(dbSource.getDsType()) || Constant.DB2.equals(dbSource.getDsType())) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = ConnectionUtil.getConnection(dbSource.getDsType(), dbSource.getDsHost(), dbSource.getDsPort(), dbSource.getDsDbName(), dbSource.getDsUserName(), dbSource.getDsPassWord()); if (conn != null) { DatabaseMetaData m_DBMetaData = conn.getMetaData(); String schema = "%"; if (dbSource.getDsSchema() != null && !"".equals(dbSource.getDsSchema())) { schema = dbSource.getDsSchema(); } ResultSet colRet = m_DBMetaData.getColumns(null, schema, dbSource.getDsTableName(), "%"); while (colRet.next()) { FieldMapping row = new FieldMapping(); row.setFieldName(colRet.getString("COLUMN_NAME")); row.setFiledSource("?"); if (colRet.getString("TYPE_NAME").toLowerCase().contains("blob") || colRet.getString("TYPE_NAME").toLowerCase().contains("clob") || colRet.getString("TYPE_NAME").toLowerCase().contains("text") || colRet.getString("TYPE_NAME").toLowerCase().contains("image")) { row.setFieldType(colRet.getString("TYPE_NAME"));// ??; } else { String typeName = ""; if (colRet.getString("TYPE_NAME").contains("()")) { typeName = colRet.getString("TYPE_NAME").substring(0, colRet.getString("TYPE_NAME").indexOf("(") + 1) + colRet.getInt("COLUMN_SIZE") + colRet.getString("TYPE_NAME") .substring(colRet.getString("TYPE_NAME").indexOf("(") + 1); } else { typeName = colRet.getString("TYPE_NAME") + "(" + colRet.getInt("COLUMN_SIZE") + ")"; } row.setFieldType(typeName); } row.setFieldOrdinalPosition(colRet.getInt("ORDINAL_POSITION")); columns.add(row); } } } catch (SQLException e) { e.printStackTrace(); return columns; } finally { if (conn != null) { ConnectionUtil.closeConnection(conn, pstmt, rs); } } } else if (Constant.MONGO.equals(dbSource.getDsType())) { MongoDao mongoDao = null; try { mongoDao = new MongoDao(dbSource.getDsHost(), dbSource.getDsPort(), dbSource.getDsDbName()); BasicDBObject basicDbObject = (BasicDBObject) mongoDao.findOne(dbSource.getDsTableName()); BsonDocument bsonDocument = basicDbObject.toBsonDocument(null, mongoDao.getClient().getDefaultCodecRegistry()); int i = 0; for (String key : basicDbObject.keySet()) { FieldMapping row = new FieldMapping(); row.setFieldName(key); row.setFiledSource("?"); row.setFieldType(bsonDocument.get(key).getBsonType().toString()); row.setFieldOrdinalPosition(i + 1); columns.add(row); } } catch (Exception e) { e.printStackTrace(); return columns; } finally { if (mongoDao != null) { mongoDao.getClient().close(); } } } else if (Constant.TRS.equals(dbSource.getDsType())) { TRSConnection conn = null; try { conn = new TRSConnection(); conn.connect(dbSource.getDsHost(), dbSource.getDsPort().toString(), dbSource.getDsUserName(), dbSource.getDsPassWord()); TRSDataBase[] trsDbs = conn.getDataBases(dbSource.getDsTableName());// TRS? TRSDataBaseColumn[] column = trsDbs[0].getColumns();// ?? for (int i = 0; i < column.length; i++) { FieldMapping row = new FieldMapping(); row.setFieldName(column[i].getFullName()); row.setFiledSource("?"); row.setFieldType(column[i].getColTypeName()); row.setFieldOrdinalPosition(i + 1); columns.add(row); } } catch (TRSException e) { e.printStackTrace(); } finally { if (conn != null) { conn.close(); } } } return columns; }
From source file:cn.com.infcn.superspider.service.impl.DbServiceImpl.java
@SuppressWarnings("static-access") @Override//from w ww .j av a 2 s . com public List<FieldMapping> getAllColumnsBySQL(DbSource dbSource) throws Exception { List<FieldMapping> columns = new ArrayList<FieldMapping>(); if (Constant.MYSQL.equals(dbSource.getDsType()) || Constant.SQLSERVER.equals(dbSource.getDsType()) || Constant.ORACLE.equals(dbSource.getDsType()) || Constant.DB2.equals(dbSource.getDsType())) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = ConnectionUtil.getConnection(dbSource.getDsType(), dbSource.getDsHost(), dbSource.getDsPort(), dbSource.getDsDbName(), dbSource.getDsUserName(), dbSource.getDsPassWord()); if (conn != null) { String sql = dbSource.getDsSql(); sql = sql.replaceAll(""", "\"").replaceAll("<", "<").replaceAll(">", ">"); if (StringUtil.isEmpty(sql)) { throw new Exception("sql???"); } if (sql.indexOf("where") != -1 || sql.indexOf("WHERE") != -1) { sql.replaceAll("where", " where 1=2 and "); } else { sql += " where 1=2 "; } pstmt = conn.prepareStatement(sql); ResultSet colRet = pstmt.executeQuery(); ResultSetMetaData metaData = colRet.getMetaData(); int count = metaData.getColumnCount(); for (int i = 0; i < count; i++) { String name = metaData.getColumnName(i + 1); String typeName = metaData.getColumnTypeName(i + 1); int size = metaData.getColumnDisplaySize(i + 1); if (DbUtil.isSpecialColumnTypeChar(typeName)) { String typeNameSize = ""; if (size > Constant.MYSQL_VARCHAR_MAX_LENGTH) { typeNameSize = Constant.MYSQL_TEXT; } else { if (typeName.contains("()")) { typeNameSize = typeName.substring(0, typeName.indexOf("(") + 1) + size + typeName.substring(typeName.indexOf("(") + 1); } else { typeNameSize = typeName + "(" + size + ")"; } } typeName = typeNameSize; } FieldMapping row = new FieldMapping(); row.setFieldName(name); row.setFiledSource("?"); row.setFieldType(typeName);// ??; columns.add(row); } } } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (conn != null) { ConnectionUtil.closeConnection(conn, pstmt, rs); } } } else if (Constant.MONGO.equals(dbSource.getDsType())) { MongoDao mongoDao = null; try { mongoDao = new MongoDao(dbSource.getDsHost(), dbSource.getDsPort(), dbSource.getDsDbName()); BasicDBObject basicDbObject = (BasicDBObject) mongoDao.findOne(dbSource.getDsTableName()); BsonDocument bsonDocument = basicDbObject.toBsonDocument(null, mongoDao.getClient().getDefaultCodecRegistry()); int i = 0; for (String key : basicDbObject.keySet()) { FieldMapping row = new FieldMapping(); row.setFieldName(key); row.setFiledSource("?"); row.setFieldType(bsonDocument.get(key).getBsonType().toString()); row.setFieldOrdinalPosition(i + 1); columns.add(row); } } catch (Exception e) { e.printStackTrace(); throw new Exception(e.getMessage(), e); } finally { if (mongoDao != null) { mongoDao.getClient().close(); } } } return columns; }