home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Components / Microsoft ASP / _SETUP.1 / ASPWizard.jar / asp / wizard / SubDBQueryData.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-20  |  12.8 KB  |  430 lines

  1. package asp.wizard;
  2.  
  3. import asp.netobjects.nfx.ui.OrderedListModel;
  4. import asp.netobjects.nfx.util.ExternalError;
  5. import asp.netobjects.nfx.util.InternalError;
  6. import asp.util.ResourceUtil;
  7. import asp.wizard.def.DefConnection;
  8. import com.sun.java.swing.DefaultListModel;
  9. import java.sql.Connection;
  10. import java.util.Vector;
  11.  
  12. public class SubDBQueryData {
  13.    private static int TBLNEW;
  14.    private static int TBLEXIST = 1;
  15.    private WizardSubDbQuery _Wizard;
  16.    private Vector _vFields = new Vector();
  17.    private Vector _vJoins = new Vector();
  18.    private Vector _vSorts = new Vector();
  19.    private Vector _vWheres = new Vector();
  20.    private Vector _vAllFields = new Vector();
  21.    private boolean _bMultipleTables = false;
  22.    // $FF: synthetic field
  23.    static Class class$asp$wizard$WVPanelDbQueryJoin;
  24.  
  25.    public void setWizard(WizardSubDbQuery wizard) {
  26.       this._Wizard = wizard;
  27.    }
  28.  
  29.    public WizardSubDbQuery getWizard() {
  30.       return this._Wizard;
  31.    }
  32.  
  33.    public void clear() {
  34.       this._vFields.removeAllElements();
  35.       this._vJoins.removeAllElements();
  36.       this._vSorts.removeAllElements();
  37.       this._vWheres.removeAllElements();
  38.    }
  39.  
  40.    public void addField(String strTable, String strField) {
  41.       for(int i = 0; i < this._vFields.size(); ++i) {
  42.          FieldOb o = (FieldOb)this._vFields.elementAt(i);
  43.          if (o.getTable().compareTo(strTable) != 0) {
  44.             this._bMultipleTables = true;
  45.             break;
  46.          }
  47.       }
  48.  
  49.       FieldOb o = new FieldOb(this, strTable, strField);
  50.       this._vFields.addElement(o);
  51.    }
  52.  
  53.    public void removeAllFields() {
  54.       this._vFields.removeAllElements();
  55.       this._bMultipleTables = false;
  56.    }
  57.  
  58.    public Vector getFields() {
  59.       return this._vFields;
  60.    }
  61.  
  62.    public void updateAliases() {
  63.       String strSelectClause = "";
  64.       Vector uniqueFields = new Vector();
  65.  
  66.       for(int i = 0; i < this._vFields.size(); ++i) {
  67.          FieldOb field = (FieldOb)this._vFields.elementAt(i);
  68.          if (uniqueFields.contains(field.getField())) {
  69.             field.setAlias(field.getTable() + "_" + field.getField());
  70.  
  71.             for(int k = 0; k < i; ++k) {
  72.                FieldOb match = (FieldOb)this._vFields.elementAt(k);
  73.                if (match.getField().compareTo(field.getField()) == 0) {
  74.                   match.setAlias(match.getTable() + "_" + match.getField());
  75.                   break;
  76.                }
  77.             }
  78.          } else {
  79.             field.setAlias("");
  80.             uniqueFields.addElement(field.getField());
  81.          }
  82.       }
  83.  
  84.    }
  85.  
  86.    public String getSelect() {
  87.       String strSelectClause = "";
  88.       this.updateAliases();
  89.  
  90.       for(int i = 0; i < this._vFields.size(); ++i) {
  91.          if (i != 0) {
  92.             strSelectClause = strSelectClause + ", ";
  93.          }
  94.  
  95.          strSelectClause = strSelectClause + ((FieldOb)this._vFields.elementAt(i)).getTable() + "." + ((FieldOb)this._vFields.elementAt(i)).getField() + "";
  96.          String alias = ((FieldOb)this._vFields.elementAt(i)).getAlias();
  97.          if (alias.compareTo("") != 0) {
  98.             strSelectClause = strSelectClause + " AS " + alias;
  99.          }
  100.       }
  101.  
  102.       return strSelectClause;
  103.    }
  104.  
  105.    public JoinOb createJoinOb(String srcTable, String srcField, String dstTable, String dstField) {
  106.       return new JoinOb(this, srcTable, srcField, dstTable, dstField);
  107.    }
  108.  
  109.    public void setJoins(Vector joins) {
  110.       this._vJoins.removeAllElements();
  111.  
  112.       for(int i = 0; i < joins.size(); ++i) {
  113.          JoinOb o = (JoinOb)joins.elementAt(i);
  114.          this._vJoins.addElement(o.cloneJoin());
  115.       }
  116.  
  117.    }
  118.  
  119.    public Vector getJoins() {
  120.       return this._vJoins;
  121.    }
  122.  
  123.    public String getFrom() throws InternalError, ExternalError {
  124.       return this.getFrom(this._vJoins);
  125.    }
  126.  
  127.    public String getFrom(Vector joins) throws InternalError, ExternalError {
  128.       this.removeUnsupportedJoins();
  129.       String joinStr = "";
  130.       OrderedListModel vTables = this.getSelectedTables();
  131.       if (((DefaultListModel)vTables).size() == 1) {
  132.          joinStr = joinStr + ((DefaultListModel)vTables).elementAt(0);
  133.       } else {
  134.          this.clearIsWritten(joins);
  135.          Vector tblPool = new Vector();
  136.          String constraints = "";
  137.          String[] joinFound = this.getNextUnwrittenJoin(tblPool, joins);
  138.  
  139.          for(boolean wrap = false; joinFound != null; wrap = true) {
  140.             if (joinStr.compareTo("") == 0) {
  141.                joinStr = joinFound[TBLEXIST];
  142.                tblPool.addElement(joinFound[TBLEXIST]);
  143.             }
  144.  
  145.             constraints = this.getJoinConstraints(joins, joinFound[TBLNEW], joinFound[TBLEXIST]);
  146.             this.markAsWritten(joins, joinFound[TBLNEW], joinFound[TBLEXIST]);
  147.             if (wrap) {
  148.                joinStr = "(" + joinStr + ")";
  149.             }
  150.  
  151.             joinStr = joinFound[TBLNEW] + " INNER JOIN " + joinStr + " ON " + constraints;
  152.             tblPool.addElement(joinFound[TBLNEW]);
  153.             joinFound = this.getNextUnwrittenJoin(tblPool, joins);
  154.          }
  155.  
  156.          if (tblPool.size() != ((DefaultListModel)vTables).size()) {
  157.             String msg = ResourceUtil.getResourceString("asp.wizard.res", class$asp$wizard$WVPanelDbQueryJoin != null ? class$asp$wizard$WVPanelDbQueryJoin : (class$asp$wizard$WVPanelDbQueryJoin = class$("asp.wizard.WVPanelDbQueryJoin")), "err.tablenotjoined");
  158.             throw new ExternalError(msg);
  159.          }
  160.       }
  161.  
  162.       return joinStr;
  163.    }
  164.  
  165.    private String getJoinConstraints(Vector joins, String newTbl, String existTbl) {
  166.       String result = "";
  167.  
  168.       for(int i = 0; i < joins.size(); ++i) {
  169.          JoinOb join = (JoinOb)joins.elementAt(i);
  170.          if (this.isMatchingTablePair(newTbl, existTbl, join)) {
  171.             if (result.compareTo("") != 0) {
  172.                result = result + " AND ";
  173.             }
  174.  
  175.             result = result + join.buildOnClause();
  176.          }
  177.       }
  178.  
  179.       return result;
  180.    }
  181.  
  182.    private void markAsWritten(Vector joins, String newTbl, String existTbl) {
  183.       for(int i = 0; i < joins.size(); ++i) {
  184.          JoinOb join = (JoinOb)joins.elementAt(i);
  185.          if (this.isMatchingTablePair(newTbl, existTbl, join)) {
  186.             join.setIsWritten(true);
  187.          }
  188.       }
  189.  
  190.    }
  191.  
  192.    private void clearIsWritten(Vector joins) {
  193.       for(int i = 0; i < joins.size(); ++i) {
  194.          JoinOb join = (JoinOb)joins.elementAt(i);
  195.          join.setIsWritten(false);
  196.       }
  197.  
  198.    }
  199.  
  200.    private boolean isMatchingTablePair(String tbl1, String tbl2, JoinOb join) {
  201.       return join.getSrcTable().compareTo(tbl1) == 0 && join.getDstTable().compareTo(tbl2) == 0 || join.getSrcTable().compareTo(tbl2) == 0 && join.getDstTable().compareTo(tbl1) == 0;
  202.    }
  203.  
  204.    private String[] getNextUnwrittenJoin(Vector tblPool, Vector joins) throws InternalError, ExternalError {
  205.       JoinOb join = null;
  206.       String[] result = null;
  207.       boolean hasUnwrittens = false;
  208.  
  209.       for(int i = 0; i < joins.size(); ++i) {
  210.          join = (JoinOb)joins.elementAt(i);
  211.          if (!join.getIsWritten()) {
  212.             hasUnwrittens = true;
  213.             if (tblPool.contains(join.getSrcTable()) && tblPool.contains(join.getDstTable())) {
  214.                String msg = ResourceUtil.getResourceString("asp.wizard.res", class$asp$wizard$WVPanelDbQueryJoin != null ? class$asp$wizard$WVPanelDbQueryJoin : (class$asp$wizard$WVPanelDbQueryJoin = class$("asp.wizard.WVPanelDbQueryJoin")), "err.loopedjoined");
  215.                throw new ExternalError(msg);
  216.             }
  217.  
  218.             if (tblPool.contains(join.getSrcTable()) || tblPool.size() == 0) {
  219.                result = new String[2];
  220.                result[TBLNEW] = join.getDstTable();
  221.                result[TBLEXIST] = join.getSrcTable();
  222.                break;
  223.             }
  224.  
  225.             if (tblPool.contains(join.getDstTable())) {
  226.                result = new String[2];
  227.                result[TBLNEW] = join.getSrcTable();
  228.                result[TBLEXIST] = join.getDstTable();
  229.                break;
  230.             }
  231.          }
  232.       }
  233.  
  234.       if (hasUnwrittens && result == null) {
  235.          String msg = ResourceUtil.getResourceString("asp.wizard.res", class$asp$wizard$WVPanelDbQueryJoin != null ? class$asp$wizard$WVPanelDbQueryJoin : (class$asp$wizard$WVPanelDbQueryJoin = class$("asp.wizard.WVPanelDbQueryJoin")), "err.unmatchedjoined");
  236.          throw new ExternalError(msg);
  237.       } else {
  238.          return result;
  239.       }
  240.    }
  241.  
  242.    public void addSort(String strTable, String strField, boolean bAscending) {
  243.       SortOb o = new SortOb(this, strTable, strField, bAscending);
  244.       this._vSorts.addElement(o);
  245.    }
  246.  
  247.    public void removeAllSorts() {
  248.       this._vSorts.removeAllElements();
  249.    }
  250.  
  251.    public Vector getSorts() {
  252.       return this._vSorts;
  253.    }
  254.  
  255.    public void setSorts(Vector sorts) {
  256.       this._vSorts = sorts;
  257.    }
  258.  
  259.    public String getOrderby() {
  260.       String strOrderbyClause = "";
  261.       int nCounter = 0;
  262.       this.removeUnsupportedSorts();
  263.  
  264.       for(int i = 0; i < this._vSorts.size(); ++i) {
  265.          if (nCounter++ > 0) {
  266.             strOrderbyClause = strOrderbyClause + ", ";
  267.          }
  268.  
  269.          strOrderbyClause = strOrderbyClause + ((SortOb)this._vSorts.elementAt(i)).buildClause();
  270.       }
  271.  
  272.       return strOrderbyClause;
  273.    }
  274.  
  275.    public void addWhere(String strPrefix, String strField, String strFilter, String strValue, String strSuffix) {
  276.       WhereOb o = new WhereOb(this, strPrefix, strField, strFilter, strValue, strSuffix);
  277.       this._vWheres.addElement(o);
  278.    }
  279.  
  280.    public void removeAllWheres() {
  281.       this._vWheres.removeAllElements();
  282.    }
  283.  
  284.    public String getWhere() {
  285.       String strWhereClause = "";
  286.       this.removeUnsupportedWheres();
  287.  
  288.       for(int i = 0; i < this._vWheres.size(); ++i) {
  289.          if (i > 0) {
  290.             strWhereClause = strWhereClause + " ";
  291.          }
  292.  
  293.          strWhereClause = strWhereClause + ((WhereOb)this._vWheres.elementAt(i)).buildClause();
  294.       }
  295.  
  296.       return strWhereClause;
  297.    }
  298.  
  299.    public Vector getWheres() {
  300.       return this._vWheres;
  301.    }
  302.  
  303.    boolean containsMultipleTables() {
  304.       return this._bMultipleTables;
  305.    }
  306.  
  307.    public String getSQLStatement() throws InternalError, ExternalError {
  308.       String strSQLStatement = "";
  309.       strSQLStatement = strSQLStatement + "Select " + this.getSelect();
  310.       strSQLStatement = strSQLStatement + " From " + this.getFrom(this._vJoins);
  311.       String WhereStr = this.getWhere();
  312.       if (WhereStr.length() > 0) {
  313.          strSQLStatement = strSQLStatement + " Where " + WhereStr;
  314.       }
  315.  
  316.       String OBStr = this.getOrderby();
  317.       if (OBStr.length() > 0) {
  318.          strSQLStatement = strSQLStatement + " Order By " + OBStr;
  319.       }
  320.  
  321.       return strSQLStatement;
  322.    }
  323.  
  324.    public OrderedListModel getTables(boolean[] containingSpaceFound) {
  325.       OrderedListModel tables = new OrderedListModel();
  326.  
  327.       try {
  328.          WizardSubDbQuery wizard = this.getWizard();
  329.          DefConnection dc = wizard.getDefConnection();
  330.          WizDbManager wdbm = WizDbManager.getInstance();
  331.          Connection conn = wdbm.getConnection(dc.getDSNName(), dc.getUserName(), dc.getPassword());
  332.          tables = WizDbManager.getTables(conn, true, containingSpaceFound);
  333.       } catch (Exception var7) {
  334.          AspWizardExceptionHandler.showMessage(0, "Error populating fields");
  335.       }
  336.  
  337.       return tables;
  338.    }
  339.  
  340.    public OrderedListModel getSelectedTables() {
  341.       OrderedListModel tables = new OrderedListModel();
  342.  
  343.       for(int i = 0; i < this._vFields.size(); ++i) {
  344.          if (!((DefaultListModel)tables).contains(((FieldOb)this._vFields.elementAt(i)).getTable())) {
  345.             tables.addElement(((FieldOb)this._vFields.elementAt(i)).getTable());
  346.          }
  347.       }
  348.  
  349.       return tables;
  350.    }
  351.  
  352.    public void updateAllPossibleFields() {
  353.       OrderedListModel tables = this.getSelectedTables();
  354.       OrderedListModel fields = new OrderedListModel();
  355.       DefConnection dc = this.getWizard().getDefConnection();
  356.       this._vAllFields.removeAllElements();
  357.  
  358.       for(int i = 0; i < ((DefaultListModel)tables).size(); ++i) {
  359.          setFields(dc, fields, ((DefaultListModel)tables).elementAt(i).toString(), (boolean[])null);
  360.  
  361.          for(int j = 0; j < ((DefaultListModel)fields).size(); ++j) {
  362.             this._vAllFields.addElement(((DefaultListModel)tables).elementAt(i).toString() + "." + ((DefaultListModel)fields).elementAt(j).toString());
  363.          }
  364.       }
  365.  
  366.    }
  367.  
  368.    public static void setFields(DefConnection dc, OrderedListModel list, String strTable, boolean[] containingSpaceFound) {
  369.       ((DefaultListModel)list).clear();
  370.  
  371.       try {
  372.          WizDbManager wdbm = WizDbManager.getInstance();
  373.          Connection conn = wdbm.getConnection(dc.getDSNName(), dc.getUserName(), dc.getPassword());
  374.          WizDbManager.getFields(conn, strTable, list, true, containingSpaceFound);
  375.       } catch (Exception var6) {
  376.          AspWizardExceptionHandler.showMessage(0, "Error populating fields");
  377.       }
  378.  
  379.    }
  380.  
  381.    public boolean fieldExists(String value) {
  382.       for(int i = 0; i < this._vAllFields.size(); ++i) {
  383.          if (value.compareTo(this._vAllFields.elementAt(i).toString()) == 0) {
  384.             return true;
  385.          }
  386.       }
  387.  
  388.       return false;
  389.    }
  390.  
  391.    public void removeUnsupportedJoins() {
  392.       for(int i = 0; i < this._vJoins.size(); ++i) {
  393.          JoinOb o = (JoinOb)this._vJoins.elementAt(i);
  394.          if (!this.fieldExists(o.getSrcTable() + "." + o.getSrcField()) || !this.fieldExists(o.getDstTable() + "." + o.getDstField())) {
  395.             this._vJoins.removeElementAt(i);
  396.          }
  397.       }
  398.  
  399.    }
  400.  
  401.    public void removeUnsupportedSorts() {
  402.       for(int i = 0; i < this._vSorts.size(); ++i) {
  403.          SortOb o = (SortOb)this._vSorts.elementAt(i);
  404.          if (!this.fieldExists(o.getTable() + "." + o.getField())) {
  405.             this._vSorts.removeElementAt(i);
  406.          }
  407.       }
  408.  
  409.    }
  410.  
  411.    public void removeUnsupportedWheres() {
  412.       for(int i = 0; i < this._vWheres.size(); ++i) {
  413.          WhereOb o = (WhereOb)this._vWheres.elementAt(i);
  414.          if (!this.fieldExists(o.getField())) {
  415.             this._vWheres.removeElementAt(i);
  416.          }
  417.       }
  418.  
  419.    }
  420.  
  421.    // $FF: synthetic method
  422.    static Class class$(String class$) {
  423.       try {
  424.          return Class.forName(class$);
  425.       } catch (ClassNotFoundException forName) {
  426.          throw new NoClassDefFoundError(((Throwable)forName).getMessage());
  427.       }
  428.    }
  429. }
  430.