/sql.wizard/src/org/netbeans/modules/jdbcwizard/builder/model/OracleQueryGenerator.java

https://bitbucket.org/alsajib/netbeans-soa · Java · 374 lines · 179 code · 30 blank · 165 comment · 34 complexity · 0b70f2739accf29ab4b104b4ec40f1b9 MD5 · raw file

  1. /*
  2. * The contents of this file are subject to the terms of the Common Development
  3. * and Distribution License (the License). You may not use this file except in
  4. * compliance with the License.
  5. *
  6. * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7. * or http://www.netbeans.org/cddl.txt.
  8. *
  9. * When distributing Covered Code, include this CDDL Header Notice in each file
  10. * and include the License file at http://www.netbeans.org/cddl.txt.
  11. * If applicable, add the following below the CDDL Header, with the fields
  12. * enclosed by brackets [] replaced by your own identifying information:
  13. * "Portions Copyrighted [year] [name of copyright owner]"
  14. *
  15. * The Original Software is NetBeans. The Initial Developer of the Original
  16. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  17. * Microsystems, Inc. All Rights Reserved.
  18. */
  19. /*
  20. *
  21. * Copyright 2005 Sun Microsystems, Inc.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. *
  35. */
  36. package org.netbeans.modules.jdbcwizard.builder.model;
  37. import org.netbeans.modules.jdbcwizard.builder.model.DBQueryModel;
  38. import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBColumn;
  39. import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBTable;
  40. import org.netbeans.modules.jdbcwizard.builder.util.XMLCharUtil;
  41. import java.util.ArrayList;
  42. import java.util.List;
  43. import java.util.logging.Logger;
  44. import java.util.logging.Level;
  45. /**
  46. * @author Venkat P
  47. */
  48. public class OracleQueryGenerator implements DBQueryModel {
  49. private String mtabName = null;
  50. private List mcolumns = null;
  51. private List mInsertColumns = null;
  52. private List mUpdateColumns = null;
  53. private List mChooseColumns = null;
  54. private List mPollColumns = null;
  55. private String mSchemaName = null;
  56. private static final Logger mLog = Logger.getLogger(OracleQueryGenerator.class.getName());
  57. private static final String INSERT_QUERY = "insertQuery";
  58. private static final String UPDATE_QUERY = "updateQuery";
  59. private OracleQueryGenerator(){
  60. }
  61. private static final OracleQueryGenerator instance = new OracleQueryGenerator();
  62. public static final OracleQueryGenerator getInstance(){
  63. if(instance != null)
  64. return instance;
  65. else
  66. return new OracleQueryGenerator();
  67. }
  68. /**
  69. * @param souTable
  70. */
  71. public void init(final DBTable souTable) {
  72. this.mtabName = souTable.getName();
  73. this.mcolumns = souTable.getColumnList();
  74. this.mInsertColumns = new ArrayList();
  75. this.mUpdateColumns = new ArrayList();
  76. this.mChooseColumns = new ArrayList();
  77. this.mPollColumns = new ArrayList();
  78. for (int cnt = 0; cnt < this.mcolumns.size(); cnt++) {
  79. final DBColumn temp = (DBColumn) this.mcolumns.get(cnt);
  80. if (temp.isInsertSelected()) {
  81. this.mInsertColumns.add(temp);
  82. }
  83. if (temp.isUpdateSelected()) {
  84. this.mUpdateColumns.add(temp);
  85. }
  86. if (temp.isChooseSelected()) {
  87. this.mChooseColumns.add(temp);
  88. }
  89. if (temp.isPollSelected()) {
  90. this.mPollColumns.add(temp);
  91. }
  92. }
  93. this.mSchemaName = souTable.getSchema();
  94. }
  95. /**
  96. * @return String
  97. * @throws Exception
  98. */
  99. public String createInsertQuery() throws Exception {
  100. final StringBuffer sb = new StringBuffer();
  101. /* sb.append("insert into");
  102. sb.append(" ");
  103. sb.append("\"" + this.mSchemaName + "\"");
  104. sb.append(".");
  105. sb.append("\"" + this.mtabName + "\"");
  106. sb.append(" ");
  107. sb.append("(");
  108. for (int i = 0; i < this.mInsertColumns.size(); i++) {
  109. sb.append("\"" + ((DBColumn) this.mInsertColumns.get(i)).getName() + "\"");
  110. if (i == this.mInsertColumns.size() - 1) {
  111. sb.append(")");
  112. } else {
  113. sb.append(",");
  114. }
  115. }
  116. sb.append(" ");
  117. sb.append("values (");
  118. for (int i = 0; i < this.mInsertColumns.size(); i++) {
  119. sb.append("?");
  120. if (i == this.mInsertColumns.size() - 1) {
  121. sb.append(")");
  122. } else {
  123. sb.append(",");
  124. }
  125. }
  126. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Insert Query " + sb.toString());
  127. return sb.toString();*/
  128. sb.append("insert into");
  129. sb.append(" ");
  130. sb.append(this.mtabName);
  131. sb.append(" ");
  132. sb.append("(");
  133. for (int i = 0; i < this.mInsertColumns.size(); i++) {
  134. sb.append(((DBColumn) this.mInsertColumns.get(i)).getName());
  135. if (i == this.mInsertColumns.size() - 1) {
  136. sb.append(")");
  137. } else {
  138. sb.append(",");
  139. }
  140. }
  141. sb.append(" ");
  142. sb.append("values (");
  143. for (int i = 0; i < this.mInsertColumns.size(); i++) {
  144. sb.append("?");
  145. if (i == this.mInsertColumns.size() - 1) {
  146. sb.append(")");
  147. } else {
  148. sb.append(",");
  149. }
  150. }
  151. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Insert Query " + sb.toString());
  152. return sb.toString();
  153. }
  154. /**
  155. * @return String
  156. * @throws Exception
  157. */
  158. public String createUpdateQuery() throws Exception {
  159. final StringBuffer sb = new StringBuffer();
  160. /*sb.append("update");
  161. sb.append(" ");
  162. sb.append("\"" + this.mSchemaName + "\"");
  163. sb.append(".");
  164. sb.append("\"" + this.mtabName + "\"");
  165. sb.append(" ");
  166. sb.append("set ");
  167. for (int i = 0; i < this.mUpdateColumns.size(); i++) {
  168. sb.append("\"" + this.mtabName + "\"");
  169. sb.append(".");
  170. sb.append("\"" + ((DBColumn) this.mUpdateColumns.get(i)).getName() + "\"");
  171. sb.append(" ");
  172. sb.append("=");
  173. sb.append(" ?");
  174. if (i != this.mUpdateColumns.size() - 1) {
  175. sb.append(",");
  176. }
  177. }
  178. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Update Query " + sb.toString());
  179. return sb.toString();*/
  180. sb.append("update");
  181. sb.append(" ");
  182. sb.append(this.mtabName);
  183. sb.append(" ");
  184. sb.append("set ");
  185. for (int i = 0; i < this.mUpdateColumns.size(); i++) {
  186. sb.append(((DBColumn) this.mUpdateColumns.get(i)).getName());
  187. sb.append(" ");
  188. sb.append("=");
  189. sb.append(" ?");
  190. if (i != this.mUpdateColumns.size() - 1) {
  191. sb.append(",");
  192. }
  193. }
  194. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Update Query " + sb.toString());
  195. return sb.toString();
  196. }
  197. /**
  198. * @return String
  199. * @throws Exception
  200. */
  201. public String createDeleteQuery() throws Exception {
  202. final StringBuffer sb = new StringBuffer();
  203. /* sb.append("delete");
  204. sb.append(" ");
  205. sb.append("from");
  206. sb.append(" ");
  207. sb.append("\"" + this.mSchemaName + "\"");
  208. sb.append(".");
  209. sb.append("\"" + this.mtabName + "\"");
  210. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Delete Query " + sb.toString());
  211. return sb.toString();*/
  212. sb.append("delete");
  213. sb.append(" ");
  214. sb.append("from");
  215. sb.append(" ");
  216. sb.append(this.mtabName);
  217. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Delete Query " + sb.toString());
  218. return sb.toString();
  219. }
  220. /**
  221. * @return String
  222. * @throws Exception
  223. */
  224. public String createFindQuery() throws Exception {
  225. final StringBuffer sb = new StringBuffer();
  226. /*sb.append("select");
  227. sb.append(" ");
  228. for (int i = 0; i < this.mChooseColumns.size(); i++) {
  229. sb.append("\"" + ((DBColumn) this.mChooseColumns.get(i)).getName() + "\"");
  230. if (i == this.mChooseColumns.size() - 1) {
  231. sb.append(" ");
  232. } else {
  233. sb.append(",");
  234. }
  235. }
  236. // selected columns
  237. sb.append("from");
  238. sb.append(" ");
  239. sb.append("\"" + this.mSchemaName + "\"");
  240. sb.append(".");
  241. sb.append("\"" + this.mtabName + "\"");
  242. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Find Query " + sb.toString());
  243. return sb.toString();*/
  244. sb.append("select");
  245. sb.append(" ");
  246. for (int i = 0; i < this.mChooseColumns.size(); i++) {
  247. sb.append(((DBColumn) this.mChooseColumns.get(i)).getName());
  248. if (i == this.mChooseColumns.size() - 1) {
  249. sb.append(" ");
  250. } else {
  251. sb.append(",");
  252. }
  253. }
  254. // selected columns
  255. sb.append("from");
  256. sb.append(" ");
  257. sb.append(this.mtabName);
  258. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Find Query " + sb.toString());
  259. return sb.toString();
  260. }
  261. /**
  262. * @return String
  263. * @throws Exception
  264. */
  265. public String createPoolQuery() throws Exception {
  266. final StringBuffer sb = new StringBuffer();
  267. /*sb.append("select");
  268. sb.append(" ");
  269. for (int i = 0; i < this.mPollColumns.size(); i++) {
  270. sb.append("\"" + ((DBColumn) this.mPollColumns.get(i)).getName() + "\"");
  271. if (i == this.mPollColumns.size() - 1) {
  272. sb.append(" ");
  273. } else {
  274. sb.append(",");
  275. }
  276. }
  277. // selected columns
  278. sb.append("from");
  279. sb.append(" ");
  280. sb.append("\"" + this.mSchemaName + "\"");
  281. sb.append(".");
  282. sb.append("\"" + this.mtabName + "\"");
  283. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Pool Query " + sb.toString());
  284. return sb.toString();*/
  285. sb.append("select");
  286. sb.append(" ");
  287. for (int i = 0; i < this.mPollColumns.size(); i++) {
  288. sb.append(((DBColumn) this.mPollColumns.get(i)).getName() );
  289. if (i == this.mPollColumns.size() - 1) {
  290. sb.append(" ");
  291. } else {
  292. sb.append(",");
  293. }
  294. }
  295. // selected columns
  296. sb.append("from");
  297. sb.append(" ");
  298. sb.append(this.mtabName);
  299. OracleQueryGenerator.mLog.log(Level.INFO, "Generated Pool Query " + sb.toString());
  300. return sb.toString();
  301. }
  302. /**
  303. * @return String
  304. * @throws Exception
  305. */
  306. public String getParamOrder(final String queryType) throws Exception {
  307. final StringBuffer sb = new StringBuffer();
  308. List tempList = null;
  309. if (OracleQueryGenerator.INSERT_QUERY.equals(queryType)) {
  310. tempList = this.mInsertColumns;
  311. }
  312. if (OracleQueryGenerator.UPDATE_QUERY.equals(queryType)) {
  313. tempList = this.mUpdateColumns;
  314. }
  315. for (int i = 0; i < tempList.size(); i++) {
  316. String ncName = XMLCharUtil.makeValidNCName(((DBColumn) tempList.get(i)).getName());
  317. sb.append(ncName);
  318. if (i == tempList.size() - 1) {
  319. sb.append("");
  320. } else {
  321. sb.append(",");
  322. }
  323. }
  324. return sb.toString();
  325. }
  326. /**
  327. * @return String
  328. * @throws Exception
  329. */
  330. public String getPrimaryKey() throws Exception {
  331. String pKey = null;
  332. final java.util.Iterator pkIter = this.mcolumns.iterator();
  333. while (pkIter.hasNext()) {
  334. final DBColumn tc = (DBColumn) pkIter.next();
  335. if (tc.isPrimaryKey()) {
  336. pKey = tc.getName();
  337. break;
  338. }
  339. }
  340. return pKey;
  341. }
  342. }