home *** CD-ROM | disk | FTP | other *** search
- package asp.wizard;
-
- import asp.netobjects.nfx.ui.OrderedListModel;
- import asp.netobjects.nfx.util.ExternalError;
- import asp.netobjects.nfx.util.InternalError;
- import asp.util.ResourceUtil;
- import asp.wizard.def.DefConnection;
- import com.sun.java.swing.DefaultListModel;
- import java.sql.Connection;
- import java.util.Vector;
-
- public class SubDBQueryData {
- private static int TBLNEW;
- private static int TBLEXIST = 1;
- private WizardSubDbQuery _Wizard;
- private Vector _vFields = new Vector();
- private Vector _vJoins = new Vector();
- private Vector _vSorts = new Vector();
- private Vector _vWheres = new Vector();
- private Vector _vAllFields = new Vector();
- private boolean _bMultipleTables = false;
- // $FF: synthetic field
- static Class class$asp$wizard$WVPanelDbQueryJoin;
-
- public void setWizard(WizardSubDbQuery wizard) {
- this._Wizard = wizard;
- }
-
- public WizardSubDbQuery getWizard() {
- return this._Wizard;
- }
-
- public void clear() {
- this._vFields.removeAllElements();
- this._vJoins.removeAllElements();
- this._vSorts.removeAllElements();
- this._vWheres.removeAllElements();
- }
-
- public void addField(String strTable, String strField) {
- for(int i = 0; i < this._vFields.size(); ++i) {
- FieldOb o = (FieldOb)this._vFields.elementAt(i);
- if (o.getTable().compareTo(strTable) != 0) {
- this._bMultipleTables = true;
- break;
- }
- }
-
- FieldOb o = new FieldOb(this, strTable, strField);
- this._vFields.addElement(o);
- }
-
- public void removeAllFields() {
- this._vFields.removeAllElements();
- this._bMultipleTables = false;
- }
-
- public Vector getFields() {
- return this._vFields;
- }
-
- public void updateAliases() {
- String strSelectClause = "";
- Vector uniqueFields = new Vector();
-
- for(int i = 0; i < this._vFields.size(); ++i) {
- FieldOb field = (FieldOb)this._vFields.elementAt(i);
- if (uniqueFields.contains(field.getField())) {
- field.setAlias(field.getTable() + "_" + field.getField());
-
- for(int k = 0; k < i; ++k) {
- FieldOb match = (FieldOb)this._vFields.elementAt(k);
- if (match.getField().compareTo(field.getField()) == 0) {
- match.setAlias(match.getTable() + "_" + match.getField());
- break;
- }
- }
- } else {
- field.setAlias("");
- uniqueFields.addElement(field.getField());
- }
- }
-
- }
-
- public String getSelect() {
- String strSelectClause = "";
- this.updateAliases();
-
- for(int i = 0; i < this._vFields.size(); ++i) {
- if (i != 0) {
- strSelectClause = strSelectClause + ", ";
- }
-
- strSelectClause = strSelectClause + ((FieldOb)this._vFields.elementAt(i)).getTable() + "." + ((FieldOb)this._vFields.elementAt(i)).getField() + "";
- String alias = ((FieldOb)this._vFields.elementAt(i)).getAlias();
- if (alias.compareTo("") != 0) {
- strSelectClause = strSelectClause + " AS " + alias;
- }
- }
-
- return strSelectClause;
- }
-
- public JoinOb createJoinOb(String srcTable, String srcField, String dstTable, String dstField) {
- return new JoinOb(this, srcTable, srcField, dstTable, dstField);
- }
-
- public void setJoins(Vector joins) {
- this._vJoins.removeAllElements();
-
- for(int i = 0; i < joins.size(); ++i) {
- JoinOb o = (JoinOb)joins.elementAt(i);
- this._vJoins.addElement(o.cloneJoin());
- }
-
- }
-
- public Vector getJoins() {
- return this._vJoins;
- }
-
- public String getFrom() throws InternalError, ExternalError {
- return this.getFrom(this._vJoins);
- }
-
- public String getFrom(Vector joins) throws InternalError, ExternalError {
- this.removeUnsupportedJoins();
- String joinStr = "";
- OrderedListModel vTables = this.getSelectedTables();
- if (((DefaultListModel)vTables).size() == 1) {
- joinStr = joinStr + ((DefaultListModel)vTables).elementAt(0);
- } else {
- this.clearIsWritten(joins);
- Vector tblPool = new Vector();
- String constraints = "";
- String[] joinFound = this.getNextUnwrittenJoin(tblPool, joins);
-
- for(boolean wrap = false; joinFound != null; wrap = true) {
- if (joinStr.compareTo("") == 0) {
- joinStr = joinFound[TBLEXIST];
- tblPool.addElement(joinFound[TBLEXIST]);
- }
-
- constraints = this.getJoinConstraints(joins, joinFound[TBLNEW], joinFound[TBLEXIST]);
- this.markAsWritten(joins, joinFound[TBLNEW], joinFound[TBLEXIST]);
- if (wrap) {
- joinStr = "(" + joinStr + ")";
- }
-
- joinStr = joinFound[TBLNEW] + " INNER JOIN " + joinStr + " ON " + constraints;
- tblPool.addElement(joinFound[TBLNEW]);
- joinFound = this.getNextUnwrittenJoin(tblPool, joins);
- }
-
- if (tblPool.size() != ((DefaultListModel)vTables).size()) {
- 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");
- throw new ExternalError(msg);
- }
- }
-
- return joinStr;
- }
-
- private String getJoinConstraints(Vector joins, String newTbl, String existTbl) {
- String result = "";
-
- for(int i = 0; i < joins.size(); ++i) {
- JoinOb join = (JoinOb)joins.elementAt(i);
- if (this.isMatchingTablePair(newTbl, existTbl, join)) {
- if (result.compareTo("") != 0) {
- result = result + " AND ";
- }
-
- result = result + join.buildOnClause();
- }
- }
-
- return result;
- }
-
- private void markAsWritten(Vector joins, String newTbl, String existTbl) {
- for(int i = 0; i < joins.size(); ++i) {
- JoinOb join = (JoinOb)joins.elementAt(i);
- if (this.isMatchingTablePair(newTbl, existTbl, join)) {
- join.setIsWritten(true);
- }
- }
-
- }
-
- private void clearIsWritten(Vector joins) {
- for(int i = 0; i < joins.size(); ++i) {
- JoinOb join = (JoinOb)joins.elementAt(i);
- join.setIsWritten(false);
- }
-
- }
-
- private boolean isMatchingTablePair(String tbl1, String tbl2, JoinOb join) {
- return join.getSrcTable().compareTo(tbl1) == 0 && join.getDstTable().compareTo(tbl2) == 0 || join.getSrcTable().compareTo(tbl2) == 0 && join.getDstTable().compareTo(tbl1) == 0;
- }
-
- private String[] getNextUnwrittenJoin(Vector tblPool, Vector joins) throws InternalError, ExternalError {
- JoinOb join = null;
- String[] result = null;
- boolean hasUnwrittens = false;
-
- for(int i = 0; i < joins.size(); ++i) {
- join = (JoinOb)joins.elementAt(i);
- if (!join.getIsWritten()) {
- hasUnwrittens = true;
- if (tblPool.contains(join.getSrcTable()) && tblPool.contains(join.getDstTable())) {
- 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");
- throw new ExternalError(msg);
- }
-
- if (tblPool.contains(join.getSrcTable()) || tblPool.size() == 0) {
- result = new String[2];
- result[TBLNEW] = join.getDstTable();
- result[TBLEXIST] = join.getSrcTable();
- break;
- }
-
- if (tblPool.contains(join.getDstTable())) {
- result = new String[2];
- result[TBLNEW] = join.getSrcTable();
- result[TBLEXIST] = join.getDstTable();
- break;
- }
- }
- }
-
- if (hasUnwrittens && result == null) {
- 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");
- throw new ExternalError(msg);
- } else {
- return result;
- }
- }
-
- public void addSort(String strTable, String strField, boolean bAscending) {
- SortOb o = new SortOb(this, strTable, strField, bAscending);
- this._vSorts.addElement(o);
- }
-
- public void removeAllSorts() {
- this._vSorts.removeAllElements();
- }
-
- public Vector getSorts() {
- return this._vSorts;
- }
-
- public void setSorts(Vector sorts) {
- this._vSorts = sorts;
- }
-
- public String getOrderby() {
- String strOrderbyClause = "";
- int nCounter = 0;
- this.removeUnsupportedSorts();
-
- for(int i = 0; i < this._vSorts.size(); ++i) {
- if (nCounter++ > 0) {
- strOrderbyClause = strOrderbyClause + ", ";
- }
-
- strOrderbyClause = strOrderbyClause + ((SortOb)this._vSorts.elementAt(i)).buildClause();
- }
-
- return strOrderbyClause;
- }
-
- public void addWhere(String strPrefix, String strField, String strFilter, String strValue, String strSuffix) {
- WhereOb o = new WhereOb(this, strPrefix, strField, strFilter, strValue, strSuffix);
- this._vWheres.addElement(o);
- }
-
- public void removeAllWheres() {
- this._vWheres.removeAllElements();
- }
-
- public String getWhere() {
- String strWhereClause = "";
- this.removeUnsupportedWheres();
-
- for(int i = 0; i < this._vWheres.size(); ++i) {
- if (i > 0) {
- strWhereClause = strWhereClause + " ";
- }
-
- strWhereClause = strWhereClause + ((WhereOb)this._vWheres.elementAt(i)).buildClause();
- }
-
- return strWhereClause;
- }
-
- public Vector getWheres() {
- return this._vWheres;
- }
-
- boolean containsMultipleTables() {
- return this._bMultipleTables;
- }
-
- public String getSQLStatement() throws InternalError, ExternalError {
- String strSQLStatement = "";
- strSQLStatement = strSQLStatement + "Select " + this.getSelect();
- strSQLStatement = strSQLStatement + " From " + this.getFrom(this._vJoins);
- String WhereStr = this.getWhere();
- if (WhereStr.length() > 0) {
- strSQLStatement = strSQLStatement + " Where " + WhereStr;
- }
-
- String OBStr = this.getOrderby();
- if (OBStr.length() > 0) {
- strSQLStatement = strSQLStatement + " Order By " + OBStr;
- }
-
- return strSQLStatement;
- }
-
- public OrderedListModel getTables(boolean[] containingSpaceFound) {
- OrderedListModel tables = new OrderedListModel();
-
- try {
- WizardSubDbQuery wizard = this.getWizard();
- DefConnection dc = wizard.getDefConnection();
- WizDbManager wdbm = WizDbManager.getInstance();
- Connection conn = wdbm.getConnection(dc.getDSNName(), dc.getUserName(), dc.getPassword());
- tables = WizDbManager.getTables(conn, true, containingSpaceFound);
- } catch (Exception var7) {
- AspWizardExceptionHandler.showMessage(0, "Error populating fields");
- }
-
- return tables;
- }
-
- public OrderedListModel getSelectedTables() {
- OrderedListModel tables = new OrderedListModel();
-
- for(int i = 0; i < this._vFields.size(); ++i) {
- if (!((DefaultListModel)tables).contains(((FieldOb)this._vFields.elementAt(i)).getTable())) {
- tables.addElement(((FieldOb)this._vFields.elementAt(i)).getTable());
- }
- }
-
- return tables;
- }
-
- public void updateAllPossibleFields() {
- OrderedListModel tables = this.getSelectedTables();
- OrderedListModel fields = new OrderedListModel();
- DefConnection dc = this.getWizard().getDefConnection();
- this._vAllFields.removeAllElements();
-
- for(int i = 0; i < ((DefaultListModel)tables).size(); ++i) {
- setFields(dc, fields, ((DefaultListModel)tables).elementAt(i).toString(), (boolean[])null);
-
- for(int j = 0; j < ((DefaultListModel)fields).size(); ++j) {
- this._vAllFields.addElement(((DefaultListModel)tables).elementAt(i).toString() + "." + ((DefaultListModel)fields).elementAt(j).toString());
- }
- }
-
- }
-
- public static void setFields(DefConnection dc, OrderedListModel list, String strTable, boolean[] containingSpaceFound) {
- ((DefaultListModel)list).clear();
-
- try {
- WizDbManager wdbm = WizDbManager.getInstance();
- Connection conn = wdbm.getConnection(dc.getDSNName(), dc.getUserName(), dc.getPassword());
- WizDbManager.getFields(conn, strTable, list, true, containingSpaceFound);
- } catch (Exception var6) {
- AspWizardExceptionHandler.showMessage(0, "Error populating fields");
- }
-
- }
-
- public boolean fieldExists(String value) {
- for(int i = 0; i < this._vAllFields.size(); ++i) {
- if (value.compareTo(this._vAllFields.elementAt(i).toString()) == 0) {
- return true;
- }
- }
-
- return false;
- }
-
- public void removeUnsupportedJoins() {
- for(int i = 0; i < this._vJoins.size(); ++i) {
- JoinOb o = (JoinOb)this._vJoins.elementAt(i);
- if (!this.fieldExists(o.getSrcTable() + "." + o.getSrcField()) || !this.fieldExists(o.getDstTable() + "." + o.getDstField())) {
- this._vJoins.removeElementAt(i);
- }
- }
-
- }
-
- public void removeUnsupportedSorts() {
- for(int i = 0; i < this._vSorts.size(); ++i) {
- SortOb o = (SortOb)this._vSorts.elementAt(i);
- if (!this.fieldExists(o.getTable() + "." + o.getField())) {
- this._vSorts.removeElementAt(i);
- }
- }
-
- }
-
- public void removeUnsupportedWheres() {
- for(int i = 0; i < this._vWheres.size(); ++i) {
- WhereOb o = (WhereOb)this._vWheres.elementAt(i);
- if (!this.fieldExists(o.getField())) {
- this._vWheres.removeElementAt(i);
- }
- }
-
- }
-
- // $FF: synthetic method
- static Class class$(String class$) {
- try {
- return Class.forName(class$);
- } catch (ClassNotFoundException forName) {
- throw new NoClassDefFoundError(((Throwable)forName).getMessage());
- }
- }
- }
-