7490097035bd0bc53a4b53c3ab6f2a2991b727e3.svn-base 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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.XmlManagerPara;
  22. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  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.SysModel;
  29. import com.sysmodel.datamodel.xmlmodel.able.Template;
  30. import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
  31. import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl;
  32. import com.sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl;
  33. import com.sysmodel.datamodel.xmlmodel.impl.MdpDataSourceImpl;
  34. import com.sysmodel.datamodel.xmlmodel.impl.PropertyImpl;
  35. import com.sysmodel.datamodel.xmlmodel.impl.SysModelImpl;
  36. public class SQLserverAdapter extends DatabaseAdapter{
  37. private final static Logger log = Logger.getLogger(SQLserverAdapter.class);
  38. private ColumnInfoService columnInfoService = new ColumnInfoService();
  39. private SysModelImpl sysmodel = SysModelImpl.getInstance();
  40. public boolean createTable(MdpClassImpl table) {
  41. String sql = generalSQL(table);
  42. log.info(sql);
  43. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(table.getDataSource());
  44. return executeDDL(MdpDataSource, sql);
  45. }
  46. public ArrayList<Vector<Object>> updateTable(MdpClassImpl table) {
  47. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(table.getDataSource());
  48. ArrayList<Vector<Object>> list = new ArrayList<Vector<Object>>();
  49. try {
  50. Vector<ColumnInfo> columninfos = getTableColumnInfo(table.getName(), MdpDataSource);
  51. if (columninfos.size() == 0) {
  52. return list;
  53. }
  54. Vector<Object> dllVec = new Vector<Object>();
  55. Vector<MdpAttributeImpl> add = columnInfoService.addDataBaseColumn(
  56. table.getAllMdpAttributes(), columninfos);
  57. for (int i = 0; i < add.size(); i++) {
  58. MdpAttributeImpl imp = add.elementAt(i);
  59. String sql = getTableModifyDllSQL(XmlManagerPara.add, imp, table.getName());
  60. boolean bresult = executeDDL(MdpDataSource, sql);
  61. dllVec = new Vector<Object>();
  62. dllVec.add("add");
  63. dllVec.add(imp);
  64. dllVec.add(bresult);
  65. dllVec.add(sql);
  66. list.add(dllVec);
  67. }
  68. Vector<ColumnInfo> delete = columnInfoService.deleteDataBaseColumn(
  69. table.getAllMdpAttributes(), columninfos);
  70. for (int i = 0; i < delete.size(); i++) {
  71. ColumnInfo imp = delete.elementAt(i);
  72. String sql = "alter table " + table.getName() + " drop column "
  73. + imp.getColumnName();
  74. boolean bresult = executeDDL(MdpDataSource, sql);
  75. dllVec = new Vector<Object>();
  76. dllVec.add("delete");
  77. dllVec.add(imp);
  78. dllVec.add(bresult);
  79. dllVec.add(sql);
  80. list.add(dllVec);
  81. }
  82. Vector<MdpAttributeImpl> update = columnInfoService.updateDataBaseColumn(
  83. table.getAllMdpAttributes(), columninfos);
  84. for (int i = 0; i < update.size(); i++) {
  85. MdpAttributeImpl imp = update.elementAt(i);
  86. String sql = getTableModifyDllSQL(XmlManagerPara.update, imp, table.getName());
  87. boolean bresult = executeDDL(MdpDataSource, sql);
  88. dllVec = new Vector<Object>();
  89. dllVec.add("update");
  90. dllVec.add(imp);
  91. dllVec.add(bresult);
  92. dllVec.add(sql);
  93. list.add(dllVec);
  94. }
  95. ColumnInfo info = columnInfoService
  96. .getColumnInfoByFieldName(columninfos, "fd_objectid");
  97. if (info == null) {
  98. String sql = "alter table " + table.getName() + " add fd_objectid varchar("
  99. + XmlManagerPara.fd_objectidlength + ")";
  100. boolean bresult = executeDDL(MdpDataSource, sql);
  101. dllVec = new Vector<Object>();
  102. dllVec.add("addfd_objectid");
  103. dllVec.add("fd_objectid");
  104. dllVec.add(bresult);
  105. dllVec.add(sql);
  106. list.add(dllVec);
  107. } else {
  108. if (!info.getColumnTypeName().equalsIgnoreCase("varchar")
  109. || info.getPrecision() != XmlManagerPara.fd_objectidlength) {
  110. String sql = "alter table " + table.getName() + " modify fd_objectid varchar("
  111. + XmlManagerPara.fd_objectidlength + ")";
  112. boolean bresult = executeDDL(MdpDataSource, sql);
  113. dllVec = new Vector<Object>();
  114. dllVec.add("updatefd_objectid");
  115. dllVec.add("fd_objectid");
  116. dllVec.add(bresult);
  117. dllVec.add(sql);
  118. list.add(dllVec);
  119. }
  120. }
  121. } catch (Exception e) {
  122. log.error(e.toString());
  123. }
  124. return list;
  125. }
  126. public boolean renameTableName(MdpClassImpl table, String newtablename) {
  127. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(table.getDataSource());
  128. return renameTableName(MdpDataSource, newtablename, table.getName());
  129. }
  130. public boolean updateTableAttribute(String action, MdpAttributeImpl attribute,
  131. MdpClassImpl table) {
  132. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(table.getDataSource());
  133. String sql = getTableModifyDllSQL(action, attribute, table.getName());
  134. return executeDDL(MdpDataSource, sql);
  135. }
  136. @Override
  137. public boolean createConstanTable(Template template) {
  138. String sql = getCreateConstanTableDLL(template);
  139. log.info("createsql=" + sql);
  140. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(template.getDataSource());
  141. return this.executeDDL(MdpDataSource, sql);
  142. }
  143. @Override
  144. public ArrayList<Vector<Object>> updateConstanTable(Template template) {
  145. String tableName = template.getTableName();
  146. MdpDataSource MdpDataSource = sysmodel.getDataSourceByCode(template.getDataSource());
  147. ArrayList<Vector<Object>> list = new ArrayList<Vector<Object>>();
  148. try {
  149. Vector<ColumnInfo> columninfos = getTableColumnInfo(tableName, MdpDataSource);
  150. if (columninfos.size() == 0) {
  151. return list;
  152. }
  153. List<PropertyImpl> Propertys = template.getListproperty();
  154. Vector<Object> dllVec = new Vector<Object>();
  155. // delete column
  156. Vector<ColumnInfo> delete = columnInfoService.deleteConstantDataBaseColumn(Propertys,
  157. columninfos);
  158. for (int i = 0; i < delete.size(); i++) {
  159. ColumnInfo imp = delete.elementAt(i);
  160. String sql = "alter table " + tableName + " drop column " + imp.getColumnName();
  161. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  162. dllVec = new Vector<Object>();
  163. dllVec.add("delete");
  164. dllVec.add(imp);
  165. dllVec.add(bresult);
  166. dllVec.add(sql);
  167. list.add(dllVec);
  168. }
  169. Vector<Property> add = columnInfoService.addConstantDataBaseColumn(Propertys,
  170. columninfos);
  171. for (int i = 0; i < add.size(); i++) {
  172. Property imp = add.elementAt(i);
  173. String sql = getCanstantModifyDllSQL(XmlManagerPara.add, imp, tableName);
  174. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  175. dllVec = new Vector<Object>();
  176. dllVec.add("add");
  177. dllVec.add(imp);
  178. dllVec.add(bresult);
  179. dllVec.add(sql);
  180. list.add(dllVec);
  181. }
  182. Vector<Property> update = columnInfoService.updateConstantDataBaseColumn(Propertys,
  183. columninfos);
  184. for (int i = 0; i < update.size(); i++) {
  185. Property imp = update.elementAt(i);
  186. String sql = getCanstantModifyDllSQL(XmlManagerPara.update, imp, tableName);
  187. boolean bresult = false;// executeDDL(MdpDataSource ,sql);
  188. dllVec = new Vector<Object>();
  189. dllVec.add("update");
  190. dllVec.add(imp);
  191. dllVec.add(bresult);
  192. dllVec.add(sql);
  193. list.add(dllVec);
  194. }
  195. } catch (Exception e) {
  196. log.error(e.toString());
  197. }
  198. return list;
  199. }
  200. public String getCreateConstanTableDLL(Template template) {
  201. StringBuffer sql = new StringBuffer();
  202. sql.append("create table " + template.getTableName()).append("( ");
  203. ArrayList<PropertyImpl> propertys = (ArrayList<PropertyImpl>) template.getListproperty();
  204. for (int i = 0; i < propertys.size(); i++) {
  205. PropertyImpl PropertyImpl = propertys.get(i);
  206. String fieldname = PropertyImpl.getName();
  207. String datatype = PropertyImpl.getDataType();
  208. if (datatype.equals(XmlManagerPara.date)) {
  209. sql.append(",").append(fieldname).append(" datetime");
  210. } else if (datatype.equals(XmlManagerPara.clob)) {
  211. sql.append(",").append(fieldname).append(" text");
  212. } else if (datatype.equals(XmlManagerPara.blob)) {
  213. sql.append(",").append(fieldname).append(" image");
  214. } else if (datatype.equals(XmlManagerPara.bool)) {
  215. sql.append(",").append(fieldname).append(" bit");
  216. } else if (datatype.equals(XmlManagerPara.image)) {
  217. sql.append(",").append(fieldname).append(" varchar(100)");
  218. } else if (datatype.equals(XmlManagerPara.number)) {
  219. String scale = PropertyImpl.getScale().equals("") ? "" : ","
  220. + PropertyImpl.getScale();
  221. if (scale.equals("")) {
  222. sql.append("," + fieldname + " float");
  223. } else {
  224. int length = PropertyImpl.getLength() == 0 ? 10 : PropertyImpl.getLength();
  225. sql.append("," + fieldname + " numeric(" + length + scale + ")");
  226. }
  227. } else if (datatype.equals(XmlManagerPara.string)) {
  228. sql.append(",").append(fieldname);
  229. sql.append(" varchar(").append(PropertyImpl.getLength()).append(")");
  230. }
  231. if (PropertyImpl.isUnique()) {
  232. sql.append(" NOT NULL primary key NONCLUSTERED ");
  233. }
  234. }
  235. sql.append(" ) ");
  236. sql.reverse();
  237. sql.deleteCharAt(sql.lastIndexOf(","));
  238. sql.reverse();
  239. return sql.toString();
  240. }
  241. @Override
  242. public boolean renameConstantTableName(Template template, String newtablename) {
  243. MdpDataSource MdpDataSource = SysModelImpl.getInstance().getDataSourceByCode(
  244. template.getDataSource());
  245. return renameTableName(MdpDataSource, newtablename, template.getTableName());
  246. }
  247. private boolean renameTableName(MdpDataSource MdpDataSource, String newtablename,
  248. String oldtablename) {
  249. String sql = "sp_rename " + oldtablename + ", " + newtablename;
  250. log.info("sql = " + sql);
  251. return executeDDL(MdpDataSource, sql);
  252. }
  253. private String generalSQL(MdpClassImpl table) {
  254. StringBuffer sql = new StringBuffer();
  255. sql.append("create table " + table.getName()).append(
  256. "(FD_OBJECTID varchar(" + XmlManagerPara.fd_objectidlength
  257. + ") NOT NULL primary key NONCLUSTERED ");
  258. ArrayList<MdpAttributeImpl> list = (ArrayList<MdpAttributeImpl>) table
  259. .getAllMdpAttributes();
  260. for (int i = 0; i < list.size(); i++) {
  261. MdpAttributeImpl attribute = list.get(i);
  262. String fieldname = attribute.getName();
  263. String datatype = attribute.getDataType();
  264. if (datatype.equals(XmlManagerPara.date)) {
  265. sql.append("," + fieldname + " datetime");
  266. } else if (datatype.equals(XmlManagerPara.clob)) {
  267. sql.append("," + fieldname + " text");
  268. } else if (datatype.equals(XmlManagerPara.blob)) {
  269. sql.append("," + fieldname + " image");
  270. } else if (datatype.equals(XmlManagerPara.bool)) {
  271. sql.append("," + fieldname + " bit");
  272. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  273. sql.append(" default " + attribute.getDefaultValue());
  274. }
  275. } else if (datatype.equals(XmlManagerPara.image)) {
  276. sql.append("," + fieldname + " varchar(100)");
  277. } else if (datatype.equals(XmlManagerPara.number)) {
  278. int length = attribute.getPrecision() == 0 ? 10 : attribute.getPrecision();
  279. String scale = attribute.getScale().equals("") ? "" : "," + attribute.getScale();
  280. if (scale.equals("")) {
  281. sql.append("," + fieldname + " float");
  282. } else {
  283. sql.append("," + fieldname + " numeric(" + length + scale + ")");
  284. }
  285. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  286. sql.append(" default " + attribute.getDefaultValue());
  287. }
  288. } else if (datatype.equals(XmlManagerPara.string)) {
  289. sql.append("," + fieldname + " varchar(" + attribute.getPrecision() + ")");
  290. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  291. sql.append(" default '" + attribute.getDefaultValue() + "'");
  292. }
  293. }
  294. }
  295. sql.append(" ) ");
  296. return sql.toString();
  297. }
  298. // ==============================================================================================
  299. /**
  300. * 获得数据库中指定表中列的信息
  301. *
  302. * @param tablename
  303. * @param con
  304. * @return
  305. */
  306. public Vector<ColumnInfo> getTableColumnInfo(String tableName, MdpDataSource datasource) {
  307. Vector<ColumnInfo> vect = new Vector<ColumnInfo>();
  308. Connection connection = getConnection(datasource);
  309. if (connection == null)
  310. return vect;
  311. Statement st = null;
  312. ResultSet rs = null;
  313. try {
  314. DatabaseTableInfo tableInfo = new DatabaseTableInfo();
  315. HashMap<String, String> MapComments = tableInfo.getColComments(connection, tableName,
  316. DataBaseType.SQLSERVER);
  317. st = connection.createStatement();
  318. rs = st.executeQuery("select * from " + tableName + " where 1=0");
  319. ResultSetMetaData rm = rs.getMetaData();
  320. int colCount = rm.getColumnCount();
  321. ColumnInfo vo = null;
  322. for (int i = 1; i <= colCount; i++) {
  323. vo = new ColumnInfo();
  324. String ColumnName = rm.getColumnName(i);
  325. vo.setDataBaseType(DataBaseType.SQLSERVER);
  326. vo.setTableName(tableName);
  327. vo.setColumnName(rm.getColumnName(i));
  328. vo.setColumnType(rm.getColumnType(i));
  329. vo.setColumnTypeName(rm.getColumnTypeName(i));
  330. vo.setPrecision(rm.getPrecision(i));
  331. vo.setScale(rm.getScale(i));
  332. vo.setDescritpion(MapComments.get(ColumnName));
  333. vo.setAutoIncrement(rm.isAutoIncrement(i));
  334. vo.setIsNullable(rm.isNullable(i));
  335. vo.setReadOnly(rm.isReadOnly(i));
  336. vo.setSigned(rm.isSigned(i));
  337. vo.setSearchable(rm.isSearchable(i));
  338. vo.setColumnDisplaySize(rm.getColumnDisplaySize(i));
  339. vect.add(vo);
  340. }
  341. MapComments.clear();
  342. } catch (Exception e) {
  343. log.error("SQLServerException-- ", e);
  344. } finally {
  345. try {
  346. if (rs != null)
  347. rs.close();
  348. if (st != null)
  349. st.close();
  350. if (connection != null)
  351. connection.close();
  352. } catch (SQLException e) {
  353. log.error("SQLServerException-- ", e);
  354. }
  355. }
  356. return vect;
  357. }
  358. private String getCanstantModifyDllSQL(String action, Property imp, String tablename) {
  359. String sql = "alter table " + tablename;
  360. String fieldname = imp.getName();
  361. String datatype = imp.getDataType();
  362. if (action.equals(XmlManagerPara.add)) {
  363. sql = sql + " add ";
  364. } else if (action.equals(XmlManagerPara.delete)) {
  365. sql = sql + " drop column " + imp.getName();
  366. return sql;
  367. } else if (action.equals(XmlManagerPara.update)) {
  368. // alter table HM_Plan alter column Conservation_Unit varchar(15);
  369. sql = "alter table " + tablename + " alter column ";
  370. }
  371. if (datatype.equals(XmlManagerPara.date)) {
  372. sql = sql + fieldname + " datetime";
  373. } else if (datatype.equals(XmlManagerPara.bool)) {
  374. sql = sql + fieldname + " bit";
  375. // sql = sql + " default null" ;
  376. }
  377. else if (datatype.equals(XmlManagerPara.number)) {
  378. int length = imp.getLength() == 0 ? 10 : imp.getLength();
  379. String scale = imp.getScale().equals("") ? "" : "," + imp.getScale();
  380. sql = sql + fieldname + " numeric(" + length + scale + ")";
  381. // sql = sql + " default null" ;
  382. } else if (datatype.equals(XmlManagerPara.string)) {
  383. sql = sql + fieldname + " varchar(" + imp.getLength() + ")";
  384. // sql = sql + " default null" ;
  385. }
  386. log.info("sql = " + sql);
  387. return sql;
  388. }
  389. private String getTableModifyDllSQL(String action, MdpAttributeImpl attribute, String tablename) {
  390. String sql = "alter table " + tablename;
  391. if (action.equals(XmlManagerPara.add)) {
  392. sql = sql + " add ";
  393. } else if (action.equals(XmlManagerPara.update)) {
  394. sql = sql + " modify ";
  395. // sql = "sp_rename " + tablename + "." + attribute.getName() +
  396. // ", ";
  397. } else if (action.equals(XmlManagerPara.delete)) {
  398. sql = sql + " drop column " + attribute.getName();
  399. return sql;
  400. }
  401. String fieldname = attribute.getName();
  402. String datatype = attribute.getDataType();
  403. if (datatype.equals(XmlManagerPara.date)) {
  404. sql = sql + fieldname + " datetime";
  405. } else if (datatype.equals("blob")) {
  406. sql = sql + fieldname + " image";
  407. }
  408. else if (datatype.equals("clob")) {
  409. sql = sql + fieldname + " text ";
  410. }
  411. else if (datatype.equals(XmlManagerPara.bool)) {
  412. sql = sql + fieldname + " bit";
  413. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  414. sql = sql + " default " + attribute.getDefaultValue();
  415. } else {
  416. sql = sql + " default null";
  417. }
  418. }
  419. else if (datatype.equals(XmlManagerPara.number)) {
  420. int length = attribute.getPrecision() == 0 ? 10 : attribute.getPrecision();
  421. String scale = attribute.getScale().equals("") ? "" : "," + attribute.getScale();
  422. sql = sql + fieldname + " numeric(" + length + scale + ")";
  423. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  424. sql = sql + " default " + attribute.getDefaultValue();
  425. } else {
  426. sql = sql + " default null";
  427. }
  428. } else if (datatype.equals(XmlManagerPara.string)) {
  429. sql = sql + fieldname;
  430. if (attribute.getPrecision() < 50) {
  431. sql = sql + " varchar(" + attribute.getPrecision() + ")";
  432. } else if (attribute.getPrecision() <= 2000 && attribute.getPrecision() >= 50) {
  433. sql = sql + " varchar(" + attribute.getPrecision() + ")";
  434. } else {
  435. sql = sql + " varchar(" + attribute.getPrecision() + ")";
  436. }
  437. if (attribute.getDefaultValue() != null && !attribute.getDefaultValue().equals("")) {
  438. sql = sql + " default '" + attribute.getDefaultValue() + "'";
  439. } else {
  440. sql = sql + " default null";
  441. }
  442. }
  443. log.info("sql = " + sql);
  444. return sql;
  445. }
  446. @Override
  447. public String getXmlByTableName(String tableName, MdpDataSource dataSource, String moduleId) {
  448. DatabaseTableInfo tableInfo = new DatabaseTableInfo();
  449. TableInfo tableVo = tableInfo.getTableInfo(tableName, dataSource, "sqlserver");
  450. Vector<ColumnInfo> ColumnInfoVo = this.getTableColumnInfo(tableName, dataSource);
  451. MdpDataSourceImpl dataSourceImpl = (MdpDataSourceImpl) dataSource;
  452. StringBuffer sb = new StringBuffer();
  453. sb.append(" \n <font color='#339900'>.......................................<br> \n");
  454. sb.append("&lt;mdpClass classid=\"请替换成推荐号\" ");
  455. sb.append("name=\"" + tableName + "\" description=\"" + tableVo.getDescritpion()
  456. + "\" type=\"" + moduleId + "\" ");
  457. sb.append("primaryKey=\"" + tableVo.getPrimaryKey()
  458. + "\" exist=\"true\" catche=\"false\" validate=\"true\" ");
  459. sb.append("dataSource=\"" + dataSourceImpl.getDataSourceid() + "\" ");
  460. sb.append("><br> \n");
  461. int colSize = ColumnInfoVo.size();
  462. ColumnInfo ColumnInfo = null;
  463. for (int i = 0; i < colSize; i++) {
  464. ColumnInfo = ColumnInfoVo.get(i);
  465. sb.append("&nbsp;&nbsp;&lt;mdpAttribute ");
  466. sb.append(" name=\"" + ColumnInfo.getColumnName() + "\" description=\""
  467. + ColumnInfo.getDescritpion() + "\"");
  468. sb.append(ColumnInfo.getColumTypeDetail());
  469. sb.append(" unit=\"\" logicPrimaryKey=\"false\" referenceType=\"0\" ");
  470. sb.append(" index=\"false\" indexType=\"\" defaultValue=\"\" notNull=\"false\" validate=\"true\" ");
  471. sb.append(" isAutoIncrement=\"false\" isReadOnly=\"false\" isSearchable=\"true\" isSigned=\"false\" ");
  472. sb.append(" columnDisplaySize=\"" + ColumnInfo.getColumnDisplaySize()
  473. + "\" fieldType=\"" + ColumnInfo.getColumnTypeName() + "("
  474. + ColumnInfo.getPrecision() + ")\" ");
  475. sb.append(" /&gt;");
  476. sb.append("<br> \n");
  477. }
  478. sb.append("&lt;/mdpClass&gt; \n <br>\n");
  479. sb.append("........................................<br></font> \n");
  480. log.info(sb.toString());
  481. ColumnInfoVo.clear();
  482. return sb.toString();
  483. }
  484. @Override
  485. public StringBuffer createTableComment(MdpClassImpl table, String type) {
  486. StringBuffer sb = new StringBuffer();
  487. sb.append("<font color='#339900'>");
  488. if (type.equals("add")) {
  489. sb.append("exec sp_addextendedproperty N'MS_Description' , N'" + table.getDescription()
  490. + "' , N'user' , N'dbo' , N'table' , N'" + table.getName() + "'<br>");
  491. sb.append("GO <br>");
  492. } else {
  493. sb.append("EXEC sp_dropextendedproperty 'MS_Description','user','dbo','table','"
  494. + table.getName() + "'<br>");
  495. sb.append("GO <br>");
  496. }
  497. ArrayList<MdpAttributeImpl> list = (ArrayList<MdpAttributeImpl>) table
  498. .getAllMdpAttributes();
  499. for (int i = 0; i < list.size(); i++) {
  500. MdpAttributeImpl attribute = list.get(i);
  501. if (type.equals("add")) {
  502. String sComment = attribute.getDescription();
  503. if (!attribute.getUnit().equals("")) {
  504. sComment = sComment + "(" + attribute.getUnit() + ")";
  505. }
  506. if (attribute.getReferenceType() != 0 && attribute.getReference() != null) {
  507. sComment = sComment + "(引用:" + attribute.getReference().getReferenceTable()
  508. + ")";
  509. }
  510. sb.append("exec sp_addextendedproperty N'MS_Description' , N'" + sComment
  511. + "',N'user',N'dbo',N'table','" + table.getName() + "', N'column', '"
  512. + attribute.getName() + "'<br>");
  513. sb.append("GO <br>");
  514. } else {
  515. sb.append("EXEC sp_dropextendedproperty 'MS_Description','user','dbo','table','"
  516. + table.getName() + "','column','" + attribute.getName() + "'<br>");
  517. sb.append("GO <br>");
  518. }
  519. }
  520. sb.append("</font>");
  521. return sb;
  522. }
  523. @Override
  524. public StringBuffer createOldTableComment(MdpClassImpl table, String type) {
  525. StringBuffer sb = new StringBuffer();
  526. sb.append("<font color='#339900'>");
  527. if (type.equals("add")) {
  528. sb.append("exec sp_addextendedproperty N'MS_Description' , N'" + table.getDescription()
  529. + "——" + table.getOldName() + "' , N'user' , N'dbo' , N'table' , N'"
  530. + table.getOldName() + "'<br>");
  531. sb.append("GO <br>");
  532. } else {
  533. sb.append("EXEC sp_dropextendedproperty 'MS_Description','user','dbo','table','"
  534. + table.getOldName() + "'<br>");
  535. sb.append("GO <br>");
  536. }
  537. ArrayList<MdpAttributeImpl> list = (ArrayList<MdpAttributeImpl>) table
  538. .getAllMdpAttributes();
  539. for (int i = 0; i < list.size(); i++) {
  540. MdpAttributeImpl attribute = list.get(i);
  541. if (attribute.getOldName() == null || attribute.getOldName().equals(""))
  542. continue;
  543. if (type.equals("add")) {
  544. String sComment = attribute.getDescription();
  545. if (!attribute.getUnit().equals("")) {
  546. sComment = sComment + "(" + attribute.getUnit() + ")";
  547. }
  548. if (attribute.getReferenceType() == 2 && attribute.getReference() != null) {
  549. SysModel sysmodel = ModelFactory.getSysmodel();
  550. MdpConstantImpl MdpConstant = (MdpConstantImpl) sysmodel
  551. .getMdpConstantByName(attribute.getReference().getReferenceTable());
  552. sComment = "(引用:" + MdpConstant.getDescription() + ")" + sComment;
  553. }
  554. sb.append("exec sp_addextendedproperty N'MS_Description' , N'" + sComment
  555. + "',N'user',N'dbo',N'table','" + table.getOldName() + "', N'column', '"
  556. + attribute.getOldName() + "'<br>");
  557. sb.append("GO <br>");
  558. } else {
  559. sb.append("EXEC sp_dropextendedproperty 'MS_Description','user','dbo','table','"
  560. + table.getOldName() + "','column','" + attribute.getOldName() + "'<br>");
  561. sb.append("GO <br>");
  562. }
  563. }
  564. sb.append("</font>");
  565. return sb;
  566. }
  567. @Override
  568. public StringBuffer createConstanTableComment(Template template, String type) {
  569. String tableName = template.getTableName();
  570. StringBuffer sb = new StringBuffer();
  571. sb.append("<font color='#339900'>");
  572. if (type.equals("add")) {
  573. sb.append("exec sp_addextendedproperty N'MS_Description' , N'"
  574. + template.getDescription() + "编码表' , N'user' , N'dbo' , N'table' , N'"
  575. + tableName + "'<br>");
  576. sb.append("GO <br>");
  577. } else {
  578. sb.append("EXEC sp_dropextendedproperty 'MS_Description','user','dbo','table','"
  579. + tableName + "'<br>");
  580. sb.append("GO <br>");
  581. }
  582. List<PropertyImpl> propertys = template.getListproperty();
  583. for (int i = 0; i < propertys.size(); i++) {
  584. PropertyImpl PropertyImpl = propertys.get(i);
  585. if (type.equals("add")) {
  586. String sComment = PropertyImpl.getDescription();
  587. sb.append("exec sp_addextendedproperty N'MS_Description' , N'" + sComment
  588. + "',N'user',N'dbo',N'table','" + tableName + "', N'column', '"
  589. + PropertyImpl.getName() + "'<br>");
  590. sb.append("GO <br>");
  591. } else {
  592. sb.append("EXEC sp_dropextendedproperty 'MS_Description','user','dbo','table','"
  593. + tableName + "','column','" + PropertyImpl.getName() + "'<br>");
  594. sb.append("GO <br>");
  595. }
  596. }
  597. sb.append("</font>");
  598. return sb;
  599. }
  600. @Override
  601. public ArrayList<String> getTableCreateKeyDll(int iDataSource) {
  602. ArrayList<String> resultlis = new ArrayList<String>();
  603. // 获得所有没有建立主键的表
  604. String sql = "select name from sys.tables where object_id not in "
  605. + "(select parent_object_id from sys.objects where type = 'PK') order by name";
  606. MdpDataSource datasource = sysmodel.getDataSourceByCode(iDataSource);
  607. Connection conn = null;
  608. Statement st = null;
  609. ResultSet rs = null;
  610. try {
  611. DbConnection dbConnection = new DbConnection();
  612. conn = dbConnection.getConnection(datasource);
  613. st = conn.createStatement();
  614. rs = st.executeQuery(sql);
  615. while (rs.next()) {
  616. String tableName = rs.getString(1);
  617. String tempSql = "";
  618. tempSql = this.getMdpClassePkDll(tableName);
  619. if (tempSql.equals("")) {
  620. tempSql = this.getMdpConstantPkDll(tableName);
  621. }
  622. if (!tempSql.equals("")) {
  623. resultlis.add(tempSql);
  624. }
  625. }
  626. } catch (SQLException e) {
  627. e.printStackTrace();
  628. } catch (ClassNotFoundException e) {
  629. e.printStackTrace();
  630. } finally {
  631. try {
  632. if (rs != null)
  633. rs.close();
  634. if (st != null)
  635. st.close();
  636. if (conn != null)
  637. conn.close();
  638. } catch (SQLException e) {
  639. e.printStackTrace();
  640. }
  641. }
  642. return resultlis;
  643. }
  644. private String getMdpConstantPkDll(String tableName) {
  645. String result = "";
  646. ArrayList<MdpConstantImpl> lis = sysmodel.getMdpConstants();
  647. for (int i = 0; i < lis.size(); i++) {
  648. MdpConstant MdpConstant = (MdpConstant) lis.get(i);
  649. if (MdpConstant.getCodeName().equalsIgnoreCase(tableName.trim())) {
  650. String colname = MdpConstant.getTemplate().getUniqueProperty().getName();
  651. result = "ALTER TABLE " + tableName + " ADD CONSTRAINT PK_" + tableName
  652. + " PRIMARY KEY NONCLUSTERED (" + colname + " ASC)";
  653. // WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE =
  654. // OFF) ON PRIMARY";
  655. break;
  656. }
  657. }
  658. return result;
  659. }
  660. private String getMdpClassePkDll(String tableName) {
  661. String result = "";
  662. ArrayList<MdpClassImpl> lis = sysmodel.getMdpClasses();
  663. for (int i = 0; i < lis.size(); i++) {
  664. MdpClass mdpClass = (MdpClass) lis.get(i);
  665. if (mdpClass.getName().toUpperCase().equals(tableName.toUpperCase())) {
  666. String colname = "FD_OBJECTID";
  667. if (mdpClass.getClassid() == 99 || mdpClass.getClassid() == 100)
  668. colname = "ClassId";
  669. result = "ALTER TABLE " + tableName + " ADD CONSTRAINT PK_" + tableName
  670. + " PRIMARY KEY NONCLUSTERED (" + colname + " DESC)";
  671. // WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE =
  672. // OFF) ON PRIMARY";
  673. break;
  674. }
  675. }
  676. return result;
  677. }
  678. public ArrayList<IndexInfo> getTableIndexInfo() {
  679. ArrayList<IndexInfo> resultlis = new ArrayList<IndexInfo>();
  680. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  681. StringBuffer sb = new StringBuffer();
  682. try {
  683. // 获得数据库表索引创建情况
  684. sb.append("select tab.name,tab.object_id, ");
  685. sb.append(" ind.name indn,ind.index_id,ind.type_desc ");
  686. sb.append("from sys.tables tab,sys.indexes ind ");
  687. sb.append("where ind.type !=0 ");
  688. sb.append(" and tab.object_id=ind.object_id ");
  689. // sb.append(" and ind.object_id = OBJECT_ID(N'SysUser_UserRoleRel') ");
  690. sb.append(" order by tab.name ");
  691. ArrayList<String[]> tablis = persistence.getSearchResult(99, sb.toString());
  692. /*
  693. * --sysindexkeys 表字段说明 --id int 表 ID --indid smallint 索引 ID --colid
  694. * smallint 列 ID --keyno smallint 该列在索引中的位置
  695. */
  696. sb = new StringBuffer();
  697. sb.append("select col.id,sys.indid,col.name ");
  698. sb.append("from sysindexkeys sys, syscolumns col ");
  699. sb.append("where sys.id=col.id ");
  700. sb.append("and sys.colid=col.colid ");
  701. // sb.append(" and sys.id=object_id( 'SysUser_UserRoleRel') ");
  702. sb.append(" and col.id in (select object_id from sys.tables ) ");
  703. sb.append("order by col.id,sys.indid,sys.keyno ");
  704. ArrayList<String[]> columlis = persistence.getSearchResult(99, sb.toString());
  705. for (int i = 0; i < tablis.size(); i++) {
  706. String[] tabArry = tablis.get(i);
  707. IndexInfo IndexInfo = new IndexInfo();
  708. IndexInfo.setTabName(tabArry[0]);
  709. IndexInfo.setIndexName(tabArry[2]);
  710. IndexInfo.setIndexType(tabArry[4]);
  711. String tabid = tabArry[1];
  712. String indexid = tabArry[3];
  713. String columNames = "";
  714. for (int j = 0; j < columlis.size(); j++) {
  715. String[] colArry = columlis.get(j);
  716. if (tabid.equals(colArry[0]) && indexid.equals(colArry[1])) {
  717. columNames = columNames + " " + colArry[2];
  718. } else {
  719. if (!columNames.equals(""))
  720. break;
  721. }
  722. }
  723. IndexInfo.setColumNames(columNames);
  724. resultlis.add(IndexInfo);
  725. }
  726. } catch (PersistenceException e) {
  727. e.printStackTrace();
  728. }
  729. return resultlis;
  730. }
  731. @Override
  732. public ArrayList<String> getTableIndexCreateDll() {
  733. ArrayList<String> result = new ArrayList<String>();
  734. ArrayList<MdpClassImpl> mdpClasses = sysmodel.getMdpClasses();
  735. int mdpClasseslen = mdpClasses.size();
  736. for (int i = 0; i < mdpClasseslen; i++) {
  737. MdpClass mdpClass = (MdpClass) mdpClasses.get(i);
  738. List<MdpAttribute> arrtibutes = mdpClass.getValidateMdpAttributes();
  739. String tableName = mdpClass.getName();
  740. for (int j = 0; j < arrtibutes.size(); j++) {
  741. MdpAttribute MdpAttribute = arrtibutes.get(j);
  742. if (MdpAttribute.isIndex()) {
  743. String fieldName = MdpAttribute.getName();
  744. String indtype = MdpAttribute.getIndexType();
  745. String indName = "index_" + fieldName;
  746. StringBuffer sb = new StringBuffer();
  747. sb.append("IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].["
  748. + tableName + "]') AND name = N'" + indName + "')");
  749. sb.append(" DROP INDEX [" + indName + "] ON [dbo].[" + tableName
  750. + "] WITH ( ONLINE = OFF );");
  751. sb.append("CREATE " + indtype + " INDEX " + indName + " ON " + tableName + " ("
  752. + fieldName + " ASC)");
  753. result.add(sb.toString());
  754. }
  755. }
  756. }
  757. return result;
  758. }
  759. }
  760. /**
  761. * 查询数据库中创建的主键名称、表名、所属字段名 SELECT 'ALTER TABLE '+sysobjects.name+' DROP
  762. * CONSTRAINT '+syscons.name+'; ', 'ALTER TABLE '+sysobjects.name+' ADD
  763. * CONSTRAINT PK_'+sysobjects.name+' PRIMARY KEY NONCLUSTERED
  764. * ('+syscolumns.name+' DESC);' --,sysobjects.name,syscons.name,syscolumns.name
  765. *
  766. * From sysobjects inner join syscolumns on sysobjects.id = syscolumns.id left
  767. * outer join (select o.name sTableName, c.Name sColName From sysobjects o inner
  768. * join sysindexes i on o.id = i.id and (i.status & 0X800) = 0X800 inner join
  769. * syscolumns c1 on c1.colid <= i.keycnt and c1.id = o.id inner join syscolumns
  770. * c on o.id = c.id and c.name = index_col (o.name, i.indid, c1.colid))
  771. * pkElements on pkElements.sTableName = sysobjects.name and pkElements.sColName
  772. * = syscolumns.name inner join sysobjects syscons on
  773. * sysobjects.id=syscons.parent_obj and syscons.xtype='PK' where sysobjects.name
  774. * in (select name from sys.tables where name not like 'BM%') and sTableName is
  775. * not null and syscolumns.name='FD_OBJECTID' order by sysobjects.name
  776. */