⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 countercolumns.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      Statement stmt = conn.createStatement();
      BigDecimal newValue = new BigDecimal(startValues.get(i).toString());
      strQuery = queryInsertCounter(counterNames.get(i).toString(), newValue);
      stmt.execute(strQuery);
      stmt.close();
    }
    return true;
  }

  /**
   * This method read value of counter parameter
   * @param tableName is table name
   * @param tableID is table ID
   * @param conn is connection
   * @param firstColumn is first column
   * @return true
   * @throws SQLException
   */
  private Vector readCounter(String tableName, String tableID, Connection conn,
      int firstColumn) throws SQLException {

    String strQuery = null;
    BigDecimal value = new BigDecimal(0);
    Vector startValues = new Vector();
    Vector counterNames = this.getCounterName(tableName, tableID);
    Vector counterValues = this.getCounterValue(tableName, tableID);
    Vector counterIncrements = this.getCounterIncrement(tableName, tableID);
    Vector defaultStartValues = this.getCounterIncrement(tableName, tableID);

    for (int i = 0; i < counterNames.size(); i++) {
      Statement stmt = conn.createStatement();
      strQuery = querySelectCounter(counterNames.get(i).toString());
	  	this.logger.write("full", "\tQuery '" + strQuery + "' will be executed");
      ResultSet rset = stmt.executeQuery(strQuery);
      if (rset.next()) {
        if (firstColumn == 0)
          value = new BigDecimal(rset.getString(0));
        else
          value = new BigDecimal(rset.getString(1));
      } else {
        insertCounter(tableName, tableID, conn);
        value = new BigDecimal(defaultStartValues.get(i).toString());
      }
      stmt.close();
      rset.close();
      startValues.addElement(value);

    }
    return startValues;
  }

  /**
   *
   * @param counterColumnName is name of the column
   * @return query
   */
  private String querySelectCounter(String counterColumnName) {
    String query = "select ";

    query += this.counterValueColumn + " from " + this.counterTableName + " where " +
        this.counterNameColumn + " = '" + counterColumnName + "'";
    return query;
  }

  /**
   *
   * @param counterColumnName is column name
   * @param counterColumnValue is column value
   * @return query
   */
  private String queryInsertCounter(String counterColumnName, BigDecimal counterColumnValue) {
    String query = "INSERT into ";

    query += this.counterTableName + " (" + this.counterNameColumn + ", " + this.counterValueColumn
        + ") VALUES('" +
        counterColumnName + "', " + counterColumnValue.intValue() + ")";
    return query;
  }

  /**
   *
   * @param counterColumnName is colulmn name
   * @param newValue is column value
   * @return query
   */

  private String queryUpdateCounter(String counterColumnName, BigDecimal newValue) {
    String query = "update ";
    query += this.counterTableName + " set " +
        this.counterValueColumn + " = " + newValue.toString() + " where " +
        this.counterNameColumn + " = '" + counterColumnName + "'";
    return query;
  }

  /**
   * This method read value of sub counter parameter
   * @param tableName is table name
   * @param tableID is table ID
   * @param conn is connection
   * @param firstColumn is first column
   * @return vector
   * @throws SQLException
   */
  public Vector readSubCounterValue(String tableName, String tableID, Connection conn,
      int firstColumn, String tableMode, ConfigReader targetConfigReader) throws SQLException, LoaderException {

    BigDecimal value = new BigDecimal(0);
    Vector startValues = new Vector();

    this.logger.write("full", "\treadSubCounterValue method is started");

    Vector columns = this.getSubCounterKeyColumns(tableName, tableID);
    Vector typs = this.getSubCounterKeyColumnsTyp(tableName, tableID);
    Vector subColumnNames = this.getSubTargetColumnName(tableName, tableID);
    Vector subIncrement = this.getSubCounterIncrement(tableName, tableID);
    Vector subCounterStartValues = this.getSubCounterStartValue(tableName, tableID);
    Vector subCounterKeyColumnValues = this.getSubCounterKeyValues(tableName, tableID);

    for (int i = 0; i < subColumnNames.size(); i++) {

      String strQuery = "select ";
      Statement stmt = conn.createStatement();
      strQuery += subColumnNames.get(i).toString() + " from " + tableName + " where ";
      for (int j = 0; j < ( (Vector)columns.get(i)).size(); j++) {
        if ( ( (Vector)subCounterKeyColumnValues.get(i)).get(j) != null &&
            !( (Vector)subCounterKeyColumnValues.get(i)).get(j).toString().equalsIgnoreCase("")) {
//ZK change this from CheckType to targetConfigReader
         try {
            if (!targetConfigReader.isNumber( ( (Vector)typs.get(i)).get(j).toString()))
                strQuery += ( (Vector)columns.get(i)).get(j).toString() + " = '"
                    + ( (Vector)subCounterKeyColumnValues.get(i)).get(j).toString() + "' and ";
              else
                strQuery += ( (Vector)columns.get(i)).get(j).toString() + " = "
                    + ( (Vector)subCounterKeyColumnValues.get(i)).get(j).toString() + " and ";
        } catch (LoaderException e) {
           LoaderException le= new LoaderException("Exception:This sql type isn't present in conf file for target database. Yuo must add it into conf file.",(Throwable)e);
           throw le;
        }
        } else
          strQuery += ( (Vector)columns.get(i)).get(j).toString() + " is null and ";
      }

      if (strQuery.endsWith(" and "))
        strQuery = strQuery.substring(0, strQuery.length() - 5);

      if (tableMode.equalsIgnoreCase("cache")) {
        //caching the subcounter value
        Object obj = subCounterCache.get(strQuery);
        if (obj == null) { //query isn't cached before
          this.logger.write("full", "\tQuery '" + strQuery + "' will be executed");
          ResultSet rset = stmt.executeQuery(strQuery);
          BigDecimal currentValue = new BigDecimal(0);
          if (rset.next()) {
            if (firstColumn == 0) {
              value = new BigDecimal(rset.getString(0));
              currentValue = value;
              while (rset.next()) {
                value = new BigDecimal(rset.getString(0));
                if (currentValue.intValue() < value.intValue())
                  currentValue = value;
              }
            } else { //firstColumnResult==1
              value = new BigDecimal(rset.getString(1));
              currentValue = value;
              while (rset.next()) {
                value = new BigDecimal(rset.getString(1));
                if (currentValue.intValue() < value.intValue())
                  currentValue = value;
              }
            }
            value = value.add(new BigDecimal(subIncrement.get(i).toString()));
          } else {
            value = new BigDecimal(subCounterStartValues.get(i).toString());
          }
          subCounterCache.put(strQuery, value);
          stmt.close();
        } else { //query is cached before
          BigDecimal oldValue = (BigDecimal)obj;
          value = oldValue.add(new BigDecimal(subIncrement.get(i).toString()));
          subCounterCache.remove(strQuery);
          subCounterCache.put(strQuery, value);
        }
      } else {//tableMode=query
        this.logger.write("full", "\tQuery '" + strQuery + "' will be executed");
        ResultSet rset = stmt.executeQuery(strQuery);
        BigDecimal currentValue = new BigDecimal(0);
        if (rset.next()) {
          if (firstColumn == 0) {
            value = new BigDecimal(rset.getString(0));
            currentValue = value;
            while (rset.next()) {
              value = new BigDecimal(rset.getString(0));
              if (currentValue.intValue() < value.intValue())
                currentValue = value;
            }
          } else { //firstColumnResult==1
            value = new BigDecimal(rset.getString(1));
            currentValue = value;
            while (rset.next()) {
              value = new BigDecimal(rset.getString(1));
              if (currentValue.intValue() < value.intValue())
                currentValue = value;
            }
          }
          value = value.add(new BigDecimal(subIncrement.get(i).toString()));
        } else {
          value = new BigDecimal(subCounterStartValues.get(i).toString());
        }
        stmt.close();

      }
      startValues.addElement(value);
    }
    this.logger.write("full", "\treadSubCounterValue method is finished");
    return startValues;
  }

  /**
   * This method reset cach for subcounter
   */
  public void resetSubCounterCache(){
    if(this.subCounterCache.size()>0)
      this.subCounterCache.clear();
  }


  /**
   * Method importAttributeValue reads value for strAttrName attribute in strTagName tag.
   * This method return this value.
   * @param doc Parsed import XML file.
   * @param strTagName The name of tag where attribute is situated.
   * @param strAttrName The name of tag attribute which reads input value.
   * @param iImportJobItem Number of ImportDefinition tag which is processed.
   * @return String - importing value.
   */
  private String importAttributeValue(Document doc, String strTagName, String strAttrName,
      int iImportJobItem) {
    String strValue = "";
    NodeList tagBasic = doc.getElementsByTagName("importDefinition");
    if (tagBasic.getLength() != 0) {
      Element docFragment = (Element)tagBasic.item(iImportJobItem);
//      NodeList tag = docFragment.getElementsByTagName(tagName);
//      for (int i = 0; i < tag.getLength(); i++) {

      tagBasic = docFragment.getElementsByTagName(strTagName);
      if (tagBasic.getLength() != 0) {
        docFragment = (Element)tagBasic.item(0);
        if (docFragment != null)
          strValue = docFragment.getAttribute(strAttrName);
      }
    }
    return strValue;
  }

  /**
   * Method counterColumnTypes is used to put types of constant columns into
   * global vector sorted in target tables. If there is an error, Exception
   * "SQLException" or "NullPointerException" is thrown.
   * @param c Connection to target database.
   * @param tableName is table name
   * @param tableID is table ID
   * @param firstColumn is first column
   * @throws SQLException Constructs an SQLException object with a reason.
   * @throws NullPointerException Constructs a NullPointerException with the specified detail message.
   */
  public void counterColumnTypes(String tableName, String tableID, Connection c,
      int firstColumn, boolean columnsSuportedTarget, ConfigReader configReaderTarget) throws SQLException, NullPointerException {
    int iCnt = 0;
    try {
      Vector columnNames = this.getSubCounterKeyColumns(tableName, tableID);
      Statement stmtConstant = c.createStatement();
      Vector typs = new Vector();
      Vector subTyps = new Vector();
      String strQuery = "select ";
	  	ResultSet rsetConstant=null;
	  
      if (columnNames.size() != 0) {
        for (int i = 0; i < columnNames.size(); i++) {
          for (int j = 0; j < ( (Vector)columnNames.get(i)).size(); j++) {
            strQuery += ( (Vector)columnNames.get(i)).get(j).toString() +
                ", ";
          }
          strQuery = strQuery.substring(0, strQuery.length() - 2);
          strQuery += " from " + tableName;
//ZK change this. Because of problems with getColumnTypeName()method. Some drivers doesn't support it.
//start 
    		if (columnsSuportedTarget){
    			  
						rsetConstant = c.getMetaData().getColumns( c.getCatalog(), null, tableName, "%" );
						String columnName = "";
						String columnType = "";
						while(rsetConstant.next()){
							columnName = rsetConstant.getString(3+firstColumn);
							columnType = rsetConstant.getString(5+firstColumn);
							Vector temp = (Vector)columnNames.get(i); 				
							for (int j = 0; j < temp.size(); j++) {									
									if( temp.get(j).toString().equalsIgnoreCase( columnName ) ){
										typs.add(columnType);
									}
							}
						}		
        }else{//TODO ZK ADDED stmtConstant.setMaxRows(1). Place this as parameter in conf file, like maxRowsSuported
					if (configReaderTarget.getMaxRowsSupported()){
        		 stmtConstant.setMaxRows(1);
    			}
             rsetConstant = stmtConstant.executeQuery(strQuery);
             
			 for (int j = 0; j < ( (Vector)columnNames.get(i)).size(); j++) {

			  typs.add(rsetConstant.getMetaData().getColumnTypeName(j + firstColumn));
          }

		  }		
          rsetConstant.close();
          subTyps.addElement(typs);
        }
      }      
      this.subCounterKeyColumnsTyp.put(tableName + "_" + tableID, subTyps);
      stmtConstant.close();

    }
    catch (SQLException ex) {
      throw ex;
    }
    catch (NullPointerException ex) {
      throw ex;
    }
  }

  /**
   * This method reset all variables
   */
  public void reset() {
    this.counterTableName = null;
    this.counterNameColumn = null;
    this.counterValueColumn = null;
    this.counterName = new Hashtable();
    this.counterStartValue = new Hashtable();
    this.counterIncrement = new Hashtable();
    this.targetColumnName = new Hashtable();
    this.targetColumnTyp = new Hashtable();
    this.valueMode = new Hashtable();
    this.counterStartValueReset = new Hashtable();

    this.subCounterName = new Hashtable();
    this.subCounterStartValue = new Hashtable();
    this.subCounterIncrement = new Hashtable();
    this.subTargetColumnName = new Hashtable();
    this.subValueMode = new Hashtable();
    this.subCounterKeyColumns = new Hashtable();
    this.subCounterKeyValues = new Hashtable();
    this.subCounterKeyColumnsTyp = new Hashtable();
    this.subTargetColumnTyp = new Hashtable();

    this.vecCounterName = new Vector();
    this.vecCounterIncrement = new Vector();
    this.vecCounterStartValue = new Vector();
    this.vecTargetColumnName = new Vector();
    this.vecValueMode = new Vector();
    this.vecCounterStartValueReset = new Vector();
    this.vecTargetTableName = new Vector();
    this.vecTargetTableID = new Vector();
    this.currentCounterValue = new Hashtable();

    this.vecSubCounterName = new Vector();
    this.vecSubCounterIncrement = new Vector();
    this.vecSubCounterStartValue = new Vector();
    this.vecSubTargetTableName = new Vector();
    this.vecSubTargetTableID = new Vector();
    this.vecSubTargetColumnName = new Vector();
    this.vecSubValueMode = new Vector();
    this.vecSubKeyColumns = new Vector();
    this.vecSubKeyColumnsTyp = new Vector();
    this.subCounterCache.clear();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -