ae2877b9ec48713a327091f4156f061b6c68aa96.svn-base 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /***************************************************
  2. * Copyright 2009-8-20, -sun,All rights reserved.
  3. * Create date : 2009-8-20
  4. * Author : sun
  5. * MYSQL 数据库表增删 DLL 操作处理类
  6. **************************************************/
  7. package com.persistence.DBdll.adapter;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.ResultSetMetaData;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Vector;
  17. import org.apache.log4j.Logger;
  18. import com.persistence.DBdll.adapter.assitant.ColumnInfo;
  19. import com.persistence.DBdll.adapter.assitant.ColumnInfoService;
  20. import com.persistence.DBdll.adapter.assitant.TableInfo;
  21. import com.persistence.DBdll.adapter.assitant.index.IndexInfo;
  22. import com.sysmodel.datamodel.Parameter.DataBaseType;
  23. import com.sysmodel.datamodel.Parameter.XmlManagerPara;
  24. import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource;
  25. import com.sysmodel.datamodel.xmlmodel.able.Property;
  26. import com.sysmodel.datamodel.xmlmodel.able.Template;
  27. import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
  28. import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl;
  29. import com.sysmodel.datamodel.xmlmodel.impl.MdpDataSourceImpl;
  30. import com.sysmodel.datamodel.xmlmodel.impl.PropertyImpl;
  31. import com.sysmodel.datamodel.xmlmodel.impl.SysModelImpl;
  32. // TODO: Auto-generated Javadoc
  33. /**
  34. * The Class MysqlAdapter.
  35. */
  36. public class MysqlAdapter extends DatabaseAdapter{
  37. /** The Constant log. */
  38. private final static Logger log = Logger.getLogger(MysqlAdapter.class);
  39. /** The Adapter tool. */
  40. ColumnInfoService columnInfoService = new ColumnInfoService();
  41. /*
  42. * (non-Javadoc)
  43. *
  44. * @see
  45. * com.persistence.DBdll.adapter.DatabaseAdapter#createTable(com.sysmodel
  46. * .datamodel.xmlmodel.impl.MdpClassImpl)
  47. */
  48. public boolean createTable(MdpClassImpl table) {
  49. String sql = generalSQL(table);
  50. log.info(sql);
  51. MdpDataSource MdpDataSource = SysModelImpl.getInstance().getDataSourceByCode(
  52. table.getDataSource());
  53. return this.executeDDL(MdpDataSource, sql);
  54. }
  55. /*
  56. * (non-Javadoc)
  57. *
  58. * @see
  59. * com.persistence.DBdll.adapter.DatabaseAdapter#updateTable(com.sysmodel
  60. * .datamodel.xmlmodel.impl.MdpClassImpl)
  61. */
  62. public ArrayList<Vector<Object>> updateTable(MdpClassImpl table) {
  63. MdpDataSource MdpDataSource = SysModelImpl.getInstance().getDataSourceByCode(
  64. table.getDataSource());
  65. ArrayList<Vector<Object>> list = new ArrayList<Vector<Object>>();
  66. try {
  67. Vector<ColumnInfo> columninfos = getTableColumnInfo(table.getName(), MdpDataSource);
  68. if (columninfos.size() == 0) {
  69. return list;
  70. }
  71. Vector<Object> dllVec = new Vector<Object>();
  72. Vector<MdpAttributeImpl> add = columnInfoService.addDataBaseColumn(
  73. table.getAllMdpAttributes(), columninfos);
  74. for (int i = 0; i < add.size(); i++) {
  75. MdpAttributeImpl imp = add.elementAt(i);
  76. String sql = getTableModifyDllSQL(XmlManagerPara.add, imp, table.getName());
  77. boolean bresult = executeDDL(MdpDataSource, sql);
  78. dllVec = new Vector<Object>();
  79. dllVec.add("add");
  80. dllVec.add(imp);
  81. dllVec.add(bresult);
  82. dllVec.add(sql);
  83. list.add(dllVec);
  84. }
  85. Vector<ColumnInfo> delete = columnInfoService.deleteDataBaseColumn(
  86. table.getAllMdpAttributes(), columninfos);
  87. for (int i = 0; i < delete.size(); i++) {
  88. ColumnInfo imp = delete.elementAt(i);
  89. String sql = "alter table " + table.getName() + " drop column "
  90. + imp.getColumnName();
  91. boolean bresult = executeDDL(MdpDataSource, sql);
  92. dllVec = new Vector<Object>();
  93. dllVec.add("delete");
  94. dllVec.add(imp);
  95. dllVec.add(bresult);
  96. dllVec.add(sql);
  97. list.add(dllVec);
  98. }
  99. Vector<MdpAttributeImpl> update = columnInfoService.updateDataBaseColumn(
  100. table.getAllMdpAttributes(), columninfos);
  101. for (int i = 0; i < update.size(); i++) {
  102. MdpAttributeImpl imp = update.elementAt(i);
  103. String sql = getTableModifyDllSQL(XmlManagerPara.update, imp, table.getName());
  104. boolean bresult = executeDDL(MdpDataSource, sql);
  105. dllVec = new Vector<Object>();
  106. dllVec.add("update");
  107. dllVec.add(imp);
  108. dllVec.add(bresult);
  109. dllVec.add(sql);
  110. list.add(dllVec);
  111. }
  112. ColumnInfo info = columnInfoService
  113. .getColumnInfoByFieldName(columninfos, "fd_objectid");
  114. if (info == null) {
  115. String sql = "alter table " + table.getName() + " add fd_objectid VARCHAR("
  116. + XmlManagerPara.fd_objectidlength + ")";
  117. boolean bresult = executeDDL(MdpDataSource, sql);
  118. dllVec = new Vector<Object>();
  119. dllVec.add("addfd_objectid");
  120. dllVec.add("fd_objectid");
  121. dllVec.add(bresult);
  122. dllVec.add(sql);
  123. list.add(dllVec);
  124. } else {
  125. if (!info.getColumnTypeName().equalsIgnoreCase("VARCHAR")
  126. || info.getPrecision() != XmlManagerPara.fd_objectidlength) {
  127. String sql = "alter table " + table.getName() + " modify fd_objectid VARCHAR("
  128. + XmlManagerPara.fd_objectidlength + ")";
  129. boolean bresult = executeDDL(MdpDataSource, sql);
  130. dllVec = new Vector<Object>();
  131. dllVec.add("updatefd_objectid");
  132. dllVec.add("fd_objectid");
  133. dllVec.add(bresult);
  134. dllVec.add(sql);
  135. list.add(dllVec);
  136. }
  137. }
  138. } catch (Exception e) {
  139. log.error(e.toString());
  140. }
  141. return list;
  142. }
  143. /*
  144. * (non-Javadoc)
  145. *
  146. * @see
  147. * com.persistence.DBdll.adapter.DatabaseAdapter#renameTableName(com.sysmodel
  148. * .datamodel.xmlmodel.impl.MdpClassImpl, java.lang.String)
  149. */
  150. public boolean renameTableName(MdpClassImpl table, String newtablename) {
  151. MdpDataSource MdpDataSource = SysModelImpl.getInstance().getDataSourceByCode(
  152. table.getDataSource());
  153. return renameTableName(MdpDataSource, newtablename, table.getName());
  154. }
  155. /*
  156. * (non-Javadoc)
  157. *
  158. * @see
  159. * com.persistence.DBdll.adapter.DatabaseAdapter#updateTableAttribute(java
  160. * .lang.String, com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl,
  161. * com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl)
  162. */
  163. public boolean updateTableAttribute(String action, MdpAttributeImpl attribute,
  164. MdpClassImpl table) {
  165. MdpDataSource MdpDataSource = SysModelImpl.getInstance().getDataSourceByCode(
  166. table.getDataSource());
  167. String sql = getTableModifyDllSQL(action, attribute, table.getName());
  168. return executeDDL(MdpDataSource, sql);
  169. }
  170. /*
  171. * (non-Javadoc)
  172. *
  173. * @see
  174. * com.persistence.DBdll.adapter.DatabaseAdapter#createConstanTable(com.
  175. * sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl)
  176. */
  177. @Override
  178. public boolean createConstanTable(Template template) {
  179. String sql = getCreateConstanTableDLL(template);
  180. log.info("createsql=" + sql);
  181. SysModelImpl sysmodel = SysModelImpl.getInstance();
  182. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(template.getDataSource());
  183. return this.executeDDL(MdpDataSource, sql);
  184. }
  185. /*
  186. * (non-Javadoc)
  187. *
  188. * @see
  189. * com.persistence.DBdll.adapter.DatabaseAdapter#updateConstanTable(com.
  190. * sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl)
  191. */
  192. @Override
  193. public ArrayList<Vector<Object>> updateConstanTable(Template template) {
  194. String tableName = template.getTableName();
  195. MdpDataSource MdpDataSource = SysModelImpl.getInstance().getDataSourceByCode(
  196. template.getDataSource());
  197. ArrayList<Vector<Object>> list = new ArrayList<Vector<Object>>();
  198. try {
  199. Vector<ColumnInfo> columninfos = getTableColumnInfo(tableName, MdpDataSource);
  200. if (columninfos.size() == 0) {
  201. return list;
  202. }
  203. List<PropertyImpl> Propertys = template.getListproperty();
  204. Vector<Object> dllVec = new Vector<Object>();
  205. // delete column
  206. Vector<ColumnInfo> delete = columnInfoService.deleteConstantDataBaseColumn(Propertys,
  207. columninfos);
  208. for (int i = 0; i < delete.size(); i++) {
  209. ColumnInfo imp = delete.elementAt(i);
  210. String sql = "alter table " + tableName + " drop column " + imp.getColumnName();
  211. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  212. dllVec = new Vector<Object>();
  213. dllVec.add("delete");
  214. dllVec.add(imp);
  215. dllVec.add(bresult);
  216. dllVec.add(sql);
  217. list.add(dllVec);
  218. }
  219. Vector<Property> add = columnInfoService.addConstantDataBaseColumn(Propertys,
  220. columninfos);
  221. for (int i = 0; i < add.size(); i++) {
  222. Property imp = (Property) add.elementAt(i);
  223. String sql = getCanstantModifyDllSQL(XmlManagerPara.add, imp, tableName);
  224. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  225. dllVec = new Vector<Object>();
  226. dllVec.add("add");
  227. dllVec.add(imp);
  228. dllVec.add(bresult);
  229. dllVec.add(sql);
  230. list.add(dllVec);
  231. }
  232. Vector<Property> update = columnInfoService.updateConstantDataBaseColumn(Propertys,
  233. columninfos);
  234. for (int i = 0; i < update.size(); i++) {
  235. Property imp = (Property) update.elementAt(i);
  236. String sql = getCanstantModifyDllSQL(XmlManagerPara.update, imp, tableName);
  237. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  238. dllVec = new Vector<Object>();
  239. dllVec.add("update");
  240. dllVec.add(imp);
  241. dllVec.add(bresult);
  242. dllVec.add(sql);
  243. list.add(dllVec);
  244. }
  245. } catch (Exception e) {
  246. log.error(e.toString());
  247. }
  248. return list;
  249. }
  250. @Override
  251. public boolean renameConstantTableName(Template template, String newtablename) {
  252. MdpDataSource MdpDataSource = SysModelImpl.getInstance().getDataSourceByCode(
  253. template.getDataSource());
  254. return renameTableName(MdpDataSource, newtablename, template.getTableName());
  255. }
  256. /**
  257. * Rename table name.
  258. *
  259. * @param MdpDataSource
  260. * the mdp data source
  261. * @param newtablename
  262. * the newtablename
  263. * @param oldtablename
  264. * the oldtablename
  265. *
  266. * @return true, if successful
  267. *
  268. * @author sun
  269. * @version 2009-8-20-10:28:52
  270. *
  271. * Rename table name.
  272. */
  273. private boolean renameTableName(MdpDataSource MdpDataSource, String newtablename,
  274. String oldtablename) {
  275. String sql = "sp_rename " + oldtablename + ", " + newtablename;
  276. log.info("sql = " + sql);
  277. return executeDDL(MdpDataSource, sql);
  278. }
  279. public String getCreateConstanTableDLL(Template template) {
  280. StringBuffer sql = new StringBuffer();
  281. sql.append("create table " + template.getTableName()).append("( ");
  282. ArrayList<PropertyImpl> propertys = (ArrayList<PropertyImpl>) template.getListproperty();
  283. for (int i = 0; i < propertys.size(); i++) {
  284. PropertyImpl PropertyImpl = (PropertyImpl) propertys.get(i);
  285. String fieldname = PropertyImpl.getName();
  286. String datatype = PropertyImpl.getDataType();
  287. if (datatype.equals(XmlManagerPara.date)) {
  288. sql.append(",").append(fieldname).append(" DATETIME");
  289. } else if (datatype.equals(XmlManagerPara.clob)) {
  290. sql.append(",").append(fieldname).append(" LONGTEXT");
  291. } else if (datatype.equals(XmlManagerPara.blob)) {
  292. sql.append(",").append(fieldname).append(" LONGBLOB");
  293. } else if (datatype.equals(XmlManagerPara.bool)) {
  294. sql.append(",").append(fieldname).append(" BIT");
  295. } else if (datatype.equals(XmlManagerPara.image)) {
  296. sql.append(",").append(fieldname).append(" VARCHAR(100)");
  297. } else if (datatype.equals(XmlManagerPara.number)) {
  298. String scale = PropertyImpl.getScale().equals("") ? "" : ","
  299. + PropertyImpl.getScale();
  300. if (scale.equals("")) {
  301. sql.append("," + fieldname + " float");
  302. } else {
  303. int length = PropertyImpl.getLength() == 0 ? 10 : PropertyImpl.getLength();
  304. sql.append("," + fieldname + " DECIMAL(" + length + scale + ")");
  305. }
  306. } else if (datatype.equals(XmlManagerPara.string)) {
  307. sql.append(",").append(fieldname);
  308. sql.append(" VARCHAR(").append(PropertyImpl.getLength()).append(")");
  309. }
  310. if (PropertyImpl.isUnique()) {
  311. sql.append(" NOT NULL primary key");
  312. }
  313. }
  314. sql.append(" ) ");
  315. sql.reverse();
  316. sql.deleteCharAt(sql.lastIndexOf(","));
  317. sql.reverse();
  318. return sql.toString();
  319. }
  320. /**
  321. * General sql.
  322. *
  323. * @param table
  324. * the table
  325. *
  326. * @return the string
  327. *
  328. * @author sun
  329. * @version 2009-8-20-10:28:52
  330. *
  331. * General sql.
  332. */
  333. private String generalSQL(MdpClassImpl table) {
  334. StringBuffer sql = new StringBuffer();
  335. sql.append("create table " + table.getName());
  336. sql.append("(FD_OBJECTID VARCHAR(" + XmlManagerPara.fd_objectidlength
  337. + ") NOT NULL primary key ");
  338. ArrayList<MdpAttributeImpl> list = (ArrayList<MdpAttributeImpl>) table
  339. .getAllMdpAttributes();
  340. for (int i = 0; i < list.size(); i++) {
  341. MdpAttributeImpl attribute = list.get(i);
  342. String fieldname = attribute.getName();
  343. String datatype = attribute.getDataType();
  344. if (datatype.equals(XmlManagerPara.date)) {
  345. sql.append("," + fieldname + " DATETIME");
  346. } else if (datatype.equals(XmlManagerPara.clob)) {
  347. sql.append("," + fieldname + " LONGTEXT");
  348. } else if (datatype.equals(XmlManagerPara.blob)) {
  349. sql.append("," + fieldname + " LONGBLOB");
  350. } else if (datatype.equals(XmlManagerPara.bool)) {
  351. sql.append("," + fieldname + " BIT(" + attribute.getPrecision() + ")");
  352. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  353. sql.append(" default " + attribute.getDefaultValue());
  354. }
  355. } else if (datatype.equals(XmlManagerPara.image)) {
  356. sql.append("," + fieldname + " VARCHAR(100)");
  357. } else if (datatype.equals(XmlManagerPara.number)) {
  358. int length = attribute.getPrecision() == 0 ? 10 : attribute.getPrecision();
  359. String scale = attribute.getScale().equals("") ? "" : "," + attribute.getScale();
  360. if (scale.equals("")) {
  361. sql.append("," + fieldname + " INTEGER(" + length + ")");
  362. } else {
  363. sql.append("," + fieldname + " DECIMAL(" + length + scale + ")");
  364. }
  365. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  366. sql.append(" default " + attribute.getDefaultValue());
  367. }
  368. } else if (datatype.equals(XmlManagerPara.string)) {
  369. sql.append("," + fieldname + " VARCHAR(" + attribute.getPrecision() + ")");
  370. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  371. sql.append(" default '" + attribute.getDefaultValue() + "'");
  372. }
  373. }
  374. }
  375. sql.append(" ) ");
  376. return sql.toString();
  377. }
  378. /**
  379. * 获得数据库中指定表中列的信息
  380. *
  381. * @param tablename
  382. * @param con
  383. * @return
  384. */
  385. public Vector<ColumnInfo> getTableColumnInfo(String tablename, MdpDataSource datasource) {
  386. Vector<ColumnInfo> vect = new Vector<ColumnInfo>();
  387. Connection connection = getConnection(datasource);
  388. if (connection == null)
  389. return vect;
  390. Statement st = null;
  391. ResultSet rs = null;
  392. DatabaseTableInfo TableInfo = new DatabaseTableInfo();
  393. try {
  394. HashMap<String, String> MapComments = TableInfo.getColComments(connection, tablename,
  395. DataBaseType.MYSQL);
  396. st = connection.createStatement();
  397. rs = st.executeQuery("select * from " + tablename + " where 1=0");
  398. ResultSetMetaData rm = rs.getMetaData();
  399. int colCount = rm.getColumnCount();// 取得列数
  400. ColumnInfo vo = null;
  401. for (int i = 1; i <= colCount; i++) {
  402. vo = new ColumnInfo();
  403. String ColumnName = rm.getColumnName(i);
  404. vo.setDataBaseType(DataBaseType.MYSQL);
  405. vo.setTableName(tablename);
  406. vo.setColumnName(rm.getColumnName(i));
  407. vo.setColumnType(rm.getColumnType(i));
  408. vo.setColumnTypeName(rm.getColumnTypeName(i));
  409. vo.setPrecision(rm.getPrecision(i));
  410. vo.setScale(rm.getScale(i));
  411. vo.setDescritpion(MapComments.get(ColumnName));
  412. vo.setAutoIncrement(rm.isAutoIncrement(i));
  413. vo.setIsNullable(rm.isNullable(i));
  414. vo.setReadOnly(rm.isReadOnly(i));
  415. vo.setSigned(rm.isSigned(i));
  416. vo.setSearchable(rm.isSearchable(i));
  417. vo.setColumnDisplaySize(rm.getColumnDisplaySize(i));
  418. vect.add(vo);
  419. }
  420. rs.close();
  421. st.close();
  422. } catch (Exception e) {
  423. e.printStackTrace();
  424. } finally {
  425. try {
  426. if (rs != null)
  427. rs.close();
  428. if (st != null)
  429. st.close();
  430. if (connection != null)
  431. connection.close();
  432. } catch (SQLException e) {
  433. e.printStackTrace();
  434. }
  435. }
  436. return vect;
  437. }
  438. /**
  439. * 获得常量表需要修改的列的DLL sql语句 Gets the canstant modify dll sql.
  440. *
  441. * @param action
  442. * the action
  443. * @param imp
  444. * the imp
  445. * @param tablename
  446. * the tablename
  447. *
  448. * @return the canstant modify dll sql
  449. *
  450. * @author sun
  451. * @version 2009-8-20-10:28:52
  452. *
  453. * Gets the canstant modify dll sql.
  454. */
  455. private String getCanstantModifyDllSQL(String action, Property imp, String tablename) {
  456. String sql = "alter table " + tablename;
  457. if (action.equals(XmlManagerPara.add)) {
  458. sql = sql + " add ";
  459. } else if (action.equals(XmlManagerPara.delete)) {
  460. sql = sql + " drop column " + imp.getName();
  461. return sql;
  462. } else if (action.equals(XmlManagerPara.update)) {
  463. sql = "sp_rename " + tablename + "." + imp.getName() + ", ";
  464. }
  465. String fieldname = imp.getName();
  466. String datatype = imp.getDataType();
  467. if (datatype.equals(XmlManagerPara.date)) {
  468. sql = sql + fieldname + " DATETIME";
  469. } else if (datatype.equals(XmlManagerPara.bool)) {
  470. sql = sql + fieldname + " BIT(" + imp.getLength() + ")";
  471. sql = sql + " default null";
  472. }
  473. else if (datatype.equals(XmlManagerPara.number)) {
  474. int length = imp.getLength() == 0 ? 10 : imp.getLength();
  475. String scale = imp.getScale().equals("") ? "" : "," + imp.getScale();
  476. sql = sql + fieldname + " DECIMAL(" + length + scale + ")";
  477. sql = sql + " default null";
  478. } else if (datatype.equals(XmlManagerPara.string)) {
  479. sql = sql + fieldname + " varchar(" + imp.getLength() + ")";
  480. sql = sql + " default null";
  481. }
  482. log.info("sql = " + sql);
  483. return sql;
  484. }
  485. /**
  486. * Gets the table modify dll sql.
  487. *
  488. * @param action
  489. * the action
  490. * @param attribute
  491. * the attribute
  492. * @param tablename
  493. * the tablename
  494. *
  495. * @return the table modify dll sql
  496. *
  497. * @author sun
  498. * @version 2009-8-20-10:28:52
  499. *
  500. * Gets the table modify dll sql.
  501. */
  502. private String getTableModifyDllSQL(String action, MdpAttributeImpl attribute, String tablename) {
  503. String sql = "alter table " + tablename;
  504. if (action.equals(XmlManagerPara.add)) {
  505. sql = sql + " add ";
  506. } else if (action.equals(XmlManagerPara.update)) {
  507. sql = sql + " modify ";
  508. } else if (action.equals(XmlManagerPara.delete)) {
  509. sql = sql + " drop column " + attribute.getName();
  510. return sql;
  511. }
  512. String fieldname = attribute.getName();
  513. String datatype = attribute.getDataType();
  514. if (datatype.equals(XmlManagerPara.date)) {
  515. sql = sql + fieldname + " DATETIME";
  516. } else if (datatype.equals("blob")) {
  517. sql = sql + fieldname + " LONGBLOB";
  518. }
  519. else if (datatype.equals("clob")) {
  520. sql = sql + fieldname + " LONGTEXT ";
  521. }
  522. else if (datatype.equals(XmlManagerPara.bool)) {
  523. sql = sql + fieldname + " BIT(" + attribute.getPrecision() + ")";
  524. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  525. sql = sql + " default " + attribute.getDefaultValue();
  526. } else {
  527. sql = sql + " default null";
  528. }
  529. }
  530. else if (datatype.equals(XmlManagerPara.number)) {
  531. int length = attribute.getPrecision() == 0 ? 10 : attribute.getPrecision();
  532. String scale = attribute.getScale().equals("") ? "" : "," + attribute.getScale();
  533. sql = sql + fieldname + " DECIMAL(" + length + scale + ")";
  534. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  535. sql = sql + " default " + attribute.getDefaultValue();
  536. } else {
  537. sql = sql + " default null";
  538. }
  539. }
  540. else if (datatype.equals(XmlManagerPara.string)) {
  541. sql = sql + fieldname;
  542. if (attribute.getPrecision() < 50) {
  543. sql = sql + " VARCHAR(" + attribute.getPrecision() + ")";
  544. } else if (attribute.getPrecision() <= 2000 && attribute.getPrecision() >= 50) {
  545. sql = sql + " VARCHAR(" + attribute.getPrecision() + ")";
  546. } else {
  547. sql = sql + " VARCHAR(" + attribute.getPrecision() + ")";
  548. }
  549. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  550. sql = sql + " default '" + attribute.getDefaultValue() + "'";
  551. } else {
  552. sql = sql + " default null";
  553. }
  554. }
  555. log.info("sql = " + sql);
  556. return sql;
  557. }
  558. /*
  559. * (non-Javadoc)
  560. *
  561. * @see
  562. * com.persistence.DBdll.adapter.DatabaseAdapter#getXmlByTableName(java.
  563. * lang.String, com.sysmodel.datamodel.xmlmodel.able.MdpDataSource,
  564. * java.lang.String)
  565. */
  566. @Override
  567. public String getXmlByTableName(String tableName, MdpDataSource dataSource, String moduleId) {
  568. DatabaseTableInfo tableInfo = new DatabaseTableInfo();
  569. TableInfo tableVo = tableInfo.getTableInfo(tableName, dataSource, DataBaseType.MYSQL);
  570. Vector<ColumnInfo> ColumnInfoVec = this.getTableColumnInfo(tableName, dataSource);
  571. MdpDataSourceImpl dataSourceImpl = (MdpDataSourceImpl) dataSource;
  572. StringBuffer sb = new StringBuffer();
  573. sb.append(" \n <font color='#339900'>.......................................<br> \n");
  574. sb.append("&lt;mdpClass classid=\"请替换成推荐号\" ");
  575. sb.append("name=\"" + tableName + "\" description=\"" + tableVo.getDescritpion()
  576. + "\" type=\"" + moduleId + "\" ");
  577. sb.append("primaryKey=\"" + tableVo.getPrimaryKey()
  578. + "\" exist=\"true\" catche=\"false\" validate=\"true\" ");
  579. sb.append("dataSource=\"" + dataSourceImpl.getDataSourceid() + "\" ");
  580. sb.append("><br> \n");
  581. int ColumnInfoVecSize = ColumnInfoVec.size();
  582. ColumnInfo ColumnInfo = new ColumnInfo();
  583. for (int i = 0; i < ColumnInfoVecSize; i++) {
  584. ColumnInfo = ColumnInfoVec.get(i);
  585. sb.append("&nbsp;&nbsp;&lt;mdpAttribute ");
  586. sb.append(" name=\"" + ColumnInfo.getColumnName() + "\" description=\""
  587. + ColumnInfo.getDescritpion() + "\"");
  588. sb.append(ColumnInfo.getColumTypeDetail());
  589. sb.append(" unit=\"\" logicPrimaryKey=\"false\" referenceType=\"0\" ");
  590. sb.append(" index=\"false\" indexType=\"\" defaultValue=\"\" notNull=\"false\" validate=\"true\" ");
  591. sb.append(" isAutoIncrement=\"false\" isReadOnly=\"false\" isSearchable=\"true\" isSigned=\"false\" ");
  592. sb.append(" columnDisplaySize=\"" + ColumnInfo.getColumnDisplaySize()
  593. + "\" fieldType=\"" + ColumnInfo.getColumnTypeName() + "("
  594. + ColumnInfo.getPrecision() + ")\" ");
  595. sb.append(" /&gt;");
  596. sb.append("<br> \n");
  597. }
  598. sb.append("&lt;/mdpClass&gt;<br>");
  599. sb.append("........................................<br></font>");
  600. ColumnInfoVec.clear();
  601. return sb.toString();
  602. }
  603. /*
  604. * (non-Javadoc)
  605. *
  606. * @see
  607. * com.persistence.DBdll.adapter.DatabaseAdapter#createConstanTableComment
  608. * (com.sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl, java.lang.String)
  609. */
  610. @Override
  611. public StringBuffer createConstanTableComment(Template template, String type) {
  612. return null;
  613. }
  614. /*
  615. * (non-Javadoc)
  616. *
  617. * @see
  618. * com.persistence.DBdll.adapter.DatabaseAdapter#createOldTableComment(com
  619. * .sysmodel.datamodel.xmlmodel.impl.MdpClassImpl, java.lang.String)
  620. */
  621. @Override
  622. public StringBuffer createOldTableComment(MdpClassImpl table, String type) {
  623. // TODO Auto-generated method stub
  624. return null;
  625. }
  626. /*
  627. * (non-Javadoc)
  628. *
  629. * @see
  630. * com.persistence.DBdll.adapter.DatabaseAdapter#createTableComment(com.
  631. * sysmodel.datamodel.xmlmodel.impl.MdpClassImpl, java.lang.String)
  632. */
  633. @Override
  634. public StringBuffer createTableComment(MdpClassImpl table, String type) {
  635. // TODO Auto-generated method stub
  636. return null;
  637. }
  638. @Override
  639. public ArrayList<String> getTableCreateKeyDll(int iDataSource) {
  640. // TODO Auto-generated method stub
  641. return null;
  642. }
  643. @Override
  644. public ArrayList<IndexInfo> getTableIndexInfo() {
  645. // TODO Auto-generated method stub
  646. return null;
  647. }
  648. @Override
  649. public ArrayList<String> getTableIndexCreateDll() {
  650. // TODO Auto-generated method stub
  651. return null;
  652. }
  653. }
  654. /*
  655. * //mysql : select TABLE_NAME from INFORMATION_SCHEMA.TABLES where
  656. * TABLE_SCHEMA='"+DbName+"'; //mysql : select TABLE_NAME from
  657. * INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='"+DbName+"';
  658. *//**
  659. * 获得Mysql数据库名称 Gets the mysql db name.
  660. *
  661. * @param dataSource
  662. * the data source
  663. *
  664. * @return the mysql db name
  665. */
  666. /*
  667. * private String getMysqlDbName(MdpDataSource dataSource){ String Url =
  668. * dataSource.getURL(); String str = Url.substring(0,Url.indexOf('?')); str =
  669. * str.substring(str.lastIndexOf('/') + 1); return str; }
  670. */