123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854 |
- package com.persistence.service.logic;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Reader;
- import java.io.StringReader;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.sql.Blob;
- import java.sql.Clob;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Iterator;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.persistence.DbConnection;
- import com.persistence.service.ResultSet.TypeUtil;
- import com.persistence.service.assitant.DataObject;
- import com.persistence.service.assitant.Field;
- import com.persistence.service.exception.PersistenceException;
- import com.persistence.service.exception.TypeUnsupportedException;
- import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource;
- /**
- * sysmodel com.sysmodel.persistence create time 2006-5-20
- *
- * @author nbs
- */
- public class DaMengLogicHelper extends LogicHelper{
- private Log log = LogFactory.getLog(DaMengLogicHelper.class);
- public Connection getConnection(MdpDataSource datasource) {
- Connection connection = super.getConnection(datasource);
- if (connection == null) {
- try {
- DbConnection dbConnection = new DbConnection();
- connection = dbConnection.getConnection(datasource);
- } catch (ClassNotFoundException e) {
- log.error(e);
- } catch (SQLException e) {
- log.error(e);
- }
- }
- return connection;
- }
- public String getDataBaseTime(Connection con) throws PersistenceException {
- String time = "";
- String sql = "select sysdate from dual";
- try {
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- Timestamp timesamp = rs.getTimestamp(1);
- if (timesamp != null) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(timesamp.getTime());
- java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss");
- time = df.format(calendar.getTime());
- }
- }
- rs.close();
- ps.close();
- } catch (Exception e) {
- throw new PersistenceException(e.getMessage());
- }
- return time;
- }
- public String insertChildData(DataObject object, Connection con, String parentid)
- throws PersistenceException {
- try {
- // 判断逻辑主键
- if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()))) {
- throw new PersistenceException("logic primary key repeat");
- }
- // 存储非lob数据
- List<Field> reLobFields = new ArrayList<Field>();
- List<Field> reOtherFields = new ArrayList<Field>();
- String sql = "insert into " + object.getTablename()
- + generalChildInsert(object.getAttributes(), reLobFields, reOtherFields);
- log.info(sql);
- PreparedStatement ps = con.prepareStatement(sql);
- Iterator<Field> it = reOtherFields.iterator();
- ps.setString(1, object.getObjectID());
- ps.setString(2, parentid);
- int i = 3;
- while (it.hasNext()) {
- Field field = it.next();
- TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
- i++;
- }
- int count = ps.executeUpdate();
- ps.close();
- if (count <= 0)
- throw new PersistenceException("PreparedStatement execute failture");
- // 存储lob数据
- if (reLobFields.size() > 0) {
- insertLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
- }
- } catch (TypeUnsupportedException e) {
- throw new PersistenceException(e.getMessage());
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- }
- return object.getObjectID();
- }
- public String insertData(DataObject object, Connection con) throws PersistenceException {
- try {
- // 判断逻辑主键
- if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()))) {
- throw new PersistenceException("logic primary key repeat");
- }
- // 存储非lob数据
- List<Field> reLobFields = new ArrayList<Field>();
- List<Field> reOtherFields = new ArrayList<Field>();
- String sql = "insert into " + object.getTablename()
- + generalInsert(object.getAttributes(), reLobFields, reOtherFields);
- PreparedStatement ps = con.prepareStatement(sql);
- Iterator<Field> it = reOtherFields.iterator();
- ps.setString(1, object.getObjectID());
- int i = 2;
- while (it.hasNext()) {
- Field field = it.next();
- System.out.println("fieldname="+field.getFieldname() +" field.getFieldvalue()="+field.getFieldvalue() + " i="+i);
- TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
- i++;
- }
- int count = ps.executeUpdate();
- ps.close();
- if (count <= 0)
- throw new PersistenceException("PreparedStatement execute failture");
- // 存储lob数据
- if (reLobFields.size() > 0) {
- insertLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
- }
- // 存储子表
- if (object.getChildobject().size() != 0) {
- List<DataObject> child = object.getChildobject();
- for (int j = 0; j < child.size(); j++) {
- DataObject tempChild = child.get(j);
- insertChildData(tempChild, con, object.getObjectID());
- }
- }
- } catch (TypeUnsupportedException e) {
- throw new PersistenceException(e.getMessage());
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- }
- return object.getObjectID();
- }
- public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
- List<Field> condition) throws PersistenceException {
- ArrayList<DataObject> al = new ArrayList<DataObject>();
- StringBuffer sb = new StringBuffer();
- String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
- + ",fd_objectid from " + object.getTablename() + generalWhere(condition, sb);
- String order = sb.toString();
- if (!order.equals(""))
- sql = sql + " order by " + sb.deleteCharAt(sb.length() - 1).toString();
- try {
- PreparedStatement ps = con.prepareStatement(sql);
- for (int i = 0; i < condition.size(); i++) {
- Field field = condition.get(i);
- TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i + 1);
- }
- ResultSet rs = ps.executeQuery();
- List<Field> alField = object.getAttributes();
- while (rs.next()) {
- DataObject temobject = new DataObject();
- temobject.setClassid(object.getClassid());
- temobject.setTablename(object.getTablename());
- temobject.setObjectID(rs.getString("fd_objectid"));
- for (int i = 0; i < alField.size(); i++) {
- Field field = alField.get(i);
- Field tempField = new Field(field.getFieldname(), null);
- tempField.setDataType(field.getDataType());
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- if (rs.getBlob(field.getFieldname()) != null) {
- tempField
- .setFieldvalue(this.readBolb(rs.getBlob(field.getFieldname())));
- }
- } else if (field.getDataType().equals(TypeUtil.CLOB)) {
- if (rs.getClob(field.getFieldname()) != null) {
- tempField.setFieldvalue(readColb(rs.getClob(field.getFieldname())));
- }
- } else {
- tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
- field.getFieldname()));
- }
- temobject.addAttribute(tempField);
- }
- al.add(temobject);
- }
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- } catch (TypeUnsupportedException e) {
- throw new PersistenceException(e.getMessage());
- }
- return al;
- }
- @Override
- public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
- String SQLcondition) throws PersistenceException {
- ArrayList<DataObject> al = new ArrayList<DataObject>();
- StringBuffer sb = new StringBuffer();
- String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
- + ",fd_objectid from " + object.getTablename();
- if (SQLcondition.length() > 0)
- sql += " " + SQLcondition;
- log.info(sql);
- try {
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- List<Field> alField = object.getAttributes();
- while (rs.next()) {
- DataObject temobject = new DataObject();
- temobject.setClassid(object.getClassid());
- temobject.setTablename(object.getTablename());
- temobject.setObjectID(rs.getString("fd_objectid"));
- for (int i = 0; i < alField.size(); i++) {
- Field field = alField.get(i);
- Field tempField = new Field(field.getFieldname(), null);
- tempField.setDataType(field.getDataType());
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- tempField.setFieldvalue(this.readBolb(rs.getBlob(field.getFieldname())));
- } else if (field.getDataType().equals(TypeUtil.CLOB)) {
- tempField.setFieldvalue(readColb(rs.getClob(field.getFieldname())));
- } else {
- tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
- field.getFieldname()));
- }
- temobject.addAttribute(tempField);
- }
- al.add(temobject);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- // throw new PersistenceException(e.getMessage());
- } catch (TypeUnsupportedException e) {
- throw new PersistenceException(e.getMessage());
- }
- return al;
- }
- public ArrayList<DataObject> searchChildData(DataObject object, Connection con, String parentid)
- throws PersistenceException {
- ArrayList<DataObject> al = new ArrayList<DataObject>();
- StringBuffer sb = new StringBuffer();
- String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
- + ",fd_objectid from " + object.getTablename() + " where fd_parentid='" + parentid
- + "'";
- String order = sb.toString();
- if (!order.equals(""))
- sql = sql + " order by " + sb.deleteCharAt(sb.length() - 1).toString();
- try {
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- List<Field> alField = object.getAttributes();
- while (rs.next()) {
- DataObject temobject = new DataObject();
- temobject.setClassid(object.getClassid());
- temobject.setTablename(object.getTablename());
- temobject.setObjectID(rs.getString("fd_objectid"));
- for (int i = 0; i < alField.size(); i++) {
- Field field = alField.get(i);
- Field tempField = new Field(field.getFieldname(), null);
- tempField.setDataType(field.getDataType());
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- tempField.setFieldvalue(this.readBolb(rs.getBlob(field.getFieldname())));
- } else if (field.getDataType().equals(TypeUtil.CLOB)) {
- tempField.setFieldvalue(readColb(rs.getClob(field.getFieldname())));
- } else {
- tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
- field.getFieldname()));
- }
- temobject.addAttribute(tempField);
- }
- al.add(temobject);
- }
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- } catch (TypeUnsupportedException e) {
- throw new PersistenceException(e.getMessage());
- }
- return al;
- }
- public DataObject searchData(DataObject object, Connection con) throws PersistenceException {
- String sql = "select " + generalList(object.getAttributes()) + " from "
- + object.getTablename() + " where " + object.getObjectIDName() + " =?";
- log.info(sql);
- try {
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setString(1,object.getObjectID());
- ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- List<Field> alField = object.getAttributes();
- for (int i = 0; i < alField.size(); i++) {
- Field field = alField.get(i);
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- field.setFieldvalue(this.readBolb(rs.getBlob(field.getFieldname())));
- } else if (field.getDataType().equals(TypeUtil.CLOB)) {
- field.setFieldvalue(readColb(rs.getClob(field.getFieldname())));
- } else {
- field.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
- field.getFieldname()));
- }
- }
- }
- rs.close();
- ps.close();
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- } catch (TypeUnsupportedException e) {
- throw new PersistenceException(e.getMessage());
- }
- return object;
- }
- public boolean updateData(DataObject object, Connection con) throws PersistenceException {
- if (!updateOneData(object, con))
- return false;
- // 王志军-2010-10-18 修改,更新子表问题
- // 更新子表
- // if(object.getChildobject().size()!=0){
- // List<DataObject> child = object.getChildobject();
- // for (int j = 0; j < child.size(); j++) {
- // DataObject tempChild = child.get(j);
- // log.info(tempChild.getObjectID()) ;
- // if(!updateOneData(tempChild,con)) return false;
- // }
- // }
- return true;
- }
- public boolean updateOneData(DataObject object, Connection con) throws PersistenceException {
- int count = -1;
- try {
- // 判断逻辑主键
- if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()),
- object.getObjectID())) {
- throw new PersistenceException("logic primary key repeat");
- }
- // 存储非lob数据
- List<Field> reLobFields = new ArrayList<Field>();
- List<Field> reOtherFields = new ArrayList<Field>();
- String sql = "update " + object.getTablename()
- + generalUpdate(object.getAttributes(), reLobFields, reOtherFields)
- + " where fd_objectid='" + object.getObjectID() + "'";
- PreparedStatement ps = con.prepareStatement(sql);
- Iterator<Field> it = reOtherFields.iterator();
- int i = 1;
- while (it.hasNext()) {
- Field field = it.next();
- System.out.println("field.getDataType()="+field.getDataType()+" field.getFieldvalue()="+field.getFieldvalue()+" i="+i);
- TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
- i++;
- }
- count = ps.executeUpdate();
- ps.close();
- if (count == 1) {
- // 存储lob数据
- if (reLobFields.size() > 0) {
- updateLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
- }
- }
- } catch (TypeUnsupportedException e) {
- throw new PersistenceException(e.getMessage());
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- }
- return count <= 0 ? false : true;
- }
- public void readBlobToOutputStream(DataObject object, Connection con, OutputStream os)
- throws PersistenceException {
- String sql = "select " + generalList(object.getAttributes()) + " from "
- + object.getTablename() + " where fd_objectid='" + object.getObjectID() + "'";
- try {
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- readBlob(rs.getBlob(1), os);
- }
- rs.close();
- ps.close();
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- }
- }
- public void readClobToWriter(DataObject object, Connection con, Writer writer)
- throws PersistenceException {
- String sql = "select " + generalList(object.getAttributes()) + " from "
- + object.getTablename() + " where fd_objectid='" + object.getObjectID() + "'";
- try {
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- readClob(rs.getClob(1), writer);
- }
- rs.close();
- ps.close();
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- }
- }
- public void storeBlob(DataObject object, Connection con, InputStream is)
- throws PersistenceException {
- String clearLob = "update " + object.getTablename()
- + generalClearList(object.getAttributes()) + " where fd_objectid ="
- + object.getObjectID();
- String sql = "select " + generalList(object.getAttributes()) + " from "
- + object.getTablename() + " where fd_objectid =" + object.getObjectID()
- + " for update";
- try {
- Statement stmt = con.createStatement();
- if (stmt.executeUpdate(clearLob) != 1)
- throw new SQLException("clear Lob failture");
- ResultSet lobDetails = stmt.executeQuery(sql);
- if (lobDetails.next()) {
- writeBlob(lobDetails.getBlob(1), is);
- }
- lobDetails.close();
- stmt.close();
- } catch (SQLException e) {
- log.error("updateLobField error ", e);
- throw new PersistenceException(e.getMessage());
- }
- }
- public void storeClob(DataObject object, Connection con, Reader reader)
- throws PersistenceException {
- String clearLob = "update " + object.getTablename()
- + generalClearList(object.getAttributes()) + " where fd_objectid ="
- + object.getObjectID();
- String sql = "select " + generalList(object.getAttributes()) + " from "
- + object.getTablename() + " where fd_objectid =" + object.getObjectID()
- + " for update";
- try {
- Statement stmt = con.createStatement();
- if (stmt.executeUpdate(clearLob) != 1)
- throw new SQLException("clear Lob failture");
- ResultSet lobDetails = stmt.executeQuery(sql);
- if (lobDetails.next()) {
- writeClob(lobDetails.getClob(1), reader);
- }
- lobDetails.close();
- stmt.close();
- } catch (SQLException e) {
- log.error("updateLobField error ", e);
- throw new PersistenceException(e.getMessage());
- }
- }
- private String generalInsert(List<Field> allFields, List<Field> reLobFields,
- List<Field> reOtherFields) {
- StringBuffer sblist = new StringBuffer(" (fd_objectid");
- StringBuffer sbvalue = new StringBuffer("values(?");
- Iterator<Field> it = allFields.iterator();
- while (it.hasNext()) {
- Field field = it.next();
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- sbvalue.append(",EMPTY_BLOB()");
- reLobFields.add(field);
- } else if (field.getDataType().equals(TypeUtil.CLOB)) {
- sbvalue.append(",EMPTY_CLOB()");
- reLobFields.add(field);
- } else {
- sbvalue.append(",?");
- reOtherFields.add(field);
- }
- sblist.append(",");
- sblist.append(field.getFieldname());
- }
- sblist.append(") ");
- sbvalue.append(")");
- return sblist.toString() + sbvalue.toString();
- }
- /**
- * @param attributes
- * @param reLobFields
- * @param reOtherFields
- * @return insert child sql
- */
- private String generalChildInsert(ArrayList<Field> attributes, List<Field> reLobFields,
- List<Field> reOtherFields) {
- StringBuffer sblist = new StringBuffer(" (fd_objectid,fd_parentid");
- StringBuffer sbvalue = new StringBuffer("values(?,?");
- Iterator<Field> it = attributes.iterator();
- while (it.hasNext()) {
- Field field = it.next();
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- sbvalue.append(",EMPTY_BLOB()");
- reLobFields.add(field);
- } else if (field.getDataType().equals(TypeUtil.CLOB)) {
- sbvalue.append(",EMPTY_CLOB()");
- reLobFields.add(field);
- } else {
- sbvalue.append(",?");
- reOtherFields.add(field);
- }
- sblist.append(",");
- sblist.append(field.getFieldname());
- }
- sblist.append(") ");
- sbvalue.append(")");
- return sblist.toString() + sbvalue.toString();
- }
- /**
- * @param attributes
- * @param reLobFields
- * @param reOtherFields
- * @return update sql
- */
- private String generalUpdate(ArrayList<Field> attributes, List<Field> reLobFields,
- List<Field> reOtherFields) {
- StringBuffer sb = new StringBuffer(" set ");
- Iterator<Field> it = attributes.iterator();
- while (it.hasNext()) {
- Field field = it.next();
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- reLobFields.add(field);
- } else if (field.getDataType().equals(TypeUtil.CLOB)) {
- reLobFields.add(field);
- } else {
- sb.append(field.getFieldname());
- sb.append("= ? ,");
- reOtherFields.add(field);
- }
- }
- return sb.deleteCharAt(sb.length() - 1).toString();
- }
- /**
- * @param tablename
- * @param objectID
- * @param reLobFields
- * @param con
- */
- private void insertLobField(String tablename, String objectID, List<Field> reLobFields,
- Connection con) throws SQLException {
- String sql = "select " + generalList(reLobFields) + " from " + tablename
- + " where fd_objectid =" + objectID + " for update";
- try {
- Statement stmt = con.createStatement();
- ResultSet lobDetails = stmt.executeQuery(sql);
- if (lobDetails.next()) {
- for (int i = 0; i < reLobFields.size(); i++) {
- Field field = reLobFields.get(i);
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- if (field.getFieldvalue() != null) {
- byte[] content = (byte[]) field.getFieldvalue();
- if (content.length > 0) {
- InputStream is = new ByteArrayInputStream(content);
- writeBlob(lobDetails.getBlob(field.getFieldname()), is);
- is.close();
- }
- }
- } else {
- if (field.getFieldvalue() != null) {
- String content = (String) field.getFieldvalue();
- if (!content.equals("")) {
- Reader reader = new StringReader(content);
- writeClob(lobDetails.getClob(field.getFieldname()), reader);
- reader.close();
- }
- }
- }
- }
- }
- lobDetails.close();
- stmt.close();
- } catch (SQLException e) {
- log.error("updateLobField error ", e);
- throw e;
- } catch (IOException e) {
- log.error("close error ", e);
- throw new SQLException(e.getMessage());
- }
- }
- /**
- * @param tablename
- * @param objectID
- * @param reLobFields
- * @param con
- */
- private void updateLobField(String tablename, String objectID, List<Field> reLobFields,
- Connection con) throws SQLException {
- String clearLob = "update " + tablename + generalClearList(reLobFields)
- + " where fd_objectid =" + objectID;
- String sql = "select " + generalList(reLobFields) + " from " + tablename
- + " where fd_objectid =" + objectID + " for update";
- try {
- Statement stmt = con.createStatement();
- if (stmt.executeUpdate(clearLob) != 1)
- throw new SQLException("clear Lob failture");
- ResultSet lobDetails = stmt.executeQuery(sql);
- if (lobDetails.next()) {
- for (int i = 0; i < reLobFields.size(); i++) {
- Field field = reLobFields.get(i);
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- if (field.getFieldvalue() != null) {
- byte[] content = (byte[]) field.getFieldvalue();
- if (content.length > 0) {
- InputStream is = new ByteArrayInputStream(content);
- writeBlob(lobDetails.getBlob(field.getFieldname()), is);
- is.close();
- }
- }
- } else {
- if (field.getFieldvalue() != null) {
- String content = (String) field.getFieldvalue();
- if (!content.equals("")) {
- Reader reader = new StringReader(content);
- writeClob(lobDetails.getClob(field.getFieldname()), reader);
- reader.close();
- }
- }
- }
- }
- }
- lobDetails.close();
- stmt.close();
- } catch (SQLException e) {
- log.error("updateLobField error ", e);
- throw e;
- } catch (IOException e) {
- log.error("close error ", e);
- throw new SQLException(e.getMessage());
- }
- }
- /**
- * @param reLobFields
- * @return
- */
- private String generalClearList(List<Field> reLobFields) {
- StringBuffer sblist = new StringBuffer(" set ");
- for (int i = 0; i < reLobFields.size(); i++) {
- Field field = reLobFields.get(i);
- if (i == 0)
- sblist.append(field.getFieldname());
- else
- sblist.append("," + field.getFieldname());
- if (field.getDataType().equals(TypeUtil.BLOB)) {
- sblist.append(" = EMPTY_BLOB()");
- } else if (field.getDataType().equals(TypeUtil.CLOB)) {
- sblist.append(" = EMPTY_CLOB()");
- }
- }
- return sblist.toString();
- }
- /**
- * @param reLobFields
- * @return
- */
- private String generalList(List<Field> reFields) {
- StringBuffer sblist = new StringBuffer();
- for (int i = 0; i < reFields.size(); i++) {
- Field field = reFields.get(i);
- if (i == 0)
- sblist.append(field.getFieldname());
- else
- sblist.append("," + field.getFieldname());
- }
- return sblist.toString();
- }
- private String generalListAndOrderBy(List<Field> reFields, StringBuffer sb) {
- StringBuffer sblist = new StringBuffer();
- for (int i = 0; i < reFields.size(); i++) {
- Field field = reFields.get(i);
- if (i == 0)
- sblist.append(field.getFieldname());
- else
- sblist.append("," + field.getFieldname());
- if (field.getOrderType() == Field.ASCEND)
- sb.append(field.getFieldname() + " asc,");
- else if (field.getOrderType() == Field.DESCEND)
- sb.append(field.getFieldname() + " desc,");
- }
- return sblist.toString();
- }
- /**
- * @param condition
- * @param sb
- * @return sql where
- */
- private String generalWhere(List<Field> condition, StringBuffer sb) {
- if (condition.size() == 0)
- return "";
- StringBuffer sbWhere = new StringBuffer(" where ");
- Iterator<Field> it = condition.iterator();
- boolean flag = true;
- while (it.hasNext()) {
- Field field = it.next();
- if (flag) {
- sbWhere.append(field.getFieldname());
- sbWhere.append(" = ? ");
- flag = false;
- } else {
- sbWhere.append(" and ");
- sbWhere.append(field.getFieldname());
- sbWhere.append(" = ?");
- }
- if (field.getOrderType() == Field.ASCEND)
- sb.append(field.getFieldname() + " asc,");
- else if (field.getOrderType() == Field.DESCEND)
- sb.append(field.getFieldname() + " desc,");
- }
- return sbWhere.toString();
- }
- @SuppressWarnings("deprecation")
- private void writeBlob(Blob blob, InputStream is) throws SQLException {
- // try {
- // OutputStream blobOutputStream = ((oracle.sql.BLOB) blob).getBinaryOutputStream();
- // byte[] buffer = new byte[1024];
- // int nread = 0; // Number of bytes read
- // while ((nread = is.read(buffer)) != -1)
- // blobOutputStream.write(buffer, 0, nread);
- // blobOutputStream.flush();
- // blobOutputStream.close();
- // } catch (SQLException e) {
- // log.error("get OutputStream error", e);
- // throw e;
- // } catch (IOException e) {
- // log.error("read InputStream error", e);
- // throw new SQLException(e.getMessage());
- // }
- }
- /**
- * @param clob
- * @param reader
- */
- @SuppressWarnings("deprecation")
- private void writeClob(Clob clob, Reader reader) throws SQLException {
- // try {
- // Writer clobWriter = ((oracle.sql.CLOB) clob).getCharacterOutputStream();
- // char[] cbuffer = new char[1024];
- // int nread = 0; // Number of bytes read
- // while ((nread = reader.read(cbuffer)) != -1)
- // clobWriter.write(cbuffer, 0, nread);
- // clobWriter.flush();
- // clobWriter.close();
- // } catch (SQLException e) {
- // log.error("get CharacterOutputStream error", e);
- // throw e;
- // } catch (IOException e) {
- // log.error("read Reader error", e);
- // throw new SQLException(e.getMessage());
- // }
- }
- private byte[] readBolb(Blob blob) throws SQLException {
- byte[] reContent = null;
- try {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- readBlob(blob, os);
- reContent = os.toByteArray();
- os.close();
- } catch (IOException e) {
- log.error("close error", e);
- throw new SQLException(e.getMessage());
- }
- return reContent;
- }
- private void readBlob(Blob blob, OutputStream os) throws SQLException {
- try {
- InputStream blobinputStream = blob.getBinaryStream();
- byte[] buffer = new byte[1024];
- int nread = 0; // Number of bytes read
- while ((nread = blobinputStream.read(buffer)) != -1)
- os.write(buffer, 0, nread);
- os.flush();
- blobinputStream.close();
- } catch (SQLException e) {
- log.error("get getBinaryStream error", e);
- throw e;
- } catch (IOException e) {
- log.error("write error", e);
- throw new SQLException(e.getMessage());
- }
- }
- private String readColb(Clob clob) throws SQLException {
- String reContent = null;
- try {
- StringWriter writer = new StringWriter();
- readClob(clob, writer);
- reContent = writer.toString();
- writer.close();
- } catch (IOException e) {
- log.error("close error", e);
- throw new SQLException(e.getMessage());
- }
- return reContent;
- }
- private void readClob(Clob clob, Writer writer) throws SQLException {
- try {
- Reader clobReader = clob.getCharacterStream();
- char[] cbuffer = new char[1024];
- int nread = 0; // Number of bytes read
- while ((nread = clobReader.read(cbuffer)) != -1)
- writer.write(cbuffer, 0, nread);
- writer.flush();
- clobReader.close();
- } catch (SQLException e) {
- log.error("get getBinaryStream error", e);
- throw e;
- } catch (IOException e) {
- log.error("write error", e);
- throw new SQLException(e.getMessage());
- }
- }
- @Override
- public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
- String SQLcondition, String dataBase, String firstPageNum, String lastPageNum)
- throws PersistenceException {
- // TODO Auto-generated method stub
- return null;
- }
- }
|