8bcb7859407f0613fc4e41e7816e3701ed9fa1b3.svn-base 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. package com.persistence.service.logic;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.io.Reader;
  8. import java.io.StringReader;
  9. import java.io.StringWriter;
  10. import java.io.Writer;
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Timestamp;
  16. import java.util.ArrayList;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import com.persistence.service.ResultSet.TypeUtil;
  22. import com.persistence.service.assitant.DataObject;
  23. import com.persistence.service.assitant.Field;
  24. import com.persistence.service.exception.PersistenceException;
  25. import com.persistence.service.exception.TypeUnsupportedException;
  26. import com.sysmodel.Util;
  27. /**
  28. * sysmodel com.sysmodel.persistence create time 2006-5-27
  29. *
  30. * @author nbs
  31. */
  32. public class DefaultLogicHelper extends LogicHelper{
  33. private Log log = LogFactory.getLog(DefaultLogicHelper.class);
  34. public String getDataBaseTime(Connection con) throws PersistenceException {
  35. return null;
  36. }
  37. public String insertChildData(DataObject object, Connection con, String parentid)
  38. throws PersistenceException {
  39. try {
  40. // 判断逻辑主键
  41. if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()))) {
  42. throw new PersistenceException("logic primary key repeat");
  43. }
  44. // 存储非lob数据
  45. List<Field> reLobFields = new ArrayList<Field>();
  46. List<Field> reOtherFields = new ArrayList<Field>();
  47. String sql = "insert into " + object.getTablename()
  48. + generalChildInsert(object.getAttributes(), reLobFields, reOtherFields);
  49. PreparedStatement ps = con.prepareStatement(sql);
  50. Iterator<Field> it = reOtherFields.iterator();
  51. ps.setString(1, object.getObjectID());
  52. ps.setString(2, parentid);
  53. int i = 3;
  54. while (it.hasNext()) {
  55. Field field = it.next();
  56. TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
  57. i++;
  58. }
  59. int count = ps.executeUpdate();
  60. ps.close();
  61. if (count <= 0)
  62. throw new PersistenceException("PreparedStatement execute failture");
  63. // 存储lob数据
  64. if (reLobFields.size() > 0) {
  65. updateLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
  66. }
  67. } catch (TypeUnsupportedException e) {
  68. throw new PersistenceException(e.getMessage());
  69. } catch (SQLException e) {
  70. throw new PersistenceException(e.getMessage());
  71. }
  72. return object.getObjectID();
  73. }
  74. public String insertData(DataObject object, Connection con) throws PersistenceException {
  75. try {
  76. // 判断逻辑主键
  77. if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()))) {
  78. throw new PersistenceException("logic primary key repeat");
  79. }
  80. // 存储非lob数据
  81. List<Field> reLobFields = new ArrayList<Field>();
  82. List<Field> reOtherFields = new ArrayList<Field>();
  83. String sql = "insert into " + object.getTablename()
  84. + generalInsert(object.getAttributes(), reLobFields, reOtherFields);
  85. PreparedStatement ps = con.prepareStatement(sql);
  86. Iterator<Field> it = reOtherFields.iterator();
  87. ps.setString(1, object.getObjectID());
  88. int i = 2;
  89. while (it.hasNext()) {
  90. Field field = it.next();
  91. TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
  92. i++;
  93. }
  94. int count = ps.executeUpdate();
  95. ps.close();
  96. if (count <= 0)
  97. throw new PersistenceException("PreparedStatement execute failture");
  98. // 存储lob数据
  99. if (reLobFields.size() > 0) {
  100. updateLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
  101. }
  102. // 存储子表
  103. if (object.getChildobject().size() != 0) {
  104. List<DataObject> child = object.getChildobject();
  105. for (int j = 0; j < child.size(); j++) {
  106. DataObject tempChild = child.get(j);
  107. insertChildData(tempChild, con, object.getObjectID());
  108. }
  109. }
  110. } catch (TypeUnsupportedException e) {
  111. throw new PersistenceException(e.getMessage());
  112. } catch (SQLException e) {
  113. throw new PersistenceException(e.getMessage());
  114. }
  115. return object.getObjectID();
  116. }
  117. public void readBlobToOutputStream(DataObject object, Connection con, OutputStream os)
  118. throws PersistenceException {
  119. Field fieldLob = object.getAttributes().get(0);
  120. String sql = "select " + fieldLob.getFieldname() + " from " + object.getTablename()
  121. + " where fd_objectid='" + object.getObjectID() + "'";
  122. try {
  123. PreparedStatement ps = con.prepareStatement(sql);
  124. ResultSet rs = ps.executeQuery();
  125. if (rs.next()) {
  126. readBlob(rs, fieldLob.getFieldname(), os);
  127. }
  128. rs.close();
  129. ps.close();
  130. } catch (SQLException e) {
  131. throw new PersistenceException(e.getMessage());
  132. }
  133. }
  134. public void readClobToWriter(DataObject object, Connection con, Writer writer)
  135. throws PersistenceException {
  136. Field fieldLob = object.getAttributes().get(0);
  137. String sql = "select " + fieldLob.getFieldname() + " from " + object.getTablename()
  138. + " where fd_objectid='" + object.getObjectID() + "'";
  139. try {
  140. PreparedStatement ps = con.prepareStatement(sql);
  141. ResultSet rs = ps.executeQuery();
  142. if (rs.next()) {
  143. readClob(rs, fieldLob.getFieldname(), writer);
  144. }
  145. rs.close();
  146. ps.close();
  147. } catch (SQLException e) {
  148. throw new PersistenceException(e.getMessage());
  149. }
  150. }
  151. public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
  152. List<Field> condition) throws PersistenceException {
  153. ArrayList<DataObject> al = new ArrayList<DataObject>();
  154. StringBuffer sb = new StringBuffer();
  155. String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
  156. + ",fd_objectid from " + object.getTablename() + generalWhere(condition, sb);
  157. String order = sb.toString();
  158. if (!order.equals(""))
  159. sql = sql + " order by " + sb.deleteCharAt(sb.length() - 1).toString();
  160. log.info(sql);
  161. PreparedStatement ps = null;
  162. ResultSet rs = null;
  163. try {
  164. ps = con.prepareStatement(sql);
  165. for (int i = 0; i < condition.size(); i++) {
  166. Field field = condition.get(i);
  167. TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i + 1);
  168. }
  169. rs = ps.executeQuery();
  170. List<Field> alField = object.getAttributes();
  171. while (rs.next()) {
  172. DataObject temobject = new DataObject();
  173. temobject.setClassid(object.getClassid());
  174. temobject.setTablename(object.getTablename());
  175. temobject.setObjectID(rs.getString("fd_objectid"));
  176. for (int i = 0; i < alField.size(); i++) {
  177. Field field = alField.get(i);
  178. Field tempField = new Field(field.getFieldname(), null);
  179. tempField.setDataType(field.getDataType());
  180. if (field.getDataType().equals(TypeUtil.BLOB)) {
  181. tempField.setFieldvalue(readBolb(rs, field.getFieldname()));
  182. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  183. tempField.setFieldvalue(readColb(rs, field.getFieldname()));
  184. } else {
  185. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  186. field.getFieldname()));
  187. }
  188. temobject.addAttribute(tempField);
  189. }
  190. al.add(temobject);
  191. }
  192. } catch (SQLException e) {
  193. throw new PersistenceException(e.getMessage());
  194. } catch (TypeUnsupportedException e) {
  195. throw new PersistenceException(e.getMessage());
  196. } finally {
  197. try {
  198. if (rs != null)
  199. rs.close();
  200. if (ps != null)
  201. ps.close();
  202. } catch (SQLException e) {
  203. e.printStackTrace();
  204. }
  205. }
  206. return al;
  207. }
  208. @Override
  209. public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
  210. String SQLcondition) throws PersistenceException {
  211. ArrayList<DataObject> al = new ArrayList<DataObject>();
  212. StringBuffer sb = new StringBuffer();
  213. String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
  214. + ",fd_objectid from " + object.getTablename();
  215. if (SQLcondition.length() > 0)
  216. sql += " " + SQLcondition;
  217. log.info(sql);
  218. PreparedStatement ps = null;
  219. ResultSet rs = null;
  220. try {
  221. ps = con.prepareStatement(sql);
  222. rs = ps.executeQuery();
  223. List<Field> alField = object.getAttributes();
  224. while (rs.next()) {
  225. DataObject temobject = new DataObject();
  226. temobject.setClassid(object.getClassid());
  227. temobject.setTablename(object.getTablename());
  228. temobject.setObjectID(rs.getString("fd_objectid"));
  229. for (int i = 0; i < alField.size(); i++) {
  230. Field field = alField.get(i);
  231. Field tempField = new Field(field.getFieldname(), null);
  232. tempField.setDataType(field.getDataType());
  233. if (field.getDataType().equals(TypeUtil.BLOB)) {
  234. tempField.setFieldvalue(readBolb(rs, field.getFieldname()));
  235. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  236. tempField.setFieldvalue(readColb(rs, field.getFieldname()));
  237. } else if (field.getDataType().equals(TypeUtil.DATE)) {
  238. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  239. field.getFieldname()));
  240. Timestamp valueTemp = (Timestamp) tempField.getFieldvalue();
  241. tempField.setFieldvalue(Util.getDateTime(field.getScale(), valueTemp));
  242. } else {
  243. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  244. field.getFieldname()));
  245. }
  246. temobject.addAttribute(tempField);
  247. }
  248. al.add(temobject);
  249. }
  250. } catch (SQLException e) {
  251. throw new PersistenceException(e.getMessage());
  252. } catch (TypeUnsupportedException e) {
  253. throw new PersistenceException(e.getMessage());
  254. } finally {
  255. try {
  256. if (rs != null)
  257. rs.close();
  258. if (ps != null)
  259. ps.close();
  260. } catch (SQLException e) {
  261. e.printStackTrace();
  262. }
  263. }
  264. return al;
  265. }
  266. public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
  267. String SQLcondition, String dataBase, String firstPageNum, String lastPageNum)
  268. throws PersistenceException {
  269. ArrayList<DataObject> al = new ArrayList<DataObject>();
  270. StringBuffer sb = new StringBuffer();
  271. StringBuffer sql = new StringBuffer();// "select "+
  272. // generalListAndOrderBy(object.getAttributes(),
  273. // sb)+ ",fd_objectid from " +
  274. // object.getTablename();
  275. String sql_temp = generalListAndOrderBy(object.getAttributes(), sb);
  276. sql.append("select ");
  277. if (dataBase.equals("db2")) {
  278. sql.append(" rownumber() over(ORDER BY fd_objectid ) AS ROWNUM_ , ");
  279. } else if (dataBase.equals("sqlserver")) {
  280. sql.append(" ROW_NUMBER() over(ORDER BY fd_objectid)AS ROWNUM_ , ");
  281. }
  282. sql.append(sql_temp).append(",fd_objectid from ").append(object.getTablename());
  283. if (SQLcondition.length() > 0)
  284. sql.append(SQLcondition);
  285. StringBuffer pagingSql = new StringBuffer();
  286. if (dataBase.equals("oracle")) {
  287. pagingSql.append("SELECT * FROM ( SELECT row_.*, rownum rownum_ FROM (");
  288. pagingSql.append(sql);
  289. pagingSql.append(") row_ WHERE rownum <= ").append(lastPageNum)
  290. .append(") WHERE rownum_ > ").append(firstPageNum);
  291. } else if (dataBase.equals("db2")) {
  292. pagingSql.append("SELECT * FROM ( ");
  293. pagingSql.append(sql);
  294. pagingSql.append(")as a where ROWNUM_ BETWEEN ").append(firstPageNum).append(" and ")
  295. .append(lastPageNum);
  296. } else if (dataBase.equals("sqlserver")) {
  297. pagingSql.append("SELECT * FROM (");
  298. pagingSql.append(sql);
  299. pagingSql.append(") as t where t.ROWNUM_ > " + firstPageNum + " and t.ROWNUM_ < "
  300. + lastPageNum);
  301. }
  302. log.info(pagingSql);
  303. PreparedStatement ps = null;
  304. ResultSet rs = null;
  305. try {
  306. ps = con.prepareStatement(pagingSql.toString());
  307. rs = ps.executeQuery();
  308. List<Field> alField = object.getAttributes();
  309. while (rs.next()) {
  310. DataObject temobject = new DataObject();
  311. temobject.setClassid(object.getClassid());
  312. temobject.setTablename(object.getTablename());
  313. temobject.setObjectID(rs.getString("fd_objectid"));
  314. for (int i = 0; i < alField.size(); i++) {
  315. Field field = alField.get(i);
  316. Field tempField = new Field(field.getFieldname(), null);
  317. tempField.setDataType(field.getDataType());
  318. if (field.getDataType().equals(TypeUtil.BLOB)) {
  319. tempField.setFieldvalue(readBolb(rs, field.getFieldname()));
  320. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  321. tempField.setFieldvalue(readColb(rs, field.getFieldname()));
  322. } else if (field.getDataType().equals(TypeUtil.DATE)) {
  323. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  324. field.getFieldname()));
  325. Timestamp valueTemp = (Timestamp) tempField.getFieldvalue();
  326. tempField.setFieldvalue(Util.getDateTime(field.getScale(), valueTemp));
  327. } else {
  328. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  329. field.getFieldname()));
  330. }
  331. temobject.addAttribute(tempField);
  332. }
  333. al.add(temobject);
  334. }
  335. } catch (SQLException e) {
  336. throw new PersistenceException(e.getMessage());
  337. } catch (TypeUnsupportedException e) {
  338. throw new PersistenceException(e.getMessage());
  339. } finally {
  340. try {
  341. if (rs != null)
  342. rs.close();
  343. if (ps != null)
  344. ps.close();
  345. } catch (SQLException e) {
  346. e.printStackTrace();
  347. }
  348. }
  349. return al;
  350. }
  351. public ArrayList<DataObject> searchChildData(DataObject object, Connection con, String parentid)
  352. throws PersistenceException {
  353. ArrayList<DataObject> al = new ArrayList<DataObject>();
  354. StringBuffer sb = new StringBuffer();
  355. String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
  356. + ",fd_objectid from " + object.getTablename() + " where fd_parentid='" + parentid
  357. + "'";
  358. String order = sb.toString();
  359. if (!order.equals(""))
  360. sql = sql + " order by " + sb.deleteCharAt(sb.length() - 1).toString();
  361. try {
  362. PreparedStatement ps = con.prepareStatement(sql);
  363. ResultSet rs = ps.executeQuery();
  364. List<Field> alField = object.getAttributes();
  365. while (rs.next()) {
  366. DataObject temobject = new DataObject();
  367. temobject.setClassid(object.getClassid());
  368. temobject.setTablename(object.getTablename());
  369. temobject.setObjectID(rs.getString("fd_objectid"));
  370. for (int i = 0; i < alField.size(); i++) {
  371. Field field = alField.get(i);
  372. Field tempField = new Field(field.getFieldname(), null);
  373. tempField.setDataType(field.getDataType());
  374. if (field.getDataType().equals(TypeUtil.BLOB)) {
  375. tempField.setFieldvalue(readBolb(rs, field.getFieldname()));
  376. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  377. tempField.setFieldvalue(readColb(rs, field.getFieldname()));
  378. } else {
  379. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  380. field.getFieldname()));
  381. }
  382. temobject.addAttribute(tempField);
  383. }
  384. al.add(temobject);
  385. }
  386. } catch (SQLException e) {
  387. throw new PersistenceException(e.getMessage());
  388. } catch (TypeUnsupportedException e) {
  389. throw new PersistenceException(e.getMessage());
  390. }
  391. return al;
  392. }
  393. public DataObject searchData(DataObject object, Connection con) throws PersistenceException {
  394. String sql = "select " + generalList(object.getAttributes()) + " from "
  395. + object.getTablename() + " where " + object.getObjectIDName() + "='"
  396. + object.getObjectID() + "'";
  397. try {
  398. PreparedStatement ps = con.prepareStatement(sql);
  399. ResultSet rs = ps.executeQuery();
  400. if (rs.next()) {
  401. List<Field> alField = object.getAttributes();
  402. for (int i = 0; i < alField.size(); i++) {
  403. Field field = alField.get(i);
  404. if (field.getDataType().equals(TypeUtil.BLOB)) {
  405. field.setFieldvalue(readBolb(rs, field.getFieldname()));
  406. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  407. field.setFieldvalue(readColb(rs, field.getFieldname()));
  408. } else {
  409. field.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  410. field.getFieldname()));
  411. }
  412. }
  413. }
  414. rs.close();
  415. ps.close();
  416. } catch (SQLException e) {
  417. throw new PersistenceException(e.getMessage());
  418. } catch (TypeUnsupportedException e) {
  419. throw new PersistenceException(e.getMessage());
  420. }
  421. return object;
  422. }
  423. public void storeBlob(DataObject object, Connection con, InputStream is)
  424. throws PersistenceException {
  425. Field fieldLob = object.getAttributes().get(0);
  426. String updateLob = "update " + object.getTablename() + " set " + fieldLob.getFieldname()
  427. + "= ? where fd_objectid ='" + object.getObjectID() + "'";
  428. storeBlob(updateLob, con, is);
  429. }
  430. public void storeClob(DataObject object, Connection con, Reader reader)
  431. throws PersistenceException {
  432. Field fieldLob = object.getAttributes().get(0);
  433. String updateLob = "update " + object.getTablename() + " set " + fieldLob.getFieldname()
  434. + "= ? where fd_objectid ='" + object.getObjectID() + "'";
  435. // 从流里读到字符串里
  436. StringBuffer sb = new StringBuffer();
  437. char[] cbuffer = new char[1024];
  438. int nread = 0;
  439. try {
  440. while ((nread = reader.read(cbuffer)) != -1) {
  441. sb.append(cbuffer, 0, nread);
  442. }
  443. cbuffer = null;
  444. String clobString = sb.toString();
  445. Reader stringReader = new StringReader(clobString);
  446. storeClob(updateLob, con, stringReader, clobString.length());
  447. stringReader.close();
  448. } catch (IOException e) {
  449. log.error("read data error ", e);
  450. throw new PersistenceException(e.getMessage());
  451. }
  452. }
  453. public boolean updateData(DataObject object, Connection con) throws PersistenceException {
  454. // 更新主表
  455. if (!updateOneData(object, con))
  456. return false;
  457. // 更新子表
  458. if (object.getChildobject().size() != 0) {
  459. List<DataObject> child = object.getChildobject();
  460. // 更新子表中需要更新的数据
  461. for (int j = 0; j < child.size(); j++) {
  462. DataObject tempChild = child.get(j);
  463. if (!updateOneData(tempChild, con))
  464. return false;
  465. }
  466. // 判断属否子表有删除的记录并删除
  467. }
  468. return true;
  469. }
  470. public boolean updateOneData(DataObject object, Connection con) throws PersistenceException {
  471. int count = -1;
  472. try {
  473. // 判断逻辑主键
  474. if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()),
  475. object.getObjectID())) {
  476. throw new PersistenceException("logic primary key repeat");
  477. }
  478. // 存储非lob数据
  479. List<Field> reLobFields = new ArrayList<Field>();
  480. List<Field> reOtherFields = new ArrayList<Field>();
  481. String sql = "update " + object.getTablename()
  482. + generalUpdate(object.getAttributes(), reLobFields, reOtherFields)
  483. + " where fd_objectid='" + object.getObjectID() + "'";
  484. PreparedStatement ps = con.prepareStatement(sql);
  485. Iterator<Field> it = reOtherFields.iterator();
  486. int i = 1;
  487. while (it.hasNext()) {
  488. Field field = it.next();
  489. TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
  490. i++;
  491. }
  492. count = ps.executeUpdate();
  493. ps.close();
  494. if (count == 1) {
  495. // 存储lob数据
  496. if (reLobFields.size() > 0) {
  497. updateLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
  498. }
  499. }
  500. } catch (TypeUnsupportedException e) {
  501. throw new PersistenceException(e.getMessage());
  502. } catch (SQLException e) {
  503. throw new PersistenceException(e.getMessage());
  504. }
  505. return count <= 0 ? false : true;
  506. }
  507. /**
  508. * @param reLobFields
  509. * @return
  510. */
  511. private String generalList(List<Field> reFields) {
  512. StringBuffer sblist = new StringBuffer();
  513. for (int i = 0; i < reFields.size(); i++) {
  514. Field field = reFields.get(i);
  515. if (i == 0)
  516. sblist.append(field.getFieldname());
  517. else
  518. sblist.append("," + field.getFieldname());
  519. }
  520. return sblist.toString();
  521. }
  522. private String generalInsert(List<Field> allFields, List<Field> reLobFields,
  523. List<Field> reOtherFields) {
  524. StringBuffer sblist = new StringBuffer(" (fd_objectid");
  525. StringBuffer sbvalue = new StringBuffer("values(?");
  526. Iterator<Field> it = allFields.iterator();
  527. while (it.hasNext()) {
  528. Field field = it.next();
  529. if (field.getDataType().equals(TypeUtil.BLOB)) {
  530. reLobFields.add(field);
  531. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  532. reLobFields.add(field);
  533. } else {
  534. sbvalue.append(",?");
  535. reOtherFields.add(field);
  536. sblist.append(",");
  537. sblist.append(field.getFieldname());
  538. }
  539. }
  540. sblist.append(") ");
  541. sbvalue.append(")");
  542. return sblist.toString() + sbvalue.toString();
  543. }
  544. /**
  545. * @param attributes
  546. * @param reLobFields
  547. * @param reOtherFields
  548. * @return insert child sql
  549. */
  550. private String generalChildInsert(ArrayList<Field> attributes, List<Field> reLobFields,
  551. List<Field> reOtherFields) {
  552. StringBuffer sblist = new StringBuffer(" (fd_objectid,fd_parentid");
  553. StringBuffer sbvalue = new StringBuffer("values(?,?");
  554. Iterator<Field> it = attributes.iterator();
  555. while (it.hasNext()) {
  556. Field field = it.next();
  557. if (field.getDataType().equals(TypeUtil.BLOB)) {
  558. reLobFields.add(field);
  559. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  560. reLobFields.add(field);
  561. } else {
  562. sbvalue.append(",?");
  563. reOtherFields.add(field);
  564. sblist.append(",");
  565. sblist.append(field.getFieldname());
  566. }
  567. }
  568. sblist.append(") ");
  569. sbvalue.append(")");
  570. return sblist.toString() + sbvalue.toString();
  571. }
  572. private String generalListAndOrderBy(List<Field> reFields, StringBuffer sb) {
  573. StringBuffer sblist = new StringBuffer();
  574. for (int i = 0; i < reFields.size(); i++) {
  575. Field field = reFields.get(i);
  576. if (i == 0)
  577. sblist.append(field.getFieldname());
  578. else
  579. sblist.append("," + field.getFieldname());
  580. if (field.getOrderType() == Field.ASCEND)
  581. sb.append(field.getFieldname() + " asc,");
  582. else if (field.getOrderType() == Field.DESCEND)
  583. sb.append(field.getFieldname() + " desc,");
  584. }
  585. return sblist.toString();
  586. }
  587. /**
  588. * @param condition
  589. * @param sb
  590. * @return sql where
  591. */
  592. private String generalWhere(List<Field> condition, StringBuffer sb) {
  593. if (condition.size() == 0)
  594. return "";
  595. StringBuffer sbWhere = new StringBuffer(" where ");
  596. Iterator<Field> it = condition.iterator();
  597. boolean flag = true;
  598. while (it.hasNext()) {
  599. Field field = it.next();
  600. String fieldOper = field.getOperatorType();
  601. if (flag) {
  602. sbWhere.append(field.getFieldname());
  603. sbWhere.append(fieldOper);
  604. sbWhere.append(" ? ");
  605. flag = false;
  606. } else {
  607. sbWhere.append(" and ");
  608. sbWhere.append(field.getFieldname());
  609. sbWhere.append(fieldOper);
  610. sbWhere.append(" ? ");
  611. }
  612. if (field.getOrderType() == Field.ASCEND)
  613. sb.append(field.getFieldname() + " asc,");
  614. else if (field.getOrderType() == Field.DESCEND)
  615. sb.append(field.getFieldname() + " desc,");
  616. }
  617. return sbWhere.toString();
  618. }
  619. /**
  620. * @param attributes
  621. * @param reLobFields
  622. * @param reOtherFields
  623. * @return update sql
  624. */
  625. private String generalUpdate(ArrayList<Field> attributes, List<Field> reLobFields,
  626. List<Field> reOtherFields) {
  627. StringBuffer sb = new StringBuffer(" set ");
  628. Iterator<Field> it = attributes.iterator();
  629. while (it.hasNext()) {
  630. Field field = it.next();
  631. if (field.getDataType().equals(TypeUtil.BLOB)) {
  632. reLobFields.add(field);
  633. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  634. reLobFields.add(field);
  635. } else {
  636. sb.append(field.getFieldname());
  637. sb.append("= ? ,");
  638. reOtherFields.add(field);
  639. }
  640. }
  641. return sb.deleteCharAt(sb.length() - 1).toString();
  642. }
  643. /**
  644. * @param tablename
  645. * @param objectID
  646. * @param reLobFields
  647. * @param con
  648. */
  649. private void updateLobField(String tablename, String objectID, List<Field> reLobFields,
  650. Connection con) throws PersistenceException {
  651. try {
  652. for (int i = 0; i < reLobFields.size(); i++) {
  653. Field field = reLobFields.get(i);
  654. if (field.getDataType().equals(TypeUtil.BLOB)) {
  655. if (field.getFieldvalue() != null) {
  656. String sql = "update " + tablename + " set " + field.getFieldname()
  657. + "= ? where fd_objectid ='" + objectID + "'";
  658. byte[] content = (byte[]) field.getFieldvalue();
  659. InputStream is = new ByteArrayInputStream(content);
  660. storeBlob(sql, con, is);
  661. is.close();
  662. }
  663. } else {
  664. if (field.getFieldvalue() != null) {
  665. String sql = "update " + tablename + " set " + field.getFieldname()
  666. + "= ? where fd_objectid ='" + objectID + "'";
  667. String content = (String) field.getFieldvalue();
  668. Reader reader = new StringReader(content);
  669. storeClob(sql, con, reader, content.length());
  670. reader.close();
  671. }
  672. }
  673. }
  674. } catch (IOException e) {
  675. log.error("close error", e);
  676. throw new PersistenceException(e.getMessage());
  677. }
  678. }
  679. private void storeBlob(String sql, Connection con, InputStream is) throws PersistenceException {
  680. try {
  681. PreparedStatement ps = con.prepareStatement(sql);
  682. ps.setBinaryStream(1, is, is.available());
  683. int count = ps.executeUpdate();
  684. ps.close();
  685. if (count != 1)
  686. throw new PersistenceException("update blob falture");
  687. } catch (SQLException e) {
  688. log.error("updateLobField error ", e);
  689. throw new PersistenceException(e.getMessage());
  690. } catch (IOException e) {
  691. log.error("updateLobField error ", e);
  692. throw new PersistenceException(e.getMessage());
  693. }
  694. }
  695. private void storeClob(String sql, Connection con, Reader reader, int charLength)
  696. throws PersistenceException {
  697. try {
  698. PreparedStatement ps = con.prepareStatement(sql);
  699. ps.setCharacterStream(1, reader, charLength);
  700. int count = ps.executeUpdate();
  701. ps.close();
  702. if (count != 1)
  703. throw new PersistenceException("update blob falture");
  704. } catch (SQLException e) {
  705. log.error("updateLobField error ", e);
  706. throw new PersistenceException(e.getMessage());
  707. }
  708. }
  709. private void readBlob(ResultSet rs, String fieldname, OutputStream os) throws SQLException {
  710. try {
  711. InputStream blobinputStream = rs.getBinaryStream(fieldname);
  712. if (blobinputStream != null) {
  713. byte[] buffer = new byte[1024];
  714. int nread = 0; // Number of bytes read
  715. while ((nread = blobinputStream.read(buffer)) != -1)
  716. os.write(buffer, 0, nread);
  717. os.flush();
  718. }
  719. blobinputStream.close();
  720. } catch (SQLException e) {
  721. log.error("get getBinaryStream error", e);
  722. throw e;
  723. } catch (IOException e) {
  724. log.error("write error", e);
  725. throw new SQLException(e.getMessage());
  726. }
  727. }
  728. private byte[] readBolb(ResultSet rs, String fieldname) throws SQLException {
  729. byte[] reContent = null;
  730. ByteArrayOutputStream os = null;
  731. try {
  732. os = new ByteArrayOutputStream();
  733. readBlob(rs, fieldname, os);
  734. reContent = os.toByteArray();
  735. os.close();
  736. } catch (IOException e) {
  737. // log.error("close error", e);
  738. throw new SQLException(e.getMessage());
  739. } finally {
  740. try {
  741. os.close();
  742. } catch (IOException e) {
  743. // TODO Auto-generated catch block
  744. e.printStackTrace();
  745. }
  746. }
  747. return reContent;
  748. }
  749. private void readClob(ResultSet rs, String fieldname, Writer writer) throws SQLException {
  750. Reader clobReader = null;
  751. try {
  752. clobReader = rs.getCharacterStream(fieldname);
  753. if (clobReader != null) {
  754. char[] cbuffer = new char[1024];
  755. int nread = 0; // Number of bytes read
  756. while ((nread = clobReader.read(cbuffer)) != -1)
  757. writer.write(cbuffer, 0, nread);
  758. writer.flush();
  759. clobReader.close();
  760. }
  761. } catch (SQLException e) {
  762. // log.error("get getBinaryStream error", e);
  763. throw e;
  764. } catch (IOException e) {
  765. // log.error("write error", e);
  766. throw new SQLException(e.getMessage());
  767. } finally {
  768. try {
  769. clobReader.close();
  770. } catch (IOException e) {
  771. // TODO Auto-generated catch block
  772. e.printStackTrace();
  773. }
  774. }
  775. }
  776. private String readColb(ResultSet rs, String fieldname) throws SQLException {
  777. String reContent = null;
  778. try {
  779. StringWriter writer = new StringWriter();
  780. readClob(rs, fieldname, writer);
  781. reContent = writer.toString();
  782. writer.close();
  783. } catch (IOException e) {
  784. log.error("close error", e);
  785. throw new SQLException(e.getMessage());
  786. }
  787. return reContent;
  788. }
  789. }