85cc6652756aef1f1dbcda08fd3a3a1715bfe5a3.svn-base 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. package com.persistence.DBdll.adapter;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.ResultSetMetaData;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Vector;
  11. import org.apache.log4j.Logger;
  12. import com.persistence.DbConnection;
  13. import com.persistence.DBdll.adapter.assitant.ColumnInfo;
  14. import com.persistence.DBdll.adapter.assitant.ColumnInfoService;
  15. import com.persistence.DBdll.adapter.assitant.TableInfo;
  16. import com.persistence.DBdll.adapter.assitant.index.IndexInfo;
  17. import com.persistence.service.PersistenceFactory;
  18. import com.persistence.service.SysPersistence;
  19. import com.persistence.service.exception.PersistenceException;
  20. import com.sysmodel.datamodel.Parameter.DataBaseType;
  21. import com.sysmodel.datamodel.Parameter.SysParameter;
  22. import com.sysmodel.datamodel.Parameter.XmlManagerPara;
  23. import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute;
  24. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  25. import com.sysmodel.datamodel.xmlmodel.able.MdpConstant;
  26. import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource;
  27. import com.sysmodel.datamodel.xmlmodel.able.Property;
  28. import com.sysmodel.datamodel.xmlmodel.able.Template;
  29. import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
  30. import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl;
  31. import com.sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl;
  32. import com.sysmodel.datamodel.xmlmodel.impl.MdpDataSourceImpl;
  33. import com.sysmodel.datamodel.xmlmodel.impl.PropertyImpl;
  34. import com.sysmodel.datamodel.xmlmodel.impl.SysModelImpl;
  35. public class DB2Adapter extends DatabaseAdapter{
  36. private final static Logger log = Logger.getLogger(DB2Adapter.class);
  37. ColumnInfoService columnInfoService = new ColumnInfoService();
  38. private SysModelImpl sysmodel = SysModelImpl.getInstance();
  39. public boolean createTable(MdpClassImpl table) {
  40. String sql = generalSQL(table);
  41. log.info(sql);
  42. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(table.getDataSource());
  43. return executeDDL(MdpDataSource, sql);
  44. }
  45. public ArrayList<Vector<Object>> updateTable(MdpClassImpl table) {
  46. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(table.getDataSource());
  47. ArrayList<Vector<Object>> list = new ArrayList<Vector<Object>>();
  48. try {
  49. Vector<ColumnInfo> columninfos = getTableColumnInfo(table.getName(), MdpDataSource);
  50. if (columninfos.size() == 0) {
  51. return list;
  52. }
  53. Vector<Object> dllVec = new Vector<Object>();
  54. Vector<MdpAttributeImpl> add = columnInfoService.addDataBaseColumn(
  55. table.getAllMdpAttributes(), columninfos);
  56. for (int i = 0; i < add.size(); i++) {
  57. MdpAttributeImpl imp = add.elementAt(i);
  58. String sql = getTableModifyDllSQL(XmlManagerPara.add, imp, table.getName());
  59. boolean bresult = executeDDL(MdpDataSource, sql);
  60. dllVec = new Vector<Object>();
  61. dllVec.add("add");
  62. dllVec.add(imp);
  63. dllVec.add(bresult);
  64. dllVec.add(sql);
  65. list.add(dllVec);
  66. }
  67. Vector<ColumnInfo> delete = columnInfoService.deleteDataBaseColumn(
  68. table.getAllMdpAttributes(), columninfos);
  69. for (int i = 0; i < delete.size(); i++) {
  70. ColumnInfo imp = delete.elementAt(i);
  71. String sql = "alter table " + table.getName() + " drop column "
  72. + imp.getColumnName();
  73. boolean bresult = executeDDL(MdpDataSource, sql);
  74. dllVec = new Vector<Object>();
  75. dllVec.add("delete");
  76. dllVec.add(imp);
  77. dllVec.add(bresult);
  78. dllVec.add(sql);
  79. list.add(dllVec);
  80. }
  81. Vector<MdpAttributeImpl> update = columnInfoService.updateDataBaseColumn(
  82. table.getAllMdpAttributes(), columninfos);
  83. for (int i = 0; i < update.size(); i++) {
  84. MdpAttributeImpl imp = update.elementAt(i);
  85. String sql = getTableModifyDllSQL(XmlManagerPara.update, imp, table.getName());
  86. boolean bresult = executeDDL(MdpDataSource, sql);
  87. dllVec = new Vector<Object>();
  88. dllVec.add("update");
  89. dllVec.add(imp);
  90. dllVec.add(bresult);
  91. dllVec.add(sql);
  92. list.add(dllVec);
  93. }
  94. ColumnInfo info = columnInfoService
  95. .getColumnInfoByFieldName(columninfos, "fd_objectid");
  96. if (info == null) {
  97. String sql = "alter table " + table.getName() + " add fd_objectid CHAR("
  98. + XmlManagerPara.fd_objectidlength + ")";
  99. boolean bresult = executeDDL(MdpDataSource, sql);
  100. dllVec = new Vector<Object>();
  101. dllVec.add("addfd_objectid");
  102. dllVec.add("fd_objectid");
  103. dllVec.add(bresult);
  104. dllVec.add(sql);
  105. list.add(dllVec);
  106. } else {
  107. if (!info.getColumnTypeName().equalsIgnoreCase("VARCHAR")
  108. || info.getPrecision() != XmlManagerPara.fd_objectidlength) {
  109. String sql = "alter table " + table.getName() + " modify fd_objectid CHAR("
  110. + XmlManagerPara.fd_objectidlength + ")";
  111. boolean bresult = executeDDL(MdpDataSource, sql);
  112. dllVec = new Vector<Object>();
  113. dllVec.add("updatefd_objectid");
  114. dllVec.add("fd_objectid");
  115. dllVec.add(bresult);
  116. dllVec.add(sql);
  117. list.add(dllVec);
  118. }
  119. }
  120. } catch (Exception e) {
  121. log.error(e.toString());
  122. }
  123. return list;
  124. }
  125. public boolean renameTableName(MdpClassImpl table, String newtablename) {
  126. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(table.getDataSource());
  127. return renameTableName(MdpDataSource, newtablename, table.getName());
  128. }
  129. public boolean updateTableAttribute(String action, MdpAttributeImpl attribute,
  130. MdpClassImpl table) {
  131. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(table.getDataSource());
  132. String sql = getTableModifyDllSQL(action, attribute, table.getName());
  133. return executeDDL(MdpDataSource, sql);
  134. }
  135. /**
  136. * 获得数据库中指定表中列的信息
  137. *
  138. * @param tablename
  139. * @param con
  140. * @return
  141. */
  142. public Vector<ColumnInfo> getTableColumnInfo(String tableName, MdpDataSource datasource) {
  143. Vector<ColumnInfo> vect = new Vector<ColumnInfo>();
  144. Connection connection = getConnection(datasource);
  145. if (connection == null)
  146. return vect;
  147. Statement st = null;
  148. ResultSet rs = null;
  149. DatabaseTableInfo TableInfo = new DatabaseTableInfo();
  150. try {
  151. HashMap<String, String> MapComments = TableInfo.getColComments(connection, tableName,
  152. DataBaseType.DB2);
  153. st = connection.createStatement();
  154. rs = st.executeQuery("select * from " + tableName + " where 1=0");
  155. ResultSetMetaData rm = rs.getMetaData();
  156. int colCount = rm.getColumnCount();// 取得列数
  157. ColumnInfo vo = null;
  158. for (int i = 1; i <= colCount; i++) {
  159. vo = new ColumnInfo();
  160. String ColumnName = rm.getColumnName(i);
  161. vo.setDataBaseType(DataBaseType.DB2);
  162. vo.setTableName(tableName);
  163. vo.setColumnName(rm.getColumnName(i));
  164. vo.setColumnType(rm.getColumnType(i));
  165. vo.setColumnTypeName(rm.getColumnTypeName(i));
  166. vo.setPrecision(rm.getPrecision(i));
  167. vo.setScale(rm.getScale(i));
  168. vo.setDescritpion(MapComments.get(ColumnName));
  169. vo.setAutoIncrement(rm.isAutoIncrement(i));
  170. vo.setIsNullable(rm.isNullable(i));
  171. vo.setReadOnly(rm.isReadOnly(i));
  172. vo.setSigned(rm.isSigned(i));
  173. vo.setSearchable(rm.isSearchable(i));
  174. vo.setColumnDisplaySize(rm.getColumnDisplaySize(i));
  175. vect.add(vo);
  176. }
  177. } catch (Exception e) {
  178. log.error("DB2Exception-- ", e);
  179. } finally {
  180. try {
  181. if (rs != null)
  182. rs.close();
  183. if (st != null)
  184. st.close();
  185. if (connection != null)
  186. connection.close();
  187. } catch (SQLException e) {
  188. log.error("DB2 SQLException-- ", e);
  189. }
  190. }
  191. return vect;
  192. }
  193. private String getCanstantModifyDllSQL(String action, Property imp, String tablename) {
  194. String sql = "alter table " + tablename;
  195. if (action.equals(XmlManagerPara.add)) {
  196. sql = sql + " add ";
  197. } else if (action.equals(XmlManagerPara.update)) {
  198. sql = sql + " modify ";
  199. } else if (action.equals(XmlManagerPara.delete)) {
  200. sql = sql + " drop column " + imp.getName();
  201. return sql;
  202. }
  203. String fieldname = imp.getName();
  204. String datatype = imp.getDataType();
  205. if (datatype.equals(XmlManagerPara.date)) {
  206. // 如果只使用MM/DD/YYY,那么使用DATE类型。
  207. // l 如果只使用HH:MM:SS, 那么使用TIME类型。
  208. // l 如果要使用日期和时间,则使用时间戳类型(TIMESTAMP)
  209. // l 可以使用Oracle中的TO_CHAR()函数来取DATE的字串来分别与DB2/400的DATE、TIME相匹配。
  210. if (imp.getScale().equals(SysParameter.date_format)) {
  211. sql = sql + fieldname + " DATE";
  212. } else if (imp.getScale().equals(SysParameter.time_format)) {
  213. sql = sql + fieldname + " TIME";
  214. } else {
  215. sql = sql + fieldname + " TIMESTAMP";
  216. }
  217. } else if (datatype.equals(XmlManagerPara.bool)) {
  218. sql = sql + fieldname + " SAMLLINT";
  219. sql = sql + " default null";
  220. }
  221. else if (datatype.equals(XmlManagerPara.number)) {
  222. int length = imp.getLength() == 0 ? 10 : imp.getLength();
  223. String scale = imp.getScale().equals("") ? "" : "," + imp.getScale();
  224. sql = sql + fieldname + " DECIMAL(" + length + scale + ")";
  225. sql = sql + " default null";
  226. } else if (datatype.equals(XmlManagerPara.string)) {
  227. sql = sql + fieldname;
  228. if (imp.getLength() <= 2000 && imp.getLength() >= 50) {
  229. sql = sql + " VARCHAR(" + imp.getLength() + ")";
  230. } else {
  231. sql = sql + " VARCHAR(" + imp.getLength() + ")";
  232. }
  233. sql = sql + " default null";
  234. }
  235. log.info("sql = " + sql);
  236. return sql;
  237. }
  238. private String getTableModifyDllSQL(String action, MdpAttributeImpl attribute, String tablename) {
  239. String sql = "alter table " + tablename;
  240. if (action.equals(XmlManagerPara.add)) {
  241. sql = sql + " add ";
  242. } else if (action.equals(XmlManagerPara.update)) {
  243. sql = sql + " modify ";
  244. } else if (action.equals(XmlManagerPara.delete)) {
  245. sql = sql + " drop column " + attribute.getName();
  246. return sql;
  247. }
  248. String fieldname = attribute.getName();
  249. String datatype = attribute.getDataType();
  250. if (datatype.equals(XmlManagerPara.date)) {
  251. if (attribute.getScale().equals(SysParameter.date_format)) {
  252. sql = sql + fieldname + " DATE";
  253. } else if (attribute.getScale().equals(SysParameter.time_format)) {
  254. sql = sql + fieldname + " TIME";
  255. } else {
  256. sql = sql + fieldname + " TIMESTAMP";
  257. }
  258. if (attribute.getDefaultValue() != null
  259. && attribute.getDefaultValue().equals(XmlManagerPara.sysdate)) {
  260. if (attribute.getScale().equals(SysParameter.date_format)) {
  261. sql = sql + " DEFAULT CURRENT DATE";
  262. } else if (attribute.getScale().equals(SysParameter.time_format)) {
  263. sql = sql + " DEFAULT CURRENT TIME";
  264. } else {
  265. sql = sql + " DEFAULT CURRENT TIMESTAMP";
  266. }
  267. }
  268. } else if (datatype.equals("blob")) {
  269. sql = sql + fieldname + " BLOB(2)";
  270. }
  271. else if (datatype.equals("clob")) {
  272. sql = sql + fieldname + " CLOB(2) ";
  273. }
  274. else if (datatype.equals(XmlManagerPara.bool)) {
  275. sql = sql + fieldname + " SAMLLINT";
  276. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  277. sql = sql + " default " + attribute.getDefaultValue();
  278. } else {
  279. sql = sql + " default null";
  280. }
  281. }
  282. else if (datatype.equals(XmlManagerPara.number)) {
  283. int length = attribute.getPrecision() == 0 ? 10 : attribute.getPrecision();
  284. String scale = attribute.getScale().equals("") ? "" : "," + attribute.getScale();
  285. sql = sql + fieldname + " DECIMAL(" + length + scale + ")";
  286. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  287. sql = sql + " default " + attribute.getDefaultValue();
  288. } else {
  289. sql = sql + " default null";
  290. }
  291. } else if (datatype.equals(XmlManagerPara.string)) {
  292. sql = sql + fieldname;
  293. if (attribute.getPrecision() < 50) {
  294. sql = sql + " CHAR(" + attribute.getPrecision() + ")";
  295. } else if (attribute.getPrecision() <= 2000 && attribute.getPrecision() >= 50) {
  296. sql = sql + " VARCHAR(" + attribute.getPrecision() + ")";
  297. } else {
  298. sql = sql + " VARCHAR(" + attribute.getPrecision() + ")";
  299. }
  300. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  301. sql = sql + " default '" + attribute.getDefaultValue() + "'";
  302. } else {
  303. sql = sql + " default null";
  304. }
  305. }
  306. log.info("sql = " + sql);
  307. return sql;
  308. }
  309. @Override
  310. public boolean createConstanTable(Template template) {
  311. String sql = getCreateConstanTableDLL(template);
  312. log.info("createsql=" + sql);
  313. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(template.getDataSource());
  314. return this.executeDDL(MdpDataSource, sql);
  315. }
  316. @Override
  317. public ArrayList<Vector<Object>> updateConstanTable(Template template) {
  318. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(template.getDataSource());
  319. ArrayList<Vector<Object>> list = new ArrayList<Vector<Object>>();
  320. String tableName = template.getTableName();
  321. try {
  322. Vector<ColumnInfo> columninfos = getTableColumnInfo(tableName, MdpDataSource);
  323. if (columninfos.size() == 0) {
  324. return list;
  325. }
  326. List<PropertyImpl> Propertys = template.getListproperty();
  327. Vector<Object> dllVec = new Vector<Object>();
  328. // delete column
  329. Vector<ColumnInfo> delete = columnInfoService.deleteConstantDataBaseColumn(Propertys,
  330. columninfos);
  331. for (int i = 0; i < delete.size(); i++) {
  332. ColumnInfo imp = delete.elementAt(i);
  333. String sql = "alter table " + tableName + " drop column " + imp.getColumnName();
  334. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  335. dllVec = new Vector<Object>();
  336. dllVec.add("delete");
  337. dllVec.add(imp);
  338. dllVec.add(bresult);
  339. dllVec.add(sql);
  340. list.add(dllVec);
  341. }
  342. Vector<Property> add = columnInfoService.addConstantDataBaseColumn(Propertys,
  343. columninfos);
  344. for (int i = 0; i < add.size(); i++) {
  345. Property imp = add.elementAt(i);
  346. String sql = getCanstantModifyDllSQL(XmlManagerPara.add, imp, tableName);
  347. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  348. dllVec = new Vector<Object>();
  349. dllVec.add("add");
  350. dllVec.add(imp);
  351. dllVec.add(bresult);
  352. dllVec.add(sql);
  353. list.add(dllVec);
  354. }
  355. Vector<Property> update = columnInfoService.updateConstantDataBaseColumn(Propertys,
  356. columninfos);
  357. for (int i = 0; i < update.size(); i++) {
  358. Property imp = update.elementAt(i);
  359. String sql = getCanstantModifyDllSQL(XmlManagerPara.update, imp, tableName);
  360. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  361. dllVec = new Vector<Object>();
  362. dllVec.add("update");
  363. dllVec.add(imp);
  364. dllVec.add(bresult);
  365. dllVec.add(sql);
  366. list.add(dllVec);
  367. }
  368. } catch (Exception e) {
  369. log.error(e.toString());
  370. }
  371. return list;
  372. }
  373. @Override
  374. public boolean renameConstantTableName(Template template, String newtablename) {
  375. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(template.getDataSource());
  376. return renameTableName(MdpDataSource, newtablename, template.getTableName());
  377. }
  378. private boolean renameTableName(MdpDataSource MdpDataSource, String newtablename,
  379. String oldtablename) {
  380. String sql = "rename " + oldtablename + " to " + newtablename;
  381. log.info("sql = " + sql);
  382. return executeDDL(MdpDataSource, sql);
  383. }
  384. private String generalSQL(MdpClassImpl table) {
  385. StringBuffer sql = new StringBuffer();
  386. sql.append("create table " + table.getName()).append(
  387. "(FD_OBJECTID CHAR(" + XmlManagerPara.fd_objectidlength
  388. + ") NOT NULL primary key ");
  389. ArrayList<MdpAttributeImpl> list = (ArrayList<MdpAttributeImpl>) table
  390. .getAllMdpAttributes();
  391. for (int i = 0; i < list.size(); i++) {
  392. MdpAttributeImpl attribute = list.get(i);
  393. String fieldname = attribute.getName();
  394. String datatype = attribute.getDataType();
  395. if (datatype.equals(XmlManagerPara.date)) {
  396. if (attribute.getScale().equals(SysParameter.date_format)) {
  397. sql.append("," + fieldname + " DATE");
  398. } else if (attribute.getScale().equals(SysParameter.time_format)) {
  399. sql.append("," + fieldname + " TIME");
  400. } else {
  401. sql.append("," + fieldname + " TIMESTAMP");
  402. }
  403. if (attribute.getDefaultValue() != null
  404. && attribute.getDefaultValue().equals(XmlManagerPara.sysdate)) {
  405. if (attribute.getScale().equals(SysParameter.date_format)) {
  406. sql.append(" DEFAULT CURRENT DATE");
  407. } else if (attribute.getScale().equals(SysParameter.time_format)) {
  408. sql.append(" DEFAULT CURRENT TIME");
  409. } else {
  410. sql.append(" DEFAULT CURRENT TIMESTAMP");
  411. }
  412. }
  413. } else if (datatype.equals(XmlManagerPara.clob)) {
  414. sql.append("," + fieldname + " CLOB(2)");
  415. } else if (datatype.equals(XmlManagerPara.blob)) {
  416. sql.append("," + fieldname + " BLOB(2)");
  417. } else if (datatype.equals(XmlManagerPara.bool)) {
  418. sql.append("," + fieldname + " SAMLLINT");
  419. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  420. sql.append(" default " + attribute.getDefaultValue());
  421. }
  422. } else if (datatype.equals(XmlManagerPara.image)) {
  423. sql.append("," + fieldname + " VARCHAR(100)");
  424. } else if (datatype.equals(XmlManagerPara.number)) {
  425. int length = attribute.getPrecision() == 0 ? 10 : attribute.getPrecision();
  426. String scale = attribute.getScale().equals("") ? "" : "," + attribute.getScale();
  427. if (scale.equals("")) {
  428. sql.append("," + fieldname + " DOUBLE");
  429. } else {
  430. sql.append("," + fieldname + " DECIMAL(" + length + scale + ")");
  431. }
  432. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  433. sql.append(" default " + attribute.getDefaultValue());
  434. }
  435. } else if (datatype.equals(XmlManagerPara.string)) {
  436. sql.append("," + fieldname + " VARCHAR(" + attribute.getPrecision() + ")");
  437. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  438. sql.append(" default '" + attribute.getDefaultValue() + "'");
  439. }
  440. }
  441. }
  442. sql.append(" ) ");
  443. return sql.toString();
  444. }
  445. public String getCreateConstanTableDLL(Template template) {
  446. StringBuffer sql = new StringBuffer();
  447. sql.append("create table " + template.getTableName()).append("( ");
  448. ArrayList<PropertyImpl> propertys = (ArrayList<PropertyImpl>) template.getListproperty();
  449. for (int i = 0; i < propertys.size(); i++) {
  450. PropertyImpl PropertyImpl = propertys.get(i);
  451. String fieldname = PropertyImpl.getName();
  452. String datatype = PropertyImpl.getDataType();
  453. if (datatype.equals(XmlManagerPara.date)) {
  454. if (PropertyImpl.getScale().equals(SysParameter.date_format)) {
  455. sql.append(",").append(fieldname).append(" DATE");
  456. } else if (PropertyImpl.getScale().equals(SysParameter.time_format)) {
  457. sql.append(",").append(fieldname).append(" TIME");
  458. } else {
  459. sql.append(",").append(fieldname).append(" TIMESTAMP");
  460. }
  461. } else if (datatype.equals(XmlManagerPara.clob)) {
  462. sql.append(",").append(fieldname).append(" CLOB(2)");
  463. } else if (datatype.equals(XmlManagerPara.blob)) {
  464. sql.append(",").append(fieldname).append(" BLOB(2)");
  465. } else if (datatype.equals(XmlManagerPara.bool)) {
  466. sql.append(",").append(fieldname).append(" SMALLINT");
  467. } else if (datatype.equals(XmlManagerPara.image)) {
  468. sql.append(",").append(fieldname).append(" VARCHAR(100)");
  469. } else if (datatype.equals(XmlManagerPara.number)) {
  470. String scale = PropertyImpl.getScale().equals("") ? "" : ","
  471. + PropertyImpl.getScale();
  472. if (scale.equals("")) {
  473. sql.append("," + fieldname + " SMALLINT");
  474. } else {
  475. int length = PropertyImpl.getLength() == 0 ? 10 : PropertyImpl.getLength();
  476. sql.append("," + fieldname + " DECIMAL(" + length + scale + ")");
  477. }
  478. } else if (datatype.equals(XmlManagerPara.string)) {
  479. sql.append(",").append(fieldname);
  480. sql.append(" VARCHAR(").append(PropertyImpl.getLength()).append(")");
  481. }
  482. if (PropertyImpl.isUnique()) {
  483. sql.append(" NOT NULL primary key");
  484. }
  485. }
  486. sql.append(" ) ");
  487. sql.reverse();
  488. sql.deleteCharAt(sql.lastIndexOf(","));
  489. sql.reverse();
  490. return sql.toString();
  491. }
  492. @Override
  493. public String getXmlByTableName(String tableName, MdpDataSource dataSource, String moduleId) {
  494. DatabaseTableInfo tableInfo = new DatabaseTableInfo();
  495. TableInfo tableVo = tableInfo.getTableInfo(tableName, dataSource, DataBaseType.DB2);
  496. Vector<ColumnInfo> ColumnInfoVec = this.getTableColumnInfo(tableName, dataSource);
  497. MdpDataSourceImpl dataSourceImpl = (MdpDataSourceImpl) dataSource;
  498. StringBuffer sb = new StringBuffer();
  499. sb.append(" \n <font color='#339900'>.......................................<br> \n");
  500. sb.append("&lt;mdpClass classid=\"请替换成推荐号\" ");
  501. sb.append("name=\"" + tableName + "\" description=\"" + tableVo.getDescritpion()
  502. + "\" type=\"" + moduleId + "\" ");
  503. sb.append("primaryKey=\"" + tableVo.getPrimaryKey()
  504. + "\" exist=\"true\" catche=\"false\" validate=\"true\" ");
  505. sb.append("dataSource=\"" + dataSourceImpl.getDataSourceid() + "\" ");
  506. sb.append("><br> \n");
  507. int colSize = ColumnInfoVec.size();
  508. ColumnInfo ColumnInfo = null;
  509. for (int i = 0; i < colSize; i++) {
  510. ColumnInfo = ColumnInfoVec.get(i);
  511. sb.append("&nbsp;&nbsp;&lt;mdpAttribute ");
  512. sb.append(" name=\"" + ColumnInfo.getColumnName() + "\" description=\""
  513. + ColumnInfo.getDescritpion() + "\"");
  514. sb.append(ColumnInfo.getColumTypeDetail());
  515. sb.append(" unit=\"\" logicPrimaryKey=\"false\" referenceType=\"0\" ");
  516. sb.append(" index=\"false\" indexType=\"\" defaultValue=\"\" notNull=\"false\" validate=\"true\" ");
  517. sb.append(" isAutoIncrement=\"false\" isReadOnly=\"false\" isSearchable=\"true\" isSigned=\"false\" ");
  518. sb.append(" columnDisplaySize=\"" + ColumnInfo.getColumnDisplaySize()
  519. + "\" fieldType=\"" + ColumnInfo.getColumnTypeName() + "("
  520. + ColumnInfo.getPrecision() + ")\" ");
  521. sb.append(" /&gt;");
  522. sb.append("<br> \n");
  523. }
  524. ColumnInfoVec.clear();
  525. sb.append("&lt;/mdpClass&gt;<br>");
  526. sb.append("........................................<br></font>");
  527. return sb.toString();
  528. }
  529. @Override
  530. public StringBuffer createTableComment(MdpClassImpl table, String type) {
  531. StringBuffer sb = new StringBuffer();
  532. sb.append("<font color='#339900'>");
  533. if (type.equals("add")) {
  534. sb.append("comment on table " + table.getName() + " is '" + table.getDescription()
  535. + "';<br>");
  536. } else {
  537. sb.append("comment on table " + table.getName() + " is '';<br>");
  538. }
  539. ArrayList<MdpAttributeImpl> list = (ArrayList<MdpAttributeImpl>) table
  540. .getAllMdpAttributes();
  541. for (int i = 0; i < list.size(); i++) {
  542. MdpAttributeImpl attribute = list.get(i);
  543. if (type.equals("add")) {
  544. String sComment = attribute.getDescription();
  545. if (!attribute.getUnit().equals("")) {
  546. sComment = sComment + "(" + attribute.getUnit() + ")";
  547. }
  548. sb.append("comment on column " + table.getName() + "." + attribute.getName()
  549. + " is '" + sComment + "';<br>");
  550. } else {
  551. sb.append("comment on column " + table.getName() + "." + attribute.getName()
  552. + " is '';<br>");
  553. }
  554. }
  555. sb.append("</font>");
  556. return sb;
  557. }
  558. @Override
  559. public StringBuffer createConstanTableComment(Template template, String type) {
  560. return null;
  561. }
  562. @Override
  563. public StringBuffer createOldTableComment(MdpClassImpl table, String type) {
  564. // TODO Auto-generated method stub
  565. return null;
  566. }
  567. @Override
  568. public ArrayList<String> getTableCreateKeyDll(int iDataSource) {
  569. ArrayList<String> resultlis = new ArrayList<String>();
  570. // 获得所有没有建立主键的表
  571. String sql = "select tabname,tabschema from syscat.tables "
  572. + "where TABSCHEMA='DB2ADMIN' and tabname not in ("
  573. + " select TABNAME from SYSCAT.KEYCOLUSE where TABSCHEMA='DB2ADMIN' "
  574. + ") ";
  575. MdpDataSource datasource = sysmodel.getDataSourceByCode(iDataSource);
  576. Connection conn = null;
  577. Statement st = null;
  578. ResultSet rs = null;
  579. try {
  580. DbConnection dbConnection = new DbConnection();
  581. conn = dbConnection.getConnection(datasource);
  582. st = conn.createStatement();
  583. rs = st.executeQuery(sql);
  584. while (rs.next()) {
  585. String tableName = rs.getString(1);
  586. String tempSql = "";
  587. tempSql = this.getMdpClassePkDll(tableName);
  588. if (tempSql.equals("")) {
  589. tempSql = this.getMdpConstantPkDll(tableName);
  590. }
  591. if (!tempSql.equals("")) {
  592. resultlis.add(tempSql);
  593. }
  594. }
  595. } catch (SQLException e) {
  596. e.printStackTrace();
  597. } catch (ClassNotFoundException e) {
  598. e.printStackTrace();
  599. } finally {
  600. try {
  601. if (rs != null)
  602. rs.close();
  603. if (st != null)
  604. st.close();
  605. if (conn != null)
  606. conn.close();
  607. } catch (SQLException e) {
  608. e.printStackTrace();
  609. }
  610. }
  611. return resultlis;
  612. }
  613. private String getMdpConstantPkDll(String tableName) {
  614. String result = "";
  615. ArrayList<MdpConstantImpl> lis = sysmodel.getMdpConstants();
  616. for (int i = 0; i < lis.size(); i++) {
  617. MdpConstant MdpConstant = (MdpConstant) lis.get(i);
  618. if (MdpConstant.getCodeName().equalsIgnoreCase(tableName.trim())) {
  619. String colname = MdpConstant.getTemplate().getUniqueProperty().getName();
  620. result = "ALTER TABLE " + tableName + " ADD CONSTRAINT PK_" + tableName
  621. + " PRIMARY KEY (" + colname + ")";
  622. break;
  623. }
  624. }
  625. return result;
  626. }
  627. private String getMdpClassePkDll(String tableName) {
  628. String result = "";
  629. ArrayList<MdpClassImpl> lis = sysmodel.getMdpClasses();
  630. for (int i = 0; i < lis.size(); i++) {
  631. MdpClass mdpClass = (MdpClass) lis.get(i);
  632. if (mdpClass.getName().toUpperCase().equals(tableName.toUpperCase())) {
  633. String colname = "FD_OBJECTID";
  634. if (mdpClass.getClassid() == 99 || mdpClass.getClassid() == 100)
  635. colname = "ClassId";
  636. result = "ALTER TABLE " + tableName + " ADD CONSTRAINT PK_" + tableName
  637. + " PRIMARY KEY (" + colname + ")";
  638. break;
  639. }
  640. }
  641. return result;
  642. }
  643. @Override
  644. public ArrayList<IndexInfo> getTableIndexInfo() {
  645. ArrayList<IndexInfo> resultlis = new ArrayList<IndexInfo>();
  646. try {
  647. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  648. String sql = "select tabname,indname,colnames,indextype from syscat.indexes where TABSCHEMA='DB2ADMIN' order by tabname";
  649. ArrayList<String[]> tablis = persistence.getSearchResult(99, sql);
  650. for (int i = 0; i < tablis.size(); i++) {
  651. String[] tabArry = tablis.get(i);
  652. IndexInfo IndexInfo = new IndexInfo();
  653. IndexInfo.setTabName(tabArry[0]);
  654. IndexInfo.setIndexName(tabArry[1]);
  655. IndexInfo.setColumNames(tabArry[2]);
  656. IndexInfo.setIndexType(tabArry[3]);
  657. resultlis.add(IndexInfo);
  658. }
  659. } catch (PersistenceException e) {
  660. e.printStackTrace();
  661. }
  662. return resultlis;
  663. }
  664. @Override
  665. public ArrayList<String> getTableIndexCreateDll() {
  666. /*
  667. * DROP INDEX "index_Name"; CREATE INDEX "index_Name" ON
  668. * ADJUNCT_IMG("NAME" ASC)
  669. *
  670. * DROP INDEX "ind_Mainid"; CREATE INDEX "ind_Mainid" ON ADJUNCT_IMG
  671. * ("MAINID" ASC) CLUSTER;
  672. */
  673. ArrayList<String> result = new ArrayList<String>();
  674. ArrayList<MdpClassImpl> mdpClasses = sysmodel.getMdpClasses();
  675. int mdpClasseslen = mdpClasses.size();
  676. for (int i = 0; i < mdpClasseslen; i++) {
  677. MdpClass mdpClass = (MdpClass) mdpClasses.get(i);
  678. List<MdpAttribute> arrtibutes = mdpClass.getValidateMdpAttributes();
  679. String tableName = mdpClass.getName();
  680. for (int j = 0; j < arrtibutes.size(); j++) {
  681. MdpAttribute MdpAttribute = arrtibutes.get(j);
  682. if (MdpAttribute.isIndex()) {
  683. String fieldName = MdpAttribute.getName();
  684. String indtype = MdpAttribute.getIndexType();
  685. String indName = "idx_" + mdpClass.getClassid() + fieldName;
  686. StringBuffer sb = new StringBuffer();
  687. // sb.append("DROP INDEX "+indName+"");
  688. sb.append("CREATE INDEX " + indName + " ON " + tableName + " (" + fieldName
  689. + " ASC) ");
  690. if ("CLUSTERED".equalsIgnoreCase(indtype)) {
  691. sb.append(" CLUSTER");
  692. }
  693. result.add(sb.toString());
  694. }
  695. }
  696. }
  697. return result;
  698. }
  699. }