SqlProviderPostgreSQL.java :  » Report » openi » org » openi » jdbc » impl » Java Open Source

Java Open Source » Report » openi 
openi » org » openi » jdbc » impl » SqlProviderPostgreSQL.java
/*********************************************************************************
 * The contents of this file are subject to the OpenI Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.openi.org/docs/LICENSE.txt
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is: OpenI Open Source
 *
 * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
 * Portions created by Loyalty Matrix, Inc. are
 * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
 *
 * Contributor(s): 
 *
 ********************************************************************************/
package org.openi.jdbc.impl;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.openi.segment.SegmentBin;
import org.openi.jdbc.SqlProvider;

public class SqlProviderPostgreSQL implements SqlProvider{
  String databaseName;
  private static Logger logger = Logger.getLogger(SqlProviderPostgreSQL.class);
  
  public SqlProviderPostgreSQL(){
  
  }
  
  
  /* (non-Javadoc)
   * @see org.openi.jdbc.impl.SqlProvider#generateFetchMinMaxSql(java.lang.String, java.lang.String)
   */
  public String generateFetchMinMaxSql(String targetTable, String targetColumn) {
    String sql ="select min(" + targetColumn + "), max(" + targetColumn + ") from " + targetTable;
    logger.debug(sql);
    return sql;
  }

  /* (non-Javadoc)
   * @see org.openi.jdbc.impl.SqlProvider#generateFetchRowCountSql(java.lang.String)
   */
  public String generateFetchRowCountSql(String targetTable) {
    String sql = "select count(*) as colcount from " + targetTable;
    logger.debug(sql);
    return sql;
  }

  /* (non-Javadoc)
   * @see org.openi.jdbc.impl.SqlProvider#generateSegmentSql(java.util.List, java.lang.String, java.lang.String)
   */
  public String generateSegmentSql(List bucketList, String targetTable, String targetColumn){
    Iterator buckets = bucketList.iterator();
    SegmentBin bin;
    String caseStatement = " CASE \n";
    String from = targetTable;
    for(int i=1; buckets.hasNext(); i++){
      // dataset.addValue(1, "comparable", "comparable1");
      bin = (SegmentBin)buckets.next();
      caseStatement += "WHEN " + targetColumn 
        + " between " 
        + bin.getStart() 
        + " AND "
        + bin.getEnd()
        + " THEN " + i + "\n";
    }
    caseStatement += " ELSE -1\n";
    caseStatement += " END ";
    String sql = "select count(" + targetColumn + ") as frequency, " 
        + caseStatement 
        + " AS " + targetColumn + "_bucket"
        + " FROM " + from 
        + " group by (" + caseStatement + ")";
    // logger.debug(sql);
    return sql;
  }
  
  /* (non-Javadoc)
   * @see org.openi.jdbc.impl.SqlProvider#generateFetchColumnDataSql(java.lang.String, java.lang.String, int)
   */
  public String generateFetchColumnDataSql(String targetTable, String targetColumn, int sampleSize){
    String sql = null;
    if(sampleSize > 0){
      sql = "select " + targetColumn + " from " + targetTable + " limit " + sampleSize;
    }else{
      sql = "select " + targetColumn + " from " + targetTable ;
    }
    logger.debug(sql);
    return sql;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.