Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package kickspot.control;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- public class BuilderMain {
- public void BuatSQL(String[] AddField, String[] AddPrimary, String[] AddClause){
- InsertBuilder builder = new InsertBuilder();
- for(int field = 0; field <AddField.length; field++){
- String NmTabel = AddField[field].split("#")[0];
- String []NmField = AddField[field].split("#")[1].split(",");
- builder.addNamaField(NmTabel, NmField);
- }
- for(int prim = 0 ; prim < AddPrimary.length ; prim++){
- String AndOr = AddPrimary[prim].split("#")[0];
- String PrimaryKey = AddPrimary[prim].split("#")[1];
- String []NmTabel = AddPrimary[prim].split("#")[2].split(",");
- builder.addPrimary(prim+AndOr,PrimaryKey, NmTabel);
- }
- for(int cl = 0 ; cl < AddClause.length ; cl++){
- String AndOr = AddClause[cl].split("#")[0]; System.out.println(AndOr);
- String Clause = AddClause[cl].split("#")[1]; System.out.println(Clause);
- String NmField = AddClause[cl].split("#")[2].split(",")[0]; System.out.println(NmField);
- String Value = AddClause[cl].split("#")[2].split(",")[1]; System.out.println(Value);
- builder.addClause(cl+AndOr,Clause,NmField, Value);
- }
- String sql = SQLDirector.buildSQL(builder);
- System.out.println(sql);
- }
- }
- class SQLDirector {
- public static String buildSQL(SQLBuilder builder) {
- StringBuffer buffer = new StringBuffer();
- buffer.append(builder.getCommand());
- buffer.append(builder.getWhat());
- buffer.append(builder.getCriteria());
- buffer.append(builder.getClause());
- return buffer.toString();
- }
- }
- abstract class SQLBuilder {
- /**
- * Gets the command attribute of the SQLBuilder object
- *
- * @return The command value or what type of Builder this is. This will return
- * a SQL command.
- * @since
- */
- public abstract String getCommand();
- /**
- * Gets the table attribute of the SQLBuilder object
- *
- * @return The table name value
- * @since
- */
- public abstract String getTable();
- /**
- * Gets the what value of the SQLBuilder object. This attribute will differ
- * based on what type of object we are using. This could be a list of columns
- * and data.
- *
- * @return The what value
- */
- public abstract String getWhat();
- /**
- * Gets the criteria attribute of the SQLBuilder object
- *
- * @return The criteria value
- */
- public abstract String getCriteria();
- /**
- * Ambil tambahan kriteria seperti AND, OR
- *
- * @return The isiWhere value
- */
- public abstract String getClause();
- }
- class InsertBuilder extends SQLBuilder {
- private String table;
- private Map columnsAndData = new HashMap();
- private Map criTeria = new HashMap();
- private Map clause = new HashMap();
- private String criteria;
- /**
- * Sets the table attribute of the InsertBuilder object
- *
- * @param table
- * The new table value
- * @since
- */
- public void setTable(String table) {
- this.table = table;
- }
- /**
- * Gets the command attribute of the InsertBuilder object
- *
- * @return The command value
- */
- public String getCommand() {
- return "SELECT ";
- }
- /**
- * Gets the table attribute of the InsertBuilder object
- *
- * @return The table value
- */
- public String getTable() {
- return table;
- }
- /**
- * Gets the what attribute of the InsertBuilder object
- *
- * @return The what value
- * @since
- */
- public String getWhat() {
- StringBuffer columns = new StringBuffer();
- StringBuffer values = new StringBuffer();
- StringBuffer what = new StringBuffer();
- String columnName = null;
- Iterator iter = columnsAndData.keySet().iterator();
- while (iter.hasNext()) {
- columnName = (String) iter.next();
- System.out.println(columnName);
- columns.append(columnName);
- values.append(columnsAndData.get(columnName));
- if (iter.hasNext()) {
- columns.append(',');
- values.append(',');
- }
- }
- // what.append(" (");
- what.append(values);
- what.append(" FROM ");
- what.append(columns);
- //what.append(") ");
- return what.toString();
- }
- /**
- * Gets the Criteria attribute of the InsertBuilder object
- *
- * @return The isiWhere value
- */
- public String getCriteria() {
- StringBuffer isiWhere = new StringBuffer();
- StringBuffer tambahKriteria = new StringBuffer();
- String AndOr = null;
- Iterator iter = criTeria.keySet().iterator();
- while (iter.hasNext()) {
- AndOr = (String) iter.next();
- tambahKriteria.append(criTeria.get(AndOr));
- if (iter.hasNext()) {
- AndOr = AndOr.substring(1);
- tambahKriteria.append(" "+AndOr+" ");
- // values.append(',');
- }
- }
- isiWhere.append(" WHERE ");
- isiWhere.append(tambahKriteria);
- return isiWhere.toString();
- }
- /**
- * Gets the criteria attribute of the InsertBuilder object
- *
- * @return The criteria value
- * @since
- */
- public String getClause() {
- StringBuffer tambahKriteria = new StringBuffer();
- StringBuffer hasil = new StringBuffer();
- String AndOr = null;
- Iterator iter = clause.keySet().iterator();
- while (iter.hasNext()) {
- AndOr = (String) iter.next();
- tambahKriteria.append(clause.get(AndOr));
- if (iter.hasNext()) {
- String tempAndOr = AndOr.substring(1);
- tambahKriteria.append(" "+tempAndOr+" ");
- }
- }
- hasil.append(" "+AndOr.substring(1)+" ");
- hasil.append(tambahKriteria);
- return hasil.toString();
- }
- /**
- * Memasukkan atribut ColumnAndData untuk InsertBuilder object
- *
- * @param columnName
- * The feature to be added to the ColumnAndData attribute
- * @param value
- * The feature to be added to the ColumnAndData attribute
- * @since
- */
- public void addColumnAndData(String columnName, Object value) {
- //for(int i=0;i<value.length;i++)
- if (value != null) {
- columnsAndData.put(columnName, value);
- }
- }
- /**
- * Memasukkan atribut ColumnAndData untuk InsertBuilder object
- *
- * @param columnName
- * The feature to be added to the ColumnAndData attribute
- * @param value
- * The feature to be added to the ColumnAndData attribute
- */
- public void addNamaField(String columnName, String[] value) {
- String namaField="";
- if (value != null) {
- for(int i=0;i<value.length;i++){
- if(namaField.isEmpty()){
- namaField = columnName+"."+value[i];
- }else{
- namaField = namaField +", "+columnName+"."+value[i];
- }
- }
- columnsAndData.put(columnName, namaField);
- }
- }
- /**
- * Memasukkan atribut criTeria untuk InsertBuilder object
- *
- * @param AndOr
- * Isinya clause seperti AND, OR
- * @param primaryKey
- * Isinya nama fied yg jd primary key
- * @param namaTabel
- * Isinya nama-nama tabel yang punya primari key sama
- */
- public void addPrimary(String AndOr, String primariKey, String[] namaTabel) {
- String Kriteria="";
- if (namaTabel != null) {
- for(int i=0;i<namaTabel.length;i++){
- if(Kriteria.isEmpty()){
- Kriteria =namaTabel[i]+"."+primariKey;
- }else{
- Kriteria = Kriteria +" = "+namaTabel[i]+"."+primariKey;
- }
- }
- //criteria = Kriteria;
- criTeria.put(AndOr,Kriteria);
- }
- }
- /**
- * Memasukkan atribut clause untuk InsertBuilder object
- *
- * @param AndOr
- * Isinya clause seperti AND, OR
- * @param LikeSmDgn
- * Isinya seperti 'LIKE' atau '='
- * @param nmField
- * Isinya field yang dipilih
- * @param value
- * Isinya value untuk menentukan isi dari field (nmField)
- */
- public void addClause(String AndOr, String LikeSmDgn, String nmField, String value) {
- String Kriteria="";
- if (nmField != null && value != null) {
- Kriteria =nmField+" "+LikeSmDgn+" "+value;
- clause.put(AndOr,Kriteria);
- }
- }
- }
Add Comment
Please, Sign In to add comment