Guest User

Untitled

a guest
Apr 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package kickspot.control;
  6.  
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.Map;
  10.  
  11. public class BuilderMain {
  12. public void BuatSQL(String[] AddField, String[] AddPrimary, String[] AddClause){
  13. InsertBuilder builder = new InsertBuilder();
  14. for(int field = 0; field <AddField.length; field++){
  15. String NmTabel = AddField[field].split("#")[0];
  16. String []NmField = AddField[field].split("#")[1].split(",");
  17. builder.addNamaField(NmTabel, NmField);
  18. }
  19. for(int prim = 0 ; prim < AddPrimary.length ; prim++){
  20. String AndOr = AddPrimary[prim].split("#")[0];
  21. String PrimaryKey = AddPrimary[prim].split("#")[1];
  22. String []NmTabel = AddPrimary[prim].split("#")[2].split(",");
  23. builder.addPrimary(prim+AndOr,PrimaryKey, NmTabel);
  24. }
  25. for(int cl = 0 ; cl < AddClause.length ; cl++){
  26. String AndOr = AddClause[cl].split("#")[0]; System.out.println(AndOr);
  27. String Clause = AddClause[cl].split("#")[1]; System.out.println(Clause);
  28. String NmField = AddClause[cl].split("#")[2].split(",")[0]; System.out.println(NmField);
  29. String Value = AddClause[cl].split("#")[2].split(",")[1]; System.out.println(Value);
  30. builder.addClause(cl+AndOr,Clause,NmField, Value);
  31. }
  32.  
  33. String sql = SQLDirector.buildSQL(builder);
  34.  
  35. System.out.println(sql);
  36. }
  37. }
  38.  
  39.  
  40.  
  41. class SQLDirector {
  42. public static String buildSQL(SQLBuilder builder) {
  43. StringBuffer buffer = new StringBuffer();
  44. buffer.append(builder.getCommand());
  45. buffer.append(builder.getWhat());
  46. buffer.append(builder.getCriteria());
  47. buffer.append(builder.getClause());
  48. return buffer.toString();
  49. }
  50. }
  51.  
  52. abstract class SQLBuilder {
  53. /**
  54. * Gets the command attribute of the SQLBuilder object
  55. *
  56. * @return The command value or what type of Builder this is. This will return
  57. * a SQL command.
  58. * @since
  59. */
  60. public abstract String getCommand();
  61.  
  62. /**
  63. * Gets the table attribute of the SQLBuilder object
  64. *
  65. * @return The table name value
  66. * @since
  67. */
  68. public abstract String getTable();
  69.  
  70. /**
  71. * Gets the what value of the SQLBuilder object. This attribute will differ
  72. * based on what type of object we are using. This could be a list of columns
  73. * and data.
  74. *
  75. * @return The what value
  76. */
  77. public abstract String getWhat();
  78.  
  79. /**
  80. * Gets the criteria attribute of the SQLBuilder object
  81. *
  82. * @return The criteria value
  83. */
  84. public abstract String getCriteria();
  85.  
  86. /**
  87. * Ambil tambahan kriteria seperti AND, OR
  88. *
  89. * @return The isiWhere value
  90. */
  91. public abstract String getClause();
  92.  
  93. }
  94.  
  95. class InsertBuilder extends SQLBuilder {
  96. private String table;
  97.  
  98. private Map columnsAndData = new HashMap();
  99.  
  100. private Map criTeria = new HashMap();
  101.  
  102. private Map clause = new HashMap();
  103.  
  104. private String criteria;
  105.  
  106.  
  107. /**
  108. * Sets the table attribute of the InsertBuilder object
  109. *
  110. * @param table
  111. * The new table value
  112. * @since
  113. */
  114. public void setTable(String table) {
  115. this.table = table;
  116. }
  117.  
  118. /**
  119. * Gets the command attribute of the InsertBuilder object
  120. *
  121. * @return The command value
  122. */
  123. public String getCommand() {
  124. return "SELECT ";
  125. }
  126.  
  127. /**
  128. * Gets the table attribute of the InsertBuilder object
  129. *
  130. * @return The table value
  131. */
  132. public String getTable() {
  133. return table;
  134. }
  135.  
  136. /**
  137. * Gets the what attribute of the InsertBuilder object
  138. *
  139. * @return The what value
  140. * @since
  141. */
  142. public String getWhat() {
  143. StringBuffer columns = new StringBuffer();
  144. StringBuffer values = new StringBuffer();
  145. StringBuffer what = new StringBuffer();
  146.  
  147. String columnName = null;
  148. Iterator iter = columnsAndData.keySet().iterator();
  149. while (iter.hasNext()) {
  150. columnName = (String) iter.next();
  151. System.out.println(columnName);
  152. columns.append(columnName);
  153. values.append(columnsAndData.get(columnName));
  154. if (iter.hasNext()) {
  155. columns.append(',');
  156. values.append(',');
  157. }
  158. }
  159.  
  160. // what.append(" (");
  161. what.append(values);
  162. what.append(" FROM ");
  163. what.append(columns);
  164. //what.append(") ");
  165. return what.toString();
  166. }
  167.  
  168. /**
  169. * Gets the Criteria attribute of the InsertBuilder object
  170. *
  171. * @return The isiWhere value
  172. */
  173. public String getCriteria() {
  174. StringBuffer isiWhere = new StringBuffer();
  175. StringBuffer tambahKriteria = new StringBuffer();
  176.  
  177. String AndOr = null;
  178. Iterator iter = criTeria.keySet().iterator();
  179. while (iter.hasNext()) {
  180. AndOr = (String) iter.next();
  181. tambahKriteria.append(criTeria.get(AndOr));
  182. if (iter.hasNext()) {
  183. AndOr = AndOr.substring(1);
  184. tambahKriteria.append(" "+AndOr+" ");
  185. // values.append(',');
  186. }
  187. }
  188. isiWhere.append(" WHERE ");
  189. isiWhere.append(tambahKriteria);
  190. return isiWhere.toString();
  191. }
  192.  
  193. /**
  194. * Gets the criteria attribute of the InsertBuilder object
  195. *
  196. * @return The criteria value
  197. * @since
  198. */
  199. public String getClause() {
  200. StringBuffer tambahKriteria = new StringBuffer();
  201. StringBuffer hasil = new StringBuffer();
  202. String AndOr = null;
  203. Iterator iter = clause.keySet().iterator();
  204. while (iter.hasNext()) {
  205. AndOr = (String) iter.next();
  206. tambahKriteria.append(clause.get(AndOr));
  207. if (iter.hasNext()) {
  208. String tempAndOr = AndOr.substring(1);
  209. tambahKriteria.append(" "+tempAndOr+" ");
  210. }
  211. }
  212. hasil.append(" "+AndOr.substring(1)+" ");
  213. hasil.append(tambahKriteria);
  214. return hasil.toString();
  215. }
  216. /**
  217. * Memasukkan atribut ColumnAndData untuk InsertBuilder object
  218. *
  219. * @param columnName
  220. * The feature to be added to the ColumnAndData attribute
  221. * @param value
  222. * The feature to be added to the ColumnAndData attribute
  223. * @since
  224. */
  225. public void addColumnAndData(String columnName, Object value) {
  226. //for(int i=0;i<value.length;i++)
  227. if (value != null) {
  228. columnsAndData.put(columnName, value);
  229. }
  230. }
  231.  
  232. /**
  233. * Memasukkan atribut ColumnAndData untuk InsertBuilder object
  234. *
  235. * @param columnName
  236. * The feature to be added to the ColumnAndData attribute
  237. * @param value
  238. * The feature to be added to the ColumnAndData attribute
  239. */
  240. public void addNamaField(String columnName, String[] value) {
  241. String namaField="";
  242. if (value != null) {
  243. for(int i=0;i<value.length;i++){
  244. if(namaField.isEmpty()){
  245. namaField = columnName+"."+value[i];
  246. }else{
  247. namaField = namaField +", "+columnName+"."+value[i];
  248. }
  249. }
  250. columnsAndData.put(columnName, namaField);
  251. }
  252. }
  253.  
  254. /**
  255. * Memasukkan atribut criTeria untuk InsertBuilder object
  256. *
  257. * @param AndOr
  258. * Isinya clause seperti AND, OR
  259. * @param primaryKey
  260. * Isinya nama fied yg jd primary key
  261. * @param namaTabel
  262. * Isinya nama-nama tabel yang punya primari key sama
  263. */
  264. public void addPrimary(String AndOr, String primariKey, String[] namaTabel) {
  265. String Kriteria="";
  266. if (namaTabel != null) {
  267. for(int i=0;i<namaTabel.length;i++){
  268. if(Kriteria.isEmpty()){
  269. Kriteria =namaTabel[i]+"."+primariKey;
  270. }else{
  271. Kriteria = Kriteria +" = "+namaTabel[i]+"."+primariKey;
  272. }
  273. }
  274. //criteria = Kriteria;
  275. criTeria.put(AndOr,Kriteria);
  276. }
  277. }
  278.  
  279. /**
  280. * Memasukkan atribut clause untuk InsertBuilder object
  281. *
  282. * @param AndOr
  283. * Isinya clause seperti AND, OR
  284. * @param LikeSmDgn
  285. * Isinya seperti 'LIKE' atau '='
  286. * @param nmField
  287. * Isinya field yang dipilih
  288. * @param value
  289. * Isinya value untuk menentukan isi dari field (nmField)
  290. */
  291. public void addClause(String AndOr, String LikeSmDgn, String nmField, String value) {
  292. String Kriteria="";
  293. if (nmField != null && value != null) {
  294. Kriteria =nmField+" "+LikeSmDgn+" "+value;
  295. clause.put(AndOr,Kriteria);
  296. }
  297. }
  298.  
  299. }
Add Comment
Please, Sign In to add comment